summaryrefslogtreecommitdiffstats
path: root/cc/picture_layer_impl.cc
blob: e4c113a5aac5e9ce34d91998d77664cbe8e5b43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/picture_layer_impl.h"

#include "base/time.h"
#include "cc/append_quads_data.h"
#include "cc/checkerboard_draw_quad.h"
#include "cc/debug_border_draw_quad.h"
#include "cc/debug_colors.h"
#include "cc/layer_tree_impl.h"
#include "cc/math_util.h"
#include "cc/quad_sink.h"
#include "cc/solid_color_draw_quad.h"
#include "cc/tile_draw_quad.h"
#include "ui/gfx/quad_f.h"
#include "ui/gfx/size_conversions.h"

namespace cc {

PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* treeImpl, int id)
    : LayerImpl(treeImpl, id),
      tilings_(this),
      pile_(PicturePileImpl::Create()),
      last_update_time_(0),
      is_mask_(false) {
}

PictureLayerImpl::~PictureLayerImpl() {
}

const char* PictureLayerImpl::layerTypeAsString() const {
  return "PictureLayer";
}

void PictureLayerImpl::appendQuads(QuadSink& quadSink,
                                   AppendQuadsData& appendQuadsData) {

  const gfx::Rect& rect = visibleContentRect();
  gfx::Rect content_rect(gfx::Point(), contentBounds());

  SharedQuadState* sharedQuadState =
      quadSink.useSharedQuadState(createSharedQuadState());
  bool clipped = false;
  gfx::QuadF target_quad = MathUtil::mapQuad(
      drawTransform(),
      gfx::QuadF(rect),
      clipped);
  bool isAxisAlignedInTarget = !clipped && target_quad.IsRectilinear();
  bool useAA = !isAxisAlignedInTarget;

  if (showDebugBorders()) {
    for (PictureLayerTilingSet::Iterator iter(&tilings_,
                                              contentsScaleX(),
                                              rect);
         iter;
         ++iter) {
      SkColor color;
      float width;
      if (*iter && iter->GetResourceId()) {
        color = DebugColors::TileBorderColor();
        width = DebugColors::TileBorderWidth(layerTreeImpl());
      } else {
        color = DebugColors::MissingTileBorderColor();
        width = DebugColors::MissingTileBorderWidth(layerTreeImpl());
      }

      scoped_ptr<DebugBorderDrawQuad> debugBorderQuad =
          DebugBorderDrawQuad::Create();
      gfx::Rect geometry_rect = iter.geometry_rect();
      debugBorderQuad->SetNew(sharedQuadState, geometry_rect, color, width);
      quadSink.append(debugBorderQuad.PassAs<DrawQuad>(), appendQuadsData);
    }
  }

  for (PictureLayerTilingSet::Iterator iter(&tilings_, contentsScaleX(), rect);
       iter;
       ++iter) {
    ResourceProvider::ResourceId resource = 0;
    if (*iter)
      resource = iter->GetResourceId();

    gfx::Rect geometry_rect = iter.geometry_rect();

    if (!resource) {
      if (drawCheckerboardForMissingTiles()) {
        // TODO(enne): Figure out how to show debug "invalidated checker" color
        scoped_ptr<CheckerboardDrawQuad> quad = CheckerboardDrawQuad::Create();
        SkColor color = DebugColors::DefaultCheckerboardColor();
        quad->SetNew(sharedQuadState, geometry_rect, color);
        if (quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData))
            appendQuadsData.numMissingTiles++;
      } else {
        scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
        quad->SetNew(sharedQuadState, geometry_rect, backgroundColor());
        if (quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData))
            appendQuadsData.numMissingTiles++;
      }
      continue;
    }

    gfx::RectF texture_rect = iter.texture_rect();
    gfx::Rect opaque_rect = iter->opaque_rect();
    opaque_rect.Intersect(content_rect);

    bool outside_left_edge = geometry_rect.x() == content_rect.x();
    bool outside_top_edge = geometry_rect.y() == content_rect.y();
    bool outside_right_edge = geometry_rect.right() == content_rect.right();
    bool outside_bottom_edge = geometry_rect.bottom() == content_rect.bottom();

    scoped_ptr<TileDrawQuad> quad = TileDrawQuad::Create();
    quad->SetNew(sharedQuadState,
                 geometry_rect,
                 opaque_rect,
                 resource,
                 texture_rect,
                 iter.texture_size(),
                 iter->contents_swizzled(),
                 outside_left_edge && useAA,
                 outside_top_edge && useAA,
                 outside_right_edge && useAA,
                 outside_bottom_edge && useAA);
    quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
  }
}

void PictureLayerImpl::dumpLayerProperties(std::string*, int indent) const {
  // TODO(enne): implement me
}

void PictureLayerImpl::didUpdateTransforms() {
  gfx::Transform current_screen_space_transform =
      screenSpaceTransform();
  double current_time =
      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
  double time_delta = 0;
  if (last_update_time_ != 0 && last_bounds_ == bounds() &&
      last_content_bounds_ == contentBounds() &&
      last_content_scale_x_ == contentsScaleX() &&
      last_content_scale_y_ == contentsScaleY()) {
    time_delta = current_time - last_update_time_;
  }
  WhichTree tree = layerTreeImpl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
  tilings_.UpdateTilePriorities(
      tree,
      layerTreeImpl()->device_viewport_size(),
      contentsScaleX(),
      contentsScaleY(),
      last_screen_space_transform_,
      current_screen_space_transform,
      time_delta);

  last_screen_space_transform_ = current_screen_space_transform;
  last_update_time_ = current_time;
  last_bounds_ = bounds();
  last_content_bounds_ = contentBounds();
  last_content_scale_x_ = contentsScaleX();
  last_content_scale_y_ = contentsScaleY();
}

void PictureLayerImpl::calculateContentsScale(
    float ideal_contents_scale,
    float* contents_scale_x,
    float* contents_scale_y,
    gfx::Size* content_bounds) {
  if (!drawsContent()) {
    DCHECK(!tilings_.num_tilings());
    return;
  }

  float min_contents_scale = layerTreeImpl()->settings().minimumContentsScale;
  ideal_contents_scale = std::max(ideal_contents_scale, min_contents_scale);

  ManageTilings(ideal_contents_scale);

  // The content scale and bounds for a PictureLayerImpl is somewhat fictitious.
  // There are (usually) several tilings at different scales.  However, the
  // content bounds is the (integer!) space in which quads are generated.
  // In order to guarantee that we can fill this integer space with any set of
  // tilings (and then map back to floating point texture coordinates), the
  // contents scale must be at least as large as the largest of the tilings.
  float max_contents_scale = min_contents_scale;
  for (size_t i = 0; i < tilings_.num_tilings(); ++i) {
    const PictureLayerTiling* tiling = tilings_.tiling_at(i);
    max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
  }

  *contents_scale_x = max_contents_scale;
  *contents_scale_y = max_contents_scale;
  *content_bounds = gfx::ToCeiledSize(
      gfx::ScaleSize(bounds(), max_contents_scale, max_contents_scale));
}

scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
                                                 gfx::Rect rect) {
  TileManager* tile_manager = layerTreeImpl()->tile_manager();

  return make_scoped_refptr(new Tile(
      tile_manager,
      pile_.get(),
      rect.size(),
      GL_RGBA,
      rect,
      tiling->contents_scale()));
}

void PictureLayerImpl::SyncFromActiveLayer() {
  DCHECK(layerTreeImpl()->IsPendingTree());
  if (!drawsContent())
    return;

  // If there is an active tree version of this layer, get a copy of its
  // tiles.  This needs to be done last, after setting invalidation and the
  // pile.
  PictureLayerImpl* active_twin = static_cast<PictureLayerImpl*>(
      layerTreeImpl()->FindActiveTreeLayerById(id()));
  if (!active_twin)
    return;
  SyncFromActiveLayer(active_twin);
}

void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
  tilings_.CloneAll(other->tilings_, invalidation_);
}

void PictureLayerImpl::SyncTiling(
    const PictureLayerTiling* tiling) {
  tilings_.Clone(tiling, invalidation_);
}

void PictureLayerImpl::SetIsMask(bool is_mask) {
  if (is_mask_ == is_mask)
    return;
  is_mask_ = is_mask;
  tilings_.Reset();
}

ResourceProvider::ResourceId PictureLayerImpl::contentsResourceId() const {
  gfx::Rect content_rect(gfx::Point(), contentBounds());
  float scale = contentsScaleX();
  for (PictureLayerTilingSet::Iterator iter(&tilings_, scale, content_rect);
       iter;
       ++iter) {
    // Mask resource not ready yet.
    if (!*iter || !iter->GetResourceId())
      return 0;
    // Masks only supported if they fit on exactly one tile.
    if (iter.geometry_rect() != content_rect)
      return 0;
    return iter->GetResourceId();
  }
  return 0;
}

void PictureLayerImpl::AddTiling(float contents_scale, gfx::Size tile_size) {
  if (contents_scale < layerTreeImpl()->settings().minimumContentsScale)
    return;

  const PictureLayerTiling* tiling = tilings_.AddTiling(
      contents_scale,
      tile_size);

  // If a new tiling is created on the active tree, sync it to the pending tree
  // so that it can share the same tiles.
  if (layerTreeImpl()->IsPendingTree())
    return;

  PictureLayerImpl* pending_twin = static_cast<PictureLayerImpl*>(
      layerTreeImpl()->FindPendingTreeLayerById(id()));
  if (!pending_twin)
    return;
  DCHECK_EQ(id(), pending_twin->id());
  pending_twin->SyncTiling(tiling);
}

gfx::Size PictureLayerImpl::TileSize() const {
  if (is_mask_) {
    int max_size = layerTreeImpl()->MaxTextureSize();
    return gfx::Size(
        std::min(max_size, contentBounds().width()),
        std::min(max_size, contentBounds().height()));
  }

  return layerTreeImpl()->settings().defaultTileSize;
}

void PictureLayerImpl::ManageTilings(float ideal_contents_scale) {
  if (drawsContent()) {
    // TODO(enne): Add tilings during pinch zoom
    // TODO(enne): Consider culling old tilings after pinch finishes.
    if (!tilings_.num_tilings()) {
      AddTiling(ideal_contents_scale, TileSize());
      // TODO(enne): Add a low-res tiling as well.
    }
  } else {
    // TODO(enne): This should be unnecessary once there are two trees.
    tilings_.Reset();
  }
}

}  // namespace cc