summaryrefslogtreecommitdiffstats
path: root/cc/layers/nine_patch_layer_impl.cc
blob: 57723a02108fc7c0ac17cc2274cfeac8dbdb13cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/layers/nine_patch_layer_impl.h"

#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "cc/layers/quad_sink.h"
#include "cc/quads/texture_draw_quad.h"
#include "ui/gfx/rect_f.h"

namespace cc {

NinePatchLayerImpl::NinePatchLayerImpl(LayerTreeImpl* tree_impl, int id)
    : LayerImpl(tree_impl, id),
      resource_id_(0) {}

NinePatchLayerImpl::~NinePatchLayerImpl() {}

ResourceProvider::ResourceId NinePatchLayerImpl::ContentsResourceId() const {
  return 0;
}

scoped_ptr<LayerImpl> NinePatchLayerImpl::CreateLayerImpl(
    LayerTreeImpl* tree_impl) {
  return NinePatchLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
}

void NinePatchLayerImpl::PushPropertiesTo(LayerImpl* layer) {
  LayerImpl::PushPropertiesTo(layer);
  NinePatchLayerImpl* layer_impl = static_cast<NinePatchLayerImpl*>(layer);

  if (!resource_id_)
    return;

  layer_impl->SetResourceId(resource_id_);
  layer_impl->SetLayout(image_bounds_, image_aperture_);
}

static gfx::RectF NormalizedRect(float x,
                                 float y,
                                 float width,
                                 float height,
                                 float total_width,
                                 float total_height) {
  return gfx::RectF(x / total_width,
                    y / total_height,
                    width / total_width,
                    height / total_height);
}

void NinePatchLayerImpl::SetLayout(gfx::Size image_bounds, gfx::Rect aperture) {
  image_bounds_ = image_bounds;
  image_aperture_ = aperture;
}

bool NinePatchLayerImpl::WillDraw(DrawMode draw_mode,
                                  ResourceProvider* resource_provider) {
  if (!resource_id_ || draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
    return false;
  return LayerImpl::WillDraw(draw_mode, resource_provider);
}

void NinePatchLayerImpl::AppendQuads(QuadSink* quad_sink,
                                     AppendQuadsData* append_quads_data) {
  DCHECK(resource_id_);

  SharedQuadState* shared_quad_state =
      quad_sink->UseSharedQuadState(CreateSharedQuadState());
  AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);

  static const bool flipped = false;
  static const bool premultiplied_alpha = true;

  DCHECK(!bounds().IsEmpty());

  // NinePatch border widths in bitmap pixel space
  int left_width = image_aperture_.x();
  int top_height = image_aperture_.y();
  int right_width = image_bounds_.width() - image_aperture_.right();
  int bottom_height = image_bounds_.height() - image_aperture_.bottom();

  // If layer can't fit the corners, clip to show the outer edges of the
  // image.
  int corner_total_width = left_width + right_width;
  int middle_width = bounds().width() - corner_total_width;
  if (middle_width < 0) {
    float left_width_proportion =
        static_cast<float>(left_width) / corner_total_width;
    int left_width_crop = middle_width * left_width_proportion;
    left_width += left_width_crop;
    right_width = bounds().width() - left_width;
    middle_width = 0;
  }
  int corner_total_height = top_height + bottom_height;
  int middle_height = bounds().height() - corner_total_height;
  if (middle_height < 0) {
    float top_height_proportion =
        static_cast<float>(top_height) / corner_total_height;
    int top_height_crop = middle_height * top_height_proportion;
    top_height += top_height_crop;
    bottom_height = bounds().height() - top_height;
    middle_height = 0;
  }

  // Patch positions in layer space
  gfx::Rect top_left(0, 0, left_width, top_height);
  gfx::Rect top_right(
      bounds().width() - right_width, 0, right_width, top_height);
  gfx::Rect bottom_left(
      0, bounds().height() - bottom_height, left_width, bottom_height);
  gfx::Rect bottom_right(
      top_right.x(), bottom_left.y(), right_width, bottom_height);
  gfx::Rect top(top_left.right(), 0, middle_width, top_height);
  gfx::Rect left(0, top_left.bottom(), left_width, middle_height);
  gfx::Rect right(top_right.x(),
                  top_right.bottom(),
                  right_width,
                  left.height());
  gfx::Rect bottom(top.x(), bottom_left.y(), top.width(), bottom_height);

  float img_width = image_bounds_.width();
  float img_height = image_bounds_.height();

  // Patch positions in bitmap UV space (from zero to one)
  gfx::RectF uv_top_left = NormalizedRect(0,
                                          0,
                                          left_width,
                                          top_height,
                                          img_width,
                                          img_height);
  gfx::RectF uv_top_right = NormalizedRect(img_width - right_width,
                                           0,
                                           right_width,
                                           top_height,
                                           img_width,
                                           img_height);
  gfx::RectF uv_bottom_left = NormalizedRect(0,
                                             img_height - bottom_height,
                                             left_width,
                                             bottom_height,
                                             img_width,
                                             img_height);
  gfx::RectF uv_bottom_right = NormalizedRect(img_width - right_width,
                                              img_height - bottom_height,
                                              right_width,
                                              bottom_height,
                                              img_width,
                                              img_height);
  gfx::RectF uv_top(uv_top_left.right(),
                   0,
                   (img_width - left_width - right_width) / img_width,
                   (top_height) / img_height);
  gfx::RectF uv_left(0,
                    uv_top_left.bottom(),
                    left_width / img_width,
                    (img_height - top_height - bottom_height) / img_height);
  gfx::RectF uv_right(uv_top_right.x(),
                     uv_top_right.bottom(),
                     right_width / img_width,
                     uv_left.height());
  gfx::RectF uv_bottom(uv_top.x(),
                      uv_bottom_left.y(),
                      uv_top.width(),
                      bottom_height / img_height);

  // Nothing is opaque here.
  // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness?
  gfx::Rect opaque_rect;
  const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
  scoped_ptr<TextureDrawQuad> quad;

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               top_left,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_top_left.origin(),
               uv_top_left.bottom_right(),
               vertex_opacity, flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               top_right,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_top_right.origin(),
               uv_top_right.bottom_right(),
               vertex_opacity, flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               bottom_left,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_bottom_left.origin(),
               uv_bottom_left.bottom_right(),
               vertex_opacity,
               flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               bottom_right,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_bottom_right.origin(),
               uv_bottom_right.bottom_right(),
               vertex_opacity,
               flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               top,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_top.origin(),
               uv_top.bottom_right(),
               vertex_opacity,
               flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               left,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_left.origin(),
               uv_left.bottom_right(),
               vertex_opacity,
               flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               right,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_right.origin(),
               uv_right.bottom_right(),
               vertex_opacity,
               flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);

  quad = TextureDrawQuad::Create();
  quad->SetNew(shared_quad_state,
               bottom,
               opaque_rect,
               resource_id_,
               premultiplied_alpha,
               uv_bottom.origin(),
               uv_bottom.bottom_right(),
               vertex_opacity,
               flipped);
  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
}

void NinePatchLayerImpl::DidLoseOutputSurface() {
  resource_id_ = 0;
}

const char* NinePatchLayerImpl::LayerTypeAsString() const {
  return "cc::NinePatchLayerImpl";
}

base::DictionaryValue* NinePatchLayerImpl::LayerTreeAsJson() const {
  base::DictionaryValue* result = LayerImpl::LayerTreeAsJson();

  base::ListValue* list = new base::ListValue;
  list->AppendInteger(image_aperture_.origin().x());
  list->AppendInteger(image_aperture_.origin().y());
  list->AppendInteger(image_aperture_.size().width());
  list->AppendInteger(image_aperture_.size().height());
  result->Set("ImageAperture", list);

  list = new base::ListValue;
  list->AppendInteger(image_bounds_.width());
  list->AppendInteger(image_bounds_.height());
  result->Set("ImageBounds", list);

  return result;
}

}  // namespace cc