1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_GRAPHICS_LAYER_H_ 28 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_GRAPHICS_LAYER_H_ 29 30 #include <memory> 31 32 #include "base/macros.h" 33 #include "cc/input/scroll_snap_data.h" 34 #include "cc/layers/content_layer_client.h" 35 #include "cc/layers/layer.h" 36 #include "third_party/blink/renderer/platform/geometry/float_point.h" 37 #include "third_party/blink/renderer/platform/geometry/float_point_3d.h" 38 #include "third_party/blink/renderer/platform/geometry/float_size.h" 39 #include "third_party/blink/renderer/platform/geometry/int_rect.h" 40 #include "third_party/blink/renderer/platform/graphics/color.h" 41 #include "third_party/blink/renderer/platform/graphics/compositing/layers_as_json.h" 42 #include "third_party/blink/renderer/platform/graphics/compositing_reasons.h" 43 #include "third_party/blink/renderer/platform/graphics/compositor_element_id.h" 44 #include "third_party/blink/renderer/platform/graphics/graphics_context.h" 45 #include "third_party/blink/renderer/platform/graphics/graphics_layer_client.h" 46 #include "third_party/blink/renderer/platform/graphics/graphics_types.h" 47 #include "third_party/blink/renderer/platform/graphics/image_orientation.h" 48 #include "third_party/blink/renderer/platform/graphics/paint/display_item_client.h" 49 #include "third_party/blink/renderer/platform/graphics/paint/paint_controller.h" 50 #include "third_party/blink/renderer/platform/graphics/paint/raster_invalidator.h" 51 #include "third_party/blink/renderer/platform/graphics/paint_invalidation_reason.h" 52 #include "third_party/blink/renderer/platform/graphics/squashing_disallowed_reasons.h" 53 #include "third_party/blink/renderer/platform/heap/handle.h" 54 #include "third_party/blink/renderer/platform/platform_export.h" 55 #include "third_party/blink/renderer/platform/transforms/transformation_matrix.h" 56 #include "third_party/blink/renderer/platform/wtf/vector.h" 57 #include "third_party/skia/include/core/SkFilterQuality.h" 58 #include "third_party/skia/include/core/SkRefCnt.h" 59 60 namespace cc { 61 class PictureLayer; 62 } // namespace cc 63 64 namespace blink { 65 66 class Image; 67 class PaintController; 68 class RasterInvalidationTracking; 69 class RasterInvalidator; 70 71 typedef Vector<GraphicsLayer*, 64> GraphicsLayerVector; 72 73 // GraphicsLayer is an abstraction for a rendering surface with backing store, 74 // which may have associated transformation and animations. 75 class PLATFORM_EXPORT GraphicsLayer : public DisplayItemClient, 76 public LayerAsJSONClient, 77 private cc::ContentLayerClient { 78 USING_FAST_MALLOC(GraphicsLayer); 79 80 public: 81 explicit GraphicsLayer(GraphicsLayerClient&); 82 ~GraphicsLayer() override; 83 Client()84 GraphicsLayerClient& Client() const { return client_; } 85 SetCompositingReasons(CompositingReasons reasons)86 void SetCompositingReasons(CompositingReasons reasons) { 87 compositing_reasons_ = reasons; 88 } GetCompositingReasons()89 CompositingReasons GetCompositingReasons() const { 90 return compositing_reasons_; 91 } 92 GetSquashingDisallowedReasons()93 SquashingDisallowedReasons GetSquashingDisallowedReasons() const { 94 return squashing_disallowed_reasons_; 95 } SetSquashingDisallowedReasons(SquashingDisallowedReasons reasons)96 void SetSquashingDisallowedReasons(SquashingDisallowedReasons reasons) { 97 squashing_disallowed_reasons_ = reasons; 98 } 99 SetOwnerNodeId(DOMNodeId id)100 void SetOwnerNodeId(DOMNodeId id) { owner_node_id_ = id; } 101 Parent()102 GraphicsLayer* Parent() const { return parent_; } 103 void SetParent(GraphicsLayer*); // Internal use only. 104 Children()105 const Vector<GraphicsLayer*>& Children() const { return children_; } 106 // Returns true if the child list changed. 107 bool SetChildren(const GraphicsLayerVector&); 108 109 // Add child layers. If the child is already parented, it will be removed from 110 // its old parent. 111 void AddChild(GraphicsLayer*); 112 113 void RemoveAllChildren(); 114 void RemoveFromParent(); 115 MaskLayer()116 GraphicsLayer* MaskLayer() const { return mask_layer_; } 117 void SetMaskLayer(GraphicsLayer*); 118 119 // The offset is the origin of the layoutObject minus the origin of the 120 // graphics layer (so either zero or negative). OffsetFromLayoutObject()121 IntSize OffsetFromLayoutObject() const { return offset_from_layout_object_; } 122 void SetOffsetFromLayoutObject(const IntSize&); 123 124 // The size of the layer. 125 const gfx::Size& Size() const; 126 void SetSize(const gfx::Size&); 127 128 void SetRenderingContext(int id); 129 DrawsContent()130 bool DrawsContent() const { return draws_content_; } 131 void SetDrawsContent(bool); 132 133 // False if no hit test data will be recorded onto this GraphicsLayer. 134 // This is different from |DrawsContent| because hit test data are internal 135 // to blink and are not copied to the cc::Layer's display list. PaintsHitTest()136 bool PaintsHitTest() const { return paints_hit_test_; } SetPaintsHitTest(bool paints)137 void SetPaintsHitTest(bool paints) { paints_hit_test_ = paints; } 138 PaintsContentOrHitTest()139 bool PaintsContentOrHitTest() const { 140 return draws_content_ || paints_hit_test_; 141 } 142 ContentsAreVisible()143 bool ContentsAreVisible() const { return contents_visible_; } 144 void SetContentsVisible(bool); 145 146 // For special cases, e.g. drawing missing tiles on Android. 147 // The compositor should never paint this color in normal cases because the 148 // Layer will paint the background by itself. 149 RGBA32 BackgroundColor() const; 150 void SetBackgroundColor(RGBA32); 151 152 // Opaque means that we know the layer contents have no alpha. 153 bool ContentsOpaque() const; 154 void SetContentsOpaque(bool); 155 156 bool BackfaceVisibility() const; 157 158 void SetHitTestable(bool); GetHitTestable()159 bool GetHitTestable() const { return hit_testable_; } 160 161 void SetFilterQuality(SkFilterQuality); 162 163 // Some GraphicsLayers paint only the foreground or the background content PaintingPhase()164 GraphicsLayerPaintingPhase PaintingPhase() const { return painting_phase_; } 165 void SetPaintingPhase(GraphicsLayerPaintingPhase); 166 167 void SetNeedsDisplay(); 168 void SetContentsNeedsDisplay(); 169 170 // Set that the position/size of the contents (image or video). 171 void SetContentsRect(const IntRect&); 172 173 // Layer contents 174 void SetContentsToImage( 175 Image*, 176 Image::ImageDecodingMode decode_mode, 177 RespectImageOrientationEnum = kRespectImageOrientation); 178 // If |prevent_contents_opaque_changes| is set to true, then calls to 179 // SetContentsOpaque() will not be passed on to |contents_layer|. Use when 180 // the client wants to have control of the opaqueness of |contents_layer| 181 // independently of what outcome painting produces. 182 void SetContentsToCcLayer(scoped_refptr<cc::Layer> contents_layer, 183 bool prevent_contents_opaque_changes); HasContentsLayer()184 bool HasContentsLayer() const { return ContentsLayer(); } ContentsLayer()185 cc::Layer* ContentsLayer() const { return contents_layer_.get(); } 186 ContentsRect()187 const IntRect& ContentsRect() const { return contents_rect_; } 188 189 // For hosting this GraphicsLayer in a native layer hierarchy. 190 cc::PictureLayer* CcLayer() const; 191 192 void UpdateTrackingRasterInvalidations(); 193 void ResetTrackedRasterInvalidations(); 194 bool HasTrackedRasterInvalidations() const; 195 RasterInvalidationTracking* GetRasterInvalidationTracking() const; 196 void TrackRasterInvalidation(const DisplayItemClient&, 197 const IntRect&, 198 PaintInvalidationReason); 199 200 IntRect InterestRect(); 201 bool PaintRecursively(); 202 // Returns true if this layer is repainted. 203 bool Paint(); 204 205 PaintController& GetPaintController() const; 206 207 void SetElementId(const CompositorElementId&); 208 209 // DisplayItemClient methods DebugName()210 String DebugName() const final { return client_.DebugName(this); } 211 IntRect VisualRect() const override; OwnerNodeId()212 DOMNodeId OwnerNodeId() const final { return owner_node_id_; } 213 214 // LayerAsJSONClient implementation. 215 void AppendAdditionalInfoAsJSON(LayerTreeFlags, 216 const cc::Layer&, 217 JSONObject&) const override; 218 219 void SetHasWillChangeTransformHint(bool); 220 HasLayerState()221 bool HasLayerState() const { return layer_state_.get(); } 222 void SetLayerState(const PropertyTreeState&, const IntPoint& layer_offset); GetPropertyTreeState()223 const PropertyTreeState& GetPropertyTreeState() const { 224 return layer_state_->state; 225 } GetOffsetFromTransformNode()226 IntPoint GetOffsetFromTransformNode() const { return layer_state_->offset; } 227 228 void SetContentsLayerState(const PropertyTreeState&, 229 const IntPoint& layer_offset); GetContentsPropertyTreeState()230 const PropertyTreeState& GetContentsPropertyTreeState() const { 231 return contents_layer_state_ ? contents_layer_state_->state 232 : GetPropertyTreeState(); 233 } GetContentsOffsetFromTransformNode()234 IntPoint GetContentsOffsetFromTransformNode() const { 235 return contents_layer_state_ ? contents_layer_state_->offset 236 : GetOffsetFromTransformNode(); 237 } 238 239 // Capture the last painted result into a PaintRecord. This GraphicsLayer 240 // must DrawsContent. The result is never nullptr. 241 sk_sp<PaintRecord> CapturePaintRecord() const; 242 SetNeedsCheckRasterInvalidation()243 void SetNeedsCheckRasterInvalidation() { 244 needs_check_raster_invalidation_ = true; 245 } 246 247 bool PaintWithoutCommitForTesting( 248 const base::Optional<IntRect>& interest_rect = base::nullopt); 249 250 protected: 251 String DebugName(const cc::Layer*) const; 252 253 private: 254 friend class CompositedLayerMappingTest; 255 friend class GraphicsLayerTest; 256 257 // cc::ContentLayerClient implementation. PaintableRegion()258 gfx::Rect PaintableRegion() final { return InterestRect(); } 259 scoped_refptr<cc::DisplayItemList> PaintContentsToDisplayList( 260 PaintingControlSetting painting_control) final; FillsBoundsCompletely()261 bool FillsBoundsCompletely() const override { return false; } 262 size_t GetApproximateUnsharedMemoryUsage() const final; 263 264 void PaintRecursivelyInternal(Vector<GraphicsLayer*>& repainted_layers); 265 void UpdateSafeOpaqueBackgroundColor(); 266 267 // Returns true if PaintController::PaintArtifact() changed and needs commit. 268 bool PaintWithoutCommit(const IntRect* interest_rect = nullptr); 269 270 // Adds a child without calling NotifyChildListChange(), so that adding 271 // children can be batched before updating. 272 void AddChildInternal(GraphicsLayer*); 273 274 #if DCHECK_IS_ON() 275 bool HasAncestor(GraphicsLayer*) const; 276 #endif 277 278 // Helper functions used by settors to keep layer's the state consistent. 279 void NotifyChildListChange(); 280 void UpdateLayerIsDrawable(); 281 void UpdateContentsLayerBounds(); 282 283 void SetContentsTo(scoped_refptr<cc::Layer>, 284 bool prevent_contents_opaque_changes); 285 286 RasterInvalidator& EnsureRasterInvalidator(); 287 void SetNeedsDisplayInRect(const IntRect&); 288 289 FloatSize VisualRectSubpixelOffset() const; 290 291 GraphicsLayerClient& client_; 292 293 // Offset from the owning layoutObject 294 IntSize offset_from_layout_object_; 295 296 TransformationMatrix transform_; 297 298 bool prevent_contents_opaque_changes_ : 1; 299 bool draws_content_ : 1; 300 bool paints_hit_test_ : 1; 301 bool contents_visible_ : 1; 302 bool hit_testable_ : 1; 303 bool needs_check_raster_invalidation_ : 1; 304 bool contents_layer_is_picture_image_layer_ : 1; 305 306 bool painted_ : 1; 307 308 GraphicsLayerPaintingPhase painting_phase_; 309 310 Vector<GraphicsLayer*> children_; 311 GraphicsLayer* parent_; 312 313 // Reference to mask layer. We don't own this. 314 GraphicsLayer* mask_layer_; 315 316 IntRect contents_rect_; 317 318 scoped_refptr<cc::PictureLayer> layer_; 319 IntSize image_size_; 320 scoped_refptr<cc::Layer> contents_layer_; 321 322 SquashingDisallowedReasons squashing_disallowed_reasons_ = 323 SquashingDisallowedReason::kNone; 324 325 mutable std::unique_ptr<PaintController> paint_controller_; 326 327 IntRect previous_interest_rect_; 328 329 struct LayerState { 330 PropertyTreeState state; 331 IntPoint offset; 332 }; 333 std::unique_ptr<LayerState> layer_state_; 334 std::unique_ptr<LayerState> contents_layer_state_; 335 336 std::unique_ptr<RasterInvalidator> raster_invalidator_; 337 RasterInvalidator::RasterInvalidationFunction raster_invalidation_function_; 338 339 DOMNodeId owner_node_id_ = kInvalidDOMNodeId; 340 CompositingReasons compositing_reasons_ = CompositingReason::kNone; 341 342 FRIEND_TEST_ALL_PREFIXES(CompositingLayerPropertyUpdaterTest, MaskLayerState); 343 344 DISALLOW_COPY_AND_ASSIGN(GraphicsLayer); 345 }; 346 347 } // namespace blink 348 349 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_GRAPHICS_LAYER_H_ 350