blob: fbf765b7694d08d73e1050fb25f3e3ec5f0a5599 [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]411ccfc2012-11-21 22:14:125#include <stdio.h>
6
[email protected]35680c02012-11-06 05:53:007#include "cc/nine_patch_layer_impl.h"
8
9#include "cc/append_quads_data.h"
10#include "cc/single_thread_proxy.h"
[email protected]586d51ed2012-12-07 20:31:4511#include "cc/test/fake_impl_proxy.h"
12#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]35680c02012-11-06 05:53:0013#include "cc/test/geometry_test_utils.h"
14#include "cc/test/layer_test_common.h"
15#include "cc/test/mock_quad_culler.h"
16#include "cc/texture_draw_quad.h"
17#include "ui/gfx/rect_conversions.h"
18#include "ui/gfx/safe_integer_conversions.h"
19#include "testing/gmock/include/gmock/gmock.h"
20#include "testing/gtest/include/gtest/gtest.h"
[email protected]c8686a02012-11-27 08:29:0021#include "ui/gfx/transform.h"
[email protected]35680c02012-11-06 05:53:0022
[email protected]ba565742012-11-10 09:29:4823namespace cc {
[email protected]35680c02012-11-06 05:53:0024namespace {
25
26gfx::Rect ToRoundedIntRect(gfx::RectF rect_f) {
27 return gfx::Rect(gfx::ToRoundedInt(rect_f.x()), gfx::ToRoundedInt(rect_f.y()), gfx::ToRoundedInt(rect_f.width()), gfx::ToRoundedInt(rect_f.height()));
28}
29
30TEST(NinePatchLayerImplTest, verifyDrawQuads)
31{
[email protected]35680c02012-11-06 05:53:0032 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30.
33 // The bounds of the layer are set to 400x400, so the draw quads
34 // generated should leave the border width (40) intact.
35 MockQuadCuller quadCuller;
36 gfx::Size bitmapSize(100, 100);
37 gfx::Size layerSize(400, 400);
38 gfx::Rect visibleContentRect(gfx::Point(), layerSize);
39 gfx::Rect apertureRect(20, 30, 40, 50);
40 gfx::Rect scaledApertureNonUniform(20, 30, 340, 350);
41
[email protected]586d51ed2012-12-07 20:31:4542 FakeImplProxy proxy;
43 FakeLayerTreeHostImpl hostImpl(&proxy);
[email protected]8bef40572012-12-11 21:38:0844 scoped_ptr<NinePatchLayerImpl> layer = NinePatchLayerImpl::create(hostImpl.activeTree(), 1);
[email protected]d76806f82012-12-05 21:41:5045 layer->drawProperties().visible_content_rect = visibleContentRect;
[email protected]35680c02012-11-06 05:53:0046 layer->setBounds(layerSize);
47 layer->setContentBounds(layerSize);
48 layer->createRenderSurface();
[email protected]d76806f82012-12-05 21:41:5049 layer->drawProperties().render_target = layer.get();
[email protected]35680c02012-11-06 05:53:0050 layer->setLayout(bitmapSize, apertureRect);
51 layer->setResourceId(1);
52
53 // This scale should not affect the generated quad geometry, but only
54 // the shared draw transform.
[email protected]c8686a02012-11-27 08:29:0055 gfx::Transform transform;
56 transform.Scale(10, 10);
[email protected]d76806f82012-12-05 21:41:5057 layer->drawProperties().target_space_transform = transform;
[email protected]35680c02012-11-06 05:53:0058
59 AppendQuadsData data;
60 layer->appendQuads(quadCuller, data);
61
62 // Verify quad rects
63 const QuadList& quads = quadCuller.quadList();
[email protected]411ccfc2012-11-21 22:14:1264 EXPECT_EQ(8, quads.size());
[email protected]35680c02012-11-06 05:53:0065 Region remaining(visibleContentRect);
66 for (size_t i = 0; i < quads.size(); ++i) {
67 DrawQuad* quad = quads[i];
[email protected]1bc93f62012-11-17 19:29:5068 gfx::Rect quadRect = quad->rect;
[email protected]35680c02012-11-06 05:53:0069
70 EXPECT_TRUE(visibleContentRect.Contains(quadRect)) << i;
71 EXPECT_TRUE(remaining.Contains(quadRect)) << i;
[email protected]411ccfc2012-11-21 22:14:1272 EXPECT_EQ(transform, quad->quadTransform());
[email protected]35680c02012-11-06 05:53:0073 remaining.Subtract(Region(quadRect));
74 }
[email protected]411ccfc2012-11-21 22:14:1275 EXPECT_RECT_EQ(scaledApertureNonUniform, remaining.bounds());
[email protected]35680c02012-11-06 05:53:0076 Region scaledApertureRegion(scaledApertureNonUniform);
[email protected]411ccfc2012-11-21 22:14:1277 EXPECT_EQ(scaledApertureRegion, remaining);
[email protected]35680c02012-11-06 05:53:0078
79 // Verify UV rects
80 gfx::Rect bitmapRect(gfx::Point(), bitmapSize);
81 Region texRemaining(bitmapRect);
82 for (size_t i = 0; i < quads.size(); ++i) {
83 DrawQuad* quad = quads[i];
[email protected]c22418b2012-11-20 23:06:2684 const TextureDrawQuad* texQuad = TextureDrawQuad::MaterialCast(quad);
[email protected]1fd555b2013-01-18 00:13:2185 gfx::RectF texRect = gfx::BoundingRect(texQuad->uv_top_left, texQuad->uv_bottom_right);
[email protected]35680c02012-11-06 05:53:0086 texRect.Scale(bitmapSize.width(), bitmapSize.height());
87 texRemaining.Subtract(Region(ToRoundedIntRect(texRect)));
88 }
[email protected]411ccfc2012-11-21 22:14:1289 EXPECT_RECT_EQ(apertureRect, texRemaining.bounds());
[email protected]35680c02012-11-06 05:53:0090 Region apertureRegion(apertureRect);
[email protected]411ccfc2012-11-21 22:14:1291 EXPECT_EQ(apertureRegion, texRemaining);
92}
93
94TEST(NinePatchLayerImplTest, verifyDrawQuadsForSqueezedLayer)
95{
96 // Test with a layer much smaller than the bitmap.
97 MockQuadCuller quadCuller;
98 gfx::Size bitmapSize(101, 101);
99 gfx::Size layerSize(51, 51);
100 gfx::Rect visibleContentRect(gfx::Point(), layerSize);
101 gfx::Rect apertureRect(20, 30, 40, 45); // rightWidth: 40, botHeight: 25
102
[email protected]586d51ed2012-12-07 20:31:45103 FakeImplProxy proxy;
104 FakeLayerTreeHostImpl hostImpl(&proxy);
[email protected]8bef40572012-12-11 21:38:08105 scoped_ptr<NinePatchLayerImpl> layer = NinePatchLayerImpl::create(hostImpl.activeTree(), 1);
[email protected]d76806f82012-12-05 21:41:50106 layer->drawProperties().visible_content_rect = visibleContentRect;
[email protected]411ccfc2012-11-21 22:14:12107 layer->setBounds(layerSize);
108 layer->setContentBounds(layerSize);
109 layer->createRenderSurface();
[email protected]d76806f82012-12-05 21:41:50110 layer->drawProperties().render_target = layer.get();
[email protected]411ccfc2012-11-21 22:14:12111 layer->setLayout(bitmapSize, apertureRect);
112 layer->setResourceId(1);
113
114 AppendQuadsData data;
115 layer->appendQuads(quadCuller, data);
116
117 // Verify corner rects fill the layer and don't overlap
118 const QuadList& quads = quadCuller.quadList();
119 EXPECT_EQ(4, quads.size());
120 Region filled;
121 for (size_t i = 0; i < quads.size(); ++i) {
122 DrawQuad* quad = quads[i];
123 gfx::Rect quadRect = quad->rect;
124
125 EXPECT_FALSE(filled.Intersects(quadRect));
126 filled.Union(quadRect);
127 }
128 Region expectedFull(visibleContentRect);
129 EXPECT_EQ(expectedFull, filled);
130
131 // Verify UV rects cover the corners of the bitmap and the crop is weighted
132 // proportionately to the relative corner sizes (for uneven apertures).
133 gfx::Rect bitmapRect(gfx::Point(), bitmapSize);
134 Region texRemaining(bitmapRect);
135 for (size_t i = 0; i < quads.size(); ++i) {
136 DrawQuad* quad = quads[i];
137 const TextureDrawQuad* texQuad = TextureDrawQuad::MaterialCast(quad);
[email protected]1fd555b2013-01-18 00:13:21138 gfx::RectF texRect = gfx::BoundingRect(texQuad->uv_top_left, texQuad->uv_bottom_right);
[email protected]411ccfc2012-11-21 22:14:12139 texRect.Scale(bitmapSize.width(), bitmapSize.height());
140 texRemaining.Subtract(Region(ToRoundedIntRect(texRect)));
141 }
142 Region expectedRemainingRegion = Region(gfx::Rect(bitmapSize));
143 expectedRemainingRegion.Subtract(gfx::Rect(0, 0, 17, 28));
144 expectedRemainingRegion.Subtract(gfx::Rect(67, 0, 34, 28));
145 expectedRemainingRegion.Subtract(gfx::Rect(0, 78, 17, 23));
146 expectedRemainingRegion.Subtract(gfx::Rect(67, 78, 34, 23));
147 EXPECT_EQ(expectedRemainingRegion, texRemaining);
[email protected]35680c02012-11-06 05:53:00148}
149
[email protected]ba565742012-11-10 09:29:48150} // namespace
151} // namespace cc