1 // Copyright 2013 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 "cc/test/fake_picture_layer_impl.h"
6 
7 #include <stddef.h>
8 
9 #include <vector>
10 
11 #include "base/memory/ptr_util.h"
12 #include "cc/test/fake_raster_source.h"
13 #include "cc/tiles/tile.h"
14 #include "cc/trees/layer_tree_impl.h"
15 
16 namespace cc {
17 
FakePictureLayerImpl(LayerTreeImpl * tree_impl,int id,scoped_refptr<RasterSource> raster_source)18 FakePictureLayerImpl::FakePictureLayerImpl(
19     LayerTreeImpl* tree_impl,
20     int id,
21     scoped_refptr<RasterSource> raster_source)
22     : PictureLayerImpl(tree_impl, id) {
23   if (raster_source) {
24     SetBounds(raster_source->GetSize());
25     SetRasterSource(raster_source, Region());
26   } else {
27     // Just to avoid crash on null RasterSource when updating tilings.
28     SetRasterSource(FakeRasterSource::CreateEmpty(gfx::Size()), Region());
29   }
30 }
31 
CreateLayerImpl(LayerTreeImpl * tree_impl)32 std::unique_ptr<LayerImpl> FakePictureLayerImpl::CreateLayerImpl(
33     LayerTreeImpl* tree_impl) {
34   return base::WrapUnique(new FakePictureLayerImpl(tree_impl, id()));
35 }
36 
PushPropertiesTo(LayerImpl * layer_impl)37 void FakePictureLayerImpl::PushPropertiesTo(LayerImpl* layer_impl) {
38   FakePictureLayerImpl* picture_layer_impl =
39       static_cast<FakePictureLayerImpl*>(layer_impl);
40   picture_layer_impl->fixed_tile_size_ = fixed_tile_size_;
41   PictureLayerImpl::PushPropertiesTo(layer_impl);
42 }
43 
AppendQuads(viz::CompositorRenderPass * render_pass,AppendQuadsData * append_quads_data)44 void FakePictureLayerImpl::AppendQuads(viz::CompositorRenderPass* render_pass,
45                                        AppendQuadsData* append_quads_data) {
46   PictureLayerImpl::AppendQuads(render_pass, append_quads_data);
47   ++append_quads_count_;
48 }
49 
CalculateTileSize(const gfx::Size & content_bounds)50 gfx::Size FakePictureLayerImpl::CalculateTileSize(
51     const gfx::Size& content_bounds) {
52   if (fixed_tile_size_.IsEmpty()) {
53     return PictureLayerImpl::CalculateTileSize(content_bounds);
54   }
55 
56   return fixed_tile_size_;
57 }
58 
HighResTiling() const59 PictureLayerTiling* FakePictureLayerImpl::HighResTiling() const {
60   PictureLayerTiling* result = nullptr;
61   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
62     PictureLayerTiling* tiling = tilings_->tiling_at(i);
63     if (tiling->resolution() == HIGH_RESOLUTION) {
64       // There should be only one high res tiling.
65       CHECK(!result);
66       result = tiling;
67     }
68   }
69   return result;
70 }
71 
LowResTiling() const72 PictureLayerTiling* FakePictureLayerImpl::LowResTiling() const {
73   PictureLayerTiling* result = nullptr;
74   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
75     PictureLayerTiling* tiling = tilings_->tiling_at(i);
76     if (tiling->resolution() == LOW_RESOLUTION) {
77       // There should be only one low res tiling.
78       CHECK(!result);
79       result = tiling;
80     }
81   }
82   return result;
83 }
84 
SetRasterSource(scoped_refptr<RasterSource> raster_source,const Region & invalidation)85 void FakePictureLayerImpl::SetRasterSource(
86     scoped_refptr<RasterSource> raster_source,
87     const Region& invalidation) {
88   Region invalidation_temp = invalidation;
89   const PictureLayerTilingSet* pending_set = nullptr;
90   const PaintWorkletRecordMap* pending_paint_worklet_records = nullptr;
91   set_gpu_raster_max_texture_size(
92       layer_tree_impl()->GetDeviceViewport().size());
93   UpdateRasterSource(raster_source, &invalidation_temp, pending_set,
94                      pending_paint_worklet_records);
95 }
96 
CreateAllTiles()97 void FakePictureLayerImpl::CreateAllTiles() {
98   for (size_t i = 0; i < num_tilings(); ++i)
99     tilings_->tiling_at(i)->CreateAllTilesForTesting();
100 }
101 
SetAllTilesReady()102 void FakePictureLayerImpl::SetAllTilesReady() {
103   for (size_t tiling_idx = 0; tiling_idx < tilings_->num_tilings();
104        ++tiling_idx) {
105     PictureLayerTiling* tiling = tilings_->tiling_at(tiling_idx);
106     SetAllTilesReadyInTiling(tiling);
107   }
108 }
109 
SetAllTilesReadyInTiling(PictureLayerTiling * tiling)110 void FakePictureLayerImpl::SetAllTilesReadyInTiling(
111     PictureLayerTiling* tiling) {
112   std::vector<Tile*> tiles = tiling->AllTilesForTesting();
113   for (size_t tile_idx = 0; tile_idx < tiles.size(); ++tile_idx) {
114     Tile* tile = tiles[tile_idx];
115     SetTileReady(tile);
116   }
117 }
118 
SetTileReady(Tile * tile)119 void FakePictureLayerImpl::SetTileReady(Tile* tile) {
120   TileDrawInfo& draw_info = tile->draw_info();
121   draw_info.SetSolidColorForTesting(true);
122   DCHECK(draw_info.IsReadyToDraw());
123 }
124 
DidBecomeActive()125 void FakePictureLayerImpl::DidBecomeActive() {
126   PictureLayerImpl::DidBecomeActive();
127   ++did_become_active_call_count_;
128 }
129 
HasValidTilePriorities() const130 bool FakePictureLayerImpl::HasValidTilePriorities() const {
131   return use_set_valid_tile_priorities_flag_
132              ? has_valid_tile_priorities_
133              : PictureLayerImpl::HasValidTilePriorities();
134 }
135 
CountTilesRequired(TileRequirementCheck is_tile_required_callback) const136 size_t FakePictureLayerImpl::CountTilesRequired(
137     TileRequirementCheck is_tile_required_callback) const {
138   if (!HasValidTilePriorities())
139     return 0;
140 
141   if (!tilings_)
142     return 0;
143 
144   if (visible_layer_rect().IsEmpty())
145     return 0;
146 
147   gfx::Rect rect = viewport_rect_for_tile_priority_in_content_space_;
148   rect.Intersect(visible_layer_rect());
149 
150   size_t count = 0;
151 
152   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
153     PictureLayerTiling* tiling = tilings_->tiling_at(i);
154     if (tiling->resolution() != HIGH_RESOLUTION &&
155         tiling->resolution() != LOW_RESOLUTION)
156       continue;
157 
158     for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
159          ++iter) {
160       const Tile* tile = *iter;
161       // A null tile (i.e. missing recording) can just be skipped.
162       // TODO(vmpstr): Verify this is true if we create tiles in raster
163       // iterators.
164       if (!tile)
165         continue;
166 
167       // We can't check tile->required_for_activation, because that value might
168       // be out of date. It is updated in the raster/eviction iterators.
169       // TODO(vmpstr): Remove the comment once you can't access this information
170       // from the tile.
171       if ((tiling->*is_tile_required_callback)(tile))
172         ++count;
173     }
174   }
175 
176   return count;
177 }
178 
CountTilesRequiredForActivation() const179 size_t FakePictureLayerImpl::CountTilesRequiredForActivation() const {
180   if (!layer_tree_impl()->IsPendingTree())
181     return 0;
182 
183   return CountTilesRequired(&PictureLayerTiling::IsTileRequiredForActivation);
184 }
185 
CountTilesRequiredForDraw() const186 size_t FakePictureLayerImpl::CountTilesRequiredForDraw() const {
187   if (!layer_tree_impl()->IsActiveTree())
188     return 0;
189 
190   return CountTilesRequired(&PictureLayerTiling::IsTileRequiredForDraw);
191 }
192 
ReleaseResources()193 void FakePictureLayerImpl::ReleaseResources() {
194   PictureLayerImpl::ReleaseResources();
195   ++release_resources_count_;
196 }
197 
ReleaseTileResources()198 void FakePictureLayerImpl::ReleaseTileResources() {
199   PictureLayerImpl::ReleaseTileResources();
200   ++release_tile_resources_count_;
201 }
202 
203 }  // namespace cc
204