summaryrefslogtreecommitdiff
path: root/chromium/cc/layers/texture_layer.cc
blob: 2325a80c793f3ca6e615f3069261e89c7a84dda1 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2010 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/layers/texture_layer.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "cc/layers/texture_layer_client.h"
#include "cc/layers/texture_layer_impl.h"
#include "cc/trees/layer_tree_host.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"

namespace cc {

scoped_refptr<TextureLayer> TextureLayer::Create(TextureLayerClient* client) {
  return scoped_refptr<TextureLayer>(new TextureLayer(client, false));
}

scoped_refptr<TextureLayer> TextureLayer::CreateForMailbox(
    TextureLayerClient* client) {
  return scoped_refptr<TextureLayer>(new TextureLayer(client, true));
}

TextureLayer::TextureLayer(TextureLayerClient* client, bool uses_mailbox)
    : Layer(),
      client_(client),
      uses_mailbox_(uses_mailbox),
      flipped_(true),
      uv_top_left_(0.f, 0.f),
      uv_bottom_right_(1.f, 1.f),
      premultiplied_alpha_(true),
      blend_background_color_(false),
      rate_limit_context_(false),
      content_committed_(false),
      texture_id_(0),
      needs_set_mailbox_(false) {
  vertex_opacity_[0] = 1.0f;
  vertex_opacity_[1] = 1.0f;
  vertex_opacity_[2] = 1.0f;
  vertex_opacity_[3] = 1.0f;
}

TextureLayer::~TextureLayer() {
}

void TextureLayer::ClearClient() {
  if (rate_limit_context_ && client_ && layer_tree_host())
    layer_tree_host()->StopRateLimiter(client_->Context3d());
  client_ = NULL;
  if (uses_mailbox_)
    SetTextureMailbox(TextureMailbox());
  else
    SetTextureId(0);
}

scoped_ptr<LayerImpl> TextureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
  return TextureLayerImpl::Create(tree_impl, id(), uses_mailbox_).
      PassAs<LayerImpl>();
}

void TextureLayer::SetFlipped(bool flipped) {
  if (flipped_ == flipped)
    return;
  flipped_ = flipped;
  SetNeedsCommit();
}

void TextureLayer::SetUV(gfx::PointF top_left, gfx::PointF bottom_right) {
  if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right)
    return;
  uv_top_left_ = top_left;
  uv_bottom_right_ = bottom_right;
  SetNeedsCommit();
}

void TextureLayer::SetVertexOpacity(float bottom_left,
                                    float top_left,
                                    float top_right,
                                    float bottom_right) {
  // Indexing according to the quad vertex generation:
  // 1--2
  // |  |
  // 0--3
  if (vertex_opacity_[0] == bottom_left &&
      vertex_opacity_[1] == top_left &&
      vertex_opacity_[2] == top_right &&
      vertex_opacity_[3] == bottom_right)
    return;
  vertex_opacity_[0] = bottom_left;
  vertex_opacity_[1] = top_left;
  vertex_opacity_[2] = top_right;
  vertex_opacity_[3] = bottom_right;
  SetNeedsCommit();
}

void TextureLayer::SetPremultipliedAlpha(bool premultiplied_alpha) {
  if (premultiplied_alpha_ == premultiplied_alpha)
    return;
  premultiplied_alpha_ = premultiplied_alpha;
  SetNeedsCommit();
}

void TextureLayer::SetBlendBackgroundColor(bool blend) {
  if (blend_background_color_ == blend)
    return;
  blend_background_color_ = blend;
  SetNeedsCommit();
}

void TextureLayer::SetRateLimitContext(bool rate_limit) {
  if (!rate_limit && rate_limit_context_ && client_ && layer_tree_host())
    layer_tree_host()->StopRateLimiter(client_->Context3d());

  rate_limit_context_ = rate_limit;
}

void TextureLayer::SetTextureId(unsigned id) {
  DCHECK(!uses_mailbox_);
  if (texture_id_ == id)
    return;
  if (texture_id_ && layer_tree_host())
    layer_tree_host()->AcquireLayerTextures();
  texture_id_ = id;
  SetNeedsCommit();
}

void TextureLayer::SetTextureMailbox(const TextureMailbox& mailbox) {
  DCHECK(uses_mailbox_);
  DCHECK(!mailbox.IsValid() || !holder_ref_ ||
         !mailbox.Equals(holder_ref_->holder()->mailbox()));
  // If we never commited the mailbox, we need to release it here.
  if (mailbox.IsValid())
    holder_ref_ = MailboxHolder::Create(mailbox);
  else
    holder_ref_.reset();
  needs_set_mailbox_ = true;
  SetNeedsCommit();
}

void TextureLayer::WillModifyTexture() {
  if (layer_tree_host() && (DrawsContent() || content_committed_)) {
    layer_tree_host()->AcquireLayerTextures();
    content_committed_ = false;
  }
}

void TextureLayer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
  Layer::SetNeedsDisplayRect(dirty_rect);

  if (rate_limit_context_ && client_ && layer_tree_host() && DrawsContent())
    layer_tree_host()->StartRateLimiter(client_->Context3d());
}

void TextureLayer::SetLayerTreeHost(LayerTreeHost* host) {
  if (layer_tree_host() == host) {
    Layer::SetLayerTreeHost(host);
    return;
  }

  if (layer_tree_host()) {
    if (texture_id_)
      layer_tree_host()->AcquireLayerTextures();
    if (rate_limit_context_ && client_)
      layer_tree_host()->StopRateLimiter(client_->Context3d());
  }
  // If we're removed from the tree, the TextureLayerImpl will be destroyed, and
  // we will need to set the mailbox again on a new TextureLayerImpl the next
  // time we push.
  if (!host && uses_mailbox_ && holder_ref_)
    needs_set_mailbox_ = true;
  Layer::SetLayerTreeHost(host);
}

bool TextureLayer::DrawsContent() const {
  return (client_ || texture_id_ || holder_ref_) && Layer::DrawsContent();
}

bool TextureLayer::Update(ResourceUpdateQueue* queue,
                          const OcclusionTracker* occlusion) {
  bool updated = Layer::Update(queue, occlusion);
  if (client_) {
    if (uses_mailbox_) {
      TextureMailbox mailbox;
      if (client_->PrepareTextureMailbox(
              &mailbox, layer_tree_host()->UsingSharedMemoryResources())) {
        SetTextureMailbox(mailbox);
        updated = true;
      }
    } else {
      DCHECK(client_->Context3d());
      texture_id_ = client_->PrepareTexture();
      if (client_->Context3d() &&
          client_->Context3d()->getGraphicsResetStatusARB() != GL_NO_ERROR)
        texture_id_ = 0;
      updated = true;
    }
  }

  // SetTextureMailbox could be called externally and the same mailbox used for
  // different textures.  Such callers notify this layer that the texture has
  // changed by calling SetNeedsDisplay, so check for that here.
  return updated || !update_rect_.IsEmpty();
}

void TextureLayer::PushPropertiesTo(LayerImpl* layer) {
  Layer::PushPropertiesTo(layer);

  TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer);
  texture_layer->set_flipped(flipped_);
  texture_layer->set_uv_top_left(uv_top_left_);
  texture_layer->set_uv_bottom_right(uv_bottom_right_);
  texture_layer->set_vertex_opacity(vertex_opacity_);
  texture_layer->set_premultiplied_alpha(premultiplied_alpha_);
  texture_layer->set_blend_background_color(blend_background_color_);
  if (uses_mailbox_ && needs_set_mailbox_) {
    TextureMailbox texture_mailbox;
    if (holder_ref_) {
      MailboxHolder* holder = holder_ref_->holder();
      TextureMailbox::ReleaseCallback callback =
          holder->GetCallbackForImplThread();
      texture_mailbox = holder->mailbox().CopyWithNewCallback(callback);
    }
    texture_layer->SetTextureMailbox(texture_mailbox);
    needs_set_mailbox_ = false;
  } else {
    texture_layer->set_texture_id(texture_id_);
  }
  content_committed_ = DrawsContent();
}

Region TextureLayer::VisibleContentOpaqueRegion() const {
  if (contents_opaque())
    return visible_content_rect();

  if (blend_background_color_ && (SkColorGetA(background_color()) == 0xFF))
    return visible_content_rect();

  return Region();
}

bool TextureLayer::BlocksPendingCommit() const {
  // Double-buffered texture layers need to be blocked until they can be made
  // triple-buffered.  Single-buffered layers already prevent draws, so
  // can block too for simplicity.
  return DrawsContent();
}

bool TextureLayer::CanClipSelf() const {
  return true;
}

TextureLayer::MailboxHolder::MainThreadReference::MainThreadReference(
    MailboxHolder* holder)
    : holder_(holder) {
  holder_->InternalAddRef();
}

TextureLayer::MailboxHolder::MainThreadReference::~MainThreadReference() {
  holder_->InternalRelease();
}

TextureLayer::MailboxHolder::MailboxHolder(const TextureMailbox& mailbox)
    : message_loop_(base::MessageLoopProxy::current()),
      internal_references_(0),
      mailbox_(mailbox),
      sync_point_(mailbox.sync_point()),
      is_lost_(false) {
}

TextureLayer::MailboxHolder::~MailboxHolder() {
  DCHECK_EQ(0u, internal_references_);
}

scoped_ptr<TextureLayer::MailboxHolder::MainThreadReference>
TextureLayer::MailboxHolder::Create(const TextureMailbox& mailbox) {
  return scoped_ptr<MainThreadReference>(new MainThreadReference(
      new MailboxHolder(mailbox)));
}

void TextureLayer::MailboxHolder::Return(unsigned sync_point, bool is_lost) {
  sync_point_ = sync_point;
  is_lost_ = is_lost;
}

TextureMailbox::ReleaseCallback
TextureLayer::MailboxHolder::GetCallbackForImplThread() {
  // We can't call GetCallbackForImplThread if we released the main thread
  // reference.
  DCHECK_GT(internal_references_, 0u);
  InternalAddRef();
  return base::Bind(&MailboxHolder::ReturnAndReleaseOnImplThread, this);
}

void TextureLayer::MailboxHolder::InternalAddRef() {
  ++internal_references_;
}

void TextureLayer::MailboxHolder::InternalRelease() {
  DCHECK(message_loop_->BelongsToCurrentThread());
  if (!--internal_references_) {
    mailbox_.RunReleaseCallback(sync_point_, is_lost_);
    mailbox_ = TextureMailbox();
  }
}

void TextureLayer::MailboxHolder::ReturnAndReleaseOnMainThread(
    unsigned sync_point, bool is_lost) {
  DCHECK(message_loop_->BelongsToCurrentThread());
  Return(sync_point, is_lost);
  InternalRelease();
}

void TextureLayer::MailboxHolder::ReturnAndReleaseOnImplThread(
    unsigned sync_point, bool is_lost) {
  message_loop_->PostTask(FROM_HERE, base::Bind(
      &MailboxHolder::ReturnAndReleaseOnMainThread,
      this, sync_point, is_lost));
}

}  // namespace cc