blob: 949c541830dfb6b359fbc9ebf3718da757b2f487 [file] [log] [blame]
sergeyvf42bf3e2016-03-11 13:45:15 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include "BakedOpRenderer.h"
20#include "Glop.h"
21#include "GlopBuilder.h"
22#include "Rect.h"
23#include "tests/common/TestUtils.h"
24#include "utils/Color.h"
25
26#include <SkPaint.h>
27
28using namespace android::uirenderer;
29
30static void expectFillEq(Glop::Fill& expectedFill, Glop::Fill& builtFill) {
31 EXPECT_EQ(expectedFill.colorEnabled, builtFill.colorEnabled);
32 if (expectedFill.colorEnabled)
33 EXPECT_EQ(expectedFill.color, builtFill.color);
34
35 EXPECT_EQ(expectedFill.filterMode, builtFill.filterMode);
36 if (expectedFill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
37 EXPECT_EQ(expectedFill.filter.color, builtFill.filter.color);
38 } else if (expectedFill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
39 Glop::Fill::Filter::Matrix& expectedMatrix = expectedFill.filter.matrix;
40 Glop::Fill::Filter::Matrix& builtMatrix = expectedFill.filter.matrix;
41 EXPECT_TRUE(std::memcmp(expectedMatrix.matrix, builtMatrix.matrix,
42 sizeof(Glop::Fill::Filter::Matrix::matrix)));
43 EXPECT_TRUE(std::memcmp(expectedMatrix.vector, builtMatrix.vector,
44 sizeof(Glop::Fill::Filter::Matrix::vector)));
45 }
46 EXPECT_EQ(expectedFill.skiaShaderData.skiaShaderType, builtFill.skiaShaderData.skiaShaderType);
47 EXPECT_EQ(expectedFill.texture.clamp, builtFill.texture.clamp);
48 EXPECT_EQ(expectedFill.texture.filter, builtFill.texture.filter);
49 EXPECT_EQ(expectedFill.texture.target, builtFill.texture.target);
50 EXPECT_EQ(expectedFill.texture.textureTransform, builtFill.texture.textureTransform);
51}
52
53static void expectBlendEq(Glop::Blend& expectedBlend, Glop::Blend& builtBlend) {
54 EXPECT_EQ(expectedBlend.src, builtBlend.src);
55 EXPECT_EQ(expectedBlend.dst, builtBlend.dst);
56}
57
58static void expectMeshEq(Glop::Mesh& expectedMesh, Glop::Mesh& builtMesh) {
59 EXPECT_EQ(expectedMesh.elementCount, builtMesh.elementCount);
60 EXPECT_EQ(expectedMesh.primitiveMode, builtMesh.primitiveMode);
61 EXPECT_EQ(expectedMesh.indices.indices, builtMesh.indices.indices);
62 EXPECT_EQ(expectedMesh.indices.bufferObject, builtMesh.indices.bufferObject);
63 EXPECT_EQ(expectedMesh.vertices.attribFlags, builtMesh.vertices.attribFlags);
64 EXPECT_EQ(expectedMesh.vertices.bufferObject, builtMesh.vertices.bufferObject);
65 EXPECT_EQ(expectedMesh.vertices.color, builtMesh.vertices.color);
66 EXPECT_EQ(expectedMesh.vertices.position, builtMesh.vertices.position);
67 EXPECT_EQ(expectedMesh.vertices.stride, builtMesh.vertices.stride);
68 EXPECT_EQ(expectedMesh.vertices.texCoord, builtMesh.vertices.texCoord);
69
70 if (builtMesh.vertices.position) {
71 for (int i = 0; i < 4; i++) {
72 TextureVertex& expectedVertex = expectedMesh.mappedVertices[i];
73 TextureVertex& builtVertex = builtMesh.mappedVertices[i];
74 EXPECT_EQ(expectedVertex.u, builtVertex.u);
75 EXPECT_EQ(expectedVertex.v, builtVertex.v);
76 EXPECT_EQ(expectedVertex.x, builtVertex.x);
77 EXPECT_EQ(expectedVertex.y, builtVertex.y);
78 }
79 }
80}
81
82static void expectTransformEq(Glop::Transform& expectedTransform, Glop::Transform& builtTransform) {
83 EXPECT_EQ(expectedTransform.canvas, builtTransform.canvas);
84 EXPECT_EQ(expectedTransform.modelView, builtTransform.modelView);
85 EXPECT_EQ(expectedTransform.transformFlags, expectedTransform.transformFlags);
86}
87
88static void expectGlopEq(Glop& expectedGlop, Glop& builtGlop) {
89 EXPECT_EQ(expectedGlop.bounds, builtGlop.bounds);
90 expectBlendEq(expectedGlop.blend, builtGlop.blend);
91 expectFillEq(expectedGlop.fill, builtGlop.fill);
92 expectMeshEq(expectedGlop.mesh, builtGlop.mesh);
93 expectTransformEq(expectedGlop.transform, builtGlop.transform);
94}
95
96static std::unique_ptr<Glop> blackUnitQuadGlop(RenderState& renderState) {
97 std::unique_ptr<Glop> glop(new Glop());
98 glop->blend = { GL_ZERO, GL_ZERO };
99 glop->mesh.elementCount = 4;
100 glop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
101 glop->mesh.indices.indices = nullptr;
102 glop->mesh.indices.bufferObject = GL_ZERO;
103 glop->mesh.vertices = {
104 renderState.meshState().getUnitQuadVBO(),
105 VertexAttribFlags::None,
106 nullptr, nullptr, nullptr,
107 kTextureVertexStride };
108 glop->transform.modelView.loadIdentity();
109 glop->fill.colorEnabled = true;
110 glop->fill.color.set(Color::Black);
111 glop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType;
112 glop->fill.filterMode = ProgramDescription::ColorFilterMode::None;
113 glop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
114 return glop;
115}
116
117RENDERTHREAD_TEST(GlopBuilder, rectSnapTest) {
118 RenderState& renderState = renderThread.renderState();
119 Caches& caches = Caches::getInstance();
120 SkPaint paint;
121 Rect dest(1, 1, 100, 100);
122 Matrix4 simpleTranslate;
123 simpleTranslate.loadTranslate(0.7, 0.7, 0);
124 Glop glop;
125 GlopBuilder(renderState, caches, &glop)
126 .setRoundRectClipState(nullptr)
127 .setMeshUnitQuad()
128 .setFillPaint(paint, 1.0f)
129 .setTransform(simpleTranslate, TransformFlags::None)
130 .setModelViewMapUnitToRectSnap(dest)
131 .build();
132
133 std::unique_ptr<Glop> goldenGlop(blackUnitQuadGlop(renderState));
134 // Rect(1,1,100,100) is the set destination,
135 // so unit quad should be translated by (1,1) and scaled by (99, 99)
136 // Tricky part: because translate (0.7, 0.7) and snapping were set in glopBuilder,
137 // unit quad also should be translate by additional (0.3, 0.3) to snap to exact pixels.
138 goldenGlop->transform.modelView.loadTranslate(1.3, 1.3, 0);
139 goldenGlop->transform.modelView.scale(99, 99, 1);
140 goldenGlop->bounds = android::uirenderer::Rect(1.70, 1.70, 100.70, 100.70);
141 goldenGlop->transform.canvas = simpleTranslate;
142 goldenGlop->fill.texture.filter = GL_NEAREST;
143 expectGlopEq(*goldenGlop, glop);
144}