OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/layers/texture_layer_impl.h" | 5 #include "cc/layers/texture_layer_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "cc/layers/quad_sink.h" | 10 #include "cc/layers/quad_sink.h" |
11 #include "cc/output/renderer.h" | 11 #include "cc/output/renderer.h" |
12 #include "cc/quads/texture_draw_quad.h" | 12 #include "cc/quads/texture_draw_quad.h" |
13 #include "cc/resources/platform_color.h" | 13 #include "cc/resources/platform_color.h" |
14 #include "cc/resources/scoped_resource.h" | 14 #include "cc/resources/scoped_resource.h" |
15 #include "cc/resources/single_release_callback.h" | 15 #include "cc/resources/single_release_callback.h" |
16 #include "cc/trees/layer_tree_impl.h" | 16 #include "cc/trees/layer_tree_impl.h" |
17 | 17 |
18 namespace cc { | 18 namespace cc { |
19 | 19 |
20 TextureLayerImpl::TextureLayerImpl(LayerTreeImpl* tree_impl, | 20 TextureLayerImpl::TextureLayerImpl(LayerTreeImpl* tree_impl, |
21 int id, | 21 int id) |
22 bool uses_mailbox) | |
23 : LayerImpl(tree_impl, id), | 22 : LayerImpl(tree_impl, id), |
24 texture_id_(0), | |
25 external_texture_resource_(0), | 23 external_texture_resource_(0), |
26 premultiplied_alpha_(true), | 24 premultiplied_alpha_(true), |
27 blend_background_color_(false), | 25 blend_background_color_(false), |
28 flipped_(true), | 26 flipped_(true), |
29 uv_top_left_(0.f, 0.f), | 27 uv_top_left_(0.f, 0.f), |
30 uv_bottom_right_(1.f, 1.f), | 28 uv_bottom_right_(1.f, 1.f), |
31 uses_mailbox_(uses_mailbox), | |
32 own_mailbox_(false), | 29 own_mailbox_(false), |
33 valid_texture_copy_(false) { | 30 valid_texture_copy_(false) { |
34 vertex_opacity_[0] = 1.0f; | 31 vertex_opacity_[0] = 1.0f; |
35 vertex_opacity_[1] = 1.0f; | 32 vertex_opacity_[1] = 1.0f; |
36 vertex_opacity_[2] = 1.0f; | 33 vertex_opacity_[2] = 1.0f; |
37 vertex_opacity_[3] = 1.0f; | 34 vertex_opacity_[3] = 1.0f; |
38 } | 35 } |
39 | 36 |
40 TextureLayerImpl::~TextureLayerImpl() { FreeTextureMailbox(); } | 37 TextureLayerImpl::~TextureLayerImpl() { FreeTextureMailbox(); } |
41 | 38 |
42 void TextureLayerImpl::SetTextureMailbox( | 39 void TextureLayerImpl::SetTextureMailbox( |
43 const TextureMailbox& mailbox, | 40 const TextureMailbox& mailbox, |
44 scoped_ptr<SingleReleaseCallback> release_callback) { | 41 scoped_ptr<SingleReleaseCallback> release_callback) { |
45 DCHECK(uses_mailbox_); | |
46 DCHECK_EQ(mailbox.IsValid(), !!release_callback); | 42 DCHECK_EQ(mailbox.IsValid(), !!release_callback); |
47 FreeTextureMailbox(); | 43 FreeTextureMailbox(); |
48 texture_mailbox_ = mailbox; | 44 texture_mailbox_ = mailbox; |
49 release_callback_ = release_callback.Pass(); | 45 release_callback_ = release_callback.Pass(); |
50 own_mailbox_ = true; | 46 own_mailbox_ = true; |
51 valid_texture_copy_ = false; | 47 valid_texture_copy_ = false; |
52 SetNeedsPushProperties(); | 48 SetNeedsPushProperties(); |
53 } | 49 } |
54 | 50 |
55 scoped_ptr<LayerImpl> TextureLayerImpl::CreateLayerImpl( | 51 scoped_ptr<LayerImpl> TextureLayerImpl::CreateLayerImpl( |
56 LayerTreeImpl* tree_impl) { | 52 LayerTreeImpl* tree_impl) { |
57 return TextureLayerImpl::Create(tree_impl, id(), uses_mailbox_). | 53 return TextureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); |
58 PassAs<LayerImpl>(); | |
59 } | 54 } |
60 | 55 |
61 void TextureLayerImpl::PushPropertiesTo(LayerImpl* layer) { | 56 void TextureLayerImpl::PushPropertiesTo(LayerImpl* layer) { |
62 LayerImpl::PushPropertiesTo(layer); | 57 LayerImpl::PushPropertiesTo(layer); |
63 | 58 |
64 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer); | 59 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer); |
65 texture_layer->SetFlipped(flipped_); | 60 texture_layer->SetFlipped(flipped_); |
66 texture_layer->SetUVTopLeft(uv_top_left_); | 61 texture_layer->SetUVTopLeft(uv_top_left_); |
67 texture_layer->SetUVBottomRight(uv_bottom_right_); | 62 texture_layer->SetUVBottomRight(uv_bottom_right_); |
68 texture_layer->SetVertexOpacity(vertex_opacity_); | 63 texture_layer->SetVertexOpacity(vertex_opacity_); |
69 texture_layer->SetPremultipliedAlpha(premultiplied_alpha_); | 64 texture_layer->SetPremultipliedAlpha(premultiplied_alpha_); |
70 texture_layer->SetBlendBackgroundColor(blend_background_color_); | 65 texture_layer->SetBlendBackgroundColor(blend_background_color_); |
71 if (uses_mailbox_ && own_mailbox_) { | 66 if (own_mailbox_) { |
72 texture_layer->SetTextureMailbox(texture_mailbox_, | 67 texture_layer->SetTextureMailbox(texture_mailbox_, |
73 release_callback_.Pass()); | 68 release_callback_.Pass()); |
74 own_mailbox_ = false; | 69 own_mailbox_ = false; |
75 } else { | |
76 texture_layer->SetTextureId(texture_id_); | |
77 } | 70 } |
78 } | 71 } |
79 | 72 |
80 bool TextureLayerImpl::WillDraw(DrawMode draw_mode, | 73 bool TextureLayerImpl::WillDraw(DrawMode draw_mode, |
81 ResourceProvider* resource_provider) { | 74 ResourceProvider* resource_provider) { |
82 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) | 75 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) |
83 return false; | 76 return false; |
84 | 77 |
85 if (uses_mailbox_) { | 78 if (own_mailbox_) { |
86 if (own_mailbox_) { | 79 DCHECK(!external_texture_resource_); |
87 DCHECK(!external_texture_resource_); | 80 if ((draw_mode == DRAW_MODE_HARDWARE && texture_mailbox_.IsTexture()) || |
88 if ((draw_mode == DRAW_MODE_HARDWARE && texture_mailbox_.IsTexture()) || | 81 (draw_mode == DRAW_MODE_SOFTWARE && |
89 (draw_mode == DRAW_MODE_SOFTWARE && | 82 texture_mailbox_.IsSharedMemory())) { |
90 texture_mailbox_.IsSharedMemory())) { | 83 external_texture_resource_ = |
91 external_texture_resource_ = | 84 resource_provider->CreateResourceFromTextureMailbox( |
92 resource_provider->CreateResourceFromTextureMailbox( | 85 texture_mailbox_, |
93 texture_mailbox_, | 86 release_callback_.Pass()); |
94 release_callback_.Pass()); | 87 DCHECK(external_texture_resource_); |
95 DCHECK(external_texture_resource_); | 88 texture_copy_.reset(); |
96 texture_copy_.reset(); | 89 valid_texture_copy_ = false; |
97 valid_texture_copy_ = false; | 90 } |
98 } | 91 if (external_texture_resource_) |
99 if (external_texture_resource_) | 92 own_mailbox_ = false; |
100 own_mailbox_ = false; | 93 } |
| 94 |
| 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(); |
| 105 |
| 106 if (!texture_copy_->id()) { |
| 107 texture_copy_->Allocate(texture_mailbox_.shared_memory_size(), |
| 108 ResourceProvider::TextureUsageAny, |
| 109 resource_provider->best_texture_format()); |
101 } | 110 } |
102 | 111 |
103 if (!valid_texture_copy_ && draw_mode == DRAW_MODE_HARDWARE && | 112 if (texture_copy_->id()) { |
104 texture_mailbox_.IsSharedMemory()) { | 113 std::vector<uint8> swizzled; |
105 DCHECK(!external_texture_resource_); | 114 uint8* pixels = |
106 // Have to upload a copy to a texture for it to be used in a | 115 static_cast<uint8*>(texture_mailbox_.shared_memory()->memory()); |
107 // hardware draw. | |
108 if (!texture_copy_) | |
109 texture_copy_ = ScopedResource::Create(resource_provider); | |
110 if (texture_copy_->size() != texture_mailbox_.shared_memory_size() || | |
111 resource_provider->InUseByConsumer(texture_copy_->id())) | |
112 texture_copy_->Free(); | |
113 | 116 |
114 if (!texture_copy_->id()) { | 117 if (!PlatformColor::SameComponentOrder(texture_copy_->format())) { |
115 texture_copy_->Allocate(texture_mailbox_.shared_memory_size(), | 118 // Swizzle colors. This is slow, but should be really uncommon. |
116 ResourceProvider::TextureUsageAny, | 119 size_t bytes = texture_mailbox_.SharedMemorySizeInBytes(); |
117 resource_provider->best_texture_format()); | 120 swizzled.resize(bytes); |
| 121 for (size_t i = 0; i < bytes; i += 4) { |
| 122 swizzled[i] = pixels[i + 2]; |
| 123 swizzled[i + 1] = pixels[i + 1]; |
| 124 swizzled[i + 2] = pixels[i]; |
| 125 swizzled[i + 3] = pixels[i + 3]; |
| 126 } |
| 127 pixels = &swizzled[0]; |
118 } | 128 } |
119 | 129 |
120 if (texture_copy_->id()) { | 130 resource_provider->SetPixels( |
121 std::vector<uint8> swizzled; | 131 texture_copy_->id(), |
122 uint8* pixels = | 132 pixels, |
123 static_cast<uint8*>(texture_mailbox_.shared_memory()->memory()); | 133 gfx::Rect(texture_mailbox_.shared_memory_size()), |
| 134 gfx::Rect(texture_mailbox_.shared_memory_size()), |
| 135 gfx::Vector2d()); |
124 | 136 |
125 if (!PlatformColor::SameComponentOrder(texture_copy_->format())) { | 137 valid_texture_copy_ = true; |
126 // Swizzle colors. This is slow, but should be really uncommon. | |
127 size_t bytes = texture_mailbox_.SharedMemorySizeInBytes(); | |
128 swizzled.resize(bytes); | |
129 for (size_t i = 0; i < bytes; i += 4) { | |
130 swizzled[i] = pixels[i + 2]; | |
131 swizzled[i + 1] = pixels[i + 1]; | |
132 swizzled[i + 2] = pixels[i]; | |
133 swizzled[i + 3] = pixels[i + 3]; | |
134 } | |
135 pixels = &swizzled[0]; | |
136 } | |
137 | |
138 resource_provider->SetPixels( | |
139 texture_copy_->id(), | |
140 pixels, | |
141 gfx::Rect(texture_mailbox_.shared_memory_size()), | |
142 gfx::Rect(texture_mailbox_.shared_memory_size()), | |
143 gfx::Vector2d()); | |
144 | |
145 valid_texture_copy_ = true; | |
146 } | |
147 } | |
148 } else if (texture_id_) { | |
149 DCHECK(!external_texture_resource_); | |
150 if (draw_mode == DRAW_MODE_HARDWARE) { | |
151 external_texture_resource_ = | |
152 resource_provider->CreateResourceFromExternalTexture( | |
153 GL_TEXTURE_2D, | |
154 texture_id_); | |
155 } | 138 } |
156 } | 139 } |
157 return (external_texture_resource_ || valid_texture_copy_) && | 140 return (external_texture_resource_ || valid_texture_copy_) && |
158 LayerImpl::WillDraw(draw_mode, resource_provider); | 141 LayerImpl::WillDraw(draw_mode, resource_provider); |
159 } | 142 } |
160 | 143 |
161 void TextureLayerImpl::AppendQuads(QuadSink* quad_sink, | 144 void TextureLayerImpl::AppendQuads(QuadSink* quad_sink, |
162 AppendQuadsData* append_quads_data) { | 145 AppendQuadsData* append_quads_data) { |
163 DCHECK(external_texture_resource_ || valid_texture_copy_); | 146 DCHECK(external_texture_resource_ || valid_texture_copy_); |
164 | 147 |
(...skipping 22 matching lines...) Expand all Loading... |
187 id, | 170 id, |
188 premultiplied_alpha_, | 171 premultiplied_alpha_, |
189 uv_top_left_, | 172 uv_top_left_, |
190 uv_bottom_right_, | 173 uv_bottom_right_, |
191 bg_color, | 174 bg_color, |
192 vertex_opacity_, | 175 vertex_opacity_, |
193 flipped_); | 176 flipped_); |
194 quad_sink->Append(quad.PassAs<DrawQuad>()); | 177 quad_sink->Append(quad.PassAs<DrawQuad>()); |
195 } | 178 } |
196 | 179 |
197 void TextureLayerImpl::DidDraw(ResourceProvider* resource_provider) { | |
198 LayerImpl::DidDraw(resource_provider); | |
199 if (uses_mailbox_ || !external_texture_resource_) | |
200 return; | |
201 // TODO(danakj): the following assert will not be true when sending resources | |
202 // to a parent compositor. A synchronization scheme (double-buffering or | |
203 // pipelining of updates) for the client will need to exist to solve this. | |
204 DCHECK(!resource_provider->InUseByConsumer(external_texture_resource_)); | |
205 resource_provider->DeleteResource(external_texture_resource_); | |
206 external_texture_resource_ = 0; | |
207 } | |
208 | |
209 Region TextureLayerImpl::VisibleContentOpaqueRegion() const { | 180 Region TextureLayerImpl::VisibleContentOpaqueRegion() const { |
210 if (contents_opaque()) | 181 if (contents_opaque()) |
211 return visible_content_rect(); | 182 return visible_content_rect(); |
212 | 183 |
213 if (blend_background_color_ && (SkColorGetA(background_color()) == 0xFF)) | 184 if (blend_background_color_ && (SkColorGetA(background_color()) == 0xFF)) |
214 return visible_content_rect(); | 185 return visible_content_rect(); |
215 | 186 |
216 return Region(); | 187 return Region(); |
217 } | 188 } |
218 | 189 |
219 void TextureLayerImpl::ReleaseResources() { | 190 void TextureLayerImpl::ReleaseResources() { |
220 if (external_texture_resource_ && !uses_mailbox_) { | 191 FreeTextureMailbox(); |
221 ResourceProvider* resource_provider = | |
222 layer_tree_impl()->resource_provider(); | |
223 resource_provider->DeleteResource(external_texture_resource_); | |
224 } else { | |
225 FreeTextureMailbox(); | |
226 } | |
227 texture_copy_.reset(); | 192 texture_copy_.reset(); |
228 texture_id_ = 0; | |
229 external_texture_resource_ = 0; | 193 external_texture_resource_ = 0; |
230 valid_texture_copy_ = false; | 194 valid_texture_copy_ = false; |
231 } | 195 } |
232 | 196 |
233 void TextureLayerImpl::SetTextureId(unsigned id) { | |
234 texture_id_ = id; | |
235 SetNeedsPushProperties(); | |
236 } | |
237 | |
238 void TextureLayerImpl::SetPremultipliedAlpha(bool premultiplied_alpha) { | 197 void TextureLayerImpl::SetPremultipliedAlpha(bool premultiplied_alpha) { |
239 premultiplied_alpha_ = premultiplied_alpha; | 198 premultiplied_alpha_ = premultiplied_alpha; |
240 SetNeedsPushProperties(); | 199 SetNeedsPushProperties(); |
241 } | 200 } |
242 | 201 |
243 void TextureLayerImpl::SetBlendBackgroundColor(bool blend) { | 202 void TextureLayerImpl::SetBlendBackgroundColor(bool blend) { |
244 blend_background_color_ = blend; | 203 blend_background_color_ = blend; |
245 SetNeedsPushProperties(); | 204 SetNeedsPushProperties(); |
246 } | 205 } |
247 | 206 |
(...skipping 21 matching lines...) Expand all Loading... |
269 vertex_opacity_[2] = vertex_opacity[2]; | 228 vertex_opacity_[2] = vertex_opacity[2]; |
270 vertex_opacity_[3] = vertex_opacity[3]; | 229 vertex_opacity_[3] = vertex_opacity[3]; |
271 SetNeedsPushProperties(); | 230 SetNeedsPushProperties(); |
272 } | 231 } |
273 | 232 |
274 const char* TextureLayerImpl::LayerTypeAsString() const { | 233 const char* TextureLayerImpl::LayerTypeAsString() const { |
275 return "cc::TextureLayerImpl"; | 234 return "cc::TextureLayerImpl"; |
276 } | 235 } |
277 | 236 |
278 void TextureLayerImpl::FreeTextureMailbox() { | 237 void TextureLayerImpl::FreeTextureMailbox() { |
279 if (!uses_mailbox_) | |
280 return; | |
281 if (own_mailbox_) { | 238 if (own_mailbox_) { |
282 DCHECK(!external_texture_resource_); | 239 DCHECK(!external_texture_resource_); |
283 if (release_callback_) | 240 if (release_callback_) |
284 release_callback_->Run(texture_mailbox_.sync_point(), false); | 241 release_callback_->Run(texture_mailbox_.sync_point(), false); |
285 texture_mailbox_ = TextureMailbox(); | 242 texture_mailbox_ = TextureMailbox(); |
286 release_callback_.reset(); | 243 release_callback_.reset(); |
287 } else if (external_texture_resource_) { | 244 } else if (external_texture_resource_) { |
288 DCHECK(!own_mailbox_); | 245 DCHECK(!own_mailbox_); |
289 ResourceProvider* resource_provider = | 246 ResourceProvider* resource_provider = |
290 layer_tree_impl()->resource_provider(); | 247 layer_tree_impl()->resource_provider(); |
291 resource_provider->DeleteResource(external_texture_resource_); | 248 resource_provider->DeleteResource(external_texture_resource_); |
292 external_texture_resource_ = 0; | 249 external_texture_resource_ = 0; |
293 } | 250 } |
294 } | 251 } |
295 | 252 |
296 } // namespace cc | 253 } // namespace cc |
OLD | NEW |