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

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: Compilation error fixes Created 7 years, 3 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) {
danakj 2013/08/27 15:42:48 One argument per line, lining up with the ( unless
powei 2013/08/27 19:04:57 Done.
54 // |border| is in layer space. It cannot exceed the bounds of the layer.
55 DCHECK_GE(bounds().width(), border.width());
56 DCHECK_GE(bounds().height(), border.height());
57
58 // |aperture| is in image space. It cannot exceed the bounds of the bitmap.
59 DCHECK(gfx::Rect(image_bounds.width(), image_bounds.height())
60 .Contains(aperture));
61
54 image_bounds_ = image_bounds; 62 image_bounds_ = image_bounds;
55 image_aperture_ = aperture; 63 image_aperture_ = aperture;
64 border_ = border;
65 fill_center_ = fill_center;
danakj 2013/08/27 15:42:48 Should this function also cause damage so it is re
powei 2013/08/27 19:04:57 Done.
56 } 66 }
57 67
58 bool NinePatchLayerImpl::WillDraw(DrawMode draw_mode, 68 bool NinePatchLayerImpl::WillDraw(DrawMode draw_mode,
59 ResourceProvider* resource_provider) { 69 ResourceProvider* resource_provider) {
60 if (!resource_id_ || draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) 70 if (!ui_resource_id_ || draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
61 return false; 71 return false;
62 return LayerImpl::WillDraw(draw_mode, resource_provider); 72 return LayerImpl::WillDraw(draw_mode, resource_provider);
63 } 73 }
64 74
65 void NinePatchLayerImpl::AppendQuads(QuadSink* quad_sink, 75 void NinePatchLayerImpl::AppendQuads(QuadSink* quad_sink,
66 AppendQuadsData* append_quads_data) { 76 AppendQuadsData* append_quads_data) {
77 if (!ui_resource_id_ || !layer_tree_impl())
danakj 2013/08/27 15:42:48 layer_tree_impl is never null, it's set non-null i
powei 2013/08/27 22:39:28 Done.
78 return;
79
80 ResourceProvider::ResourceId resource =
81 layer_tree_impl()->ResourceIdForUIResource(ui_resource_id_);
82
83 if (!resource)
84 return;
85
67 SharedQuadState* shared_quad_state = 86 SharedQuadState* shared_quad_state =
68 quad_sink->UseSharedQuadState(CreateSharedQuadState()); 87 quad_sink->UseSharedQuadState(CreateSharedQuadState());
69 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data); 88 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
70 89
71 if (!resource_id_)
72 return;
73
74 static const bool flipped = false; 90 static const bool flipped = false;
75 static const bool premultiplied_alpha = true; 91 static const bool premultiplied_alpha = true;
76 92
77 DCHECK(!bounds().IsEmpty()); 93 DCHECK(!bounds().IsEmpty());
78 94
79 // NinePatch border widths in bitmap pixel space 95 // NinePatch border widths in layer space
danakj 2013/08/27 15:42:48 nit: Period.
powei 2013/08/27 22:39:28 Done.
80 int left_width = image_aperture_.x(); 96 int left_width = border_.x();
danakj 2013/08/27 15:42:48 maybe prefixing all these variables with layer_ or
powei 2013/08/27 22:39:28 Done.
81 int top_height = image_aperture_.y(); 97 int top_height = border_.y();
82 int right_width = image_bounds_.width() - image_aperture_.right(); 98 int right_width = border_.width() - left_width;
83 int bottom_height = image_bounds_.height() - image_aperture_.bottom(); 99 int bottom_height = border_.height() - top_height;
84 100
85 // If layer can't fit the corners, clip to show the outer edges of the 101 // If layer can't fit the corners, clip to show the outer edges of the
86 // image. 102 // image.
87 int corner_total_width = left_width + right_width; 103 int corner_total_width = left_width + right_width;
88 int middle_width = bounds().width() - corner_total_width; 104 int middle_width = bounds().width() - corner_total_width;
89 if (middle_width < 0) { 105 if (middle_width < 0) {
90 float left_width_proportion = 106 float left_width_proportion =
91 static_cast<float>(left_width) / corner_total_width; 107 static_cast<float>(left_width) / corner_total_width;
92 int left_width_crop = middle_width * left_width_proportion; 108 int left_width_crop = middle_width * left_width_proportion;
93 left_width += left_width_crop; 109 left_width += left_width_crop;
(...skipping 19 matching lines...) Expand all
113 0, bounds().height() - bottom_height, left_width, bottom_height); 129 0, bounds().height() - bottom_height, left_width, bottom_height);
114 gfx::Rect bottom_right( 130 gfx::Rect bottom_right(
115 top_right.x(), bottom_left.y(), right_width, bottom_height); 131 top_right.x(), bottom_left.y(), right_width, bottom_height);
116 gfx::Rect top(top_left.right(), 0, middle_width, top_height); 132 gfx::Rect top(top_left.right(), 0, middle_width, top_height);
117 gfx::Rect left(0, top_left.bottom(), left_width, middle_height); 133 gfx::Rect left(0, top_left.bottom(), left_width, middle_height);
118 gfx::Rect right(top_right.x(), 134 gfx::Rect right(top_right.x(),
119 top_right.bottom(), 135 top_right.bottom(),
120 right_width, 136 right_width,
121 left.height()); 137 left.height());
122 gfx::Rect bottom(top.x(), bottom_left.y(), top.width(), bottom_height); 138 gfx::Rect bottom(top.x(), bottom_left.y(), top.width(), bottom_height);
139 gfx::Rect center(left_width, top_height,
140 middle_width,
141 middle_height);
123 142
143 // Note the following values are in image (bitmap) space.
124 float img_width = image_bounds_.width(); 144 float img_width = image_bounds_.width();
125 float img_height = image_bounds_.height(); 145 float img_height = image_bounds_.height();
126 146
147 int img_aperture_left_width = image_aperture_.x();
148 int img_aperture_top_height = image_aperture_.y();
149 int img_aperture_right_width = img_width - image_aperture_.right();
150 int img_aperture_bottom_height = img_height - image_aperture_.bottom();
127 // Patch positions in bitmap UV space (from zero to one) 151 // Patch positions in bitmap UV space (from zero to one)
128 gfx::RectF uv_top_left = NormalizedRect(0, 152 gfx::RectF uv_top_left = NormalizedRect(0,
129 0, 153 0,
130 left_width, 154 img_aperture_left_width,
131 top_height, 155 img_aperture_top_height,
132 img_width, 156 img_width,
133 img_height); 157 img_height);
134 gfx::RectF uv_top_right = NormalizedRect(img_width - right_width, 158 gfx::RectF uv_top_right = NormalizedRect(img_width - img_aperture_right_width,
135 0, 159 0,
136 right_width, 160 img_aperture_right_width,
137 top_height, 161 img_aperture_top_height,
138 img_width, 162 img_width,
139 img_height); 163 img_height);
140 gfx::RectF uv_bottom_left = NormalizedRect(0, 164 gfx::RectF uv_bottom_left =
141 img_height - bottom_height, 165 NormalizedRect(0,
142 left_width, 166 img_height - img_aperture_bottom_height,
143 bottom_height, 167 img_aperture_left_width,
144 img_width, 168 img_aperture_bottom_height,
145 img_height); 169 img_width,
146 gfx::RectF uv_bottom_right = NormalizedRect(img_width - right_width, 170 img_height);
147 img_height - bottom_height, 171 gfx::RectF uv_bottom_right =
148 right_width, 172 NormalizedRect(img_width - img_aperture_right_width,
149 bottom_height, 173 img_height - img_aperture_bottom_height,
150 img_width, 174 img_aperture_right_width,
151 img_height); 175 img_aperture_bottom_height,
152 gfx::RectF uv_top(uv_top_left.right(), 176 img_width,
153 0, 177 img_height);
154 (img_width - left_width - right_width) / img_width, 178 gfx::RectF uv_top(
155 (top_height) / img_height); 179 uv_top_left.right(),
156 gfx::RectF uv_left(0, 180 0,
157 uv_top_left.bottom(), 181 (img_width - img_aperture_left_width - img_aperture_right_width) /
158 left_width / img_width, 182 img_width,
159 (img_height - top_height - bottom_height) / img_height); 183 (img_aperture_top_height) / img_height);
184 gfx::RectF uv_left(
185 0,
186 uv_top_left.bottom(),
187 img_aperture_left_width / img_width,
188 (img_height - img_aperture_top_height - img_aperture_bottom_height) /
189 img_height);
160 gfx::RectF uv_right(uv_top_right.x(), 190 gfx::RectF uv_right(uv_top_right.x(),
161 uv_top_right.bottom(), 191 uv_top_right.bottom(),
162 right_width / img_width, 192 img_aperture_right_width / img_width,
163 uv_left.height()); 193 uv_left.height());
164 gfx::RectF uv_bottom(uv_top.x(), 194 gfx::RectF uv_bottom(uv_top.x(),
165 uv_bottom_left.y(), 195 uv_bottom_left.y(),
166 uv_top.width(), 196 uv_top.width(),
167 bottom_height / img_height); 197 img_aperture_bottom_height / img_height);
198 gfx::RectF uv_center(uv_top_left.right(),
199 uv_top_left.bottom(),
200 uv_top.width(),
201 uv_left.height());
168 202
169 // Nothing is opaque here. 203 // Nothing is opaque here.
170 // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness? 204 // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness?
171 gfx::Rect opaque_rect; 205 gfx::Rect opaque_rect;
172 const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f}; 206 const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
173 scoped_ptr<TextureDrawQuad> quad; 207 scoped_ptr<TextureDrawQuad> quad;
174 208
175 quad = TextureDrawQuad::Create(); 209 quad = TextureDrawQuad::Create();
176 quad->SetNew(shared_quad_state, 210 quad->SetNew(shared_quad_state,
177 top_left, 211 top_left,
178 opaque_rect, 212 opaque_rect,
179 resource_id_, 213 resource,
180 premultiplied_alpha, 214 premultiplied_alpha,
181 uv_top_left.origin(), 215 uv_top_left.origin(),
182 uv_top_left.bottom_right(), 216 uv_top_left.bottom_right(),
183 SK_ColorTRANSPARENT, 217 SK_ColorTRANSPARENT,
184 vertex_opacity, 218 vertex_opacity,
185 flipped); 219 flipped);
186 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 220 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
187 221
188 quad = TextureDrawQuad::Create(); 222 quad = TextureDrawQuad::Create();
189 quad->SetNew(shared_quad_state, 223 quad->SetNew(shared_quad_state,
190 top_right, 224 top_right,
191 opaque_rect, 225 opaque_rect,
192 resource_id_, 226 resource,
193 premultiplied_alpha, 227 premultiplied_alpha,
194 uv_top_right.origin(), 228 uv_top_right.origin(),
195 uv_top_right.bottom_right(), 229 uv_top_right.bottom_right(),
196 SK_ColorTRANSPARENT, 230 SK_ColorTRANSPARENT,
197 vertex_opacity, 231 vertex_opacity,
198 flipped); 232 flipped);
199 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 233 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
200 234
201 quad = TextureDrawQuad::Create(); 235 quad = TextureDrawQuad::Create();
202 quad->SetNew(shared_quad_state, 236 quad->SetNew(shared_quad_state,
203 bottom_left, 237 bottom_left,
204 opaque_rect, 238 opaque_rect,
205 resource_id_, 239 resource,
206 premultiplied_alpha, 240 premultiplied_alpha,
207 uv_bottom_left.origin(), 241 uv_bottom_left.origin(),
208 uv_bottom_left.bottom_right(), 242 uv_bottom_left.bottom_right(),
209 SK_ColorTRANSPARENT, 243 SK_ColorTRANSPARENT,
210 vertex_opacity, 244 vertex_opacity,
211 flipped); 245 flipped);
212 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 246 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
213 247
214 quad = TextureDrawQuad::Create(); 248 quad = TextureDrawQuad::Create();
215 quad->SetNew(shared_quad_state, 249 quad->SetNew(shared_quad_state,
216 bottom_right, 250 bottom_right,
217 opaque_rect, 251 opaque_rect,
218 resource_id_, 252 resource,
219 premultiplied_alpha, 253 premultiplied_alpha,
220 uv_bottom_right.origin(), 254 uv_bottom_right.origin(),
221 uv_bottom_right.bottom_right(), 255 uv_bottom_right.bottom_right(),
222 SK_ColorTRANSPARENT, 256 SK_ColorTRANSPARENT,
223 vertex_opacity, 257 vertex_opacity,
224 flipped); 258 flipped);
225 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 259 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
226 260
227 quad = TextureDrawQuad::Create(); 261 quad = TextureDrawQuad::Create();
228 quad->SetNew(shared_quad_state, 262 quad->SetNew(shared_quad_state,
229 top, 263 top,
230 opaque_rect, 264 opaque_rect,
231 resource_id_, 265 resource,
232 premultiplied_alpha, 266 premultiplied_alpha,
233 uv_top.origin(), 267 uv_top.origin(),
234 uv_top.bottom_right(), 268 uv_top.bottom_right(),
235 SK_ColorTRANSPARENT, 269 SK_ColorTRANSPARENT,
236 vertex_opacity, 270 vertex_opacity,
237 flipped); 271 flipped);
238 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 272 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
239 273
240 quad = TextureDrawQuad::Create(); 274 quad = TextureDrawQuad::Create();
241 quad->SetNew(shared_quad_state, 275 quad->SetNew(shared_quad_state,
242 left, 276 left,
243 opaque_rect, 277 opaque_rect,
244 resource_id_, 278 resource,
245 premultiplied_alpha, 279 premultiplied_alpha,
246 uv_left.origin(), 280 uv_left.origin(),
247 uv_left.bottom_right(), 281 uv_left.bottom_right(),
248 SK_ColorTRANSPARENT, 282 SK_ColorTRANSPARENT,
249 vertex_opacity, 283 vertex_opacity,
250 flipped); 284 flipped);
251 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 285 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
252 286
253 quad = TextureDrawQuad::Create(); 287 quad = TextureDrawQuad::Create();
254 quad->SetNew(shared_quad_state, 288 quad->SetNew(shared_quad_state,
255 right, 289 right,
256 opaque_rect, 290 opaque_rect,
257 resource_id_, 291 resource,
258 premultiplied_alpha, 292 premultiplied_alpha,
259 uv_right.origin(), 293 uv_right.origin(),
260 uv_right.bottom_right(), 294 uv_right.bottom_right(),
261 SK_ColorTRANSPARENT, 295 SK_ColorTRANSPARENT,
262 vertex_opacity, 296 vertex_opacity,
263 flipped); 297 flipped);
264 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 298 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
265 299
266 quad = TextureDrawQuad::Create(); 300 quad = TextureDrawQuad::Create();
267 quad->SetNew(shared_quad_state, 301 quad->SetNew(shared_quad_state,
268 bottom, 302 bottom,
269 opaque_rect, 303 opaque_rect,
270 resource_id_, 304 resource,
271 premultiplied_alpha, 305 premultiplied_alpha,
272 uv_bottom.origin(), 306 uv_bottom.origin(),
273 uv_bottom.bottom_right(), 307 uv_bottom.bottom_right(),
274 SK_ColorTRANSPARENT, 308 SK_ColorTRANSPARENT,
275 vertex_opacity, 309 vertex_opacity,
276 flipped); 310 flipped);
277 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 311 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
278 }
279 312
280 void NinePatchLayerImpl::DidLoseOutputSurface() { 313 if (fill_center_) {
281 resource_id_ = 0; 314 quad = TextureDrawQuad::Create();
315 quad->SetNew(shared_quad_state,
316 center,
317 opaque_rect,
318 resource,
319 premultiplied_alpha,
320 uv_center.origin(),
321 uv_center.bottom_right(),
322 SK_ColorTRANSPARENT,
323 vertex_opacity,
324 flipped);
325 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
326 }
282 } 327 }
283 328
284 const char* NinePatchLayerImpl::LayerTypeAsString() const { 329 const char* NinePatchLayerImpl::LayerTypeAsString() const {
285 return "cc::NinePatchLayerImpl"; 330 return "cc::NinePatchLayerImpl";
286 } 331 }
287 332
288 base::DictionaryValue* NinePatchLayerImpl::LayerTreeAsJson() const { 333 base::DictionaryValue* NinePatchLayerImpl::LayerTreeAsJson() const {
289 base::DictionaryValue* result = LayerImpl::LayerTreeAsJson(); 334 base::DictionaryValue* result = LayerImpl::LayerTreeAsJson();
290 335
291 base::ListValue* list = new base::ListValue; 336 base::ListValue* list = new base::ListValue;
292 list->AppendInteger(image_aperture_.origin().x()); 337 list->AppendInteger(image_aperture_.origin().x());
293 list->AppendInteger(image_aperture_.origin().y()); 338 list->AppendInteger(image_aperture_.origin().y());
294 list->AppendInteger(image_aperture_.size().width()); 339 list->AppendInteger(image_aperture_.size().width());
295 list->AppendInteger(image_aperture_.size().height()); 340 list->AppendInteger(image_aperture_.size().height());
296 result->Set("ImageAperture", list); 341 result->Set("ImageAperture", list);
297 342
298 list = new base::ListValue; 343 list = new base::ListValue;
299 list->AppendInteger(image_bounds_.width()); 344 list->AppendInteger(image_bounds_.width());
300 list->AppendInteger(image_bounds_.height()); 345 list->AppendInteger(image_bounds_.height());
301 result->Set("ImageBounds", list); 346 result->Set("ImageBounds", list);
302 347
348 list = new base::ListValue;
349 list->AppendInteger(border_.width());
danakj 2013/08/27 15:42:48 Border is a rect, why not save it all? Also we hav
powei 2013/08/27 22:39:28 Done.
350 list->AppendInteger(border_.height());
351 result->Set("Border", list);
352
353 base::FundamentalValue* fill_center =
354 base::Value::CreateBooleanValue(fill_center_);
355 result->Set("FillCenter", fill_center);
356
303 return result; 357 return result;
304 } 358 }
305 359
306 } // namespace cc 360 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698