blob: c8bfc99fac92d001675498fa9dc23921ac7dd3e9 [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
sergeyvf42bf3e2016-03-11 13:45:15 -080019#include "Glop.h"
20#include "GlopBuilder.h"
21#include "Rect.h"
22#include "tests/common/TestUtils.h"
23#include "utils/Color.h"
24
25#include <SkPaint.h>
26
27using namespace android::uirenderer;
28
29static void expectFillEq(Glop::Fill& expectedFill, Glop::Fill& builtFill) {
30 EXPECT_EQ(expectedFill.colorEnabled, builtFill.colorEnabled);
John Reck1bcacfd2017-11-03 10:12:19 -070031 if (expectedFill.colorEnabled) EXPECT_EQ(expectedFill.color, builtFill.color);
sergeyvf42bf3e2016-03-11 13:45:15 -080032
33 EXPECT_EQ(expectedFill.filterMode, builtFill.filterMode);
34 if (expectedFill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
35 EXPECT_EQ(expectedFill.filter.color, builtFill.filter.color);
36 } else if (expectedFill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
37 Glop::Fill::Filter::Matrix& expectedMatrix = expectedFill.filter.matrix;
38 Glop::Fill::Filter::Matrix& builtMatrix = expectedFill.filter.matrix;
39 EXPECT_TRUE(std::memcmp(expectedMatrix.matrix, builtMatrix.matrix,
John Reck1bcacfd2017-11-03 10:12:19 -070040 sizeof(Glop::Fill::Filter::Matrix::matrix)));
sergeyvf42bf3e2016-03-11 13:45:15 -080041 EXPECT_TRUE(std::memcmp(expectedMatrix.vector, builtMatrix.vector,
John Reck1bcacfd2017-11-03 10:12:19 -070042 sizeof(Glop::Fill::Filter::Matrix::vector)));
sergeyvf42bf3e2016-03-11 13:45:15 -080043 }
44 EXPECT_EQ(expectedFill.skiaShaderData.skiaShaderType, builtFill.skiaShaderData.skiaShaderType);
45 EXPECT_EQ(expectedFill.texture.clamp, builtFill.texture.clamp);
46 EXPECT_EQ(expectedFill.texture.filter, builtFill.texture.filter);
John Reck1bcacfd2017-11-03 10:12:19 -070047 EXPECT_TRUE((expectedFill.texture.texture && builtFill.texture.texture) ||
48 (!expectedFill.texture.texture && !builtFill.texture.texture));
sergeyv2a38c422016-10-25 15:21:50 -070049 if (expectedFill.texture.texture) {
50 EXPECT_EQ(expectedFill.texture.texture->target(), builtFill.texture.texture->target());
51 }
sergeyvf42bf3e2016-03-11 13:45:15 -080052 EXPECT_EQ(expectedFill.texture.textureTransform, builtFill.texture.textureTransform);
53}
54
55static void expectBlendEq(Glop::Blend& expectedBlend, Glop::Blend& builtBlend) {
56 EXPECT_EQ(expectedBlend.src, builtBlend.src);
57 EXPECT_EQ(expectedBlend.dst, builtBlend.dst);
58}
59
60static void expectMeshEq(Glop::Mesh& expectedMesh, Glop::Mesh& builtMesh) {
61 EXPECT_EQ(expectedMesh.elementCount, builtMesh.elementCount);
62 EXPECT_EQ(expectedMesh.primitiveMode, builtMesh.primitiveMode);
63 EXPECT_EQ(expectedMesh.indices.indices, builtMesh.indices.indices);
64 EXPECT_EQ(expectedMesh.indices.bufferObject, builtMesh.indices.bufferObject);
65 EXPECT_EQ(expectedMesh.vertices.attribFlags, builtMesh.vertices.attribFlags);
66 EXPECT_EQ(expectedMesh.vertices.bufferObject, builtMesh.vertices.bufferObject);
67 EXPECT_EQ(expectedMesh.vertices.color, builtMesh.vertices.color);
68 EXPECT_EQ(expectedMesh.vertices.position, builtMesh.vertices.position);
69 EXPECT_EQ(expectedMesh.vertices.stride, builtMesh.vertices.stride);
70 EXPECT_EQ(expectedMesh.vertices.texCoord, builtMesh.vertices.texCoord);
71
72 if (builtMesh.vertices.position) {
73 for (int i = 0; i < 4; i++) {
74 TextureVertex& expectedVertex = expectedMesh.mappedVertices[i];
75 TextureVertex& builtVertex = builtMesh.mappedVertices[i];
76 EXPECT_EQ(expectedVertex.u, builtVertex.u);
77 EXPECT_EQ(expectedVertex.v, builtVertex.v);
78 EXPECT_EQ(expectedVertex.x, builtVertex.x);
79 EXPECT_EQ(expectedVertex.y, builtVertex.y);
80 }
81 }
82}
83
84static void expectTransformEq(Glop::Transform& expectedTransform, Glop::Transform& builtTransform) {
85 EXPECT_EQ(expectedTransform.canvas, builtTransform.canvas);
86 EXPECT_EQ(expectedTransform.modelView, builtTransform.modelView);
87 EXPECT_EQ(expectedTransform.transformFlags, expectedTransform.transformFlags);
88}
89
90static void expectGlopEq(Glop& expectedGlop, Glop& builtGlop) {
sergeyvf42bf3e2016-03-11 13:45:15 -080091 expectBlendEq(expectedGlop.blend, builtGlop.blend);
92 expectFillEq(expectedGlop.fill, builtGlop.fill);
93 expectMeshEq(expectedGlop.mesh, builtGlop.mesh);
94 expectTransformEq(expectedGlop.transform, builtGlop.transform);
95}
96
97static std::unique_ptr<Glop> blackUnitQuadGlop(RenderState& renderState) {
98 std::unique_ptr<Glop> glop(new Glop());
John Reck1bcacfd2017-11-03 10:12:19 -070099 glop->blend = {GL_ZERO, GL_ZERO};
sergeyvf42bf3e2016-03-11 13:45:15 -0800100 glop->mesh.elementCount = 4;
101 glop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
102 glop->mesh.indices.indices = nullptr;
103 glop->mesh.indices.bufferObject = GL_ZERO;
John Reck1bcacfd2017-11-03 10:12:19 -0700104 glop->mesh.vertices = {renderState.meshState().getUnitQuadVBO(),
105 VertexAttribFlags::None,
106 nullptr,
107 nullptr,
108 nullptr,
109 kTextureVertexStride};
sergeyvf42bf3e2016-03-11 13:45:15 -0800110 glop->transform.modelView.loadIdentity();
111 glop->fill.colorEnabled = true;
112 glop->fill.color.set(Color::Black);
113 glop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType;
114 glop->fill.filterMode = ProgramDescription::ColorFilterMode::None;
John Reck1bcacfd2017-11-03 10:12:19 -0700115 glop->fill.texture = {nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr};
sergeyvf42bf3e2016-03-11 13:45:15 -0800116 return glop;
117}
118
Greg Daniel98c78dad2017-01-04 14:45:56 -0500119RENDERTHREAD_OPENGL_PIPELINE_TEST(GlopBuilder, rectSnapTest) {
sergeyvf42bf3e2016-03-11 13:45:15 -0800120 RenderState& renderState = renderThread.renderState();
121 Caches& caches = Caches::getInstance();
122 SkPaint paint;
123 Rect dest(1, 1, 100, 100);
124 Matrix4 simpleTranslate;
125 simpleTranslate.loadTranslate(0.7, 0.7, 0);
126 Glop glop;
127 GlopBuilder(renderState, caches, &glop)
128 .setRoundRectClipState(nullptr)
129 .setMeshUnitQuad()
130 .setFillPaint(paint, 1.0f)
131 .setTransform(simpleTranslate, TransformFlags::None)
132 .setModelViewMapUnitToRectSnap(dest)
133 .build();
134
135 std::unique_ptr<Glop> goldenGlop(blackUnitQuadGlop(renderState));
136 // Rect(1,1,100,100) is the set destination,
137 // so unit quad should be translated by (1,1) and scaled by (99, 99)
138 // Tricky part: because translate (0.7, 0.7) and snapping were set in glopBuilder,
139 // unit quad also should be translate by additional (0.3, 0.3) to snap to exact pixels.
140 goldenGlop->transform.modelView.loadTranslate(1.3, 1.3, 0);
141 goldenGlop->transform.modelView.scale(99, 99, 1);
sergeyvf42bf3e2016-03-11 13:45:15 -0800142 goldenGlop->transform.canvas = simpleTranslate;
143 goldenGlop->fill.texture.filter = GL_NEAREST;
144 expectGlopEq(*goldenGlop, glop);
145}