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.
5 #include "cc/layers/tiled_layer_impl.h"
7 #include "base/basictypes.h"
8 #include "base/strings/stringprintf.h"
9 #include "cc/base/math_util.h"
10 #include "cc/debug/debug_colors.h"
11 #include "cc/layers/append_quads_data.h"
12 #include "cc/quads/checkerboard_draw_quad.h"
13 #include "cc/quads/debug_border_draw_quad.h"
14 #include "cc/quads/solid_color_draw_quad.h"
15 #include "cc/quads/tile_draw_quad.h"
16 #include "cc/resources/layer_tiling_data.h"
17 #include "cc/trees/occlusion_tracker.h"
18 #include "third_party/khronos/GLES2/gl2.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "ui/gfx/quad_f.h"
24 class DrawableTile
: public LayerTilingData::Tile
{
26 static scoped_ptr
<DrawableTile
> Create() {
27 return make_scoped_ptr(new DrawableTile());
30 ResourceProvider::ResourceId
resource_id() const { return resource_id_
; }
31 void set_resource_id(ResourceProvider::ResourceId resource_id
) {
32 resource_id_
= resource_id
;
34 bool contents_swizzled() { return contents_swizzled_
; }
35 void set_contents_swizzled(bool contents_swizzled
) {
36 contents_swizzled_
= contents_swizzled
;
40 DrawableTile() : resource_id_(0), contents_swizzled_(false) {}
42 ResourceProvider::ResourceId resource_id_
;
43 bool contents_swizzled_
;
45 DISALLOW_COPY_AND_ASSIGN(DrawableTile
);
48 TiledLayerImpl::TiledLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
49 : LayerImpl(tree_impl
, id
), skips_draw_(true) {}
51 TiledLayerImpl::~TiledLayerImpl() {
54 ResourceProvider::ResourceId
TiledLayerImpl::ContentsResourceId() const {
55 // This function is only valid for single texture layers, e.g. masks.
57 // It's possible the mask layer is created but has no size or otherwise
59 if (tiler_
->num_tiles_x() == 0 || tiler_
->num_tiles_y() == 0)
62 // Any other number of tiles other than 0 or 1 is incorrect for masks.
63 DCHECK_EQ(tiler_
->num_tiles_x(), 1);
64 DCHECK_EQ(tiler_
->num_tiles_y(), 1);
66 DrawableTile
* tile
= TileAt(0, 0);
67 ResourceProvider::ResourceId resource_id
= tile
? tile
->resource_id() : 0;
71 bool TiledLayerImpl::HasTileAt(int i
, int j
) const {
72 return !!tiler_
->TileAt(i
, j
);
75 bool TiledLayerImpl::HasResourceIdForTileAt(int i
, int j
) const {
76 return HasTileAt(i
, j
) && TileAt(i
, j
)->resource_id();
79 DrawableTile
* TiledLayerImpl::TileAt(int i
, int j
) const {
80 return static_cast<DrawableTile
*>(tiler_
->TileAt(i
, j
));
83 DrawableTile
* TiledLayerImpl::CreateTile(int i
, int j
) {
84 scoped_ptr
<DrawableTile
> tile(DrawableTile::Create());
85 DrawableTile
* added_tile
= tile
.get();
86 tiler_
->AddTile(tile
.PassAs
<LayerTilingData::Tile
>(), i
, j
);
91 void TiledLayerImpl::GetDebugBorderProperties(SkColor
* color
,
93 *color
= DebugColors::TiledContentLayerBorderColor();
94 *width
= DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
97 scoped_ptr
<LayerImpl
> TiledLayerImpl::CreateLayerImpl(
98 LayerTreeImpl
* tree_impl
) {
99 return TiledLayerImpl::Create(tree_impl
, id()).PassAs
<LayerImpl
>();
102 void TiledLayerImpl::AsValueInto(base::DictionaryValue
* state
) const {
103 LayerImpl::AsValueInto(state
);
104 state
->Set("invalidation", MathUtil::AsValue(update_rect()).release());
107 size_t TiledLayerImpl::GPUMemoryUsageInBytes() const {
109 const size_t kMemoryUsagePerTileInBytes
=
110 4 * tiler_
->tile_size().width() * tiler_
->tile_size().height();
111 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
112 iter
!= tiler_
->tiles().end();
114 const DrawableTile
* tile
= static_cast<DrawableTile
*>(iter
->second
);
116 if (!tile
->resource_id())
118 amount
+= kMemoryUsagePerTileInBytes
;
123 void TiledLayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
124 LayerImpl::PushPropertiesTo(layer
);
126 TiledLayerImpl
* tiled_layer
= static_cast<TiledLayerImpl
*>(layer
);
128 tiled_layer
->set_skips_draw(skips_draw_
);
129 tiled_layer
->SetTilingData(*tiler_
);
131 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
132 iter
!= tiler_
->tiles().end();
134 int i
= iter
->first
.first
;
135 int j
= iter
->first
.second
;
136 DrawableTile
* tile
= static_cast<DrawableTile
*>(iter
->second
);
138 tiled_layer
->PushTileProperties(i
,
142 tile
->contents_swizzled());
146 bool TiledLayerImpl::WillDraw(DrawMode draw_mode
,
147 ResourceProvider
* resource_provider
) {
148 if (!tiler_
|| tiler_
->has_empty_bounds() ||
149 visible_content_rect().IsEmpty() ||
150 draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
)
152 return LayerImpl::WillDraw(draw_mode
, resource_provider
);
155 void TiledLayerImpl::AppendQuads(
156 RenderPass
* render_pass
,
157 const OcclusionTracker
<LayerImpl
>& occlusion_tracker
,
158 AppendQuadsData
* append_quads_data
) {
160 DCHECK(!tiler_
->has_empty_bounds());
161 DCHECK(!visible_content_rect().IsEmpty());
163 gfx::Rect content_rect
= visible_content_rect();
164 SharedQuadState
* shared_quad_state
=
165 render_pass
->CreateAndAppendSharedQuadState();
166 PopulateSharedQuadState(shared_quad_state
);
168 AppendDebugBorderQuad(
169 render_pass
, content_bounds(), shared_quad_state
, append_quads_data
);
171 int left
, top
, right
, bottom
;
172 tiler_
->ContentRectToTileIndices(content_rect
, &left
, &top
, &right
, &bottom
);
174 if (ShowDebugBorders()) {
175 for (int j
= top
; j
<= bottom
; ++j
) {
176 for (int i
= left
; i
<= right
; ++i
) {
177 DrawableTile
* tile
= TileAt(i
, j
);
178 gfx::Rect tile_rect
= tiler_
->tile_bounds(i
, j
);
179 gfx::Rect visible_tile_rect
= tile_rect
;
180 SkColor border_color
;
183 if (skips_draw_
|| !tile
|| !tile
->resource_id()) {
184 border_color
= DebugColors::MissingTileBorderColor();
185 border_width
= DebugColors::MissingTileBorderWidth(layer_tree_impl());
187 border_color
= DebugColors::HighResTileBorderColor();
188 border_width
= DebugColors::HighResTileBorderWidth(layer_tree_impl());
190 DebugBorderDrawQuad
* debug_border_quad
=
191 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
192 debug_border_quad
->SetNew(shared_quad_state
,
204 for (int j
= top
; j
<= bottom
; ++j
) {
205 for (int i
= left
; i
<= right
; ++i
) {
206 DrawableTile
* tile
= TileAt(i
, j
);
207 gfx::Rect tile_rect
= tiler_
->tile_bounds(i
, j
);
208 gfx::Rect display_rect
= tile_rect
;
209 tile_rect
.Intersect(content_rect
);
212 if (tile_rect
.IsEmpty())
215 gfx::Rect visible_tile_rect
=
216 occlusion_tracker
.UnoccludedContentRect(tile_rect
, draw_transform());
217 if (visible_tile_rect
.IsEmpty())
220 if (!tile
|| !tile
->resource_id()) {
221 SkColor checker_color
;
222 if (ShowDebugBorders()) {
224 tile
? DebugColors::InvalidatedTileCheckerboardColor()
225 : DebugColors::EvictedTileCheckerboardColor();
227 checker_color
= DebugColors::DefaultCheckerboardColor();
230 CheckerboardDrawQuad
* checkerboard_quad
=
231 render_pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
232 checkerboard_quad
->SetNew(
233 shared_quad_state
, tile_rect
, visible_tile_rect
, checker_color
);
234 append_quads_data
->num_missing_tiles
++;
238 gfx::Rect tile_opaque_rect
=
239 contents_opaque() ? tile_rect
: gfx::IntersectRects(
240 tile
->opaque_rect(), tile_rect
);
242 // Keep track of how the top left has moved, so the texture can be
243 // offset the same amount.
244 gfx::Vector2d display_offset
= tile_rect
.origin() - display_rect
.origin();
245 gfx::Vector2d texture_offset
=
246 tiler_
->texture_offset(i
, j
) + display_offset
;
247 gfx::RectF tex_coord_rect
= gfx::RectF(tile_rect
.size()) + texture_offset
;
249 float tile_width
= static_cast<float>(tiler_
->tile_size().width());
250 float tile_height
= static_cast<float>(tiler_
->tile_size().height());
251 gfx::Size
texture_size(tile_width
, tile_height
);
253 TileDrawQuad
* quad
= render_pass
->CreateAndAppendDrawQuad
<TileDrawQuad
>();
254 quad
->SetNew(shared_quad_state
,
261 tile
->contents_swizzled());
266 void TiledLayerImpl::SetTilingData(const LayerTilingData
& tiler
) {
270 tiler_
= LayerTilingData::Create(tiler
.tile_size(),
271 tiler
.has_border_texels()
272 ? LayerTilingData::HAS_BORDER_TEXELS
273 : LayerTilingData::NO_BORDER_TEXELS
);
278 void TiledLayerImpl::PushTileProperties(
281 ResourceProvider::ResourceId resource_id
,
282 const gfx::Rect
& opaque_rect
,
283 bool contents_swizzled
) {
284 DrawableTile
* tile
= TileAt(i
, j
);
286 tile
= CreateTile(i
, j
);
287 tile
->set_resource_id(resource_id
);
288 tile
->set_opaque_rect(opaque_rect
);
289 tile
->set_contents_swizzled(contents_swizzled
);
292 void TiledLayerImpl::PushInvalidTile(int i
, int j
) {
293 DrawableTile
* tile
= TileAt(i
, j
);
295 tile
= CreateTile(i
, j
);
296 tile
->set_resource_id(0);
297 tile
->set_opaque_rect(gfx::Rect());
298 tile
->set_contents_swizzled(false);
301 Region
TiledLayerImpl::VisibleContentOpaqueRegion() const {
304 if (contents_opaque())
305 return visible_content_rect();
306 return tiler_
->OpaqueRegionInContentRect(visible_content_rect());
309 void TiledLayerImpl::ReleaseResources() {
313 const char* TiledLayerImpl::LayerTypeAsString() const {
314 return "cc::TiledLayerImpl";