sergeyv | f42bf3e | 2016-03-11 13:45:15 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
sergeyv | f42bf3e | 2016-03-11 13:45:15 -0800 | [diff] [blame] | 19 | #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 | |
| 27 | using namespace android::uirenderer; |
| 28 | |
| 29 | static void expectFillEq(Glop::Fill& expectedFill, Glop::Fill& builtFill) { |
| 30 | EXPECT_EQ(expectedFill.colorEnabled, builtFill.colorEnabled); |
| 31 | if (expectedFill.colorEnabled) |
| 32 | EXPECT_EQ(expectedFill.color, builtFill.color); |
| 33 | |
| 34 | EXPECT_EQ(expectedFill.filterMode, builtFill.filterMode); |
| 35 | if (expectedFill.filterMode == ProgramDescription::ColorFilterMode::Blend) { |
| 36 | EXPECT_EQ(expectedFill.filter.color, builtFill.filter.color); |
| 37 | } else if (expectedFill.filterMode == ProgramDescription::ColorFilterMode::Matrix) { |
| 38 | Glop::Fill::Filter::Matrix& expectedMatrix = expectedFill.filter.matrix; |
| 39 | Glop::Fill::Filter::Matrix& builtMatrix = expectedFill.filter.matrix; |
| 40 | EXPECT_TRUE(std::memcmp(expectedMatrix.matrix, builtMatrix.matrix, |
| 41 | sizeof(Glop::Fill::Filter::Matrix::matrix))); |
| 42 | EXPECT_TRUE(std::memcmp(expectedMatrix.vector, builtMatrix.vector, |
| 43 | sizeof(Glop::Fill::Filter::Matrix::vector))); |
| 44 | } |
| 45 | EXPECT_EQ(expectedFill.skiaShaderData.skiaShaderType, builtFill.skiaShaderData.skiaShaderType); |
| 46 | EXPECT_EQ(expectedFill.texture.clamp, builtFill.texture.clamp); |
| 47 | EXPECT_EQ(expectedFill.texture.filter, builtFill.texture.filter); |
| 48 | EXPECT_EQ(expectedFill.texture.target, builtFill.texture.target); |
| 49 | EXPECT_EQ(expectedFill.texture.textureTransform, builtFill.texture.textureTransform); |
| 50 | } |
| 51 | |
| 52 | static void expectBlendEq(Glop::Blend& expectedBlend, Glop::Blend& builtBlend) { |
| 53 | EXPECT_EQ(expectedBlend.src, builtBlend.src); |
| 54 | EXPECT_EQ(expectedBlend.dst, builtBlend.dst); |
| 55 | } |
| 56 | |
| 57 | static void expectMeshEq(Glop::Mesh& expectedMesh, Glop::Mesh& builtMesh) { |
| 58 | EXPECT_EQ(expectedMesh.elementCount, builtMesh.elementCount); |
| 59 | EXPECT_EQ(expectedMesh.primitiveMode, builtMesh.primitiveMode); |
| 60 | EXPECT_EQ(expectedMesh.indices.indices, builtMesh.indices.indices); |
| 61 | EXPECT_EQ(expectedMesh.indices.bufferObject, builtMesh.indices.bufferObject); |
| 62 | EXPECT_EQ(expectedMesh.vertices.attribFlags, builtMesh.vertices.attribFlags); |
| 63 | EXPECT_EQ(expectedMesh.vertices.bufferObject, builtMesh.vertices.bufferObject); |
| 64 | EXPECT_EQ(expectedMesh.vertices.color, builtMesh.vertices.color); |
| 65 | EXPECT_EQ(expectedMesh.vertices.position, builtMesh.vertices.position); |
| 66 | EXPECT_EQ(expectedMesh.vertices.stride, builtMesh.vertices.stride); |
| 67 | EXPECT_EQ(expectedMesh.vertices.texCoord, builtMesh.vertices.texCoord); |
| 68 | |
| 69 | if (builtMesh.vertices.position) { |
| 70 | for (int i = 0; i < 4; i++) { |
| 71 | TextureVertex& expectedVertex = expectedMesh.mappedVertices[i]; |
| 72 | TextureVertex& builtVertex = builtMesh.mappedVertices[i]; |
| 73 | EXPECT_EQ(expectedVertex.u, builtVertex.u); |
| 74 | EXPECT_EQ(expectedVertex.v, builtVertex.v); |
| 75 | EXPECT_EQ(expectedVertex.x, builtVertex.x); |
| 76 | EXPECT_EQ(expectedVertex.y, builtVertex.y); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void expectTransformEq(Glop::Transform& expectedTransform, Glop::Transform& builtTransform) { |
| 82 | EXPECT_EQ(expectedTransform.canvas, builtTransform.canvas); |
| 83 | EXPECT_EQ(expectedTransform.modelView, builtTransform.modelView); |
| 84 | EXPECT_EQ(expectedTransform.transformFlags, expectedTransform.transformFlags); |
| 85 | } |
| 86 | |
| 87 | static void expectGlopEq(Glop& expectedGlop, Glop& builtGlop) { |
| 88 | EXPECT_EQ(expectedGlop.bounds, builtGlop.bounds); |
| 89 | expectBlendEq(expectedGlop.blend, builtGlop.blend); |
| 90 | expectFillEq(expectedGlop.fill, builtGlop.fill); |
| 91 | expectMeshEq(expectedGlop.mesh, builtGlop.mesh); |
| 92 | expectTransformEq(expectedGlop.transform, builtGlop.transform); |
| 93 | } |
| 94 | |
| 95 | static std::unique_ptr<Glop> blackUnitQuadGlop(RenderState& renderState) { |
| 96 | std::unique_ptr<Glop> glop(new Glop()); |
| 97 | glop->blend = { GL_ZERO, GL_ZERO }; |
| 98 | glop->mesh.elementCount = 4; |
| 99 | glop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 100 | glop->mesh.indices.indices = nullptr; |
| 101 | glop->mesh.indices.bufferObject = GL_ZERO; |
| 102 | glop->mesh.vertices = { |
| 103 | renderState.meshState().getUnitQuadVBO(), |
| 104 | VertexAttribFlags::None, |
| 105 | nullptr, nullptr, nullptr, |
| 106 | kTextureVertexStride }; |
| 107 | glop->transform.modelView.loadIdentity(); |
| 108 | glop->fill.colorEnabled = true; |
| 109 | glop->fill.color.set(Color::Black); |
| 110 | glop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType; |
| 111 | glop->fill.filterMode = ProgramDescription::ColorFilterMode::None; |
| 112 | glop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
| 113 | return glop; |
| 114 | } |
| 115 | |
| 116 | RENDERTHREAD_TEST(GlopBuilder, rectSnapTest) { |
| 117 | RenderState& renderState = renderThread.renderState(); |
| 118 | Caches& caches = Caches::getInstance(); |
| 119 | SkPaint paint; |
| 120 | Rect dest(1, 1, 100, 100); |
| 121 | Matrix4 simpleTranslate; |
| 122 | simpleTranslate.loadTranslate(0.7, 0.7, 0); |
| 123 | Glop glop; |
| 124 | GlopBuilder(renderState, caches, &glop) |
| 125 | .setRoundRectClipState(nullptr) |
| 126 | .setMeshUnitQuad() |
| 127 | .setFillPaint(paint, 1.0f) |
| 128 | .setTransform(simpleTranslate, TransformFlags::None) |
| 129 | .setModelViewMapUnitToRectSnap(dest) |
| 130 | .build(); |
| 131 | |
| 132 | std::unique_ptr<Glop> goldenGlop(blackUnitQuadGlop(renderState)); |
| 133 | // Rect(1,1,100,100) is the set destination, |
| 134 | // so unit quad should be translated by (1,1) and scaled by (99, 99) |
| 135 | // Tricky part: because translate (0.7, 0.7) and snapping were set in glopBuilder, |
| 136 | // unit quad also should be translate by additional (0.3, 0.3) to snap to exact pixels. |
| 137 | goldenGlop->transform.modelView.loadTranslate(1.3, 1.3, 0); |
| 138 | goldenGlop->transform.modelView.scale(99, 99, 1); |
| 139 | goldenGlop->bounds = android::uirenderer::Rect(1.70, 1.70, 100.70, 100.70); |
| 140 | goldenGlop->transform.canvas = simpleTranslate; |
| 141 | goldenGlop->fill.texture.filter = GL_NEAREST; |
| 142 | expectGlopEq(*goldenGlop, glop); |
| 143 | } |