Implement android_webview url intercepting.
[chromium-blink-merge.git] / cc / damage_tracker.cc
blob12901292837e5f338f8606ce82cdcdc8a256362f
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "config.h"
7 #include "CCDamageTracker.h"
9 #include "CCLayerImpl.h"
10 #include "CCLayerTreeHostCommon.h"
11 #include "CCMathUtil.h"
12 #include "CCRenderSurface.h"
13 #include <public/WebFilterOperations.h>
15 using WebKit::WebTransformationMatrix;
17 namespace cc {
19 scoped_ptr<CCDamageTracker> CCDamageTracker::create()
21 return make_scoped_ptr(new CCDamageTracker());
24 CCDamageTracker::CCDamageTracker()
25 : m_forceFullDamageNextUpdate(false),
26 m_currentRectHistory(new RectMap),
27 m_nextRectHistory(new RectMap)
31 CCDamageTracker::~CCDamageTracker()
35 static inline void expandRectWithFilters(FloatRect& rect, const WebKit::WebFilterOperations& filters)
37 int top, right, bottom, left;
38 filters.getOutsets(top, right, bottom, left);
39 rect.move(-left, -top);
40 rect.expand(left + right, top + bottom);
43 static inline void expandDamageRectInsideRectWithFilters(FloatRect& damageRect, const FloatRect& preFilterRect, const WebKit::WebFilterOperations& filters)
45 FloatRect expandedDamageRect = damageRect;
46 expandRectWithFilters(expandedDamageRect, filters);
47 FloatRect filterRect = preFilterRect;
48 expandRectWithFilters(filterRect, filters);
50 expandedDamageRect.intersect(filterRect);
51 damageRect.unite(expandedDamageRect);
54 void CCDamageTracker::updateDamageTrackingState(const std::vector<CCLayerImpl*>& layerList, int targetSurfaceLayerID, bool targetSurfacePropertyChangedOnlyFromDescendant, const IntRect& targetSurfaceContentRect, CCLayerImpl* targetSurfaceMaskLayer, const WebKit::WebFilterOperations& filters)
57 // This function computes the "damage rect" of a target surface, and updates the state
58 // that is used to correctly track damage across frames. The damage rect is the region
59 // of the surface that may have changed and needs to be redrawn. This can be used to
60 // scissor what is actually drawn, to save GPU computation and bandwidth.
62 // The surface's damage rect is computed as the union of all possible changes that
63 // have happened to the surface since the last frame was drawn. This includes:
64 // - any changes for existing layers/surfaces that contribute to the target surface
65 // - layers/surfaces that existed in the previous frame, but no longer exist.
67 // The basic algorithm for computing the damage region is as follows:
69 // 1. compute damage caused by changes in active/new layers
70 // for each layer in the layerList:
71 // if the layer is actually a renderSurface:
72 // add the surface's damage to our target surface.
73 // else
74 // add the layer's damage to the target surface.
76 // 2. compute damage caused by the target surface's mask, if it exists.
78 // 3. compute damage caused by old layers/surfaces that no longer exist
79 // for each leftover layer:
80 // add the old layer/surface bounds to the target surface damage.
82 // 4. combine all partial damage rects to get the full damage rect.
84 // Additional important points:
86 // - This algorithm is implicitly recursive; it assumes that descendant surfaces have
87 // already computed their damage.
89 // - Changes to layers/surfaces indicate "damage" to the target surface; If a layer is
90 // not changed, it does NOT mean that the layer can skip drawing. All layers that
91 // overlap the damaged region still need to be drawn. For example, if a layer
92 // changed its opacity, then layers underneath must be re-drawn as well, even if
93 // they did not change.
95 // - If a layer/surface property changed, the old bounds and new bounds may overlap...
96 // i.e. some of the exposed region may not actually be exposing anything. But this
97 // does not artificially inflate the damage rect. If the layer changed, its entire
98 // old bounds would always need to be redrawn, regardless of how much it overlaps
99 // with the layer's new bounds, which also need to be entirely redrawn.
101 // - See comments in the rest of the code to see what exactly is considered a "change"
102 // in a layer/surface.
104 // - To correctly manage exposed rects, two RectMaps are maintained:
106 // 1. The "current" map contains all the layer bounds that contributed to the
107 // previous frame (even outside the previous damaged area). If a layer changes
108 // or does not exist anymore, those regions are then exposed and damage the
109 // target surface. As the algorithm progresses, entries are removed from the
110 // map until it has only leftover layers that no longer exist.
112 // 2. The "next" map starts out empty, and as the algorithm progresses, every
113 // layer/surface that contributes to the surface is added to the map.
115 // 3. After the damage rect is computed, the two maps are swapped, so that the
116 // damage tracker is ready for the next frame.
119 // These functions cannot be bypassed with early-exits, even if we know what the
120 // damage will be for this frame, because we need to update the damage tracker state
121 // to correctly track the next frame.
122 FloatRect damageFromActiveLayers = trackDamageFromActiveLayers(layerList, targetSurfaceLayerID);
123 FloatRect damageFromSurfaceMask = trackDamageFromSurfaceMask(targetSurfaceMaskLayer);
124 FloatRect damageFromLeftoverRects = trackDamageFromLeftoverRects();
126 FloatRect damageRectForThisUpdate;
128 if (m_forceFullDamageNextUpdate || targetSurfacePropertyChangedOnlyFromDescendant) {
129 damageRectForThisUpdate = targetSurfaceContentRect;
130 m_forceFullDamageNextUpdate = false;
131 } else {
132 // FIXME: can we clamp this damage to the surface's content rect? (affects performance, but not correctness)
133 damageRectForThisUpdate = damageFromActiveLayers;
134 damageRectForThisUpdate.uniteIfNonZero(damageFromSurfaceMask);
135 damageRectForThisUpdate.uniteIfNonZero(damageFromLeftoverRects);
137 if (filters.hasFilterThatMovesPixels())
138 expandRectWithFilters(damageRectForThisUpdate, filters);
141 // Damage accumulates until we are notified that we actually did draw on that frame.
142 m_currentDamageRect.uniteIfNonZero(damageRectForThisUpdate);
144 // The next history map becomes the current map for the next frame. Note this must
145 // happen every frame to correctly track changes, even if damage accumulates over
146 // multiple frames before actually being drawn.
147 swap(m_currentRectHistory, m_nextRectHistory);
150 FloatRect CCDamageTracker::removeRectFromCurrentFrame(int layerID, bool& layerIsNew)
152 RectMap::iterator iter = m_currentRectHistory->find(layerID);
153 layerIsNew = iter == m_currentRectHistory->end();
154 if (layerIsNew)
155 return FloatRect();
157 FloatRect ret = iter->second;
158 m_currentRectHistory->erase(iter);
159 return ret;
162 void CCDamageTracker::saveRectForNextFrame(int layerID, const FloatRect& targetSpaceRect)
164 // This layer should not yet exist in next frame's history.
165 ASSERT(layerID > 0);
166 ASSERT(m_nextRectHistory->find(layerID) == m_nextRectHistory->end());
167 (*m_nextRectHistory)[layerID] = targetSpaceRect;
170 FloatRect CCDamageTracker::trackDamageFromActiveLayers(const std::vector<CCLayerImpl*>& layerList, int targetSurfaceLayerID)
172 FloatRect damageRect = FloatRect();
174 for (unsigned layerIndex = 0; layerIndex < layerList.size(); ++layerIndex) {
175 // Visit layers in back-to-front order.
176 CCLayerImpl* layer = layerList[layerIndex];
178 if (CCLayerTreeHostCommon::renderSurfaceContributesToTarget<CCLayerImpl>(layer, targetSurfaceLayerID))
179 extendDamageForRenderSurface(layer, damageRect);
180 else
181 extendDamageForLayer(layer, damageRect);
184 return damageRect;
187 FloatRect CCDamageTracker::trackDamageFromSurfaceMask(CCLayerImpl* targetSurfaceMaskLayer)
189 FloatRect damageRect = FloatRect();
191 if (!targetSurfaceMaskLayer)
192 return damageRect;
194 // Currently, if there is any change to the mask, we choose to damage the entire
195 // surface. This could potentially be optimized later, but it is not expected to be a
196 // common case.
197 if (targetSurfaceMaskLayer->layerPropertyChanged() || !targetSurfaceMaskLayer->updateRect().isEmpty())
198 damageRect = FloatRect(FloatPoint::zero(), FloatSize(targetSurfaceMaskLayer->bounds()));
200 return damageRect;
203 FloatRect CCDamageTracker::trackDamageFromLeftoverRects()
205 // After computing damage for all active layers, any leftover items in the current
206 // rect history correspond to layers/surfaces that no longer exist. So, these regions
207 // are now exposed on the target surface.
209 FloatRect damageRect = FloatRect();
211 for (RectMap::iterator it = m_currentRectHistory->begin(); it != m_currentRectHistory->end(); ++it)
212 damageRect.unite(it->second);
214 m_currentRectHistory->clear();
216 return damageRect;
219 static bool layerNeedsToRedrawOntoItsTargetSurface(CCLayerImpl* layer)
221 // If the layer does NOT own a surface but has SurfacePropertyChanged,
222 // this means that its target surface is affected and needs to be redrawn.
223 // However, if the layer DOES own a surface, then the SurfacePropertyChanged
224 // flag should not be used here, because that flag represents whether the
225 // layer's surface has changed.
226 if (layer->renderSurface())
227 return layer->layerPropertyChanged();
228 return layer->layerPropertyChanged() || layer->layerSurfacePropertyChanged();
231 void CCDamageTracker::extendDamageForLayer(CCLayerImpl* layer, FloatRect& targetDamageRect)
233 // There are two ways that a layer can damage a region of the target surface:
234 // 1. Property change (e.g. opacity, position, transforms):
235 // - the entire region of the layer itself damages the surface.
236 // - the old layer region also damages the surface, because this region is now exposed.
237 // - note that in many cases the old and new layer rects may overlap, which is fine.
239 // 2. Repaint/update: If a region of the layer that was repainted/updated, that
240 // region damages the surface.
242 // Property changes take priority over update rects.
244 // This method is called when we want to consider how a layer contributes to its
245 // targetRenderSurface, even if that layer owns the targetRenderSurface itself.
246 // To consider how a layer's targetSurface contributes to the ancestorSurface,
247 // extendDamageForRenderSurface() must be called instead.
249 bool layerIsNew = false;
250 FloatRect oldRectInTargetSpace = removeRectFromCurrentFrame(layer->id(), layerIsNew);
252 FloatRect rectInTargetSpace = CCMathUtil::mapClippedRect(layer->drawTransform(), FloatRect(FloatPoint::zero(), layer->contentBounds()));
253 saveRectForNextFrame(layer->id(), rectInTargetSpace);
255 if (layerIsNew || layerNeedsToRedrawOntoItsTargetSurface(layer)) {
256 // If a layer is new or has changed, then its entire layer rect affects the target surface.
257 targetDamageRect.uniteIfNonZero(rectInTargetSpace);
259 // The layer's old region is now exposed on the target surface, too.
260 // Note oldRectInTargetSpace is already in target space.
261 targetDamageRect.uniteIfNonZero(oldRectInTargetSpace);
262 } else if (!layer->updateRect().isEmpty()) {
263 // If the layer properties havent changed, then the the target surface is only
264 // affected by the layer's update area, which could be empty.
265 FloatRect updateContentRect = layer->updateRect();
266 float widthScale = layer->contentBounds().width() / static_cast<float>(layer->bounds().width());
267 float heightScale = layer->contentBounds().height() / static_cast<float>(layer->bounds().height());
268 updateContentRect.scale(widthScale, heightScale);
270 FloatRect updateRectInTargetSpace = CCMathUtil::mapClippedRect(layer->drawTransform(), updateContentRect);
271 targetDamageRect.uniteIfNonZero(updateRectInTargetSpace);
275 void CCDamageTracker::extendDamageForRenderSurface(CCLayerImpl* layer, FloatRect& targetDamageRect)
277 // There are two ways a "descendant surface" can damage regions of the "target surface":
278 // 1. Property change:
279 // - a surface's geometry can change because of
280 // - changes to descendants (i.e. the subtree) that affect the surface's content rect
281 // - changes to ancestor layers that propagate their property changes to their entire subtree.
282 // - just like layers, both the old surface rect and new surface rect will
283 // damage the target surface in this case.
285 // 2. Damage rect: This surface may have been damaged by its own layerList as well, and that damage
286 // should propagate to the target surface.
289 CCRenderSurface* renderSurface = layer->renderSurface();
291 bool surfaceIsNew = false;
292 FloatRect oldSurfaceRect = removeRectFromCurrentFrame(layer->id(), surfaceIsNew);
294 FloatRect surfaceRectInTargetSpace = renderSurface->drawableContentRect(); // already includes replica if it exists.
295 saveRectForNextFrame(layer->id(), surfaceRectInTargetSpace);
297 FloatRect damageRectInLocalSpace;
298 if (surfaceIsNew || renderSurface->surfacePropertyChanged() || layer->layerSurfacePropertyChanged()) {
299 // The entire surface contributes damage.
300 damageRectInLocalSpace = renderSurface->contentRect();
302 // The surface's old region is now exposed on the target surface, too.
303 targetDamageRect.uniteIfNonZero(oldSurfaceRect);
304 } else {
305 // Only the surface's damageRect will damage the target surface.
306 damageRectInLocalSpace = renderSurface->damageTracker()->currentDamageRect();
309 // If there was damage, transform it to target space, and possibly contribute its reflection if needed.
310 if (!damageRectInLocalSpace.isEmpty()) {
311 const WebTransformationMatrix& drawTransform = renderSurface->drawTransform();
312 FloatRect damageRectInTargetSpace = CCMathUtil::mapClippedRect(drawTransform, damageRectInLocalSpace);
313 targetDamageRect.uniteIfNonZero(damageRectInTargetSpace);
315 if (layer->replicaLayer()) {
316 const WebTransformationMatrix& replicaDrawTransform = renderSurface->replicaDrawTransform();
317 targetDamageRect.uniteIfNonZero(CCMathUtil::mapClippedRect(replicaDrawTransform, damageRectInLocalSpace));
321 // If there was damage on the replica's mask, then the target surface receives that damage as well.
322 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
323 CCLayerImpl* replicaMaskLayer = layer->replicaLayer()->maskLayer();
325 bool replicaIsNew = false;
326 removeRectFromCurrentFrame(replicaMaskLayer->id(), replicaIsNew);
328 const WebTransformationMatrix& replicaDrawTransform = renderSurface->replicaDrawTransform();
329 FloatRect replicaMaskLayerRect = CCMathUtil::mapClippedRect(replicaDrawTransform, FloatRect(FloatPoint::zero(), FloatSize(replicaMaskLayer->bounds().width(), replicaMaskLayer->bounds().height())));
330 saveRectForNextFrame(replicaMaskLayer->id(), replicaMaskLayerRect);
332 // In the current implementation, a change in the replica mask damages the entire replica region.
333 if (replicaIsNew || replicaMaskLayer->layerPropertyChanged() || !replicaMaskLayer->updateRect().isEmpty())
334 targetDamageRect.uniteIfNonZero(replicaMaskLayerRect);
337 // If the layer has a background filter, this may cause pixels in our surface to be expanded, so we will need to expand any damage
338 // at or below this layer. We expand the damage from this layer too, as we need to readback those pixels from the surface with only
339 // the contents of layers below this one in them. This means we need to redraw any pixels in the surface being used for the blur in
340 // this layer this frame.
341 if (layer->backgroundFilters().hasFilterThatMovesPixels())
342 expandDamageRectInsideRectWithFilters(targetDamageRect, surfaceRectInTargetSpace, layer->backgroundFilters());
345 } // namespace cc