BakedOpDispatcherTests.cpp revision e98a046d3110eb2ff11a17f4ff27764b803a1379
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
24#include <SkDashPathEffect.h>
25
26using namespace android::uirenderer;
27
28static BakedOpRenderer::LightInfo sLightInfo;
29static Rect sBaseClip(100, 100);
30
31class ValidatingBakedOpRenderer : public BakedOpRenderer {
32public:
33    ValidatingBakedOpRenderer(RenderState& renderState, std::function<void(const Glop& glop)> validator)
34            : BakedOpRenderer(Caches::getInstance(), renderState, true, sLightInfo)
35            , mValidator(validator) {
36        mGlopReceiver = ValidatingGlopReceiver;
37    }
38private:
39    static void ValidatingGlopReceiver(BakedOpRenderer& renderer, const Rect* dirtyBounds,
40            const ClipBase* clip, const Glop& glop) {
41
42        auto vbor = reinterpret_cast<ValidatingBakedOpRenderer*>(&renderer);
43        vbor->mValidator(glop);
44    }
45    std::function<void(const Glop& glop)> mValidator;
46};
47
48typedef void (*BakedOpReceiver)(BakedOpRenderer&, const BakedOpState&);
49
50static void testUnmergedGlopDispatch(renderthread::RenderThread& renderThread, RecordedOp* op,
51        std::function<void(const Glop& glop)> glopVerifier) {
52    // Create op, and wrap with basic state.
53    LinearAllocator allocator;
54    auto snapshot = TestUtils::makeSnapshot(Matrix4::identity(), sBaseClip);
55    auto state = BakedOpState::tryConstruct(allocator, *snapshot, *op);
56    ASSERT_NE(nullptr, state);
57
58    int glopCount = 0;
59    auto glopReceiver = [&glopVerifier, &glopCount] (const Glop& glop) {
60        ASSERT_EQ(glopCount++, 0) << "Only one Glop expected";
61        glopVerifier(glop);
62    };
63    ValidatingBakedOpRenderer renderer(renderThread.renderState(), glopReceiver);
64
65    // Dispatch based on op type created, similar to Frame/LayerBuilder dispatch behavior
66#define X(Type) \
67        [](BakedOpRenderer& renderer, const BakedOpState& state) { \
68            BakedOpDispatcher::on##Type(renderer, static_cast<const Type&>(*(state.op)), state); \
69        },
70    static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X);
71#undef X
72    unmergedReceivers[op->opId](renderer, *state);
73    ASSERT_EQ(1, glopCount) << "Exactly one Glop expected";
74}
75
76RENDERTHREAD_TEST(BakedOpDispatcher, pathTexture_positionOvalArc) {
77    SkPaint strokePaint;
78    strokePaint.setStyle(SkPaint::kStroke_Style);
79    strokePaint.setStrokeWidth(4);
80
81    float intervals[] = {1.0f, 1.0f};
82    auto dashEffect = SkDashPathEffect::Create(intervals, 2, 0);
83    strokePaint.setPathEffect(dashEffect);
84    dashEffect->unref();
85
86    auto textureGlopVerifier = [] (const Glop& glop) {
87        // validate glop produced by renderPathTexture (so texture, unit quad)
88        auto texture = glop.fill.texture.texture;
89        ASSERT_NE(nullptr, texture);
90        float expectedOffset = floor(4 * 1.5f + 0.5f);
91        EXPECT_EQ(expectedOffset, reinterpret_cast<PathTexture*>(texture)->offset)
92                << "Should see conservative offset from PathCache::computeBounds";
93        Rect expectedBounds(10, 15, 20, 25);
94        expectedBounds.outset(expectedOffset);
95
96        Matrix4 expectedModelView;
97        expectedModelView.loadTranslate(10 - expectedOffset, 15 - expectedOffset, 0);
98        expectedModelView.scale(10 + 2 * expectedOffset, 10 + 2 * expectedOffset, 1);
99        EXPECT_EQ(expectedModelView, glop.transform.modelView)
100                << "X and Y offsets, and scale both applied to model view";
101    };
102
103    // Arc and Oval will render functionally the same glop, differing only in texture content
104    ArcOp arcOp(Rect(10, 15, 20, 25), Matrix4::identity(), nullptr, &strokePaint, 0, 270, true);
105    testUnmergedGlopDispatch(renderThread, &arcOp, textureGlopVerifier);
106
107    OvalOp ovalOp(Rect(10, 15, 20, 25), Matrix4::identity(), nullptr, &strokePaint);
108    testUnmergedGlopDispatch(renderThread, &ovalOp, textureGlopVerifier);
109}
110
111RENDERTHREAD_TEST(BakedOpDispatcher, onLayerOp_bufferless) {
112    SkPaint layerPaint;
113    layerPaint.setAlpha(128);
114    OffscreenBuffer* buffer = nullptr; // no providing a buffer, should hit rect fallback case
115    LayerOp op(Rect(10, 10), Matrix4::identity(), nullptr, &layerPaint, &buffer);
116    testUnmergedGlopDispatch(renderThread, &op, [&renderThread] (const Glop& glop) {
117        // rect glop is dispatched with paint props applied
118        EXPECT_EQ(renderThread.renderState().meshState().getUnitQuadVBO(),
119                glop.mesh.vertices.bufferObject) << "Unit quad should be drawn";
120        EXPECT_EQ(nullptr, glop.fill.texture.texture) << "Should be no texture when layer is null";
121        EXPECT_FLOAT_EQ(128 / 255.0f, glop.fill.color.a) << "Rect quad should use op alpha";
122    });
123}
124