cc: Make VideoResourceUpdater use CopyToResource instead of SetPixels.
[chromium-blink-merge.git] / cc / layers / texture_layer_impl.cc
blobd9ddee7d27fda903fb5a23cbe039dd6586da5b20
1 // Copyright 2011 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/layers/texture_layer_impl.h"
7 #include <vector>
9 #include "base/strings/stringprintf.h"
10 #include "cc/output/renderer.h"
11 #include "cc/quads/texture_draw_quad.h"
12 #include "cc/resources/platform_color.h"
13 #include "cc/resources/scoped_resource.h"
14 #include "cc/resources/single_release_callback_impl.h"
15 #include "cc/trees/layer_tree_impl.h"
16 #include "cc/trees/occlusion.h"
18 namespace cc {
20 TextureLayerImpl::TextureLayerImpl(LayerTreeImpl* tree_impl, int id)
21 : LayerImpl(tree_impl, id),
22 external_texture_resource_(0),
23 premultiplied_alpha_(true),
24 blend_background_color_(false),
25 flipped_(true),
26 nearest_neighbor_(false),
27 uv_top_left_(0.f, 0.f),
28 uv_bottom_right_(1.f, 1.f),
29 own_mailbox_(false),
30 valid_texture_copy_(false) {
31 vertex_opacity_[0] = 1.0f;
32 vertex_opacity_[1] = 1.0f;
33 vertex_opacity_[2] = 1.0f;
34 vertex_opacity_[3] = 1.0f;
37 TextureLayerImpl::~TextureLayerImpl() { FreeTextureMailbox(); }
39 void TextureLayerImpl::SetTextureMailbox(
40 const TextureMailbox& mailbox,
41 scoped_ptr<SingleReleaseCallbackImpl> release_callback) {
42 DCHECK_EQ(mailbox.IsValid(), !!release_callback);
43 FreeTextureMailbox();
44 texture_mailbox_ = mailbox;
45 release_callback_ = release_callback.Pass();
46 own_mailbox_ = true;
47 valid_texture_copy_ = false;
48 SetNeedsPushProperties();
51 scoped_ptr<LayerImpl> TextureLayerImpl::CreateLayerImpl(
52 LayerTreeImpl* tree_impl) {
53 return TextureLayerImpl::Create(tree_impl, id());
56 void TextureLayerImpl::PushPropertiesTo(LayerImpl* layer) {
57 LayerImpl::PushPropertiesTo(layer);
59 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer);
60 texture_layer->SetFlipped(flipped_);
61 texture_layer->SetUVTopLeft(uv_top_left_);
62 texture_layer->SetUVBottomRight(uv_bottom_right_);
63 texture_layer->SetVertexOpacity(vertex_opacity_);
64 texture_layer->SetPremultipliedAlpha(premultiplied_alpha_);
65 texture_layer->SetBlendBackgroundColor(blend_background_color_);
66 texture_layer->SetNearestNeighbor(nearest_neighbor_);
67 if (own_mailbox_) {
68 texture_layer->SetTextureMailbox(texture_mailbox_,
69 release_callback_.Pass());
70 own_mailbox_ = false;
74 bool TextureLayerImpl::WillDraw(DrawMode draw_mode,
75 ResourceProvider* resource_provider) {
76 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
77 return false;
79 if (own_mailbox_) {
80 DCHECK(!external_texture_resource_);
81 if ((draw_mode == DRAW_MODE_HARDWARE && texture_mailbox_.IsTexture()) ||
82 (draw_mode == DRAW_MODE_SOFTWARE &&
83 texture_mailbox_.IsSharedMemory())) {
84 external_texture_resource_ =
85 resource_provider->CreateResourceFromTextureMailbox(
86 texture_mailbox_, release_callback_.Pass());
87 DCHECK(external_texture_resource_);
88 texture_copy_ = nullptr;
89 valid_texture_copy_ = false;
91 if (external_texture_resource_)
92 own_mailbox_ = false;
95 if (!valid_texture_copy_ && draw_mode == DRAW_MODE_HARDWARE &&
96 texture_mailbox_.IsSharedMemory()) {
97 DCHECK(!external_texture_resource_);
98 // Have to upload a copy to a texture for it to be used in a
99 // hardware draw.
100 if (!texture_copy_)
101 texture_copy_ = ScopedResource::Create(resource_provider);
102 if (texture_copy_->size() != texture_mailbox_.shared_memory_size() ||
103 resource_provider->InUseByConsumer(texture_copy_->id()))
104 texture_copy_->Free();
106 if (!texture_copy_->id()) {
107 texture_copy_->Allocate(texture_mailbox_.shared_memory_size(),
108 ResourceProvider::TextureHintImmutable,
109 resource_provider->best_texture_format());
112 if (texture_copy_->id()) {
113 std::vector<uint8> swizzled;
114 uint8* pixels = texture_mailbox_.shared_bitmap()->pixels();
116 if (!PlatformColor::SameComponentOrder(texture_copy_->format())) {
117 // Swizzle colors. This is slow, but should be really uncommon.
118 size_t bytes = texture_mailbox_.SharedMemorySizeInBytes();
119 swizzled.resize(bytes);
120 for (size_t i = 0; i < bytes; i += 4) {
121 swizzled[i] = pixels[i + 2];
122 swizzled[i + 1] = pixels[i + 1];
123 swizzled[i + 2] = pixels[i];
124 swizzled[i + 3] = pixels[i + 3];
126 pixels = &swizzled[0];
129 resource_provider->SetPixels(
130 texture_copy_->id(),
131 pixels,
132 gfx::Rect(texture_mailbox_.shared_memory_size()),
133 gfx::Rect(texture_mailbox_.shared_memory_size()),
134 gfx::Vector2d());
136 valid_texture_copy_ = true;
139 return (external_texture_resource_ || valid_texture_copy_) &&
140 LayerImpl::WillDraw(draw_mode, resource_provider);
143 void TextureLayerImpl::AppendQuads(RenderPass* render_pass,
144 AppendQuadsData* append_quads_data) {
145 DCHECK(external_texture_resource_ || valid_texture_copy_);
147 SharedQuadState* shared_quad_state =
148 render_pass->CreateAndAppendSharedQuadState();
149 PopulateSharedQuadState(shared_quad_state);
151 AppendDebugBorderQuad(
152 render_pass, content_bounds(), shared_quad_state, append_quads_data);
154 SkColor bg_color = blend_background_color_ ?
155 background_color() : SK_ColorTRANSPARENT;
156 bool opaque = contents_opaque() || (SkColorGetA(bg_color) == 0xFF);
158 gfx::Rect quad_rect(content_bounds());
159 gfx::Rect opaque_rect = opaque ? quad_rect : gfx::Rect();
160 gfx::Rect visible_quad_rect =
161 draw_properties().occlusion_in_content_space.GetUnoccludedContentRect(
162 quad_rect);
163 if (visible_quad_rect.IsEmpty())
164 return;
166 TextureDrawQuad* quad =
167 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
168 ResourceProvider::ResourceId id =
169 valid_texture_copy_ ? texture_copy_->id() : external_texture_resource_;
170 quad->SetNew(shared_quad_state,
171 quad_rect,
172 opaque_rect,
173 visible_quad_rect,
175 premultiplied_alpha_,
176 uv_top_left_,
177 uv_bottom_right_,
178 bg_color,
179 vertex_opacity_,
180 flipped_,
181 nearest_neighbor_);
184 SimpleEnclosedRegion TextureLayerImpl::VisibleContentOpaqueRegion() const {
185 if (contents_opaque())
186 return SimpleEnclosedRegion(visible_content_rect());
188 if (blend_background_color_ && (SkColorGetA(background_color()) == 0xFF))
189 return SimpleEnclosedRegion(visible_content_rect());
191 return SimpleEnclosedRegion();
194 void TextureLayerImpl::ReleaseResources() {
195 FreeTextureMailbox();
196 texture_copy_ = nullptr;
197 external_texture_resource_ = 0;
198 valid_texture_copy_ = false;
201 void TextureLayerImpl::SetPremultipliedAlpha(bool premultiplied_alpha) {
202 premultiplied_alpha_ = premultiplied_alpha;
203 SetNeedsPushProperties();
206 void TextureLayerImpl::SetBlendBackgroundColor(bool blend) {
207 blend_background_color_ = blend;
208 SetNeedsPushProperties();
211 void TextureLayerImpl::SetFlipped(bool flipped) {
212 flipped_ = flipped;
213 SetNeedsPushProperties();
216 void TextureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) {
217 nearest_neighbor_ = nearest_neighbor;
218 SetNeedsPushProperties();
221 void TextureLayerImpl::SetUVTopLeft(const gfx::PointF top_left) {
222 uv_top_left_ = top_left;
223 SetNeedsPushProperties();
226 void TextureLayerImpl::SetUVBottomRight(const gfx::PointF bottom_right) {
227 uv_bottom_right_ = bottom_right;
228 SetNeedsPushProperties();
231 // 1--2
232 // | |
233 // 0--3
234 void TextureLayerImpl::SetVertexOpacity(const float vertex_opacity[4]) {
235 vertex_opacity_[0] = vertex_opacity[0];
236 vertex_opacity_[1] = vertex_opacity[1];
237 vertex_opacity_[2] = vertex_opacity[2];
238 vertex_opacity_[3] = vertex_opacity[3];
239 SetNeedsPushProperties();
242 const char* TextureLayerImpl::LayerTypeAsString() const {
243 return "cc::TextureLayerImpl";
246 void TextureLayerImpl::FreeTextureMailbox() {
247 if (own_mailbox_) {
248 DCHECK(!external_texture_resource_);
249 if (release_callback_) {
250 release_callback_->Run(texture_mailbox_.sync_point(),
251 false,
252 layer_tree_impl()->BlockingMainThreadTaskRunner());
254 texture_mailbox_ = TextureMailbox();
255 release_callback_ = nullptr;
256 } else if (external_texture_resource_) {
257 DCHECK(!own_mailbox_);
258 ResourceProvider* resource_provider =
259 layer_tree_impl()->resource_provider();
260 resource_provider->DeleteResource(external_texture_resource_);
261 external_texture_resource_ = 0;
265 } // namespace cc