summaryrefslogtreecommitdiffstats
path: root/ui/gfx/compositor/layer.cc
blob: ace17e8b960b7afb6c3e8660f0eb6eaa555965db (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 (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/gfx/canvas_skia.h"
#include "ui/gfx/point3.h"

namespace ui {

Layer::Layer(Compositor* compositor)
    : compositor_(compositor),
      texture_(compositor->CreateTexture()),
      parent_(NULL),
      visible_(true),
      fills_bounds_opaquely_(false),
      layer_updated_externally_(false),
      opacity_(1.0f),
      delegate_(NULL) {
}

Layer::Layer(Compositor* compositor, TextureParam texture_param)
    : compositor_(compositor),
      texture_(texture_param == LAYER_HAS_TEXTURE ?
          compositor->CreateTexture() : NULL),
      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;
}

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();
}

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::SetTransform(const ui::Transform& transform) {
  transform_ = transform;

  if (parent() && fills_bounds_opaquely_)
    parent()->RecomputeHole();
}

void Layer::SetBounds(const gfx::Rect& bounds) {
  bounds_ = bounds;

  if (parent() && fills_bounds_opaquely_)
    parent()->RecomputeHole();
}

// 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) {
  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);
  compositor_->SchedulePaint();
}

void Layer::Draw() {
  const float combined_opacity = GetCombinedOpacity();
  if (!texture_.get() || combined_opacity == 0.0f)
    return;

  UpdateLayerCanvas();

  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()));
  }

  // 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 = compositor_->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();
}

void Layer::SetOpacity(float alpha) {
  bool was_opaque = GetCombinedOpacity() == 1.0f;
  opacity_ = alpha;
  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));
    }
  }
}

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 = invalid_rect_.Intersect(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() {
  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();
      return;
    }
  }
  // no opaque child layers, set hole_rect_ to empty
  hole_rect_ = gfx::Rect();
}

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;
}

}  // namespace ui