From 419a1e7ef53468e494d21c66ea7f63c0c522d208 Mon Sep 17 00:00:00 2001 From: Chris Craik Date: Tue, 8 Mar 2016 16:24:12 -0800 Subject: [PATCH] Add initial BakedOpDispatcher tests bug:26571145 bug:26923968 bug:27389290 Change-Id: If8ba33732d09b335171f87d5efc419641bafa126 --- libs/hwui/Android.mk | 1 + libs/hwui/BakedOpRenderer.cpp | 2 +- libs/hwui/BakedOpRenderer.h | 23 ++-- .../tests/unit/BakedOpDispatcherTests.cpp | 109 ++++++++++++++++++ 4 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 libs/hwui/tests/unit/BakedOpDispatcherTests.cpp diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk index 70995acbb4ca4..60f7a190ba783 100644 --- a/libs/hwui/Android.mk +++ b/libs/hwui/Android.mk @@ -252,6 +252,7 @@ LOCAL_SRC_FILES += \ ifeq (true, $(HWUI_NEW_OPS)) LOCAL_SRC_FILES += \ + tests/unit/BakedOpDispatcherTests.cpp \ tests/unit/BakedOpStateTests.cpp \ tests/unit/FrameBuilderTests.cpp \ tests/unit/LeakCheckTests.cpp \ diff --git a/libs/hwui/BakedOpRenderer.cpp b/libs/hwui/BakedOpRenderer.cpp index 98493d73776eb..da5eccaf1c125 100644 --- a/libs/hwui/BakedOpRenderer.cpp +++ b/libs/hwui/BakedOpRenderer.cpp @@ -324,7 +324,7 @@ void BakedOpRenderer::prepareRender(const Rect* dirtyBounds, const ClipBase* cli } } -void BakedOpRenderer::renderGlop(const Rect* dirtyBounds, const ClipBase* clip, +void BakedOpRenderer::renderGlopImpl(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop) { prepareRender(dirtyBounds, clip); mRenderState.render(glop, mRenderTarget.orthoMatrix); diff --git a/libs/hwui/BakedOpRenderer.h b/libs/hwui/BakedOpRenderer.h index 4b652553b6b9a..1b4065aa0aa6b 100644 --- a/libs/hwui/BakedOpRenderer.h +++ b/libs/hwui/BakedOpRenderer.h @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef ANDROID_HWUI_BAKED_OP_RENDERER_H -#define ANDROID_HWUI_BAKED_OP_RENDERER_H +#pragma once #include "BakedOpState.h" #include "Matrix.h" @@ -41,6 +40,7 @@ struct ClipBase; */ class BakedOpRenderer { public: + typedef void (*GlopReceiver)(BakedOpRenderer&, const Rect*, const ClipBase*, const Glop&); /** * Position agnostic shadow lighting info. Used with all shadow ops in scene. */ @@ -54,8 +54,10 @@ public: uint8_t spotShadowAlpha; }; - BakedOpRenderer(Caches& caches, RenderState& renderState, bool opaque, const LightInfo& lightInfo) - : mRenderState(renderState) + BakedOpRenderer(Caches& caches, RenderState& renderState, bool opaque, + const LightInfo& lightInfo) + : mGlopReceiver(DefaultGlopReceiver) + , mRenderState(renderState) , mCaches(caches) , mOpaque(opaque) , mLightInfo(lightInfo) { @@ -81,7 +83,9 @@ public: } void renderFunctor(const FunctorOp& op, const BakedOpState& state); - void renderGlop(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop); + void renderGlop(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop) { + mGlopReceiver(*this, dirtyBounds, clip, glop); + } bool offscreenRenderTarget() { return mRenderTarget.offscreenBuffer != nullptr; } void dirtyRenderTarget(const Rect& dirtyRect); bool didDraw() const { return mHasDrawn; } @@ -95,7 +99,14 @@ public: drawRects(ltrb, 4, paint); } void drawRects(const float* rects, int count, const SkPaint* paint); +protected: + GlopReceiver mGlopReceiver; private: + static void DefaultGlopReceiver(BakedOpRenderer& renderer, const Rect* dirtyBounds, + const ClipBase* clip, const Glop& glop) { + renderer.renderGlopImpl(dirtyBounds, clip, glop); + } + void renderGlopImpl(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop); void setViewport(uint32_t width, uint32_t height); void clearColorBuffer(const Rect& clearRect); void prepareRender(const Rect* dirtyBounds, const ClipBase* clip); @@ -136,5 +147,3 @@ private: }; // namespace uirenderer }; // namespace android - -#endif // ANDROID_HWUI_BAKED_OP_RENDERER_H diff --git a/libs/hwui/tests/unit/BakedOpDispatcherTests.cpp b/libs/hwui/tests/unit/BakedOpDispatcherTests.cpp new file mode 100644 index 0000000000000..654ddc6a28e58 --- /dev/null +++ b/libs/hwui/tests/unit/BakedOpDispatcherTests.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include + +using namespace android::uirenderer; + +static BakedOpRenderer::LightInfo sLightInfo; +static Rect sBaseClip(100, 100); + +class ValidatingBakedOpRenderer : public BakedOpRenderer { +public: + ValidatingBakedOpRenderer(RenderState& renderState, std::function validator) + : BakedOpRenderer(Caches::getInstance(), renderState, true, sLightInfo) + , mValidator(validator) { + mGlopReceiver = ValidatingGlopReceiver; + } +private: + static void ValidatingGlopReceiver(BakedOpRenderer& renderer, const Rect* dirtyBounds, + const ClipBase* clip, const Glop& glop) { + + auto vbor = reinterpret_cast(&renderer); + vbor->mValidator(glop); + } + std::function mValidator; +}; + +typedef void (*BakedOpReceiver)(BakedOpRenderer&, const BakedOpState&); + +static void testUnmergedGlopDispatch(renderthread::RenderThread& renderThread, RecordedOp* op, + std::function glopVerifier) { + // Create op, and wrap with basic state. + LinearAllocator allocator; + auto snapshot = TestUtils::makeSnapshot(Matrix4::identity(), sBaseClip); + auto state = BakedOpState::tryConstruct(allocator, *snapshot, *op); + ASSERT_NE(nullptr, state); + + int glopCount = 0; + auto glopReceiver = [&glopVerifier, &glopCount] (const Glop& glop) { + ASSERT_EQ(glopCount++, 0) << "Only one Glop expected"; + glopVerifier(glop); + }; + ValidatingBakedOpRenderer renderer(renderThread.renderState(), glopReceiver); + + // Dispatch based on op type created, similar to Frame/LayerBuilder dispatch behavior +#define X(Type) \ + [](BakedOpRenderer& renderer, const BakedOpState& state) { \ + BakedOpDispatcher::on##Type(renderer, static_cast(*(state.op)), state); \ + }, + static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X); +#undef X + unmergedReceivers[op->opId](renderer, *state); + ASSERT_EQ(1, glopCount) << "Exactly one Glop expected"; +} + +RENDERTHREAD_TEST(BakedOpDispatcher, onArc_position) { + SkPaint strokePaint; + strokePaint.setStyle(SkPaint::kStroke_Style); + strokePaint.setStrokeWidth(4); + ArcOp op(Rect(10, 15, 20, 25), Matrix4::identity(), nullptr, &strokePaint, 0, 270, true); + testUnmergedGlopDispatch(renderThread, &op, [] (const Glop& glop) { + // validate glop produced by renderPathTexture (so texture, unit quad) + auto texture = glop.fill.texture.texture; + ASSERT_NE(nullptr, texture); + float expectedOffset = floor(4 * 1.5f + 0.5f); + EXPECT_EQ(expectedOffset, reinterpret_cast(texture)->offset) + << "Should see conservative offset from PathCache::computeBounds"; + Rect expectedBounds(10, 15, 20, 25); + expectedBounds.outset(expectedOffset); + EXPECT_EQ(expectedBounds, glop.bounds) << "bounds outset by stroke 'offset'"; + Matrix4 expectedModelView; + expectedModelView.loadTranslate(10 - expectedOffset, 15 - expectedOffset, 0); + expectedModelView.scale(10 + 2 * expectedOffset, 10 + 2 * expectedOffset, 1); + EXPECT_EQ(expectedModelView, glop.transform.modelView) + << "X and Y offsets, and scale both applied to model view"; + }); +} + +RENDERTHREAD_TEST(BakedOpDispatcher, onLayerOp_bufferless) { + SkPaint layerPaint; + layerPaint.setAlpha(128); + OffscreenBuffer* buffer = nullptr; // no providing a buffer, should hit rect fallback case + LayerOp op(Rect(10, 10), Matrix4::identity(), nullptr, &layerPaint, &buffer); + testUnmergedGlopDispatch(renderThread, &op, [&renderThread] (const Glop& glop) { + // rect glop is dispatched with paint props applied + EXPECT_EQ(renderThread.renderState().meshState().getUnitQuadVBO(), + glop.mesh.vertices.bufferObject) << "Unit quad should be drawn"; + EXPECT_EQ(nullptr, glop.fill.texture.texture) << "Should be no texture when layer is null"; + EXPECT_FLOAT_EQ(128 / 255.0f, glop.fill.color.a) << "Rect quad should use op alpha"; + }); +}