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
3 // found in the LICENSE file.
5 #include "cc/layers/nine_patch_layer_impl.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/values.h"
9 #include "cc/base/math_util.h"
10 #include "cc/quads/texture_draw_quad.h"
11 #include "cc/trees/layer_tree_impl.h"
12 #include "cc/trees/occlusion_tracker.h"
13 #include "ui/gfx/rect_f.h"
17 NinePatchLayerImpl::NinePatchLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
18 : UIResourceLayerImpl(tree_impl
, id
),
19 fill_center_(false) {}
21 NinePatchLayerImpl::~NinePatchLayerImpl() {}
23 scoped_ptr
<LayerImpl
> NinePatchLayerImpl::CreateLayerImpl(
24 LayerTreeImpl
* tree_impl
) {
25 return NinePatchLayerImpl::Create(tree_impl
, id()).PassAs
<LayerImpl
>();
28 void NinePatchLayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
29 UIResourceLayerImpl::PushPropertiesTo(layer
);
30 NinePatchLayerImpl
* layer_impl
= static_cast<NinePatchLayerImpl
*>(layer
);
32 layer_impl
->SetLayout(image_aperture_
, border_
, fill_center_
);
35 static gfx::RectF
NormalizedRect(float x
,
41 return gfx::RectF(x
/ total_width
,
44 height
/ total_height
);
47 void NinePatchLayerImpl::SetLayout(const gfx::Rect
& aperture
,
48 const gfx::Rect
& border
,
50 // This check imposes an ordering on the call sequence. An UIResource must
51 // exist before SetLayout can be called.
52 DCHECK(ui_resource_id_
);
54 if (image_aperture_
== aperture
&&
55 border_
== border
&& fill_center_
== fill_center
)
58 image_aperture_
= aperture
;
60 fill_center_
= fill_center
;
62 NoteLayerPropertyChanged();
65 void NinePatchLayerImpl::CheckGeometryLimitations() {
66 // |border| is in layer space. It cannot exceed the bounds of the layer.
67 DCHECK_GE(bounds().width(), border_
.width());
68 DCHECK_GE(bounds().height(), border_
.height());
70 // Sanity Check on |border|
71 DCHECK_LE(border_
.x(), border_
.width());
72 DCHECK_LE(border_
.y(), border_
.height());
73 DCHECK_GE(border_
.x(), 0);
74 DCHECK_GE(border_
.y(), 0);
76 // |aperture| is in image space. It cannot exceed the bounds of the bitmap.
77 DCHECK(!image_aperture_
.size().IsEmpty());
78 DCHECK(gfx::Rect(image_bounds_
).Contains(image_aperture_
))
79 << "image_bounds_ " << gfx::Rect(image_bounds_
).ToString()
80 << " image_aperture_ " << image_aperture_
.ToString();
83 void NinePatchLayerImpl::AppendQuads(
84 RenderPass
* render_pass
,
85 const OcclusionTracker
<LayerImpl
>& occlusion_tracker
,
86 AppendQuadsData
* append_quads_data
) {
87 CheckGeometryLimitations();
88 SharedQuadState
* shared_quad_state
=
89 render_pass
->CreateAndAppendSharedQuadState();
90 PopulateSharedQuadState(shared_quad_state
);
92 AppendDebugBorderQuad(
93 render_pass
, content_bounds(), shared_quad_state
, append_quads_data
);
98 ResourceProvider::ResourceId resource
=
99 layer_tree_impl()->ResourceIdForUIResource(ui_resource_id_
);
104 static const bool flipped
= false;
105 static const bool premultiplied_alpha
= true;
107 DCHECK(!bounds().IsEmpty());
109 // NinePatch border widths in layer space.
110 int layer_left_width
= border_
.x();
111 int layer_top_height
= border_
.y();
112 int layer_right_width
= border_
.width() - layer_left_width
;
113 int layer_bottom_height
= border_
.height() - layer_top_height
;
115 int layer_middle_width
= bounds().width() - border_
.width();
116 int layer_middle_height
= bounds().height() - border_
.height();
118 // Patch positions in layer space
119 gfx::Rect
layer_top_left(0, 0, layer_left_width
, layer_top_height
);
120 gfx::Rect
layer_top_right(bounds().width() - layer_right_width
,
124 gfx::Rect
layer_bottom_left(0,
125 bounds().height() - layer_bottom_height
,
127 layer_bottom_height
);
128 gfx::Rect
layer_bottom_right(layer_top_right
.x(),
129 layer_bottom_left
.y(),
131 layer_bottom_height
);
133 layer_top_left
.right(), 0, layer_middle_width
, layer_top_height
);
134 gfx::Rect
layer_left(
135 0, layer_top_left
.bottom(), layer_left_width
, layer_middle_height
);
136 gfx::Rect
layer_right(layer_top_right
.x(),
137 layer_top_right
.bottom(),
139 layer_left
.height());
140 gfx::Rect
layer_bottom(layer_top
.x(),
141 layer_bottom_left
.y(),
143 layer_bottom_height
);
144 gfx::Rect
layer_center(layer_left_width
,
147 layer_middle_height
);
149 // Note the following values are in image (bitmap) space.
150 float image_width
= image_bounds_
.width();
151 float image_height
= image_bounds_
.height();
153 int image_aperture_left_width
= image_aperture_
.x();
154 int image_aperture_top_height
= image_aperture_
.y();
155 int image_aperture_right_width
= image_width
- image_aperture_
.right();
156 int image_aperture_bottom_height
= image_height
- image_aperture_
.bottom();
157 // Patch positions in bitmap UV space (from zero to one)
158 gfx::RectF uv_top_left
= NormalizedRect(0,
160 image_aperture_left_width
,
161 image_aperture_top_height
,
164 gfx::RectF uv_top_right
=
165 NormalizedRect(image_width
- image_aperture_right_width
,
167 image_aperture_right_width
,
168 image_aperture_top_height
,
171 gfx::RectF uv_bottom_left
=
173 image_height
- image_aperture_bottom_height
,
174 image_aperture_left_width
,
175 image_aperture_bottom_height
,
178 gfx::RectF uv_bottom_right
=
179 NormalizedRect(image_width
- image_aperture_right_width
,
180 image_height
- image_aperture_bottom_height
,
181 image_aperture_right_width
,
182 image_aperture_bottom_height
,
188 (image_width
- image_aperture_left_width
- image_aperture_right_width
) /
190 (image_aperture_top_height
) / image_height
);
191 gfx::RectF
uv_left(0,
192 uv_top_left
.bottom(),
193 image_aperture_left_width
/ image_width
,
194 (image_height
- image_aperture_top_height
-
195 image_aperture_bottom_height
) /
197 gfx::RectF
uv_right(uv_top_right
.x(),
198 uv_top_right
.bottom(),
199 image_aperture_right_width
/ image_width
,
201 gfx::RectF
uv_bottom(uv_top
.x(),
204 image_aperture_bottom_height
/ image_height
);
205 gfx::RectF
uv_center(uv_top_left
.right(),
206 uv_top_left
.bottom(),
210 // Nothing is opaque here.
211 // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness?
212 gfx::Rect opaque_rect
;
213 gfx::Rect visible_rect
;
214 const float vertex_opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
217 occlusion_tracker
.UnoccludedContentRect(layer_top_left
, draw_transform());
218 if (!visible_rect
.IsEmpty()) {
219 TextureDrawQuad
* quad
=
220 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
221 quad
->SetNew(shared_quad_state
,
227 uv_top_left
.origin(),
228 uv_top_left
.bottom_right(),
234 visible_rect
= occlusion_tracker
.UnoccludedContentRect(layer_top_right
,
236 if (!visible_rect
.IsEmpty()) {
237 TextureDrawQuad
* quad
=
238 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
239 quad
->SetNew(shared_quad_state
,
245 uv_top_right
.origin(),
246 uv_top_right
.bottom_right(),
252 visible_rect
= occlusion_tracker
.UnoccludedContentRect(layer_bottom_left
,
254 if (!visible_rect
.IsEmpty()) {
255 TextureDrawQuad
* quad
=
256 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
257 quad
->SetNew(shared_quad_state
,
263 uv_bottom_left
.origin(),
264 uv_bottom_left
.bottom_right(),
270 visible_rect
= occlusion_tracker
.UnoccludedContentRect(layer_bottom_right
,
272 if (!visible_rect
.IsEmpty()) {
273 TextureDrawQuad
* quad
=
274 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
275 quad
->SetNew(shared_quad_state
,
281 uv_bottom_right
.origin(),
282 uv_bottom_right
.bottom_right(),
289 occlusion_tracker
.UnoccludedContentRect(layer_top
, draw_transform());
290 if (!visible_rect
.IsEmpty()) {
291 TextureDrawQuad
* quad
=
292 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
293 quad
->SetNew(shared_quad_state
,
300 uv_top
.bottom_right(),
307 occlusion_tracker
.UnoccludedContentRect(layer_left
, draw_transform());
308 if (!visible_rect
.IsEmpty()) {
309 TextureDrawQuad
* quad
=
310 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
311 quad
->SetNew(shared_quad_state
,
318 uv_left
.bottom_right(),
325 occlusion_tracker
.UnoccludedContentRect(layer_right
, draw_transform());
326 if (!visible_rect
.IsEmpty()) {
327 TextureDrawQuad
* quad
=
328 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
329 quad
->SetNew(shared_quad_state
,
336 uv_right
.bottom_right(),
343 occlusion_tracker
.UnoccludedContentRect(layer_bottom
, draw_transform());
344 if (!visible_rect
.IsEmpty()) {
345 TextureDrawQuad
* quad
=
346 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
347 quad
->SetNew(shared_quad_state
,
354 uv_bottom
.bottom_right(),
362 occlusion_tracker
.UnoccludedContentRect(layer_center
, draw_transform());
363 if (!visible_rect
.IsEmpty()) {
364 TextureDrawQuad
* quad
=
365 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
366 quad
->SetNew(shared_quad_state
,
373 uv_center
.bottom_right(),
381 const char* NinePatchLayerImpl::LayerTypeAsString() const {
382 return "cc::NinePatchLayerImpl";
385 base::DictionaryValue
* NinePatchLayerImpl::LayerTreeAsJson() const {
386 base::DictionaryValue
* result
= LayerImpl::LayerTreeAsJson();
388 base::ListValue
* list
= new base::ListValue
;
389 list
->AppendInteger(image_aperture_
.origin().x());
390 list
->AppendInteger(image_aperture_
.origin().y());
391 list
->AppendInteger(image_aperture_
.size().width());
392 list
->AppendInteger(image_aperture_
.size().height());
393 result
->Set("ImageAperture", list
);
395 list
= new base::ListValue
;
396 list
->AppendInteger(image_bounds_
.width());
397 list
->AppendInteger(image_bounds_
.height());
398 result
->Set("ImageBounds", list
);
400 result
->Set("Border", MathUtil::AsValue(border_
).release());
402 base::FundamentalValue
* fill_center
=
403 base::Value::CreateBooleanValue(fill_center_
);
404 result
->Set("FillCenter", fill_center
);