picture_layer_impl.h 13 KB

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