Chris Craik | 419a1e7 | 2016-03-08 16:24:12 -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 | |
| 19 | #include <RecordedOp.h> |
| 20 | #include <BakedOpDispatcher.h> |
| 21 | #include <BakedOpRenderer.h> |
| 22 | #include <tests/common/TestUtils.h> |
| 23 | |
| 24 | using namespace android::uirenderer; |
| 25 | |
| 26 | static BakedOpRenderer::LightInfo sLightInfo; |
| 27 | static Rect sBaseClip(100, 100); |
| 28 | |
| 29 | class ValidatingBakedOpRenderer : public BakedOpRenderer { |
| 30 | public: |
| 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 | } |
| 36 | private: |
| 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 | |
| 46 | typedef void (*BakedOpReceiver)(BakedOpRenderer&, const BakedOpState&); |
| 47 | |
| 48 | static 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 | |
| 74 | RENDERTHREAD_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); |
Chris Craik | 70969cc | 2016-03-30 18:09:17 -0700 | [diff] [blame] | 88 | #if !HWUI_NEW_OPS |
Chris Craik | 419a1e7 | 2016-03-08 16:24:12 -0800 | [diff] [blame] | 89 | EXPECT_EQ(expectedBounds, glop.bounds) << "bounds outset by stroke 'offset'"; |
Chris Craik | 70969cc | 2016-03-30 18:09:17 -0700 | [diff] [blame] | 90 | #endif |
Chris Craik | 419a1e7 | 2016-03-08 16:24:12 -0800 | [diff] [blame] | 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 | |
| 99 | RENDERTHREAD_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 | } |