TestUtils.h revision c1c54062f8cc9d47bdea820ae5ab6aef260b4488
1/* 2 * Copyright (C) 2015 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#pragma once 18 19#include <DeviceInfo.h> 20#include <DisplayList.h> 21#include <Matrix.h> 22#include <Rect.h> 23#include <RenderNode.h> 24#include <hwui/Bitmap.h> 25#include <renderstate/RenderState.h> 26#include <renderthread/RenderThread.h> 27#include <Snapshot.h> 28 29#include <RecordedOp.h> 30#include <RecordingCanvas.h> 31 32#include <memory> 33 34namespace android { 35namespace uirenderer { 36 37#define EXPECT_MATRIX_APPROX_EQ(a, b) \ 38 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) 39 40#define EXPECT_RECT_APPROX_EQ(a, b) \ 41 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) \ 42 && MathUtils::areEqual((a).top, (b).top) \ 43 && MathUtils::areEqual((a).right, (b).right) \ 44 && MathUtils::areEqual((a).bottom, (b).bottom)); 45 46#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \ 47 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \ 48 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \ 49 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \ 50 } else { \ 51 ADD_FAILURE() << "ClipState not a rect"; \ 52 } 53/** 54 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope 55 * (for e.g. accessing its RenderState) 56 */ 57#define RENDERTHREAD_TEST(test_case_name, test_name) \ 58 class test_case_name##_##test_name##_RenderThreadTest { \ 59 public: \ 60 static void doTheThing(renderthread::RenderThread& renderThread); \ 61 }; \ 62 TEST(test_case_name, test_name) { \ 63 TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing); \ 64 }; \ 65 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread) 66 67/** 68 * Sets a property value temporarily, generally for the duration of a test, restoring the previous 69 * value when going out of scope. 70 * 71 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled. 72 */ 73template <typename T> 74class ScopedProperty { 75public: 76 ScopedProperty(T& property, T newValue) 77 : mPropertyPtr(&property) 78 , mOldValue(property) { 79 property = newValue; 80 } 81 ~ScopedProperty() { 82 *mPropertyPtr = mOldValue; 83 } 84private: 85 T* mPropertyPtr; 86 T mOldValue; 87}; 88 89class TestUtils { 90public: 91 class SignalingDtor { 92 public: 93 SignalingDtor() 94 : mSignal(nullptr) {} 95 explicit SignalingDtor(int* signal) 96 : mSignal(signal) {} 97 void setSignal(int* signal) { 98 mSignal = signal; 99 } 100 ~SignalingDtor() { 101 if (mSignal) { 102 (*mSignal)++; 103 } 104 } 105 private: 106 int* mSignal; 107 }; 108 109 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) { 110 for (int i = 0; i < 16; i++) { 111 if (!MathUtils::areEqual(a[i], b[i])) { 112 return false; 113 } 114 } 115 return true; 116 } 117 118 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) { 119 std::unique_ptr<Snapshot> snapshot(new Snapshot()); 120 snapshot->clip(clip, SkRegion::kReplace_Op); // store clip first, so it isn't transformed 121 *(snapshot->transform) = transform; 122 return snapshot; 123 } 124 125 static SkBitmap createSkBitmap(int width, int height, 126 SkColorType colorType = kN32_SkColorType) { 127 SkBitmap bitmap; 128 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); 129 SkImageInfo info = SkImageInfo::Make(width, height, 130 colorType, kPremul_SkAlphaType, colorSpace); 131 bitmap.setInfo(info); 132 Bitmap::allocateHeapBitmap(&bitmap, nullptr); 133 return bitmap; 134 } 135 136 static sp<DeferredLayerUpdater> createTextureLayerUpdater( 137 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, 138 const SkMatrix& transform); 139 140 template<class CanvasType> 141 static std::unique_ptr<DisplayList> createDisplayList(int width, int height, 142 std::function<void(CanvasType& canvas)> canvasCallback) { 143 CanvasType canvas(width, height); 144 canvasCallback(canvas); 145 return std::unique_ptr<DisplayList>(canvas.finishRecording()); 146 } 147 148 static sp<RenderNode> createNode(int left, int top, int right, int bottom, 149 std::function<void(RenderProperties& props, Canvas& canvas)> setup) { 150#if HWUI_NULL_GPU 151 // if RenderNodes are being sync'd/used, device info will be needed, since 152 // DeviceInfo::maxTextureSize() affects layer property 153 DeviceInfo::initialize(); 154#endif 155 156 sp<RenderNode> node = new RenderNode(); 157 RenderProperties& props = node->mutateStagingProperties(); 158 props.setLeftTopRightBottom(left, top, right, bottom); 159 if (setup) { 160 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(props.getWidth(), 161 props.getHeight())); 162 setup(props, *canvas.get()); 163 node->setStagingDisplayList(canvas->finishRecording(), nullptr); 164 } 165 node->setPropertyFieldsDirty(0xFFFFFFFF); 166 return node; 167 } 168 169 template<class RecordingCanvasType> 170 static sp<RenderNode> createNode(int left, int top, int right, int bottom, 171 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) { 172#if HWUI_NULL_GPU 173 // if RenderNodes are being sync'd/used, device info will be needed, since 174 // DeviceInfo::maxTextureSize() affects layer property 175 DeviceInfo::initialize(); 176#endif 177 178 sp<RenderNode> node = new RenderNode(); 179 RenderProperties& props = node->mutateStagingProperties(); 180 props.setLeftTopRightBottom(left, top, right, bottom); 181 if (setup) { 182 RecordingCanvasType canvas(props.getWidth(), props.getHeight()); 183 setup(props, canvas); 184 node->setStagingDisplayList(canvas.finishRecording(), nullptr); 185 } 186 node->setPropertyFieldsDirty(0xFFFFFFFF); 187 return node; 188 } 189 190 static void recordNode(RenderNode& node, 191 std::function<void(Canvas&)> contentCallback) { 192 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas( 193 node.stagingProperties().getWidth(), node.stagingProperties().getHeight())); 194 contentCallback(*canvas.get()); 195 node.setStagingDisplayList(canvas->finishRecording(), nullptr); 196 } 197 198 /** 199 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging 200 * properties and DisplayList moved to the render copies. 201 * 202 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded. 203 * For this reason, this should generally only be called once on a tree. 204 */ 205 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) { 206 syncHierarchyPropertiesAndDisplayListImpl(node.get()); 207 } 208 209 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) { 210 syncHierarchyPropertiesAndDisplayList(node); 211 return node; 212 } 213 214 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback; 215 216 class TestTask : public renderthread::RenderTask { 217 public: 218 explicit TestTask(RtCallback rtCallback) 219 : rtCallback(rtCallback) {} 220 virtual ~TestTask() {} 221 virtual void run() override; 222 RtCallback rtCallback; 223 }; 224 225 /** 226 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely. 227 */ 228 static void runOnRenderThread(RtCallback rtCallback) { 229 TestTask task(rtCallback); 230 renderthread::RenderThread::getInstance().queueAndWait(&task); 231 } 232 233 static bool isRenderThreadRunning() { 234 return renderthread::RenderThread::hasInstance(); 235 } 236 237 static SkColor interpolateColor(float fraction, SkColor start, SkColor end); 238 239 static void layoutTextUnscaled(const SkPaint& paint, const char* text, 240 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, 241 float* outTotalAdvance, Rect* outBounds); 242 243 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, 244 const SkPaint& paint, float x, float y); 245 246 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, 247 const SkPaint& paint, const SkPath& path); 248 249 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str); 250 251private: 252 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) { 253 node->syncProperties(); 254 node->syncDisplayList(nullptr); 255 auto displayList = node->getDisplayList(); 256 if (displayList) { 257 for (auto&& childOp : displayList->getChildren()) { 258 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode); 259 } 260 } 261 } 262 263}; // class TestUtils 264 265} /* namespace uirenderer */ 266} /* namespace android */ 267