Chromoting: Fix connection failure strings to match Directory Service
[chromium-blink-merge.git] / cc / layers / nine_patch_layer_impl.cc
blob82ba48011c6ca4a7b1173ee0608b61eb58547333
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.h"
13 #include "ui/gfx/geometry/rect_f.h"
15 namespace cc {
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());
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,
36 float y,
37 float width,
38 float height,
39 float total_width,
40 float total_height) {
41 return gfx::RectF(x / total_width,
42 y / total_height,
43 width / total_width,
44 height / total_height);
47 void NinePatchLayerImpl::SetLayout(const gfx::Rect& aperture,
48 const gfx::Rect& border,
49 bool fill_center) {
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)
56 return;
58 image_aperture_ = aperture;
59 border_ = border;
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 Occlusion& occlusion_in_content_space,
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);
95 if (!ui_resource_id_)
96 return;
98 ResourceProvider::ResourceId resource =
99 layer_tree_impl()->ResourceIdForUIResource(ui_resource_id_);
101 if (!resource)
102 return;
104 static const bool flipped = false;
105 static const bool nearest_neighbor = false;
106 static const bool premultiplied_alpha = true;
108 DCHECK(!bounds().IsEmpty());
110 // NinePatch border widths in layer space.
111 int layer_left_width = border_.x();
112 int layer_top_height = border_.y();
113 int layer_right_width = border_.width() - layer_left_width;
114 int layer_bottom_height = border_.height() - layer_top_height;
116 int layer_middle_width = bounds().width() - border_.width();
117 int layer_middle_height = bounds().height() - border_.height();
119 // Patch positions in layer space
120 gfx::Rect layer_top_left(0, 0, layer_left_width, layer_top_height);
121 gfx::Rect layer_top_right(bounds().width() - layer_right_width,
123 layer_right_width,
124 layer_top_height);
125 gfx::Rect layer_bottom_left(0,
126 bounds().height() - layer_bottom_height,
127 layer_left_width,
128 layer_bottom_height);
129 gfx::Rect layer_bottom_right(layer_top_right.x(),
130 layer_bottom_left.y(),
131 layer_right_width,
132 layer_bottom_height);
133 gfx::Rect layer_top(
134 layer_top_left.right(), 0, layer_middle_width, layer_top_height);
135 gfx::Rect layer_left(
136 0, layer_top_left.bottom(), layer_left_width, layer_middle_height);
137 gfx::Rect layer_right(layer_top_right.x(),
138 layer_top_right.bottom(),
139 layer_right_width,
140 layer_left.height());
141 gfx::Rect layer_bottom(layer_top.x(),
142 layer_bottom_left.y(),
143 layer_top.width(),
144 layer_bottom_height);
145 gfx::Rect layer_center(layer_left_width,
146 layer_top_height,
147 layer_middle_width,
148 layer_middle_height);
150 // Note the following values are in image (bitmap) space.
151 float image_width = image_bounds_.width();
152 float image_height = image_bounds_.height();
154 int image_aperture_left_width = image_aperture_.x();
155 int image_aperture_top_height = image_aperture_.y();
156 int image_aperture_right_width = image_width - image_aperture_.right();
157 int image_aperture_bottom_height = image_height - image_aperture_.bottom();
158 // Patch positions in bitmap UV space (from zero to one)
159 gfx::RectF uv_top_left = NormalizedRect(0,
161 image_aperture_left_width,
162 image_aperture_top_height,
163 image_width,
164 image_height);
165 gfx::RectF uv_top_right =
166 NormalizedRect(image_width - image_aperture_right_width,
168 image_aperture_right_width,
169 image_aperture_top_height,
170 image_width,
171 image_height);
172 gfx::RectF uv_bottom_left =
173 NormalizedRect(0,
174 image_height - image_aperture_bottom_height,
175 image_aperture_left_width,
176 image_aperture_bottom_height,
177 image_width,
178 image_height);
179 gfx::RectF uv_bottom_right =
180 NormalizedRect(image_width - image_aperture_right_width,
181 image_height - image_aperture_bottom_height,
182 image_aperture_right_width,
183 image_aperture_bottom_height,
184 image_width,
185 image_height);
186 gfx::RectF uv_top(
187 uv_top_left.right(),
189 (image_width - image_aperture_left_width - image_aperture_right_width) /
190 image_width,
191 (image_aperture_top_height) / image_height);
192 gfx::RectF uv_left(0,
193 uv_top_left.bottom(),
194 image_aperture_left_width / image_width,
195 (image_height - image_aperture_top_height -
196 image_aperture_bottom_height) /
197 image_height);
198 gfx::RectF uv_right(uv_top_right.x(),
199 uv_top_right.bottom(),
200 image_aperture_right_width / image_width,
201 uv_left.height());
202 gfx::RectF uv_bottom(uv_top.x(),
203 uv_bottom_left.y(),
204 uv_top.width(),
205 image_aperture_bottom_height / image_height);
206 gfx::RectF uv_center(uv_top_left.right(),
207 uv_top_left.bottom(),
208 uv_top.width(),
209 uv_left.height());
211 gfx::Rect opaque_rect;
212 gfx::Rect visible_rect;
213 const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
214 const bool opaque = layer_tree_impl()->IsUIResourceOpaque(ui_resource_id_);
216 visible_rect =
217 occlusion_in_content_space.GetUnoccludedContentRect(layer_top_left);
218 opaque_rect = opaque ? visible_rect : gfx::Rect();
219 if (!visible_rect.IsEmpty()) {
220 TextureDrawQuad* quad =
221 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
222 quad->SetNew(shared_quad_state,
223 layer_top_left,
224 opaque_rect,
225 visible_rect,
226 resource,
227 premultiplied_alpha,
228 uv_top_left.origin(),
229 uv_top_left.bottom_right(),
230 SK_ColorTRANSPARENT,
231 vertex_opacity,
232 flipped,
233 nearest_neighbor);
236 visible_rect =
237 occlusion_in_content_space.GetUnoccludedContentRect(layer_top_right);
238 opaque_rect = opaque ? visible_rect : gfx::Rect();
239 if (!visible_rect.IsEmpty()) {
240 TextureDrawQuad* quad =
241 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
242 quad->SetNew(shared_quad_state,
243 layer_top_right,
244 opaque_rect,
245 visible_rect,
246 resource,
247 premultiplied_alpha,
248 uv_top_right.origin(),
249 uv_top_right.bottom_right(),
250 SK_ColorTRANSPARENT,
251 vertex_opacity,
252 flipped,
253 nearest_neighbor);
256 visible_rect =
257 occlusion_in_content_space.GetUnoccludedContentRect(layer_bottom_left);
258 opaque_rect = opaque ? visible_rect : gfx::Rect();
259 if (!visible_rect.IsEmpty()) {
260 TextureDrawQuad* quad =
261 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
262 quad->SetNew(shared_quad_state,
263 layer_bottom_left,
264 opaque_rect,
265 visible_rect,
266 resource,
267 premultiplied_alpha,
268 uv_bottom_left.origin(),
269 uv_bottom_left.bottom_right(),
270 SK_ColorTRANSPARENT,
271 vertex_opacity,
272 flipped,
273 nearest_neighbor);
276 visible_rect =
277 occlusion_in_content_space.GetUnoccludedContentRect(layer_bottom_right);
278 opaque_rect = opaque ? visible_rect : gfx::Rect();
279 if (!visible_rect.IsEmpty()) {
280 TextureDrawQuad* quad =
281 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
282 quad->SetNew(shared_quad_state,
283 layer_bottom_right,
284 opaque_rect,
285 visible_rect,
286 resource,
287 premultiplied_alpha,
288 uv_bottom_right.origin(),
289 uv_bottom_right.bottom_right(),
290 SK_ColorTRANSPARENT,
291 vertex_opacity,
292 flipped,
293 nearest_neighbor);
296 visible_rect = occlusion_in_content_space.GetUnoccludedContentRect(layer_top);
297 opaque_rect = opaque ? visible_rect : gfx::Rect();
298 if (!visible_rect.IsEmpty()) {
299 TextureDrawQuad* quad =
300 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
301 quad->SetNew(shared_quad_state,
302 layer_top,
303 opaque_rect,
304 visible_rect,
305 resource,
306 premultiplied_alpha,
307 uv_top.origin(),
308 uv_top.bottom_right(),
309 SK_ColorTRANSPARENT,
310 vertex_opacity,
311 flipped,
312 nearest_neighbor);
315 visible_rect =
316 occlusion_in_content_space.GetUnoccludedContentRect(layer_left);
317 opaque_rect = opaque ? visible_rect : gfx::Rect();
318 if (!visible_rect.IsEmpty()) {
319 TextureDrawQuad* quad =
320 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
321 quad->SetNew(shared_quad_state,
322 layer_left,
323 opaque_rect,
324 visible_rect,
325 resource,
326 premultiplied_alpha,
327 uv_left.origin(),
328 uv_left.bottom_right(),
329 SK_ColorTRANSPARENT,
330 vertex_opacity,
331 flipped,
332 nearest_neighbor);
335 visible_rect =
336 occlusion_in_content_space.GetUnoccludedContentRect(layer_right);
337 opaque_rect = opaque ? visible_rect : gfx::Rect();
338 if (!visible_rect.IsEmpty()) {
339 TextureDrawQuad* quad =
340 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
341 quad->SetNew(shared_quad_state,
342 layer_right,
343 opaque_rect,
344 layer_right,
345 resource,
346 premultiplied_alpha,
347 uv_right.origin(),
348 uv_right.bottom_right(),
349 SK_ColorTRANSPARENT,
350 vertex_opacity,
351 flipped,
352 nearest_neighbor);
355 visible_rect =
356 occlusion_in_content_space.GetUnoccludedContentRect(layer_bottom);
357 opaque_rect = opaque ? visible_rect : gfx::Rect();
358 if (!visible_rect.IsEmpty()) {
359 TextureDrawQuad* quad =
360 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
361 quad->SetNew(shared_quad_state,
362 layer_bottom,
363 opaque_rect,
364 visible_rect,
365 resource,
366 premultiplied_alpha,
367 uv_bottom.origin(),
368 uv_bottom.bottom_right(),
369 SK_ColorTRANSPARENT,
370 vertex_opacity,
371 flipped,
372 nearest_neighbor);
375 if (fill_center_) {
376 visible_rect =
377 occlusion_in_content_space.GetUnoccludedContentRect(layer_center);
378 opaque_rect = opaque ? visible_rect : gfx::Rect();
379 if (!visible_rect.IsEmpty()) {
380 TextureDrawQuad* quad =
381 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
382 quad->SetNew(shared_quad_state,
383 layer_center,
384 opaque_rect,
385 visible_rect,
386 resource,
387 premultiplied_alpha,
388 uv_center.origin(),
389 uv_center.bottom_right(),
390 SK_ColorTRANSPARENT,
391 vertex_opacity,
392 flipped,
393 nearest_neighbor);
398 const char* NinePatchLayerImpl::LayerTypeAsString() const {
399 return "cc::NinePatchLayerImpl";
402 base::DictionaryValue* NinePatchLayerImpl::LayerTreeAsJson() const {
403 base::DictionaryValue* result = LayerImpl::LayerTreeAsJson();
405 base::ListValue* list = new base::ListValue;
406 list->AppendInteger(image_aperture_.origin().x());
407 list->AppendInteger(image_aperture_.origin().y());
408 list->AppendInteger(image_aperture_.size().width());
409 list->AppendInteger(image_aperture_.size().height());
410 result->Set("ImageAperture", list);
412 list = new base::ListValue;
413 list->AppendInteger(image_bounds_.width());
414 list->AppendInteger(image_bounds_.height());
415 result->Set("ImageBounds", list);
417 result->Set("Border", MathUtil::AsValue(border_).release());
419 result->SetBoolean("FillCenter", fill_center_);
421 return result;
424 } // namespace cc