OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "cc/layers/texture_layer_impl.h" | |
6 | |
7 #include <vector> | |
8 | |
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" | |
17 | |
18 namespace cc { | |
19 | |
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; | |
35 } | |
36 | |
37 TextureLayerImpl::~TextureLayerImpl() { FreeTextureMailbox(); } | |
38 | |
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(); | |
49 } | |
50 | |
51 scoped_ptr<LayerImpl> TextureLayerImpl::CreateLayerImpl( | |
52 LayerTreeImpl* tree_impl) { | |
53 return TextureLayerImpl::Create(tree_impl, id()); | |
54 } | |
55 | |
56 void TextureLayerImpl::PushPropertiesTo(LayerImpl* layer) { | |
57 LayerImpl::PushPropertiesTo(layer); | |
58 | |
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; | |
71 } | |
72 } | |
73 | |
74 bool TextureLayerImpl::WillDraw(DrawMode draw_mode, | |
75 ResourceProvider* resource_provider) { | |
76 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) | |
77 return false; | |
78 | |
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; | |
90 } | |
91 if (external_texture_resource_) | |
92 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::TEXTURE_HINT_IMMUTABLE, | |
109 resource_provider->best_texture_format()); | |
110 } | |
111 | |
112 if (texture_copy_->id()) { | |
113 std::vector<uint8> swizzled; | |
114 uint8* pixels = texture_mailbox_.shared_bitmap()->pixels(); | |
115 | |
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]; | |
125 } | |
126 pixels = &swizzled[0]; | |
127 } | |
128 | |
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()); | |
135 | |
136 valid_texture_copy_ = true; | |
137 } | |
138 } | |
139 return (external_texture_resource_ || valid_texture_copy_) && | |
140 LayerImpl::WillDraw(draw_mode, resource_provider); | |
141 } | |
142 | |
143 void TextureLayerImpl::AppendQuads(RenderPass* render_pass, | |
144 AppendQuadsData* append_quads_data) { | |
145 DCHECK(external_texture_resource_ || valid_texture_copy_); | |
146 | |
147 SharedQuadState* shared_quad_state = | |
148 render_pass->CreateAndAppendSharedQuadState(); | |
149 PopulateSharedQuadState(shared_quad_state); | |
150 | |
151 AppendDebugBorderQuad( | |
152 render_pass, content_bounds(), shared_quad_state, append_quads_data); | |
153 | |
154 SkColor bg_color = blend_background_color_ ? | |
155 background_color() : SK_ColorTRANSPARENT; | |
156 bool opaque = contents_opaque() || (SkColorGetA(bg_color) == 0xFF); | |
157 | |
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; | |
165 | |
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, | |
174 id, | |
175 premultiplied_alpha_, | |
176 uv_top_left_, | |
177 uv_bottom_right_, | |
178 bg_color, | |
179 vertex_opacity_, | |
180 flipped_, | |
181 nearest_neighbor_); | |
182 } | |
183 | |
184 SimpleEnclosedRegion TextureLayerImpl::VisibleContentOpaqueRegion() const { | |
185 if (contents_opaque()) | |
186 return SimpleEnclosedRegion(visible_content_rect()); | |
187 | |
188 if (blend_background_color_ && (SkColorGetA(background_color()) == 0xFF)) | |
189 return SimpleEnclosedRegion(visible_content_rect()); | |
190 | |
191 return SimpleEnclosedRegion(); | |
192 } | |
193 | |
194 void TextureLayerImpl::ReleaseResources() { | |
195 FreeTextureMailbox(); | |
196 texture_copy_ = nullptr; | |
197 external_texture_resource_ = 0; | |
198 valid_texture_copy_ = false; | |
199 } | |
200 | |
201 void TextureLayerImpl::SetPremultipliedAlpha(bool premultiplied_alpha) { | |
202 premultiplied_alpha_ = premultiplied_alpha; | |
203 SetNeedsPushProperties(); | |
204 } | |
205 | |
206 void TextureLayerImpl::SetBlendBackgroundColor(bool blend) { | |
207 blend_background_color_ = blend; | |
208 SetNeedsPushProperties(); | |
209 } | |
210 | |
211 void TextureLayerImpl::SetFlipped(bool flipped) { | |
212 flipped_ = flipped; | |
213 SetNeedsPushProperties(); | |
214 } | |
215 | |
216 void TextureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) { | |
217 nearest_neighbor_ = nearest_neighbor; | |
218 SetNeedsPushProperties(); | |
219 } | |
220 | |
221 void TextureLayerImpl::SetUVTopLeft(const gfx::PointF top_left) { | |
222 uv_top_left_ = top_left; | |
223 SetNeedsPushProperties(); | |
224 } | |
225 | |
226 void TextureLayerImpl::SetUVBottomRight(const gfx::PointF bottom_right) { | |
227 uv_bottom_right_ = bottom_right; | |
228 SetNeedsPushProperties(); | |
229 } | |
230 | |
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(); | |
240 } | |
241 | |
242 const char* TextureLayerImpl::LayerTypeAsString() const { | |
243 return "cc::TextureLayerImpl"; | |
244 } | |
245 | |
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()); | |
253 } | |
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; | |
262 } | |
263 } | |
264 | |
265 } // namespace cc | |
OLD | NEW |