BakedOpDispatcherTests.cpp revision 92a5d4b99a5554adca0e94627d44d2bd7f4f0bc0
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 19#include <RecordedOp.h> 20#include <BakedOpDispatcher.h> 21#include <BakedOpRenderer.h> 22#include <tests/common/TestUtils.h> 23 24using namespace android::uirenderer; 25 26static BakedOpRenderer::LightInfo sLightInfo; 27static Rect sBaseClip(100, 100); 28 29class ValidatingBakedOpRenderer : public BakedOpRenderer { 30public: 31 ValidatingBakedOpRenderer(RenderState& renderState, std::function<void(const Glop& glop)> validator) 32 : BakedOpRenderer(Caches::getInstance(), renderState, true, sLightInfo) 33 , mValidator(validator) { 34 mGlopReceiver = ValidatingGlopReceiver; 35 } 36private: 37 static void ValidatingGlopReceiver(BakedOpRenderer& renderer, const Rect* dirtyBounds, 38 const ClipBase* clip, const Glop& glop) { 39 40 auto vbor = reinterpret_cast<ValidatingBakedOpRenderer*>(&renderer); 41 vbor->mValidator(glop); 42 } 43 std::function<void(const Glop& glop)> mValidator; 44}; 45 46typedef void (*BakedOpReceiver)(BakedOpRenderer&, const BakedOpState&); 47 48static void testUnmergedGlopDispatch(renderthread::RenderThread& renderThread, RecordedOp* op, 49 std::function<void(const Glop& glop)> glopVerifier) { 50 // Create op, and wrap with basic state. 51 LinearAllocator allocator; 52 auto snapshot = TestUtils::makeSnapshot(Matrix4::identity(), sBaseClip); 53 auto state = BakedOpState::tryConstruct(allocator, *snapshot, *op); 54 ASSERT_NE(nullptr, state); 55 56 int glopCount = 0; 57 auto glopReceiver = [&glopVerifier, &glopCount] (const Glop& glop) { 58 ASSERT_EQ(glopCount++, 0) << "Only one Glop expected"; 59 glopVerifier(glop); 60 }; 61 ValidatingBakedOpRenderer renderer(renderThread.renderState(), glopReceiver); 62 63 // Dispatch based on op type created, similar to Frame/LayerBuilder dispatch behavior 64#define X(Type) \ 65 [](BakedOpRenderer& renderer, const BakedOpState& state) { \ 66 BakedOpDispatcher::on##Type(renderer, static_cast<const Type&>(*(state.op)), state); \ 67 }, 68 static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X); 69#undef X 70 unmergedReceivers[op->opId](renderer, *state); 71 ASSERT_EQ(1, glopCount) << "Exactly one Glop expected"; 72} 73 74RENDERTHREAD_TEST(BakedOpDispatcher, onArc_position) { 75 SkPaint strokePaint; 76 strokePaint.setStyle(SkPaint::kStroke_Style); 77 strokePaint.setStrokeWidth(4); 78 ArcOp op(Rect(10, 15, 20, 25), Matrix4::identity(), nullptr, &strokePaint, 0, 270, true); 79 testUnmergedGlopDispatch(renderThread, &op, [] (const Glop& glop) { 80 // validate glop produced by renderPathTexture (so texture, unit quad) 81 auto texture = glop.fill.texture.texture; 82 ASSERT_NE(nullptr, texture); 83 float expectedOffset = floor(4 * 1.5f + 0.5f); 84 EXPECT_EQ(expectedOffset, reinterpret_cast<PathTexture*>(texture)->offset) 85 << "Should see conservative offset from PathCache::computeBounds"; 86 Rect expectedBounds(10, 15, 20, 25); 87 expectedBounds.outset(expectedOffset); 88#if !HWUI_NEW_OPS 89 EXPECT_EQ(expectedBounds, glop.bounds) << "bounds outset by stroke 'offset'"; 90#endif 91 Matrix4 expectedModelView; 92 expectedModelView.loadTranslate(10 - expectedOffset, 15 - expectedOffset, 0); 93 expectedModelView.scale(10 + 2 * expectedOffset, 10 + 2 * expectedOffset, 1); 94 EXPECT_EQ(expectedModelView, glop.transform.modelView) 95 << "X and Y offsets, and scale both applied to model view"; 96 }); 97} 98 99RENDERTHREAD_TEST(BakedOpDispatcher, onLayerOp_bufferless) { 100 SkPaint layerPaint; 101 layerPaint.setAlpha(128); 102 OffscreenBuffer* buffer = nullptr; // no providing a buffer, should hit rect fallback case 103 LayerOp op(Rect(10, 10), Matrix4::identity(), nullptr, &layerPaint, &buffer); 104 testUnmergedGlopDispatch(renderThread, &op, [&renderThread] (const Glop& glop) { 105 // rect glop is dispatched with paint props applied 106 EXPECT_EQ(renderThread.renderState().meshState().getUnitQuadVBO(), 107 glop.mesh.vertices.bufferObject) << "Unit quad should be drawn"; 108 EXPECT_EQ(nullptr, glop.fill.texture.texture) << "Should be no texture when layer is null"; 109 EXPECT_FLOAT_EQ(128 / 255.0f, glop.fill.color.a) << "Rect quad should use op alpha"; 110 }); 111} 112 113static int getGlopTransformFlags(renderthread::RenderThread& renderThread, RecordedOp* op) { 114 int result = 0; 115 testUnmergedGlopDispatch(renderThread, op, [&result] (const Glop& glop) { 116 result = glop.transform.transformFlags; 117 }); 118 return result; 119} 120 121RENDERTHREAD_TEST(BakedOpDispatcher, offsetFlags) { 122 Rect bounds(10, 15, 20, 25); 123 SkPaint paint; 124 SkPaint aaPaint; 125 aaPaint.setAntiAlias(true); 126 127 RoundRectOp roundRectOp(bounds, Matrix4::identity(), nullptr, &paint, 0, 270); 128 EXPECT_EQ(TransformFlags::None, getGlopTransformFlags(renderThread, &roundRectOp)) 129 << "Expect no offset for round rect op."; 130 131 const float points[4] = {0.5, 0.5, 1.0, 1.0}; 132 PointsOp antiAliasedPointsOp(bounds, Matrix4::identity(), nullptr, &aaPaint, points, 4); 133 EXPECT_EQ(TransformFlags::None, getGlopTransformFlags(renderThread, &antiAliasedPointsOp)) 134 << "Expect no offset for AA points."; 135 PointsOp pointsOp(bounds, Matrix4::identity(), nullptr, &paint, points, 4); 136 EXPECT_EQ(TransformFlags::OffsetByFudgeFactor, getGlopTransformFlags(renderThread, &pointsOp)) 137 << "Expect an offset for non-AA points."; 138 139 LinesOp antiAliasedLinesOp(bounds, Matrix4::identity(), nullptr, &aaPaint, points, 4); 140 EXPECT_EQ(TransformFlags::None, getGlopTransformFlags(renderThread, &antiAliasedLinesOp)) 141 << "Expect no offset for AA lines."; 142 LinesOp linesOp(bounds, Matrix4::identity(), nullptr, &paint, points, 4); 143 EXPECT_EQ(TransformFlags::OffsetByFudgeFactor, getGlopTransformFlags(renderThread, &linesOp)) 144 << "Expect an offset for non-AA lines."; 145}