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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
// Copyright (c) 2011 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 "ui/gfx/compositor/layer.h"
#include <algorithm>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "ui/base/animation/animation.h"
#include "ui/gfx/compositor/layer_animator.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/point3.h"
namespace ui {
Layer::Layer(Compositor* compositor)
: type_(LAYER_HAS_TEXTURE),
compositor_(compositor),
parent_(NULL),
visible_(true),
fills_bounds_opaquely_(false),
layer_updated_externally_(false),
opacity_(1.0f),
delegate_(NULL) {
}
Layer::Layer(Compositor* compositor, LayerType type)
: type_(type),
compositor_(compositor),
parent_(NULL),
visible_(true),
fills_bounds_opaquely_(false),
layer_updated_externally_(false),
opacity_(1.0f),
delegate_(NULL) {
}
Layer::~Layer() {
if (parent_)
parent_->Remove(this);
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->parent_ = NULL;
}
Compositor* Layer::GetCompositor() {
return compositor_ ? compositor_ : parent_ ? parent_->GetCompositor() : NULL;
}
void Layer::SetCompositor(Compositor* compositor) {
// This function must only be called once, with a valid compositor, and only
// for the compositor's root layer.
DCHECK(!compositor_);
DCHECK(compositor);
DCHECK_EQ(compositor->root_layer(), this);
compositor_ = compositor;
}
void Layer::Add(Layer* child) {
if (child->parent_)
child->parent_->Remove(child);
child->parent_ = this;
children_.push_back(child);
if (child->fills_bounds_opaquely())
RecomputeHole();
}
void Layer::Remove(Layer* child) {
std::vector<Layer*>::iterator i =
std::find(children_.begin(), children_.end(), child);
DCHECK(i != children_.end());
children_.erase(i);
child->parent_ = NULL;
if (child->fills_bounds_opaquely())
RecomputeHole();
child->DropTextures();
}
bool Layer::Contains(const Layer* other) const {
for (const Layer* parent = other; parent; parent = parent->parent()) {
if (parent == this)
return true;
}
return false;
}
void Layer::SetAnimation(Animation* animation) {
if (animation) {
if (!animator_.get())
animator_.reset(new LayerAnimator(this));
animation->Start();
animator_->SetAnimation(animation);
} else {
animator_.reset();
}
}
void Layer::SetTransform(const ui::Transform& transform) {
StopAnimatingIfNecessary(LayerAnimator::TRANSFORM);
if (animator_.get() && animator_->IsRunning()) {
animator_->AnimateTransform(transform);
return;
}
SetTransformImmediately(transform);
}
void Layer::SetBounds(const gfx::Rect& bounds) {
StopAnimatingIfNecessary(LayerAnimator::LOCATION);
if (animator_.get() && animator_->IsRunning() &&
bounds.size() == bounds_.size()) {
animator_->AnimateToPoint(bounds.origin());
return;
}
SetBoundsImmediately(bounds);
}
void Layer::SetOpacity(float opacity) {
StopAnimatingIfNecessary(LayerAnimator::OPACITY);
if (animator_.get() && animator_->IsRunning()) {
animator_->AnimateOpacity(opacity);
return;
}
SetOpacityImmediately(opacity);
}
void Layer::SetVisible(bool visible) {
visible_ = visible;
if (!visible_)
DropTextures();
}
bool Layer::ShouldDraw() {
return type_ == LAYER_HAS_TEXTURE && GetCombinedOpacity() > 0.0f &&
!hole_rect_.Contains(gfx::Rect(gfx::Point(0, 0), bounds_.size()));
}
// static
void Layer::ConvertPointToLayer(const Layer* source,
const Layer* target,
gfx::Point* point) {
const Layer* inner = NULL;
const Layer* outer = NULL;
if (source->Contains(target)) {
inner = target;
outer = source;
inner->ConvertPointFromAncestor(outer, point);
} else if (target->Contains(source)) {
inner = source;
outer = target;
inner->ConvertPointForAncestor(outer, point);
} else {
NOTREACHED(); // |source| and |target| are in unrelated hierarchies.
}
}
void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) {
if (fills_bounds_opaquely_ == fills_bounds_opaquely)
return;
fills_bounds_opaquely_ = fills_bounds_opaquely;
if (parent())
parent()->RecomputeHole();
}
void Layer::SetExternalTexture(ui::Texture* texture) {
DCHECK(texture);
layer_updated_externally_ = true;
texture_ = texture;
}
void Layer::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) {
DCHECK_EQ(type_, LAYER_HAS_TEXTURE);
if (!texture_.get())
texture_ = GetCompositor()->CreateTexture();
texture_->SetCanvas(canvas, origin, bounds_.size());
invalid_rect_ = gfx::Rect();
}
void Layer::SchedulePaint(const gfx::Rect& invalid_rect) {
invalid_rect_ = invalid_rect_.Union(invalid_rect);
if (GetCompositor())
GetCompositor()->SchedulePaint();
}
void Layer::Draw() {
DCHECK(GetCompositor());
if (!ShouldDraw())
return;
UpdateLayerCanvas();
// Layer drew nothing, no texture was created.
if (!texture_.get())
return;
ui::TextureDrawParams texture_draw_params;
for (Layer* layer = this; layer; layer = layer->parent_) {
texture_draw_params.transform.ConcatTransform(layer->transform_);
texture_draw_params.transform.ConcatTranslate(
static_cast<float>(layer->bounds_.x()),
static_cast<float>(layer->bounds_.y()));
}
const float combined_opacity = GetCombinedOpacity();
// Only blend for transparent child layers (and when we're forcing
// transparency). The root layer will clobber the cleared bg.
const bool is_root = parent_ == NULL;
const bool forcing_transparency = combined_opacity < 1.0f;
const bool is_opaque = fills_bounds_opaquely_ || !has_valid_alpha_channel();
texture_draw_params.blend = !is_root && (forcing_transparency || !is_opaque);
texture_draw_params.compositor_size = GetCompositor()->size();
texture_draw_params.opacity = combined_opacity;
texture_draw_params.has_valid_alpha_channel = has_valid_alpha_channel();
hole_rect_ = hole_rect_.Intersect(
gfx::Rect(0, 0, bounds_.width(), bounds_.height()));
if (hole_rect_.IsEmpty()) {
DrawRegion(texture_draw_params,
gfx::Rect(0, 0, bounds_.width(), bounds_.height()));
} else {
// Top (above the hole).
DrawRegion(texture_draw_params, gfx::Rect(0,
0,
bounds_.width(),
hole_rect_.y()));
// Left (of the hole).
DrawRegion(texture_draw_params, gfx::Rect(0,
hole_rect_.y(),
hole_rect_.x(),
hole_rect_.height()));
// Right (of the hole).
DrawRegion(texture_draw_params, gfx::Rect(
hole_rect_.right(),
hole_rect_.y(),
bounds_.width() - hole_rect_.right(),
hole_rect_.height()));
// Bottom (below the hole).
DrawRegion(texture_draw_params, gfx::Rect(
0,
hole_rect_.bottom(),
bounds_.width(),
bounds_.height() - hole_rect_.bottom()));
}
}
void Layer::DrawTree() {
if (!visible_)
return;
Draw();
for (size_t i = 0; i < children_.size(); ++i)
children_.at(i)->DrawTree();
}
float Layer::GetCombinedOpacity() const {
float opacity = opacity_;
Layer* current = this->parent_;
while (current) {
opacity *= current->opacity_;
current = current->parent_;
}
return opacity;
}
void Layer::DrawRegion(const ui::TextureDrawParams& params,
const gfx::Rect& region_to_draw) {
if (!region_to_draw.IsEmpty())
texture_->Draw(params, region_to_draw);
}
void Layer::UpdateLayerCanvas() {
// If we have no delegate, that means that whoever constructed the Layer is
// setting its canvas directly with SetCanvas().
if (!delegate_ || layer_updated_externally_)
return;
gfx::Rect local_bounds = gfx::Rect(gfx::Point(), bounds_.size());
gfx::Rect draw_rect = texture_.get() ? invalid_rect_.Intersect(local_bounds) :
local_bounds;
if (draw_rect.IsEmpty()) {
invalid_rect_ = gfx::Rect();
return;
}
scoped_ptr<gfx::Canvas> canvas(gfx::Canvas::CreateCanvas(
draw_rect.width(), draw_rect.height(), false));
canvas->TranslateInt(-draw_rect.x(), -draw_rect.y());
delegate_->OnPaintLayer(canvas.get());
SetCanvas(*canvas->AsCanvasSkia(), draw_rect.origin());
}
void Layer::RecomputeHole() {
if (type_ == LAYER_HAS_NO_TEXTURE)
return;
// If there are no opaque child layers, hole_rect_ should remain empty.
hole_rect_ = gfx::Rect();
for (size_t i = 0; i < children_.size(); ++i) {
if (children_[i]->fills_bounds_opaquely() &&
children_[i]->GetCombinedOpacity() == 1.0f &&
!children_[i]->transform().HasChange()) {
hole_rect_ = children_[i]->bounds();
break;
}
}
// Free up texture memory if the hole fills bounds of layer
if (!ShouldDraw() && !layer_updated_externally_)
texture_ = NULL;
}
void Layer::DropTextures() {
if (!layer_updated_externally_)
texture_ = NULL;
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->DropTextures();
}
bool Layer::ConvertPointForAncestor(const Layer* ancestor,
gfx::Point* point) const {
ui::Transform transform;
bool result = GetTransformRelativeTo(ancestor, &transform);
gfx::Point3f p(*point);
transform.TransformPoint(p);
*point = p.AsPoint();
return result;
}
bool Layer::ConvertPointFromAncestor(const Layer* ancestor,
gfx::Point* point) const {
ui::Transform transform;
bool result = GetTransformRelativeTo(ancestor, &transform);
gfx::Point3f p(*point);
transform.TransformPointReverse(p);
*point = p.AsPoint();
return result;
}
bool Layer::GetTransformRelativeTo(const Layer* ancestor,
ui::Transform* transform) const {
const Layer* p = this;
for (; p && p != ancestor; p = p->parent()) {
if (p->transform().HasChange())
transform->ConcatTransform(p->transform());
transform->ConcatTranslate(static_cast<float>(p->bounds().x()),
static_cast<float>(p->bounds().y()));
}
return p == ancestor;
}
void Layer::StopAnimatingIfNecessary(
LayerAnimator::AnimationProperty property) {
if (!animator_.get() || !animator_->IsRunning() ||
!animator_->got_initial_tick()) {
return;
}
if (property != LayerAnimator::LOCATION &&
animator_->IsAnimating(LayerAnimator::LOCATION)) {
SetBoundsImmediately(
gfx::Rect(animator_->GetTargetPoint(), bounds_.size()));
}
if (property != LayerAnimator::OPACITY &&
animator_->IsAnimating(LayerAnimator::OPACITY)) {
SetOpacityImmediately(animator_->GetTargetOpacity());
}
if (property != LayerAnimator::TRANSFORM &&
animator_->IsAnimating(LayerAnimator::TRANSFORM)) {
SetTransformImmediately(animator_->GetTargetTransform());
}
animator_.reset();
}
void Layer::SetBoundsImmediately(const gfx::Rect& bounds) {
bounds_ = bounds;
if (parent() && fills_bounds_opaquely_)
parent()->RecomputeHole();
}
void Layer::SetTransformImmediately(const ui::Transform& transform) {
transform_ = transform;
if (parent() && fills_bounds_opaquely_)
parent()->RecomputeHole();
}
void Layer::SetOpacityImmediately(float opacity) {
bool was_opaque = GetCombinedOpacity() == 1.0f;
opacity_ = opacity;
bool is_opaque = GetCombinedOpacity() == 1.0f;
// If our opacity has changed we need to recompute our hole, our parent's hole
// and the holes of all our descendants.
if (was_opaque != is_opaque) {
if (parent_)
parent_->RecomputeHole();
std::queue<Layer*> to_process;
to_process.push(this);
while (!to_process.empty()) {
Layer* current = to_process.front();
to_process.pop();
current->RecomputeHole();
for (size_t i = 0; i < current->children_.size(); ++i)
to_process.push(current->children_.at(i));
}
}
}
void Layer::SetBoundsFromAnimator(const gfx::Rect& bounds) {
SetBoundsImmediately(bounds);
}
void Layer::SetTransformFromAnimator(const Transform& transform) {
SetTransformImmediately(transform);
}
void Layer::SetOpacityFromAnimator(float opacity) {
SetOpacityImmediately(opacity);
}
} // namespace ui
|