Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1060)

Side by Side Diff: cc/layers/nine_patch_layer_impl.cc

Issue 22870016: Update the nine patch layer to use UI resources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor format change and changed initial value for fill_center_ in nine patch layer Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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/nine_patch_layer_impl.h" 5 #include "cc/layers/nine_patch_layer_impl.h"
6 6
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "cc/layers/quad_sink.h" 9 #include "cc/layers/quad_sink.h"
10 #include "cc/quads/texture_draw_quad.h" 10 #include "cc/quads/texture_draw_quad.h"
11 #include "cc/trees/layer_tree_impl.h"
11 #include "ui/gfx/rect_f.h" 12 #include "ui/gfx/rect_f.h"
12 13
13 namespace cc { 14 namespace cc {
14 15
15 NinePatchLayerImpl::NinePatchLayerImpl(LayerTreeImpl* tree_impl, int id) 16 NinePatchLayerImpl::NinePatchLayerImpl(LayerTreeImpl* tree_impl, int id)
16 : LayerImpl(tree_impl, id), 17 : LayerImpl(tree_impl, id),
17 resource_id_(0) {} 18 fill_center_(false),
19 ui_resource_id_(0) {}
18 20
19 NinePatchLayerImpl::~NinePatchLayerImpl() {} 21 NinePatchLayerImpl::~NinePatchLayerImpl() {}
20 22
21 ResourceProvider::ResourceId NinePatchLayerImpl::ContentsResourceId() const { 23 ResourceProvider::ResourceId NinePatchLayerImpl::ContentsResourceId() const {
22 return 0; 24 return 0;
23 } 25 }
24 26
25 scoped_ptr<LayerImpl> NinePatchLayerImpl::CreateLayerImpl( 27 scoped_ptr<LayerImpl> NinePatchLayerImpl::CreateLayerImpl(
26 LayerTreeImpl* tree_impl) { 28 LayerTreeImpl* tree_impl) {
27 return NinePatchLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); 29 return NinePatchLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
28 } 30 }
29 31
30 void NinePatchLayerImpl::PushPropertiesTo(LayerImpl* layer) { 32 void NinePatchLayerImpl::PushPropertiesTo(LayerImpl* layer) {
31 LayerImpl::PushPropertiesTo(layer); 33 LayerImpl::PushPropertiesTo(layer);
32 NinePatchLayerImpl* layer_impl = static_cast<NinePatchLayerImpl*>(layer); 34 NinePatchLayerImpl* layer_impl = static_cast<NinePatchLayerImpl*>(layer);
33 35
34 if (!resource_id_) 36 layer_impl->set_ui_resource_id(ui_resource_id_);
35 return; 37 layer_impl->SetLayout(image_bounds_, image_aperture_, border_, fill_center_);
36
37 layer_impl->SetResourceId(resource_id_);
38 layer_impl->SetLayout(image_bounds_, image_aperture_);
39 } 38 }
40 39
41 static gfx::RectF NormalizedRect(float x, 40 static gfx::RectF NormalizedRect(float x,
42 float y, 41 float y,
43 float width, 42 float width,
44 float height, 43 float height,
45 float total_width, 44 float total_width,
46 float total_height) { 45 float total_height) {
47 return gfx::RectF(x / total_width, 46 return gfx::RectF(x / total_width,
48 y / total_height, 47 y / total_height,
49 width / total_width, 48 width / total_width,
50 height / total_height); 49 height / total_height);
51 } 50 }
52 51
53 void NinePatchLayerImpl::SetLayout(gfx::Size image_bounds, gfx::Rect aperture) { 52 void NinePatchLayerImpl::SetLayout(gfx::Size image_bounds, gfx::Rect aperture,
53 gfx::Rect border, bool fill_center) {
54 image_bounds_ = image_bounds; 54 image_bounds_ = image_bounds;
55 image_aperture_ = aperture; 55 image_aperture_ = aperture;
56 border_ = border;
57 fill_center_ = fill_center;
56 } 58 }
57 59
58 bool NinePatchLayerImpl::WillDraw(DrawMode draw_mode, 60 bool NinePatchLayerImpl::WillDraw(DrawMode draw_mode,
59 ResourceProvider* resource_provider) { 61 ResourceProvider* resource_provider) {
60 if (!resource_id_ || draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) 62 if (!ui_resource_id_ || draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
61 return false; 63 return false;
62 return LayerImpl::WillDraw(draw_mode, resource_provider); 64 return LayerImpl::WillDraw(draw_mode, resource_provider);
63 } 65 }
64 66
65 void NinePatchLayerImpl::AppendQuads(QuadSink* quad_sink, 67 void NinePatchLayerImpl::AppendQuads(QuadSink* quad_sink,
66 AppendQuadsData* append_quads_data) { 68 AppendQuadsData* append_quads_data) {
67 DCHECK(resource_id_); 69 if (!ui_resource_id_ || !layer_tree_impl())
70 return;
71
72 ResourceProvider::ResourceId resource =
73 layer_tree_impl()->ResourceIdForUIResource(ui_resource_id_);
74
75 if (!resource)
76 return;
68 77
69 SharedQuadState* shared_quad_state = 78 SharedQuadState* shared_quad_state =
70 quad_sink->UseSharedQuadState(CreateSharedQuadState()); 79 quad_sink->UseSharedQuadState(CreateSharedQuadState());
71 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data); 80 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
72 81
73 static const bool flipped = false; 82 static const bool flipped = false;
74 static const bool premultiplied_alpha = true; 83 static const bool premultiplied_alpha = true;
75 84
76 DCHECK(!bounds().IsEmpty()); 85 DCHECK(!bounds().IsEmpty());
77 86
78 // NinePatch border widths in bitmap pixel space 87 // NinePatch border widths in bitmap pixel space
79 int left_width = image_aperture_.x(); 88 // NinePatch border widths in layer space
80 int top_height = image_aperture_.y(); 89 int left_width = border_.x();
81 int right_width = image_bounds_.width() - image_aperture_.right(); 90 int top_height = border_.y();
82 int bottom_height = image_bounds_.height() - image_aperture_.bottom(); 91 int right_width = border_.width() - left_width;
92 int bottom_height = border_.height() - top_height;
83 93
84 // If layer can't fit the corners, clip to show the outer edges of the 94 // If layer can't fit the corners, clip to show the outer edges of the
85 // image. 95 // image.
86 int corner_total_width = left_width + right_width; 96 int corner_total_width = left_width + right_width;
87 int middle_width = bounds().width() - corner_total_width; 97 int middle_width = bounds().width() - corner_total_width;
88 if (middle_width < 0) { 98 if (middle_width < 0) {
89 float left_width_proportion = 99 float left_width_proportion =
90 static_cast<float>(left_width) / corner_total_width; 100 static_cast<float>(left_width) / corner_total_width;
91 int left_width_crop = middle_width * left_width_proportion; 101 int left_width_crop = middle_width * left_width_proportion;
92 left_width += left_width_crop; 102 left_width += left_width_crop;
(...skipping 19 matching lines...) Expand all
112 0, bounds().height() - bottom_height, left_width, bottom_height); 122 0, bounds().height() - bottom_height, left_width, bottom_height);
113 gfx::Rect bottom_right( 123 gfx::Rect bottom_right(
114 top_right.x(), bottom_left.y(), right_width, bottom_height); 124 top_right.x(), bottom_left.y(), right_width, bottom_height);
115 gfx::Rect top(top_left.right(), 0, middle_width, top_height); 125 gfx::Rect top(top_left.right(), 0, middle_width, top_height);
116 gfx::Rect left(0, top_left.bottom(), left_width, middle_height); 126 gfx::Rect left(0, top_left.bottom(), left_width, middle_height);
117 gfx::Rect right(top_right.x(), 127 gfx::Rect right(top_right.x(),
118 top_right.bottom(), 128 top_right.bottom(),
119 right_width, 129 right_width,
120 left.height()); 130 left.height());
121 gfx::Rect bottom(top.x(), bottom_left.y(), top.width(), bottom_height); 131 gfx::Rect bottom(top.x(), bottom_left.y(), top.width(), bottom_height);
132 gfx::Rect center(left_width, top_height,
133 middle_width,
134 middle_height);
122 135
123 float img_width = image_bounds_.width(); 136 float img_width = image_bounds_.width();
124 float img_height = image_bounds_.height(); 137 float img_height = image_bounds_.height();
125 138
139 int aperture_left_width = image_aperture_.x();
140 int aperture_top_height = image_aperture_.y();
141 int aperture_right_width = img_width - image_aperture_.right();
142 int aperture_bottom_height = img_height - image_aperture_.bottom();
126 // Patch positions in bitmap UV space (from zero to one) 143 // Patch positions in bitmap UV space (from zero to one)
127 gfx::RectF uv_top_left = NormalizedRect(0, 144 gfx::RectF uv_top_left = NormalizedRect(
128 0, 145 0, 0, aperture_left_width, aperture_top_height, img_width, img_height);
129 left_width, 146 gfx::RectF uv_top_right = NormalizedRect(img_width - aperture_right_width,
130 top_height,
131 img_width,
132 img_height);
133 gfx::RectF uv_top_right = NormalizedRect(img_width - right_width,
134 0, 147 0,
135 right_width, 148 aperture_right_width,
136 top_height, 149 aperture_top_height,
137 img_width, 150 img_width,
138 img_height); 151 img_height);
139 gfx::RectF uv_bottom_left = NormalizedRect(0, 152 gfx::RectF uv_bottom_left =
140 img_height - bottom_height, 153 NormalizedRect(0,
141 left_width, 154 img_height - aperture_bottom_height,
142 bottom_height, 155 aperture_left_width,
143 img_width, 156 aperture_bottom_height,
144 img_height); 157 img_width,
145 gfx::RectF uv_bottom_right = NormalizedRect(img_width - right_width, 158 img_height);
146 img_height - bottom_height, 159 gfx::RectF uv_bottom_right =
147 right_width, 160 NormalizedRect(img_width - aperture_right_width,
148 bottom_height, 161 img_height - aperture_bottom_height,
149 img_width, 162 aperture_right_width,
150 img_height); 163 aperture_bottom_height,
151 gfx::RectF uv_top(uv_top_left.right(), 164 img_width,
152 0, 165 img_height);
153 (img_width - left_width - right_width) / img_width, 166 gfx::RectF uv_top(
154 (top_height) / img_height); 167 uv_top_left.right(),
155 gfx::RectF uv_left(0, 168 0,
156 uv_top_left.bottom(), 169 (img_width - aperture_left_width - aperture_right_width) / img_width,
157 left_width / img_width, 170 (aperture_top_height) / img_height);
158 (img_height - top_height - bottom_height) / img_height); 171 gfx::RectF uv_left(
172 0,
173 uv_top_left.bottom(),
174 aperture_left_width / img_width,
175 (img_height - aperture_top_height - aperture_bottom_height) / img_height);
159 gfx::RectF uv_right(uv_top_right.x(), 176 gfx::RectF uv_right(uv_top_right.x(),
160 uv_top_right.bottom(), 177 uv_top_right.bottom(),
161 right_width / img_width, 178 aperture_right_width / img_width,
162 uv_left.height()); 179 uv_left.height());
163 gfx::RectF uv_bottom(uv_top.x(), 180 gfx::RectF uv_bottom(uv_top.x(),
164 uv_bottom_left.y(), 181 uv_bottom_left.y(),
165 uv_top.width(), 182 uv_top.width(),
166 bottom_height / img_height); 183 aperture_bottom_height / img_height);
184 gfx::RectF uv_center(uv_top_left.right(),
185 uv_top_left.bottom(),
186 uv_top.width(),
187 uv_left.height());
167 188
168 // Nothing is opaque here. 189 // Nothing is opaque here.
169 // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness? 190 // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness?
170 gfx::Rect opaque_rect; 191 gfx::Rect opaque_rect;
171 const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f}; 192 const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
172 scoped_ptr<TextureDrawQuad> quad; 193 scoped_ptr<TextureDrawQuad> quad;
173 194
174 quad = TextureDrawQuad::Create(); 195 quad = TextureDrawQuad::Create();
175 quad->SetNew(shared_quad_state, 196 quad->SetNew(shared_quad_state,
176 top_left, 197 top_left,
177 opaque_rect, 198 opaque_rect,
178 resource_id_, 199 resource,
179 premultiplied_alpha, 200 premultiplied_alpha,
180 uv_top_left.origin(), 201 uv_top_left.origin(),
181 uv_top_left.bottom_right(), 202 uv_top_left.bottom_right(),
182 SK_ColorTRANSPARENT, 203 SK_ColorTRANSPARENT,
183 vertex_opacity, 204 vertex_opacity,
184 flipped); 205 flipped);
185 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 206 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
186 207
187 quad = TextureDrawQuad::Create(); 208 quad = TextureDrawQuad::Create();
188 quad->SetNew(shared_quad_state, 209 quad->SetNew(shared_quad_state,
189 top_right, 210 top_right,
190 opaque_rect, 211 opaque_rect,
191 resource_id_, 212 resource,
192 premultiplied_alpha, 213 premultiplied_alpha,
193 uv_top_right.origin(), 214 uv_top_right.origin(),
194 uv_top_right.bottom_right(), 215 uv_top_right.bottom_right(),
195 SK_ColorTRANSPARENT, 216 SK_ColorTRANSPARENT,
196 vertex_opacity, 217 vertex_opacity,
197 flipped); 218 flipped);
198 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 219 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
199 220
200 quad = TextureDrawQuad::Create(); 221 quad = TextureDrawQuad::Create();
201 quad->SetNew(shared_quad_state, 222 quad->SetNew(shared_quad_state,
202 bottom_left, 223 bottom_left,
203 opaque_rect, 224 opaque_rect,
204 resource_id_, 225 resource,
205 premultiplied_alpha, 226 premultiplied_alpha,
206 uv_bottom_left.origin(), 227 uv_bottom_left.origin(),
207 uv_bottom_left.bottom_right(), 228 uv_bottom_left.bottom_right(),
208 SK_ColorTRANSPARENT, 229 SK_ColorTRANSPARENT,
209 vertex_opacity, 230 vertex_opacity,
210 flipped); 231 flipped);
211 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 232 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
212 233
213 quad = TextureDrawQuad::Create(); 234 quad = TextureDrawQuad::Create();
214 quad->SetNew(shared_quad_state, 235 quad->SetNew(shared_quad_state,
215 bottom_right, 236 bottom_right,
216 opaque_rect, 237 opaque_rect,
217 resource_id_, 238 resource,
218 premultiplied_alpha, 239 premultiplied_alpha,
219 uv_bottom_right.origin(), 240 uv_bottom_right.origin(),
220 uv_bottom_right.bottom_right(), 241 uv_bottom_right.bottom_right(),
221 SK_ColorTRANSPARENT, 242 SK_ColorTRANSPARENT,
222 vertex_opacity, 243 vertex_opacity,
223 flipped); 244 flipped);
224 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 245 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
225 246
226 quad = TextureDrawQuad::Create(); 247 quad = TextureDrawQuad::Create();
227 quad->SetNew(shared_quad_state, 248 quad->SetNew(shared_quad_state,
228 top, 249 top,
229 opaque_rect, 250 opaque_rect,
230 resource_id_, 251 resource,
231 premultiplied_alpha, 252 premultiplied_alpha,
232 uv_top.origin(), 253 uv_top.origin(),
233 uv_top.bottom_right(), 254 uv_top.bottom_right(),
234 SK_ColorTRANSPARENT, 255 SK_ColorTRANSPARENT,
235 vertex_opacity, 256 vertex_opacity,
236 flipped); 257 flipped);
237 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 258 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
238 259
239 quad = TextureDrawQuad::Create(); 260 quad = TextureDrawQuad::Create();
240 quad->SetNew(shared_quad_state, 261 quad->SetNew(shared_quad_state,
241 left, 262 left,
242 opaque_rect, 263 opaque_rect,
243 resource_id_, 264 resource,
244 premultiplied_alpha, 265 premultiplied_alpha,
245 uv_left.origin(), 266 uv_left.origin(),
246 uv_left.bottom_right(), 267 uv_left.bottom_right(),
247 SK_ColorTRANSPARENT, 268 SK_ColorTRANSPARENT,
248 vertex_opacity, 269 vertex_opacity,
249 flipped); 270 flipped);
250 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 271 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
251 272
252 quad = TextureDrawQuad::Create(); 273 quad = TextureDrawQuad::Create();
253 quad->SetNew(shared_quad_state, 274 quad->SetNew(shared_quad_state,
254 right, 275 right,
255 opaque_rect, 276 opaque_rect,
256 resource_id_, 277 resource,
257 premultiplied_alpha, 278 premultiplied_alpha,
258 uv_right.origin(), 279 uv_right.origin(),
259 uv_right.bottom_right(), 280 uv_right.bottom_right(),
260 SK_ColorTRANSPARENT, 281 SK_ColorTRANSPARENT,
261 vertex_opacity, 282 vertex_opacity,
262 flipped); 283 flipped);
263 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 284 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
264 285
265 quad = TextureDrawQuad::Create(); 286 quad = TextureDrawQuad::Create();
266 quad->SetNew(shared_quad_state, 287 quad->SetNew(shared_quad_state,
267 bottom, 288 bottom,
268 opaque_rect, 289 opaque_rect,
269 resource_id_, 290 resource,
270 premultiplied_alpha, 291 premultiplied_alpha,
271 uv_bottom.origin(), 292 uv_bottom.origin(),
272 uv_bottom.bottom_right(), 293 uv_bottom.bottom_right(),
273 SK_ColorTRANSPARENT, 294 SK_ColorTRANSPARENT,
274 vertex_opacity, 295 vertex_opacity,
275 flipped); 296 flipped);
276 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 297 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
277 }
278 298
279 void NinePatchLayerImpl::DidLoseOutputSurface() { 299 if (fill_center_) {
280 resource_id_ = 0; 300 quad = TextureDrawQuad::Create();
301 quad->SetNew(shared_quad_state,
302 center,
303 opaque_rect,
304 resource,
305 premultiplied_alpha,
306 uv_center.origin(),
307 uv_center.bottom_right(),
308 SK_ColorTRANSPARENT,
309 vertex_opacity,
310 flipped);
311 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
312 }
281 } 313 }
282 314
283 const char* NinePatchLayerImpl::LayerTypeAsString() const { 315 const char* NinePatchLayerImpl::LayerTypeAsString() const {
284 return "cc::NinePatchLayerImpl"; 316 return "cc::NinePatchLayerImpl";
285 } 317 }
286 318
287 base::DictionaryValue* NinePatchLayerImpl::LayerTreeAsJson() const { 319 base::DictionaryValue* NinePatchLayerImpl::LayerTreeAsJson() const {
288 base::DictionaryValue* result = LayerImpl::LayerTreeAsJson(); 320 base::DictionaryValue* result = LayerImpl::LayerTreeAsJson();
289 321
290 base::ListValue* list = new base::ListValue; 322 base::ListValue* list = new base::ListValue;
291 list->AppendInteger(image_aperture_.origin().x()); 323 list->AppendInteger(image_aperture_.origin().x());
292 list->AppendInteger(image_aperture_.origin().y()); 324 list->AppendInteger(image_aperture_.origin().y());
293 list->AppendInteger(image_aperture_.size().width()); 325 list->AppendInteger(image_aperture_.size().width());
294 list->AppendInteger(image_aperture_.size().height()); 326 list->AppendInteger(image_aperture_.size().height());
295 result->Set("ImageAperture", list); 327 result->Set("ImageAperture", list);
296 328
297 list = new base::ListValue; 329 list = new base::ListValue;
298 list->AppendInteger(image_bounds_.width()); 330 list->AppendInteger(image_bounds_.width());
299 list->AppendInteger(image_bounds_.height()); 331 list->AppendInteger(image_bounds_.height());
300 result->Set("ImageBounds", list); 332 result->Set("ImageBounds", list);
301 333
302 return result; 334 return result;
303 } 335 }
304 336
305 } // namespace cc 337 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698