[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 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. |
| 4 | |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 5 | #include "nine_patch_layer_impl.h" |
| 6 | |
[email protected] | 9a571043 | 2012-11-09 20:52:35 | [diff] [blame] | 7 | #include "base/stringprintf.h" |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 8 | #include "cc/quad_sink.h" |
| 9 | #include "cc/texture_draw_quad.h" |
| 10 | #include "ui/gfx/rect_f.h" |
| 11 | |
| 12 | namespace cc { |
| 13 | |
| 14 | NinePatchLayerImpl::NinePatchLayerImpl(int id) |
| 15 | : LayerImpl(id) |
| 16 | , m_resourceId(0) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | NinePatchLayerImpl::~NinePatchLayerImpl() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | ResourceProvider::ResourceId NinePatchLayerImpl::contentsResourceId() const |
| 25 | { |
| 26 | return 0; |
| 27 | } |
| 28 | |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 29 | void NinePatchLayerImpl::willDraw(ResourceProvider* resourceProvider) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | static 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 | |
| 38 | void NinePatchLayerImpl::setLayout(const gfx::Size& imageBounds, const gfx::Rect& aperture) |
| 39 | { |
| 40 | m_imageBounds = imageBounds; |
| 41 | m_imageAperture = aperture; |
| 42 | } |
| 43 | |
| 44 | void 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] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 63 | // 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] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 84 | // 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] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 89 | gfx::Rect top(topLeft.right(), 0, middleWidth, topHeight); |
| 90 | gfx::Rect left(0, topLeft.bottom(), leftWidth, middleHeight); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 91 | 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] | 35d7e96 | 2012-11-17 06:10:06 | [diff] [blame] | 107 | // Nothing is opaque here. |
| 108 | // TODO(danakj): Should we look at the SkBitmaps to determine opaqueness? |
| 109 | gfx::Rect opaqueRect; |
[email protected] | c22418b | 2012-11-20 23:06:26 | [diff] [blame] | 110 | scoped_ptr<TextureDrawQuad> quad; |
[email protected] | 35d7e96 | 2012-11-17 06:10:06 | [diff] [blame] | 111 | |
[email protected] | c22418b | 2012-11-20 23:06:26 | [diff] [blame] | 112 | 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] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void NinePatchLayerImpl::didDraw(ResourceProvider* resourceProvider) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | void NinePatchLayerImpl::didLoseContext() |
| 150 | { |
| 151 | m_resourceId = 0; |
| 152 | } |
| 153 | |
| 154 | const char* NinePatchLayerImpl::layerTypeAsString() const |
| 155 | { |
| 156 | return "NinePatchLayer"; |
| 157 | } |
| 158 | |
[email protected] | 9a571043 | 2012-11-09 20:52:35 | [diff] [blame] | 159 | void 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] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 166 | } |