BakedOpDispatcherTests.cpp revision 419a1e7ef53468e494d21c66ea7f63c0c522d208
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        EXPECT_EQ(expectedBounds, glop.bounds) << "bounds outset by stroke 'offset'";
89        Matrix4 expectedModelView;
90        expectedModelView.loadTranslate(10 - expectedOffset, 15 - expectedOffset, 0);
91        expectedModelView.scale(10 + 2 * expectedOffset, 10 + 2 * expectedOffset, 1);
92        EXPECT_EQ(expectedModelView, glop.transform.modelView)
93                << "X and Y offsets, and scale both applied to model view";
94    });
95}
96
97RENDERTHREAD_TEST(BakedOpDispatcher, onLayerOp_bufferless) {
98    SkPaint layerPaint;
99    layerPaint.setAlpha(128);
100    OffscreenBuffer* buffer = nullptr; // no providing a buffer, should hit rect fallback case
101    LayerOp op(Rect(10, 10), Matrix4::identity(), nullptr, &layerPaint, &buffer);
102    testUnmergedGlopDispatch(renderThread, &op, [&renderThread] (const Glop& glop) {
103        // rect glop is dispatched with paint props applied
104        EXPECT_EQ(renderThread.renderState().meshState().getUnitQuadVBO(),
105                glop.mesh.vertices.bufferObject) << "Unit quad should be drawn";
106        EXPECT_EQ(nullptr, glop.fill.texture.texture) << "Should be no texture when layer is null";
107        EXPECT_FLOAT_EQ(128 / 255.0f, glop.fill.color.a) << "Rect quad should use op alpha";
108    });
109}
110