| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "config.h" | |
| 6 | |
| 7 #if USE(ACCELERATED_COMPOSITING) | |
| 8 | |
| 9 #include "CCDamageTracker.h" | |
| 10 | |
| 11 #include "CCLayerImpl.h" | |
| 12 #include "CCLayerTreeHostCommon.h" | |
| 13 #include "CCMathUtil.h" | |
| 14 #include "CCRenderSurface.h" | |
| 15 #include <public/WebFilterOperations.h> | |
| 16 | |
| 17 using WebKit::WebTransformationMatrix; | |
| 18 | |
| 19 namespace cc { | |
| 20 | |
| 21 scoped_ptr<CCDamageTracker> CCDamageTracker::create() | |
| 22 { | |
| 23 return make_scoped_ptr(new CCDamageTracker()); | |
| 24 } | |
| 25 | |
| 26 CCDamageTracker::CCDamageTracker() | |
| 27 : m_forceFullDamageNextUpdate(false), | |
| 28 m_currentRectHistory(new RectMap), | |
| 29 m_nextRectHistory(new RectMap) | |
| 30 { | |
| 31 } | |
| 32 | |
| 33 CCDamageTracker::~CCDamageTracker() | |
| 34 { | |
| 35 } | |
| 36 | |
| 37 static inline void expandRectWithFilters(FloatRect& rect, const WebKit::WebFilte
rOperations& filters) | |
| 38 { | |
| 39 int top, right, bottom, left; | |
| 40 filters.getOutsets(top, right, bottom, left); | |
| 41 rect.move(-left, -top); | |
| 42 rect.expand(left + right, top + bottom); | |
| 43 } | |
| 44 | |
| 45 static inline void expandDamageRectInsideRectWithFilters(FloatRect& damageRect,
const FloatRect& preFilterRect, const WebKit::WebFilterOperations& filters) | |
| 46 { | |
| 47 FloatRect expandedDamageRect = damageRect; | |
| 48 expandRectWithFilters(expandedDamageRect, filters); | |
| 49 FloatRect filterRect = preFilterRect; | |
| 50 expandRectWithFilters(filterRect, filters); | |
| 51 | |
| 52 expandedDamageRect.intersect(filterRect); | |
| 53 damageRect.unite(expandedDamageRect); | |
| 54 } | |
| 55 | |
| 56 void CCDamageTracker::updateDamageTrackingState(const std::vector<CCLayerImpl*>&
layerList, int targetSurfaceLayerID, bool targetSurfacePropertyChangedOnlyFromD
escendant, const IntRect& targetSurfaceContentRect, CCLayerImpl* targetSurfaceMa
skLayer, const WebKit::WebFilterOperations& filters) | |
| 57 { | |
| 58 // | |
| 59 // This function computes the "damage rect" of a target surface, and updates
the state | |
| 60 // that is used to correctly track damage across frames. The damage rect is
the region | |
| 61 // of the surface that may have changed and needs to be redrawn. This can be
used to | |
| 62 // scissor what is actually drawn, to save GPU computation and bandwidth. | |
| 63 // | |
| 64 // The surface's damage rect is computed as the union of all possible change
s that | |
| 65 // have happened to the surface since the last frame was drawn. This include
s: | |
| 66 // - any changes for existing layers/surfaces that contribute to the targe
t surface | |
| 67 // - layers/surfaces that existed in the previous frame, but no longer exi
st. | |
| 68 // | |
| 69 // The basic algorithm for computing the damage region is as follows: | |
| 70 // | |
| 71 // 1. compute damage caused by changes in active/new layers | |
| 72 // for each layer in the layerList: | |
| 73 // if the layer is actually a renderSurface: | |
| 74 // add the surface's damage to our target surface. | |
| 75 // else | |
| 76 // add the layer's damage to the target surface. | |
| 77 // | |
| 78 // 2. compute damage caused by the target surface's mask, if it exists. | |
| 79 // | |
| 80 // 3. compute damage caused by old layers/surfaces that no longer exist | |
| 81 // for each leftover layer: | |
| 82 // add the old layer/surface bounds to the target surface damage. | |
| 83 // | |
| 84 // 4. combine all partial damage rects to get the full damage rect. | |
| 85 // | |
| 86 // Additional important points: | |
| 87 // | |
| 88 // - This algorithm is implicitly recursive; it assumes that descendant surf
aces have | |
| 89 // already computed their damage. | |
| 90 // | |
| 91 // - Changes to layers/surfaces indicate "damage" to the target surface; If
a layer is | |
| 92 // not changed, it does NOT mean that the layer can skip drawing. All laye
rs that | |
| 93 // overlap the damaged region still need to be drawn. For example, if a la
yer | |
| 94 // changed its opacity, then layers underneath must be re-drawn as well, e
ven if | |
| 95 // they did not change. | |
| 96 // | |
| 97 // - If a layer/surface property changed, the old bounds and new bounds may
overlap... | |
| 98 // i.e. some of the exposed region may not actually be exposing anything.
But this | |
| 99 // does not artificially inflate the damage rect. If the layer changed, it
s entire | |
| 100 // old bounds would always need to be redrawn, regardless of how much it o
verlaps | |
| 101 // with the layer's new bounds, which also need to be entirely redrawn. | |
| 102 // | |
| 103 // - See comments in the rest of the code to see what exactly is considered
a "change" | |
| 104 // in a layer/surface. | |
| 105 // | |
| 106 // - To correctly manage exposed rects, two RectMaps are maintained: | |
| 107 // | |
| 108 // 1. The "current" map contains all the layer bounds that contributed
to the | |
| 109 // previous frame (even outside the previous damaged area). If a lay
er changes | |
| 110 // or does not exist anymore, those regions are then exposed and dam
age the | |
| 111 // target surface. As the algorithm progresses, entries are removed
from the | |
| 112 // map until it has only leftover layers that no longer exist. | |
| 113 // | |
| 114 // 2. The "next" map starts out empty, and as the algorithm progresses,
every | |
| 115 // layer/surface that contributes to the surface is added to the map
. | |
| 116 // | |
| 117 // 3. After the damage rect is computed, the two maps are swapped, so t
hat the | |
| 118 // damage tracker is ready for the next frame. | |
| 119 // | |
| 120 | |
| 121 // These functions cannot be bypassed with early-exits, even if we know what
the | |
| 122 // damage will be for this frame, because we need to update the damage track
er state | |
| 123 // to correctly track the next frame. | |
| 124 FloatRect damageFromActiveLayers = trackDamageFromActiveLayers(layerList, ta
rgetSurfaceLayerID); | |
| 125 FloatRect damageFromSurfaceMask = trackDamageFromSurfaceMask(targetSurfaceMa
skLayer); | |
| 126 FloatRect damageFromLeftoverRects = trackDamageFromLeftoverRects(); | |
| 127 | |
| 128 FloatRect damageRectForThisUpdate; | |
| 129 | |
| 130 if (m_forceFullDamageNextUpdate || targetSurfacePropertyChangedOnlyFromDesce
ndant) { | |
| 131 damageRectForThisUpdate = targetSurfaceContentRect; | |
| 132 m_forceFullDamageNextUpdate = false; | |
| 133 } else { | |
| 134 // FIXME: can we clamp this damage to the surface's content rect? (affec
ts performance, but not correctness) | |
| 135 damageRectForThisUpdate = damageFromActiveLayers; | |
| 136 damageRectForThisUpdate.uniteIfNonZero(damageFromSurfaceMask); | |
| 137 damageRectForThisUpdate.uniteIfNonZero(damageFromLeftoverRects); | |
| 138 | |
| 139 if (filters.hasFilterThatMovesPixels()) | |
| 140 expandRectWithFilters(damageRectForThisUpdate, filters); | |
| 141 } | |
| 142 | |
| 143 // Damage accumulates until we are notified that we actually did draw on tha
t frame. | |
| 144 m_currentDamageRect.uniteIfNonZero(damageRectForThisUpdate); | |
| 145 | |
| 146 // The next history map becomes the current map for the next frame. Note thi
s must | |
| 147 // happen every frame to correctly track changes, even if damage accumulates
over | |
| 148 // multiple frames before actually being drawn. | |
| 149 swap(m_currentRectHistory, m_nextRectHistory); | |
| 150 } | |
| 151 | |
| 152 FloatRect CCDamageTracker::removeRectFromCurrentFrame(int layerID, bool& layerIs
New) | |
| 153 { | |
| 154 layerIsNew = !m_currentRectHistory->contains(layerID); | |
| 155 | |
| 156 // take() will remove the entry from the map, or if not found, return a defa
ult (empty) rect. | |
| 157 return m_currentRectHistory->take(layerID); | |
| 158 } | |
| 159 | |
| 160 void CCDamageTracker::saveRectForNextFrame(int layerID, const FloatRect& targetS
paceRect) | |
| 161 { | |
| 162 // This layer should not yet exist in next frame's history. | |
| 163 ASSERT(layerID > 0); | |
| 164 ASSERT(m_nextRectHistory->find(layerID) == m_nextRectHistory->end()); | |
| 165 m_nextRectHistory->set(layerID, targetSpaceRect); | |
| 166 } | |
| 167 | |
| 168 FloatRect CCDamageTracker::trackDamageFromActiveLayers(const std::vector<CCLayer
Impl*>& layerList, int targetSurfaceLayerID) | |
| 169 { | |
| 170 FloatRect damageRect = FloatRect(); | |
| 171 | |
| 172 for (unsigned layerIndex = 0; layerIndex < layerList.size(); ++layerIndex) { | |
| 173 // Visit layers in back-to-front order. | |
| 174 CCLayerImpl* layer = layerList[layerIndex]; | |
| 175 | |
| 176 if (CCLayerTreeHostCommon::renderSurfaceContributesToTarget<CCLayerImpl>
(layer, targetSurfaceLayerID)) | |
| 177 extendDamageForRenderSurface(layer, damageRect); | |
| 178 else | |
| 179 extendDamageForLayer(layer, damageRect); | |
| 180 } | |
| 181 | |
| 182 return damageRect; | |
| 183 } | |
| 184 | |
| 185 FloatRect CCDamageTracker::trackDamageFromSurfaceMask(CCLayerImpl* targetSurface
MaskLayer) | |
| 186 { | |
| 187 FloatRect damageRect = FloatRect(); | |
| 188 | |
| 189 if (!targetSurfaceMaskLayer) | |
| 190 return damageRect; | |
| 191 | |
| 192 // Currently, if there is any change to the mask, we choose to damage the en
tire | |
| 193 // surface. This could potentially be optimized later, but it is not expecte
d to be a | |
| 194 // common case. | |
| 195 if (targetSurfaceMaskLayer->layerPropertyChanged() || !targetSurfaceMaskLaye
r->updateRect().isEmpty()) | |
| 196 damageRect = FloatRect(FloatPoint::zero(), FloatSize(targetSurfaceMaskLa
yer->bounds())); | |
| 197 | |
| 198 return damageRect; | |
| 199 } | |
| 200 | |
| 201 FloatRect CCDamageTracker::trackDamageFromLeftoverRects() | |
| 202 { | |
| 203 // After computing damage for all active layers, any leftover items in the c
urrent | |
| 204 // rect history correspond to layers/surfaces that no longer exist. So, thes
e regions | |
| 205 // are now exposed on the target surface. | |
| 206 | |
| 207 FloatRect damageRect = FloatRect(); | |
| 208 | |
| 209 for (RectMap::iterator it = m_currentRectHistory->begin(); it != m_currentRe
ctHistory->end(); ++it) | |
| 210 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE | |
| 211 damageRect.unite(it->value); | |
| 212 #else | |
| 213 damageRect.unite(it->second); | |
| 214 #endif | |
| 215 | |
| 216 m_currentRectHistory->clear(); | |
| 217 | |
| 218 return damageRect; | |
| 219 } | |
| 220 | |
| 221 static bool layerNeedsToRedrawOntoItsTargetSurface(CCLayerImpl* layer) | |
| 222 { | |
| 223 // If the layer does NOT own a surface but has SurfacePropertyChanged, | |
| 224 // this means that its target surface is affected and needs to be redrawn. | |
| 225 // However, if the layer DOES own a surface, then the SurfacePropertyChanged
| |
| 226 // flag should not be used here, because that flag represents whether the | |
| 227 // layer's surface has changed. | |
| 228 if (layer->renderSurface()) | |
| 229 return layer->layerPropertyChanged(); | |
| 230 return layer->layerPropertyChanged() || layer->layerSurfacePropertyChanged()
; | |
| 231 } | |
| 232 | |
| 233 void CCDamageTracker::extendDamageForLayer(CCLayerImpl* layer, FloatRect& target
DamageRect) | |
| 234 { | |
| 235 // There are two ways that a layer can damage a region of the target surface
: | |
| 236 // 1. Property change (e.g. opacity, position, transforms): | |
| 237 // - the entire region of the layer itself damages the surface. | |
| 238 // - the old layer region also damages the surface, because this regi
on is now exposed. | |
| 239 // - note that in many cases the old and new layer rects may overlap,
which is fine. | |
| 240 // | |
| 241 // 2. Repaint/update: If a region of the layer that was repainted/updated,
that | |
| 242 // region damages the surface. | |
| 243 // | |
| 244 // Property changes take priority over update rects. | |
| 245 // | |
| 246 // This method is called when we want to consider how a layer contributes to
its | |
| 247 // targetRenderSurface, even if that layer owns the targetRenderSurface itse
lf. | |
| 248 // To consider how a layer's targetSurface contributes to the ancestorSurfac
e, | |
| 249 // extendDamageForRenderSurface() must be called instead. | |
| 250 | |
| 251 bool layerIsNew = false; | |
| 252 FloatRect oldRectInTargetSpace = removeRectFromCurrentFrame(layer->id(), lay
erIsNew); | |
| 253 | |
| 254 FloatRect rectInTargetSpace = CCMathUtil::mapClippedRect(layer->drawTransfor
m(), FloatRect(FloatPoint::zero(), layer->contentBounds())); | |
| 255 saveRectForNextFrame(layer->id(), rectInTargetSpace); | |
| 256 | |
| 257 if (layerIsNew || layerNeedsToRedrawOntoItsTargetSurface(layer)) { | |
| 258 // If a layer is new or has changed, then its entire layer rect affects
the target surface. | |
| 259 targetDamageRect.uniteIfNonZero(rectInTargetSpace); | |
| 260 | |
| 261 // The layer's old region is now exposed on the target surface, too. | |
| 262 // Note oldRectInTargetSpace is already in target space. | |
| 263 targetDamageRect.uniteIfNonZero(oldRectInTargetSpace); | |
| 264 } else if (!layer->updateRect().isEmpty()) { | |
| 265 // If the layer properties havent changed, then the the target surface i
s only | |
| 266 // affected by the layer's update area, which could be empty. | |
| 267 FloatRect updateContentRect = layer->updateRect(); | |
| 268 float widthScale = layer->contentBounds().width() / static_cast<float>(l
ayer->bounds().width()); | |
| 269 float heightScale = layer->contentBounds().height() / static_cast<float>
(layer->bounds().height()); | |
| 270 updateContentRect.scale(widthScale, heightScale); | |
| 271 | |
| 272 FloatRect updateRectInTargetSpace = CCMathUtil::mapClippedRect(layer->dr
awTransform(), updateContentRect); | |
| 273 targetDamageRect.uniteIfNonZero(updateRectInTargetSpace); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 void CCDamageTracker::extendDamageForRenderSurface(CCLayerImpl* layer, FloatRect
& targetDamageRect) | |
| 278 { | |
| 279 // There are two ways a "descendant surface" can damage regions of the "targ
et surface": | |
| 280 // 1. Property change: | |
| 281 // - a surface's geometry can change because of | |
| 282 // - changes to descendants (i.e. the subtree) that affect the su
rface's content rect | |
| 283 // - changes to ancestor layers that propagate their property cha
nges to their entire subtree. | |
| 284 // - just like layers, both the old surface rect and new surface rect
will | |
| 285 // damage the target surface in this case. | |
| 286 // | |
| 287 // 2. Damage rect: This surface may have been damaged by its own layerList
as well, and that damage | |
| 288 // should propagate to the target surface. | |
| 289 // | |
| 290 | |
| 291 CCRenderSurface* renderSurface = layer->renderSurface(); | |
| 292 | |
| 293 bool surfaceIsNew = false; | |
| 294 FloatRect oldSurfaceRect = removeRectFromCurrentFrame(layer->id(), surfaceIs
New); | |
| 295 | |
| 296 FloatRect surfaceRectInTargetSpace = renderSurface->drawableContentRect(); /
/ already includes replica if it exists. | |
| 297 saveRectForNextFrame(layer->id(), surfaceRectInTargetSpace); | |
| 298 | |
| 299 FloatRect damageRectInLocalSpace; | |
| 300 if (surfaceIsNew || renderSurface->surfacePropertyChanged() || layer->layerS
urfacePropertyChanged()) { | |
| 301 // The entire surface contributes damage. | |
| 302 damageRectInLocalSpace = renderSurface->contentRect(); | |
| 303 | |
| 304 // The surface's old region is now exposed on the target surface, too. | |
| 305 targetDamageRect.uniteIfNonZero(oldSurfaceRect); | |
| 306 } else { | |
| 307 // Only the surface's damageRect will damage the target surface. | |
| 308 damageRectInLocalSpace = renderSurface->damageTracker()->currentDamageRe
ct(); | |
| 309 } | |
| 310 | |
| 311 // If there was damage, transform it to target space, and possibly contribut
e its reflection if needed. | |
| 312 if (!damageRectInLocalSpace.isEmpty()) { | |
| 313 const WebTransformationMatrix& drawTransform = renderSurface->drawTransf
orm(); | |
| 314 FloatRect damageRectInTargetSpace = CCMathUtil::mapClippedRect(drawTrans
form, damageRectInLocalSpace); | |
| 315 targetDamageRect.uniteIfNonZero(damageRectInTargetSpace); | |
| 316 | |
| 317 if (layer->replicaLayer()) { | |
| 318 const WebTransformationMatrix& replicaDrawTransform = renderSurface-
>replicaDrawTransform(); | |
| 319 targetDamageRect.uniteIfNonZero(CCMathUtil::mapClippedRect(replicaDr
awTransform, damageRectInLocalSpace)); | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 // If there was damage on the replica's mask, then the target surface receiv
es that damage as well. | |
| 324 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) { | |
| 325 CCLayerImpl* replicaMaskLayer = layer->replicaLayer()->maskLayer(); | |
| 326 | |
| 327 bool replicaIsNew = false; | |
| 328 removeRectFromCurrentFrame(replicaMaskLayer->id(), replicaIsNew); | |
| 329 | |
| 330 const WebTransformationMatrix& replicaDrawTransform = renderSurface->rep
licaDrawTransform(); | |
| 331 FloatRect replicaMaskLayerRect = CCMathUtil::mapClippedRect(replicaDrawT
ransform, FloatRect(FloatPoint::zero(), FloatSize(replicaMaskLayer->bounds().wid
th(), replicaMaskLayer->bounds().height()))); | |
| 332 saveRectForNextFrame(replicaMaskLayer->id(), replicaMaskLayerRect); | |
| 333 | |
| 334 // In the current implementation, a change in the replica mask damages t
he entire replica region. | |
| 335 if (replicaIsNew || replicaMaskLayer->layerPropertyChanged() || !replica
MaskLayer->updateRect().isEmpty()) | |
| 336 targetDamageRect.uniteIfNonZero(replicaMaskLayerRect); | |
| 337 } | |
| 338 | |
| 339 // If the layer has a background filter, this may cause pixels in our surfac
e to be expanded, so we will need to expand any damage | |
| 340 // at or below this layer. We expand the damage from this layer too, as we n
eed to readback those pixels from the surface with only | |
| 341 // the contents of layers below this one in them. This means we need to redr
aw any pixels in the surface being used for the blur in | |
| 342 // this layer this frame. | |
| 343 if (layer->backgroundFilters().hasFilterThatMovesPixels()) | |
| 344 expandDamageRectInsideRectWithFilters(targetDamageRect, surfaceRectInTar
getSpace, layer->backgroundFilters()); | |
| 345 } | |
| 346 | |
| 347 } // namespace cc | |
| 348 | |
| 349 #endif // USE(ACCELERATED_COMPOSITING) | |
| OLD | NEW |