1 // Copyright 2012 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 #ifndef CC_LAYERS_PICTURE_LAYER_IMPL_H_
6 #define CC_LAYERS_PICTURE_LAYER_IMPL_H_
7 
8 #include <stddef.h>
9 
10 #include <map>
11 #include <string>
12 #include <vector>
13 
14 #include "base/memory/ptr_util.h"
15 #include "cc/cc_export.h"
16 #include "cc/layers/layer.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/layers/tile_size_calculator.h"
19 #include "cc/paint/discardable_image_map.h"
20 #include "cc/paint/image_id.h"
21 #include "cc/tiles/picture_layer_tiling.h"
22 #include "cc/tiles/picture_layer_tiling_set.h"
23 #include "cc/tiles/tiling_set_eviction_queue.h"
24 #include "cc/trees/image_animation_controller.h"
25 
26 namespace cc {
27 
28 class AppendQuadsData;
29 class MicroBenchmarkImpl;
30 class Tile;
31 
32 class CC_EXPORT PictureLayerImpl
33     : public LayerImpl,
34       public PictureLayerTilingClient,
35       public ImageAnimationController::AnimationDriver {
36  public:
Create(LayerTreeImpl * tree_impl,int id)37   static std::unique_ptr<PictureLayerImpl> Create(LayerTreeImpl* tree_impl,
38                                                   int id) {
39     return base::WrapUnique(new PictureLayerImpl(tree_impl, id));
40   }
41   PictureLayerImpl(const PictureLayerImpl&) = delete;
42   ~PictureLayerImpl() override;
43 
44   PictureLayerImpl& operator=(const PictureLayerImpl&) = delete;
45 
SetIsBackdropFilterMask(bool is_backdrop_filter_mask)46   void SetIsBackdropFilterMask(bool is_backdrop_filter_mask) {
47     is_backdrop_filter_mask_ = is_backdrop_filter_mask;
48   }
is_backdrop_filter_mask()49   bool is_backdrop_filter_mask() const { return is_backdrop_filter_mask_; }
50 
51   // LayerImpl overrides.
52   const char* LayerTypeAsString() const override;
53   std::unique_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override;
54   void PushPropertiesTo(LayerImpl* layer) override;
55   void AppendQuads(viz::RenderPass* render_pass,
56                    AppendQuadsData* append_quads_data) override;
57   void NotifyTileStateChanged(const Tile* tile) override;
58   gfx::Rect GetDamageRect() const override;
59   void ResetChangeTracking() override;
60   void ResetRasterScale();
61   void DidBeginTracing() override;
62   void ReleaseResources() override;
63   void ReleaseTileResources() override;
64   void RecreateTileResources() override;
65   Region GetInvalidationRegionForDebugging() override;
66   gfx::Rect GetEnclosingRectInTargetSpace() const override;
67   gfx::ContentColorUsage GetContentColorUsage() const override;
68 
69   // PictureLayerTilingClient overrides.
70   std::unique_ptr<Tile> CreateTile(const Tile::CreateInfo& info) override;
71   gfx::Size CalculateTileSize(const gfx::Size& content_bounds) override;
72   const Region* GetPendingInvalidation() override;
73   const PictureLayerTiling* GetPendingOrActiveTwinTiling(
74       const PictureLayerTiling* tiling) const override;
75   bool HasValidTilePriorities() const override;
76   bool RequiresHighResToDraw() const override;
77   const PaintWorkletRecordMap& GetPaintWorkletRecords() const override;
78 
79   // ImageAnimationController::AnimationDriver overrides.
80   bool ShouldAnimate(PaintImage::Id paint_image_id) const override;
81 
set_gpu_raster_max_texture_size(gfx::Size gpu_raster_max_texture_size)82   void set_gpu_raster_max_texture_size(gfx::Size gpu_raster_max_texture_size) {
83     gpu_raster_max_texture_size_ = gpu_raster_max_texture_size;
84   }
85 
gpu_raster_max_texture_size()86   gfx::Size gpu_raster_max_texture_size() {
87     return gpu_raster_max_texture_size_;
88   }
89 
90   void UpdateRasterSource(
91       scoped_refptr<RasterSource> raster_source,
92       Region* new_invalidation,
93       const PictureLayerTilingSet* pending_set,
94       const PaintWorkletRecordMap* pending_paint_worklet_records);
95   bool UpdateTiles();
96   // Returns true if the LCD state changed.
97   bool UpdateCanUseLCDTextAfterCommit();
98 
99   // Mask-related functions.
100   void GetContentsResourceId(viz::ResourceId* resource_id,
101                              gfx::Size* resource_size,
102                              gfx::SizeF* resource_uv_size) const override;
103 
104   void SetNearestNeighbor(bool nearest_neighbor);
105 
106   void SetUseTransformedRasterization(bool use);
107 
108   void SetDirectlyCompositedImageSize(base::Optional<gfx::Size>);
109 
110   size_t GPUMemoryUsageInBytes() const override;
111 
112   void RunMicroBenchmark(MicroBenchmarkImpl* benchmark) override;
113 
114   bool CanHaveTilings() const;
115 
picture_layer_tiling_set()116   PictureLayerTilingSet* picture_layer_tiling_set() { return tilings_.get(); }
117 
118   // Functions used by tile manager.
119   PictureLayerImpl* GetPendingOrActiveTwinLayer() const;
120   bool IsOnActiveOrPendingTree() const;
121 
122   // Used for benchmarking
GetRasterSource()123   RasterSource* GetRasterSource() const { return raster_source_.get(); }
124 
125   // This enum is the return value of the InvalidateRegionForImages() call. The
126   // possible values represent the fact that there are no images on this layer
127   // (kNoImages), the fact that the invalidation images don't cause an
128   // invalidation on this layer (kNoInvalidation), or the fact that the layer
129   // was invalidated (kInvalidated).
130   enum class ImageInvalidationResult {
131     kNoImages,
132     kNoInvalidation,
133     kInvalidated,
134   };
135 
136   ImageInvalidationResult InvalidateRegionForImages(
137       const PaintImageIdFlatSet& images_to_invalidate);
138 
can_use_lcd_text()139   bool can_use_lcd_text() const { return can_use_lcd_text_; }
140 
InvalidationForTesting()141   const Region& InvalidationForTesting() const { return invalidation_; }
142 
143   // Set the paint result (PaintRecord) for a given PaintWorkletInput.
144   void SetPaintWorkletRecord(scoped_refptr<const PaintWorkletInput>,
145                              sk_sp<PaintRecord>);
146 
147   // Retrieve the map of PaintWorkletInputs to their painted results
148   // (PaintRecords). If a PaintWorkletInput has not been painted yet, it will
149   // map to nullptr.
GetPaintWorkletRecordMap()150   const PaintWorkletRecordMap& GetPaintWorkletRecordMap() const {
151     return paint_worklet_records_;
152   }
153 
content_bounds()154   gfx::Size content_bounds() { return content_bounds_; }
155 
156   // Invalidates all PaintWorklets in this layer who depend on the given
157   // property to be painted. Used when the value for the property is changed by
158   // an animation, at which point the PaintWorklet must be re-painted.
159   void InvalidatePaintWorklets(const PaintWorkletInput::PropertyKey& key);
160 
161  protected:
162   PictureLayerImpl(LayerTreeImpl* tree_impl, int id);
163   PictureLayerTiling* AddTiling(const gfx::AxisTransform2d& contents_transform);
164   void RemoveAllTilings();
165   void AddTilingsForRasterScale();
166   void AddLowResolutionTilingIfNeeded();
167   bool ShouldAdjustRasterScale() const;
168   void RecalculateRasterScales();
169   gfx::Vector2dF CalculateRasterTranslation(float raster_scale);
170   void CleanUpTilingsOnActiveLayer(
171       const std::vector<PictureLayerTiling*>& used_tilings);
172   float MinimumContentsScale() const;
173   float MaximumContentsScale() const;
174   void UpdateViewportRectForTilePriorityInContentSpace();
175   PictureLayerImpl* GetRecycledTwinLayer() const;
176   float GetDirectlyCompositedImageRasterScale() const;
177 
178   void SanityCheckTilingState() const;
179 
180   void GetDebugBorderProperties(SkColor* color, float* width) const override;
181   void GetAllPrioritizedTilesForTracing(
182       std::vector<PrioritizedTile>* prioritized_tiles) const override;
183   void AsValueInto(base::trace_event::TracedValue* dict) const override;
184 
185   void UpdateIdealScales();
186   float MaximumTilingContentsScale() const;
187   std::unique_ptr<PictureLayerTilingSet> CreatePictureLayerTilingSet();
188 
189   void RegisterAnimatedImages();
190   void UnregisterAnimatedImages();
191 
192   // Set the collection of PaintWorkletInput as well as their PaintImageId that
193   // are part of this layer.
194   void SetPaintWorkletInputs(
195       const std::vector<DiscardableImageMap::PaintWorkletInputWithImageId>&
196           inputs);
197 
198   PictureLayerImpl* twin_layer_;
199 
200   std::unique_ptr<PictureLayerTilingSet> tilings_;
201   scoped_refptr<RasterSource> raster_source_;
202   Region invalidation_;
203 
204   // Ideal scales are calcuated from the transforms applied to the layer. They
205   // represent the best known scale from the layer to the final output.
206   // Page scale is from user pinch/zoom.
207   float ideal_page_scale_;
208   // Device scale is from screen dpi, and it comes from device scale facter.
209   float ideal_device_scale_;
210   // Source scale comes from javascript css scale.
211   float ideal_source_scale_;
212   // Contents scale = device scale * page scale * source scale.
213   float ideal_contents_scale_;
214 
215   // Raster scales are set from ideal scales. They are scales we choose to
216   // raster at. They may not match the ideal scales at times to avoid raster for
217   // performance reasons.
218   float raster_page_scale_;
219   float raster_device_scale_;
220   float raster_source_scale_;
221   float raster_contents_scale_;
222   float low_res_raster_contents_scale_;
223 
224   bool is_backdrop_filter_mask_ : 1;
225 
226   bool was_screen_space_transform_animating_ : 1;
227   bool only_used_low_res_last_append_quads_ : 1;
228 
229   bool nearest_neighbor_ : 1;
230   bool use_transformed_rasterization_ : 1;
231   bool can_use_lcd_text_ : 1;
232 
233   base::Optional<gfx::Size> directly_composited_image_size_;
234 
235   // Use this instead of |visible_layer_rect()| for tiling calculations. This
236   // takes external viewport and transform for tile priority into account.
237   gfx::Rect viewport_rect_for_tile_priority_in_content_space_;
238 
239   gfx::Size gpu_raster_max_texture_size_;
240 
241   // List of tilings that were used last time we appended quads. This can be
242   // used as an optimization not to remove tilings if they are still being
243   // drawn. Note that accessing this vector should only be done in the context
244   // of comparing pointers, since objects pointed to are not guaranteed to
245   // exist.
246   std::vector<PictureLayerTiling*> last_append_quads_tilings_;
247 
248   // The set of PaintWorkletInputs that are part of this PictureLayerImpl, and
249   // their painted results (if any). During commit, Blink hands us a set of
250   // PaintWorkletInputs that are part of this layer. These are then painted
251   // asynchronously on a worklet thread, triggered from
252   // |LayerTreeHostImpl::UpdateSyncTreeAfterCommitOrImplSideInvalidation|.
253   PaintWorkletRecordMap paint_worklet_records_;
254 
255   gfx::Size content_bounds_;
256   TileSizeCalculator tile_size_calculator_;
257 
258   // Denotes an area that is damaged and needs redraw. This is in the layer's
259   // space.
260   gfx::Rect damage_rect_;
261 };
262 
263 }  // namespace cc
264 
265 #endif  // CC_LAYERS_PICTURE_LAYER_IMPL_H_
266