blob: 223583fe99f8173511fe3ef54d510acb6de2d465 [file] [log] [blame]
[email protected]35680c02012-11-06 05:53:001// 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.
4
[email protected]35680c02012-11-06 05:53:005#include "nine_patch_layer_impl.h"
6
[email protected]9a5710432012-11-09 20:52:357#include "base/stringprintf.h"
[email protected]35680c02012-11-06 05:53:008#include "cc/quad_sink.h"
9#include "cc/texture_draw_quad.h"
10#include "ui/gfx/rect_f.h"
11
12namespace cc {
13
14NinePatchLayerImpl::NinePatchLayerImpl(int id)
15 : LayerImpl(id)
16 , m_resourceId(0)
17{
18}
19
20NinePatchLayerImpl::~NinePatchLayerImpl()
21{
22}
23
24ResourceProvider::ResourceId NinePatchLayerImpl::contentsResourceId() const
25{
26 return 0;
27}
28
[email protected]35680c02012-11-06 05:53:0029void NinePatchLayerImpl::willDraw(ResourceProvider* resourceProvider)
30{
31}
32
33static gfx::RectF normalizedRect(float x, float y, float width, float height, float totalWidth, float totalHeight)
34{
35 return gfx::RectF(x / totalWidth, y / totalHeight, width / totalWidth, height / totalHeight);
36}
37
38void NinePatchLayerImpl::setLayout(const gfx::Size& imageBounds, const gfx::Rect& aperture)
39{
40 m_imageBounds = imageBounds;
41 m_imageAperture = aperture;
42}
43
44void NinePatchLayerImpl::appendQuads(QuadSink& quadSink, AppendQuadsData& appendQuadsData)
45{
46 if (!m_resourceId)
47 return;
48
49 SharedQuadState* sharedQuadState = quadSink.useSharedQuadState(createSharedQuadState());
50 appendDebugBorderQuad(quadSink, sharedQuadState, appendQuadsData);
51
52 static const bool flipped = false;
53 static const bool premultipliedAlpha = true;
54
55 DCHECK(!bounds().IsEmpty());
56
57 // NinePatch border widths in bitmap pixel space
58 int leftWidth = m_imageAperture.x();
59 int topHeight = m_imageAperture.y();
60 int rightWidth = m_imageBounds.width() - m_imageAperture.right();
61 int bottomHeight = m_imageBounds.height() - m_imageAperture.bottom();
62
[email protected]411ccfc2012-11-21 22:14:1263 // If layer can't fit the corners, clip to show the outer edges of the
64 // image.
65 int cornerTotalWidth = leftWidth + rightWidth;
66 int middleWidth = bounds().width() - cornerTotalWidth;
67 if (middleWidth < 0) {
68 float leftWidthProportion = static_cast<float>(leftWidth) / cornerTotalWidth;
69 int leftWidthCrop = middleWidth * leftWidthProportion;
70 leftWidth += leftWidthCrop;
71 rightWidth = bounds().width() - leftWidth;
72 middleWidth = 0;
73 }
74 int cornerTotalHeight = topHeight + bottomHeight;
75 int middleHeight = bounds().height() - cornerTotalHeight;
76 if (middleHeight < 0) {
77 float topHeightProportion = static_cast<float>(topHeight) / cornerTotalHeight;
78 int topHeightCrop = middleHeight * topHeightProportion;
79 topHeight += topHeightCrop;
80 bottomHeight = bounds().height() - topHeight;
81 middleHeight = 0;
82 }
83
[email protected]35680c02012-11-06 05:53:0084 // Patch positions in layer space
85 gfx::Rect topLeft(0, 0, leftWidth, topHeight);
86 gfx::Rect topRight(bounds().width() - rightWidth, 0, rightWidth, topHeight);
87 gfx::Rect bottomLeft(0, bounds().height() - bottomHeight, leftWidth, bottomHeight);
88 gfx::Rect bottomRight(topRight.x(), bottomLeft.y(), rightWidth, bottomHeight);
[email protected]411ccfc2012-11-21 22:14:1289 gfx::Rect top(topLeft.right(), 0, middleWidth, topHeight);
90 gfx::Rect left(0, topLeft.bottom(), leftWidth, middleHeight);
[email protected]35680c02012-11-06 05:53:0091 gfx::Rect right(topRight.x(), topRight.bottom(), rightWidth, left.height());
92 gfx::Rect bottom(top.x(), bottomLeft.y(), top.width(), bottomHeight);
93
94 float imgWidth = m_imageBounds.width();
95 float imgHeight = m_imageBounds.height();
96
97 // Patch positions in bitmap UV space (from zero to one)
98 gfx::RectF uvTopLeft = normalizedRect(0, 0, leftWidth, topHeight, imgWidth, imgHeight);
99 gfx::RectF uvTopRight = normalizedRect(imgWidth - rightWidth, 0, rightWidth, topHeight, imgWidth, imgHeight);
100 gfx::RectF uvBottomLeft = normalizedRect(0, imgHeight - bottomHeight, leftWidth, bottomHeight, imgWidth, imgHeight);
101 gfx::RectF uvBottomRight = normalizedRect(imgWidth - rightWidth, imgHeight - bottomHeight, rightWidth, bottomHeight, imgWidth, imgHeight);
102 gfx::RectF uvTop(uvTopLeft.right(), 0, (imgWidth - leftWidth - rightWidth) / imgWidth, (topHeight) / imgHeight);
103 gfx::RectF uvLeft(0, uvTopLeft.bottom(), leftWidth / imgWidth, (imgHeight - topHeight - bottomHeight) / imgHeight);
104 gfx::RectF uvRight(uvTopRight.x(), uvTopRight.bottom(), rightWidth / imgWidth, uvLeft.height());
105 gfx::RectF uvBottom(uvTop.x(), uvBottomLeft.y(), uvTop.width(), bottomHeight / imgHeight);
106
[email protected]35d7e962012-11-17 06:10:06107 // Nothing is opaque here.
108 // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness?
109 gfx::Rect opaqueRect;
[email protected]c22418b2012-11-20 23:06:26110 scoped_ptr<TextureDrawQuad> quad;
[email protected]35d7e962012-11-17 06:10:06111
[email protected]c22418b2012-11-20 23:06:26112 quad = TextureDrawQuad::Create();
113 quad->SetNew(sharedQuadState, topLeft, opaqueRect, m_resourceId, premultipliedAlpha, uvTopLeft, flipped);
114 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
115
116 quad = TextureDrawQuad::Create();
117 quad->SetNew(sharedQuadState, topRight, opaqueRect, m_resourceId, premultipliedAlpha, uvTopRight, flipped);
118 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
119
120 quad = TextureDrawQuad::Create();
121 quad->SetNew(sharedQuadState, bottomLeft, opaqueRect, m_resourceId, premultipliedAlpha, uvBottomLeft, flipped);
122 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
123
124 quad = TextureDrawQuad::Create();
125 quad->SetNew(sharedQuadState, bottomRight, opaqueRect, m_resourceId, premultipliedAlpha, uvBottomRight, flipped);
126 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
127
128 quad = TextureDrawQuad::Create();
129 quad->SetNew(sharedQuadState, top, opaqueRect, m_resourceId, premultipliedAlpha, uvTop, flipped);
130 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
131
132 quad = TextureDrawQuad::Create();
133 quad->SetNew(sharedQuadState, left, opaqueRect, m_resourceId, premultipliedAlpha, uvLeft, flipped);
134 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
135
136 quad = TextureDrawQuad::Create();
137 quad->SetNew(sharedQuadState, right, opaqueRect, m_resourceId, premultipliedAlpha, uvRight, flipped);
138 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
139
140 quad = TextureDrawQuad::Create();
141 quad->SetNew(sharedQuadState, bottom, opaqueRect, m_resourceId, premultipliedAlpha, uvBottom, flipped);
142 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
[email protected]35680c02012-11-06 05:53:00143}
144
145void NinePatchLayerImpl::didDraw(ResourceProvider* resourceProvider)
146{
147}
148
149void NinePatchLayerImpl::didLoseContext()
150{
151 m_resourceId = 0;
152}
153
154const char* NinePatchLayerImpl::layerTypeAsString() const
155{
156 return "NinePatchLayer";
157}
158
[email protected]9a5710432012-11-09 20:52:35159void NinePatchLayerImpl::dumpLayerProperties(std::string* str, int indent) const
160{
161 str->append(indentString(indent));
162 base::StringAppendF(str, "imageAperture: %s\n", m_imageAperture.ToString().c_str());
163 LayerImpl::dumpLayerProperties(str, indent);
164}
165
[email protected]35680c02012-11-06 05:53:00166}