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
|
// 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 "nine_patch_layer_impl.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "cc/quad_sink.h"
#include "cc/texture_draw_quad.h"
#include "ui/gfx/rect_f.h"
namespace cc {
NinePatchLayerImpl::NinePatchLayerImpl(LayerTreeImpl* treeImpl, int id)
: LayerImpl(treeImpl, id)
, m_resourceId(0)
{
}
NinePatchLayerImpl::~NinePatchLayerImpl()
{
}
ResourceProvider::ResourceId NinePatchLayerImpl::contentsResourceId() const
{
return 0;
}
void NinePatchLayerImpl::willDraw(ResourceProvider* resourceProvider)
{
}
static gfx::RectF normalizedRect(float x, float y, float width, float height, float totalWidth, float totalHeight)
{
return gfx::RectF(x / totalWidth, y / totalHeight, width / totalWidth, height / totalHeight);
}
void NinePatchLayerImpl::setLayout(const gfx::Size& imageBounds, const gfx::Rect& aperture)
{
m_imageBounds = imageBounds;
m_imageAperture = aperture;
}
void NinePatchLayerImpl::appendQuads(QuadSink& quadSink, AppendQuadsData& appendQuadsData)
{
if (!m_resourceId)
return;
SharedQuadState* sharedQuadState = quadSink.useSharedQuadState(createSharedQuadState());
appendDebugBorderQuad(quadSink, sharedQuadState, appendQuadsData);
static const bool flipped = false;
static const bool premultipliedAlpha = true;
DCHECK(!bounds().IsEmpty());
// NinePatch border widths in bitmap pixel space
int leftWidth = m_imageAperture.x();
int topHeight = m_imageAperture.y();
int rightWidth = m_imageBounds.width() - m_imageAperture.right();
int bottomHeight = m_imageBounds.height() - m_imageAperture.bottom();
// If layer can't fit the corners, clip to show the outer edges of the
// image.
int cornerTotalWidth = leftWidth + rightWidth;
int middleWidth = bounds().width() - cornerTotalWidth;
if (middleWidth < 0) {
float leftWidthProportion = static_cast<float>(leftWidth) / cornerTotalWidth;
int leftWidthCrop = middleWidth * leftWidthProportion;
leftWidth += leftWidthCrop;
rightWidth = bounds().width() - leftWidth;
middleWidth = 0;
}
int cornerTotalHeight = topHeight + bottomHeight;
int middleHeight = bounds().height() - cornerTotalHeight;
if (middleHeight < 0) {
float topHeightProportion = static_cast<float>(topHeight) / cornerTotalHeight;
int topHeightCrop = middleHeight * topHeightProportion;
topHeight += topHeightCrop;
bottomHeight = bounds().height() - topHeight;
middleHeight = 0;
}
// Patch positions in layer space
gfx::Rect topLeft(0, 0, leftWidth, topHeight);
gfx::Rect topRight(bounds().width() - rightWidth, 0, rightWidth, topHeight);
gfx::Rect bottomLeft(0, bounds().height() - bottomHeight, leftWidth, bottomHeight);
gfx::Rect bottomRight(topRight.x(), bottomLeft.y(), rightWidth, bottomHeight);
gfx::Rect top(topLeft.right(), 0, middleWidth, topHeight);
gfx::Rect left(0, topLeft.bottom(), leftWidth, middleHeight);
gfx::Rect right(topRight.x(), topRight.bottom(), rightWidth, left.height());
gfx::Rect bottom(top.x(), bottomLeft.y(), top.width(), bottomHeight);
float imgWidth = m_imageBounds.width();
float imgHeight = m_imageBounds.height();
// Patch positions in bitmap UV space (from zero to one)
gfx::RectF uvTopLeft = normalizedRect(0, 0, leftWidth, topHeight, imgWidth, imgHeight);
gfx::RectF uvTopRight = normalizedRect(imgWidth - rightWidth, 0, rightWidth, topHeight, imgWidth, imgHeight);
gfx::RectF uvBottomLeft = normalizedRect(0, imgHeight - bottomHeight, leftWidth, bottomHeight, imgWidth, imgHeight);
gfx::RectF uvBottomRight = normalizedRect(imgWidth - rightWidth, imgHeight - bottomHeight, rightWidth, bottomHeight, imgWidth, imgHeight);
gfx::RectF uvTop(uvTopLeft.right(), 0, (imgWidth - leftWidth - rightWidth) / imgWidth, (topHeight) / imgHeight);
gfx::RectF uvLeft(0, uvTopLeft.bottom(), leftWidth / imgWidth, (imgHeight - topHeight - bottomHeight) / imgHeight);
gfx::RectF uvRight(uvTopRight.x(), uvTopRight.bottom(), rightWidth / imgWidth, uvLeft.height());
gfx::RectF uvBottom(uvTop.x(), uvBottomLeft.y(), uvTop.width(), bottomHeight / imgHeight);
// Nothing is opaque here.
// TODO(danakj): Should we look at the SkBitmaps to determine opaqueness?
gfx::Rect opaqueRect;
const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
scoped_ptr<TextureDrawQuad> quad;
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, topLeft, opaqueRect, m_resourceId, premultipliedAlpha, uvTopLeft.origin(), uvTopLeft.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, topRight, opaqueRect, m_resourceId, premultipliedAlpha, uvTopRight.origin(), uvTopRight.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, bottomLeft, opaqueRect, m_resourceId, premultipliedAlpha, uvBottomLeft.origin(), uvBottomLeft.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, bottomRight, opaqueRect, m_resourceId, premultipliedAlpha, uvBottomRight.origin(), uvBottomRight.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, top, opaqueRect, m_resourceId, premultipliedAlpha, uvTop.origin(), uvTop.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, left, opaqueRect, m_resourceId, premultipliedAlpha, uvLeft.origin(), uvLeft.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, right, opaqueRect, m_resourceId, premultipliedAlpha, uvRight.origin(), uvRight.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
quad = TextureDrawQuad::Create();
quad->SetNew(sharedQuadState, bottom, opaqueRect, m_resourceId, premultipliedAlpha, uvBottom.origin(), uvBottom.bottom_right(), vertex_opacity, flipped);
quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
}
void NinePatchLayerImpl::didDraw(ResourceProvider* resourceProvider)
{
}
void NinePatchLayerImpl::didLoseOutputSurface()
{
m_resourceId = 0;
}
const char* NinePatchLayerImpl::layerTypeAsString() const
{
return "NinePatchLayer";
}
void NinePatchLayerImpl::dumpLayerProperties(std::string* str, int indent) const
{
str->append(indentString(indent));
base::StringAppendF(str, "imageAperture: %s\n", m_imageAperture.ToString().c_str());
LayerImpl::dumpLayerProperties(str, indent);
}
base::DictionaryValue* NinePatchLayerImpl::layerTreeAsJson() const
{
base::DictionaryValue* result = LayerImpl::layerTreeAsJson();
base::ListValue* list = new base::ListValue;
list->AppendInteger(m_imageAperture.origin().x());
list->AppendInteger(m_imageAperture.origin().y());
list->AppendInteger(m_imageAperture.size().width());
list->AppendInteger(m_imageAperture.size().height());
result->Set("ImageAperture", list);
return result;
}
}
|