Setup a experiment to enable background tracing.
[chromium-blink-merge.git] / cc / blink / web_external_texture_layer_impl.cc
blob0b6bbdbfd3dcb3d58e3f265c36b339d14d291248
1 // Copyright 2014 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.
5 #include "cc/blink/web_external_texture_layer_impl.h"
7 #include "cc/blink/web_external_bitmap_impl.h"
8 #include "cc/blink/web_layer_impl.h"
9 #include "cc/layers/texture_layer.h"
10 #include "cc/resources/resource_update_queue.h"
11 #include "cc/resources/single_release_callback.h"
12 #include "cc/resources/texture_mailbox.h"
13 #include "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
14 #include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
15 #include "third_party/WebKit/public/platform/WebFloatRect.h"
16 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
17 #include "third_party/WebKit/public/platform/WebSize.h"
18 #include "third_party/khronos/GLES2/gl2.h"
20 using cc::TextureLayer;
21 using cc::ResourceUpdateQueue;
23 namespace cc_blink {
25 WebExternalTextureLayerImpl::WebExternalTextureLayerImpl(
26 blink::WebExternalTextureLayerClient* client)
27 : client_(client) {
28 cc::TextureLayerClient* cc_client = client_ ? this : nullptr;
29 scoped_refptr<TextureLayer> layer =
30 TextureLayer::CreateForMailbox(WebLayerImpl::LayerSettings(), cc_client);
31 layer->SetIsDrawable(true);
32 layer_.reset(new WebLayerImpl(layer));
35 WebExternalTextureLayerImpl::~WebExternalTextureLayerImpl() {
36 static_cast<TextureLayer*>(layer_->layer())->ClearClient();
39 blink::WebLayer* WebExternalTextureLayerImpl::layer() {
40 return layer_.get();
43 void WebExternalTextureLayerImpl::clearTexture() {
44 TextureLayer* layer = static_cast<TextureLayer*>(layer_->layer());
45 layer->ClearTexture();
48 void WebExternalTextureLayerImpl::setOpaque(bool opaque) {
49 static_cast<TextureLayer*>(layer_->layer())->SetContentsOpaque(opaque);
52 void WebExternalTextureLayerImpl::setPremultipliedAlpha(
53 bool premultiplied_alpha) {
54 static_cast<TextureLayer*>(layer_->layer())
55 ->SetPremultipliedAlpha(premultiplied_alpha);
58 void WebExternalTextureLayerImpl::setBlendBackgroundColor(bool blend) {
59 static_cast<TextureLayer*>(layer_->layer())->SetBlendBackgroundColor(blend);
62 void WebExternalTextureLayerImpl::setRateLimitContext(bool rate_limit) {
63 static_cast<TextureLayer*>(layer_->layer())->SetRateLimitContext(rate_limit);
66 void WebExternalTextureLayerImpl::setNearestNeighbor(bool nearest_neighbor) {
67 static_cast<TextureLayer*>(layer_->layer())
68 ->SetNearestNeighbor(nearest_neighbor);
71 bool WebExternalTextureLayerImpl::PrepareTextureMailbox(
72 cc::TextureMailbox* mailbox,
73 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
74 bool use_shared_memory) {
75 blink::WebExternalTextureMailbox client_mailbox;
76 WebExternalBitmapImpl* bitmap = nullptr;
78 if (use_shared_memory)
79 bitmap = AllocateBitmap();
80 if (!client_->prepareMailbox(&client_mailbox, bitmap)) {
81 if (bitmap)
82 free_bitmaps_.push_back(bitmap);
83 return false;
85 gpu::Mailbox name;
86 name.SetName(client_mailbox.name);
87 if (bitmap) {
88 *mailbox = cc::TextureMailbox(bitmap->shared_bitmap(), bitmap->size());
89 } else {
90 *mailbox =
91 cc::TextureMailbox(name, GL_TEXTURE_2D, client_mailbox.syncPoint);
93 mailbox->set_allow_overlay(client_mailbox.allowOverlay);
94 mailbox->set_nearest_neighbor(client_mailbox.nearestNeighbor);
96 if (mailbox->IsValid()) {
97 *release_callback = cc::SingleReleaseCallback::Create(
98 base::Bind(&WebExternalTextureLayerImpl::DidReleaseMailbox,
99 this->AsWeakPtr(),
100 client_mailbox,
101 bitmap));
104 return true;
107 WebExternalBitmapImpl* WebExternalTextureLayerImpl::AllocateBitmap() {
108 if (!free_bitmaps_.empty()) {
109 WebExternalBitmapImpl* result = free_bitmaps_.back();
110 free_bitmaps_.weak_erase(free_bitmaps_.end() - 1);
111 return result;
113 return new WebExternalBitmapImpl;
116 // static
117 void WebExternalTextureLayerImpl::DidReleaseMailbox(
118 base::WeakPtr<WebExternalTextureLayerImpl> layer,
119 const blink::WebExternalTextureMailbox& mailbox,
120 WebExternalBitmapImpl* bitmap,
121 unsigned sync_point,
122 bool lost_resource) {
123 DCHECK(layer);
124 blink::WebExternalTextureMailbox available_mailbox;
125 memcpy(available_mailbox.name, mailbox.name, sizeof(available_mailbox.name));
126 available_mailbox.syncPoint = sync_point;
127 if (bitmap)
128 layer->free_bitmaps_.push_back(bitmap);
129 layer->client_->mailboxReleased(available_mailbox, lost_resource);
132 } // namespace cc_blink