c66e124ca92d0c906097f2d8e0ede1eef7ef1c5d
[platform/framework/web/crosswalk.git] / src / cc / layers / tiled_layer_impl.cc
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.
4
5 #include "cc/layers/tiled_layer_impl.h"
6
7 #include "base/basictypes.h"
8 #include "base/debug/trace_event_argument.h"
9 #include "base/strings/stringprintf.h"
10 #include "cc/base/math_util.h"
11 #include "cc/base/simple_enclosed_region.h"
12 #include "cc/debug/debug_colors.h"
13 #include "cc/layers/append_quads_data.h"
14 #include "cc/quads/checkerboard_draw_quad.h"
15 #include "cc/quads/debug_border_draw_quad.h"
16 #include "cc/quads/solid_color_draw_quad.h"
17 #include "cc/quads/tile_draw_quad.h"
18 #include "cc/resources/layer_tiling_data.h"
19 #include "cc/trees/occlusion.h"
20 #include "third_party/khronos/GLES2/gl2.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/gfx/geometry/quad_f.h"
23
24 namespace cc {
25
26 class DrawableTile : public LayerTilingData::Tile {
27  public:
28   static scoped_ptr<DrawableTile> Create() {
29     return make_scoped_ptr(new DrawableTile());
30   }
31
32   ResourceProvider::ResourceId resource_id() const { return resource_id_; }
33   void set_resource_id(ResourceProvider::ResourceId resource_id) {
34     resource_id_ = resource_id;
35   }
36   bool contents_swizzled() { return contents_swizzled_; }
37   void set_contents_swizzled(bool contents_swizzled) {
38     contents_swizzled_ = contents_swizzled;
39   }
40
41  private:
42   DrawableTile() : resource_id_(0), contents_swizzled_(false) {}
43
44   ResourceProvider::ResourceId resource_id_;
45   bool contents_swizzled_;
46
47   DISALLOW_COPY_AND_ASSIGN(DrawableTile);
48 };
49
50 TiledLayerImpl::TiledLayerImpl(LayerTreeImpl* tree_impl, int id)
51     : LayerImpl(tree_impl, id), skips_draw_(true) {}
52
53 TiledLayerImpl::~TiledLayerImpl() {
54 }
55
56 void TiledLayerImpl::GetContentsResourceId(
57     ResourceProvider::ResourceId* resource_id,
58     gfx::Size* resource_size) const {
59   // This function is only valid for single texture layers, e.g. masks.
60   DCHECK(tiler_);
61   // It's possible the mask layer is created but has no size or otherwise
62   // can't draw.
63   if (tiler_->num_tiles_x() == 0 || tiler_->num_tiles_y() == 0) {
64     *resource_id = 0;
65     return;
66   }
67
68   // Any other number of tiles other than 0 or 1 is incorrect for masks.
69   DCHECK_EQ(tiler_->num_tiles_x(), 1);
70   DCHECK_EQ(tiler_->num_tiles_y(), 1);
71
72   DrawableTile* tile = TileAt(0, 0);
73   *resource_id = tile ? tile->resource_id() : 0;
74   *resource_size = tiler_->tile_size();
75 }
76
77 bool TiledLayerImpl::HasTileAt(int i, int j) const {
78   return !!tiler_->TileAt(i, j);
79 }
80
81 bool TiledLayerImpl::HasResourceIdForTileAt(int i, int j) const {
82   return HasTileAt(i, j) && TileAt(i, j)->resource_id();
83 }
84
85 DrawableTile* TiledLayerImpl::TileAt(int i, int j) const {
86   return static_cast<DrawableTile*>(tiler_->TileAt(i, j));
87 }
88
89 DrawableTile* TiledLayerImpl::CreateTile(int i, int j) {
90   scoped_ptr<DrawableTile> tile(DrawableTile::Create());
91   DrawableTile* added_tile = tile.get();
92   tiler_->AddTile(tile.Pass(), i, j);
93
94   return added_tile;
95 }
96
97 void TiledLayerImpl::GetDebugBorderProperties(SkColor* color,
98                                               float* width) const {
99   *color = DebugColors::TiledContentLayerBorderColor();
100   *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
101 }
102
103 scoped_ptr<LayerImpl> TiledLayerImpl::CreateLayerImpl(
104     LayerTreeImpl* tree_impl) {
105   return TiledLayerImpl::Create(tree_impl, id());
106 }
107
108 void TiledLayerImpl::AsValueInto(base::debug::TracedValue* state) const {
109   LayerImpl::AsValueInto(state);
110   state->BeginArray("invalidation");
111   MathUtil::AddToTracedValue(update_rect(), state);
112   state->EndArray();
113 }
114
115 size_t TiledLayerImpl::GPUMemoryUsageInBytes() const {
116   size_t amount = 0;
117   const size_t kMemoryUsagePerTileInBytes =
118       4 * tiler_->tile_size().width() * tiler_->tile_size().height();
119   for (LayerTilingData::TileMap::const_iterator iter = tiler_->tiles().begin();
120        iter != tiler_->tiles().end();
121        ++iter) {
122     const DrawableTile* tile = static_cast<DrawableTile*>(iter->second);
123     DCHECK(tile);
124     if (!tile->resource_id())
125       continue;
126     amount += kMemoryUsagePerTileInBytes;
127   }
128   return amount;
129 }
130
131 void TiledLayerImpl::PushPropertiesTo(LayerImpl* layer) {
132   LayerImpl::PushPropertiesTo(layer);
133
134   TiledLayerImpl* tiled_layer = static_cast<TiledLayerImpl*>(layer);
135
136   tiled_layer->set_skips_draw(skips_draw_);
137   tiled_layer->SetTilingData(*tiler_);
138
139   for (LayerTilingData::TileMap::const_iterator iter = tiler_->tiles().begin();
140        iter != tiler_->tiles().end();
141        ++iter) {
142     int i = iter->first.first;
143     int j = iter->first.second;
144     DrawableTile* tile = static_cast<DrawableTile*>(iter->second);
145     DCHECK(tile);
146     tiled_layer->PushTileProperties(i,
147                                     j,
148                                     tile->resource_id(),
149                                     tile->contents_swizzled());
150   }
151 }
152
153 bool TiledLayerImpl::WillDraw(DrawMode draw_mode,
154                               ResourceProvider* resource_provider) {
155   if (!tiler_ || tiler_->has_empty_bounds() ||
156       visible_content_rect().IsEmpty() ||
157       draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
158     return false;
159   return LayerImpl::WillDraw(draw_mode, resource_provider);
160 }
161
162 void TiledLayerImpl::AppendQuads(RenderPass* render_pass,
163                                  const Occlusion& occlusion_in_content_space,
164                                  AppendQuadsData* append_quads_data) {
165   DCHECK(tiler_);
166   DCHECK(!tiler_->has_empty_bounds());
167   DCHECK(!visible_content_rect().IsEmpty());
168
169   gfx::Rect content_rect = visible_content_rect();
170   SharedQuadState* shared_quad_state =
171       render_pass->CreateAndAppendSharedQuadState();
172   PopulateSharedQuadState(shared_quad_state);
173
174   AppendDebugBorderQuad(
175       render_pass, content_bounds(), shared_quad_state, append_quads_data);
176
177   int left, top, right, bottom;
178   tiler_->ContentRectToTileIndices(content_rect, &left, &top, &right, &bottom);
179
180   if (ShowDebugBorders()) {
181     for (int j = top; j <= bottom; ++j) {
182       for (int i = left; i <= right; ++i) {
183         DrawableTile* tile = TileAt(i, j);
184         gfx::Rect tile_rect = tiler_->tile_bounds(i, j);
185         gfx::Rect visible_tile_rect = tile_rect;
186         SkColor border_color;
187         float border_width;
188
189         if (skips_draw_ || !tile || !tile->resource_id()) {
190           border_color = DebugColors::MissingTileBorderColor();
191           border_width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
192         } else {
193           border_color = DebugColors::HighResTileBorderColor();
194           border_width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
195         }
196         DebugBorderDrawQuad* debug_border_quad =
197             render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
198         debug_border_quad->SetNew(shared_quad_state,
199                                   tile_rect,
200                                   visible_tile_rect,
201                                   border_color,
202                                   border_width);
203       }
204     }
205   }
206
207   if (skips_draw_)
208     return;
209
210   for (int j = top; j <= bottom; ++j) {
211     for (int i = left; i <= right; ++i) {
212       DrawableTile* tile = TileAt(i, j);
213       gfx::Rect tile_rect = tiler_->tile_bounds(i, j);
214       gfx::Rect display_rect = tile_rect;
215       tile_rect.Intersect(content_rect);
216
217       // Skip empty tiles.
218       if (tile_rect.IsEmpty())
219         continue;
220
221       gfx::Rect visible_tile_rect =
222           occlusion_in_content_space.GetUnoccludedContentRect(tile_rect);
223       if (visible_tile_rect.IsEmpty())
224         continue;
225
226       if (!tile || !tile->resource_id()) {
227         SkColor checker_color;
228         if (ShowDebugBorders()) {
229           checker_color =
230               tile ? DebugColors::InvalidatedTileCheckerboardColor()
231                    : DebugColors::EvictedTileCheckerboardColor();
232         } else {
233           checker_color = DebugColors::DefaultCheckerboardColor();
234         }
235
236         CheckerboardDrawQuad* checkerboard_quad =
237             render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
238         checkerboard_quad->SetNew(
239             shared_quad_state, tile_rect, visible_tile_rect, checker_color);
240         append_quads_data->num_missing_tiles++;
241         continue;
242       }
243
244       gfx::Rect tile_opaque_rect = contents_opaque() ? tile_rect : gfx::Rect();
245
246       // Keep track of how the top left has moved, so the texture can be
247       // offset the same amount.
248       gfx::Vector2d display_offset = tile_rect.origin() - display_rect.origin();
249       gfx::Vector2d texture_offset =
250           tiler_->texture_offset(i, j) + display_offset;
251       gfx::RectF tex_coord_rect = gfx::RectF(tile_rect.size()) + texture_offset;
252
253       float tile_width = static_cast<float>(tiler_->tile_size().width());
254       float tile_height = static_cast<float>(tiler_->tile_size().height());
255       gfx::Size texture_size(tile_width, tile_height);
256
257       TileDrawQuad* quad = render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
258       quad->SetNew(shared_quad_state,
259                    tile_rect,
260                    tile_opaque_rect,
261                    visible_tile_rect,
262                    tile->resource_id(),
263                    tex_coord_rect,
264                    texture_size,
265                    tile->contents_swizzled());
266     }
267   }
268 }
269
270 void TiledLayerImpl::SetTilingData(const LayerTilingData& tiler) {
271   if (tiler_) {
272     tiler_->reset();
273   } else {
274     tiler_ = LayerTilingData::Create(tiler.tile_size(),
275                                      tiler.has_border_texels()
276                                          ? LayerTilingData::HAS_BORDER_TEXELS
277                                          : LayerTilingData::NO_BORDER_TEXELS);
278   }
279   *tiler_ = tiler;
280 }
281
282 void TiledLayerImpl::PushTileProperties(
283     int i,
284     int j,
285     ResourceProvider::ResourceId resource_id,
286     bool contents_swizzled) {
287   DrawableTile* tile = TileAt(i, j);
288   if (!tile)
289     tile = CreateTile(i, j);
290   tile->set_resource_id(resource_id);
291   tile->set_contents_swizzled(contents_swizzled);
292 }
293
294 void TiledLayerImpl::PushInvalidTile(int i, int j) {
295   DrawableTile* tile = TileAt(i, j);
296   if (!tile)
297     tile = CreateTile(i, j);
298   tile->set_resource_id(0);
299   tile->set_contents_swizzled(false);
300 }
301
302 SimpleEnclosedRegion TiledLayerImpl::VisibleContentOpaqueRegion() const {
303   if (skips_draw_)
304     return SimpleEnclosedRegion();
305   return LayerImpl::VisibleContentOpaqueRegion();
306 }
307
308 void TiledLayerImpl::ReleaseResources() {
309   tiler_->reset();
310 }
311
312 const char* TiledLayerImpl::LayerTypeAsString() const {
313   return "cc::TiledLayerImpl";
314 }
315
316 }  // namespace cc