TestUtils.h revision 98c78dad1969e2321cfee2085faa55d95bba7e29
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 <Properties.h> 23#include <Rect.h> 24#include <RenderNode.h> 25#include <hwui/Bitmap.h> 26#include <pipeline/skia/SkiaRecordingCanvas.h> 27#include <renderstate/RenderState.h> 28#include <renderthread/RenderThread.h> 29#include <Snapshot.h> 30 31#include <RecordedOp.h> 32#include <RecordingCanvas.h> 33 34#include <memory> 35 36namespace android { 37namespace uirenderer { 38 39#define EXPECT_MATRIX_APPROX_EQ(a, b) \ 40 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) 41 42#define EXPECT_RECT_APPROX_EQ(a, b) \ 43 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) \ 44 && MathUtils::areEqual((a).top, (b).top) \ 45 && MathUtils::areEqual((a).right, (b).right) \ 46 && MathUtils::areEqual((a).bottom, (b).bottom)); 47 48#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \ 49 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \ 50 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \ 51 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \ 52 } else { \ 53 ADD_FAILURE() << "ClipState not a rect"; \ 54 } 55 56#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \ 57 TEST(test_case_name, test_name##_##pipeline) { \ 58 RenderPipelineType oldType = Properties::getRenderPipelineType(); \ 59 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline); \ 60 functionCall; \ 61 Properties::overrideRenderPipelineType(oldType); \ 62 }; 63 64/** 65 * Like gtests' TEST, but only runs with the OpenGL RenderPipelineType 66 */ 67#define OPENGL_PIPELINE_TEST(test_case_name, test_name) \ 68 class test_case_name##_##test_name##_HwuiTest { \ 69 public: \ 70 static void doTheThing(); \ 71 }; \ 72 INNER_PIPELINE_TEST(test_case_name, test_name, OpenGL, \ 73 test_case_name##_##test_name##_HwuiTest::doTheThing()) \ 74 void test_case_name##_##test_name##_HwuiTest::doTheThing() 75 76#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \ 77 INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \ 78 TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing)) 79 80/** 81 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope 82 * (for e.g. accessing its RenderState) 83 */ 84#define RENDERTHREAD_TEST(test_case_name, test_name) \ 85 class test_case_name##_##test_name##_RenderThreadTest { \ 86 public: \ 87 static void doTheThing(renderthread::RenderThread& renderThread); \ 88 }; \ 89 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, OpenGL); \ 90 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \ 91 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \ 92 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread) 93 94/** 95 * Like RENDERTHREAD_TEST, but only runs with the OpenGL RenderPipelineType 96 */ 97#define RENDERTHREAD_OPENGL_PIPELINE_TEST(test_case_name, test_name) \ 98 class test_case_name##_##test_name##_RenderThreadTest { \ 99 public: \ 100 static void doTheThing(renderthread::RenderThread& renderThread); \ 101 }; \ 102 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, OpenGL); \ 103 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread) 104 105/** 106 * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes 107 */ 108#define RENDERTHREAD_SKIA_PIPELINE_TEST(test_case_name, test_name) \ 109 class test_case_name##_##test_name##_RenderThreadTest { \ 110 public: \ 111 static void doTheThing(renderthread::RenderThread& renderThread); \ 112 }; \ 113 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \ 114 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \ 115 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread) 116 117/** 118 * Sets a property value temporarily, generally for the duration of a test, restoring the previous 119 * value when going out of scope. 120 * 121 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled. 122 */ 123template <typename T> 124class ScopedProperty { 125public: 126 ScopedProperty(T& property, T newValue) 127 : mPropertyPtr(&property) 128 , mOldValue(property) { 129 property = newValue; 130 } 131 ~ScopedProperty() { 132 *mPropertyPtr = mOldValue; 133 } 134private: 135 T* mPropertyPtr; 136 T mOldValue; 137}; 138 139class TestUtils { 140public: 141 class SignalingDtor { 142 public: 143 SignalingDtor() 144 : mSignal(nullptr) {} 145 explicit SignalingDtor(int* signal) 146 : mSignal(signal) {} 147 void setSignal(int* signal) { 148 mSignal = signal; 149 } 150 ~SignalingDtor() { 151 if (mSignal) { 152 (*mSignal)++; 153 } 154 } 155 private: 156 int* mSignal; 157 }; 158 159 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) { 160 for (int i = 0; i < 16; i++) { 161 if (!MathUtils::areEqual(a[i], b[i])) { 162 return false; 163 } 164 } 165 return true; 166 } 167 168 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) { 169 std::unique_ptr<Snapshot> snapshot(new Snapshot()); 170 // store clip first, so it isn't transformed 171 snapshot->setClip(clip.left, clip.top, clip.right, clip.bottom); 172 *(snapshot->transform) = transform; 173 return snapshot; 174 } 175 176 static sk_sp<Bitmap> createBitmap(int width, int height, 177 SkColorType colorType = kN32_SkColorType) { 178 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType); 179 return Bitmap::allocateHeapBitmap(info); 180 } 181 182 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) { 183 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType); 184 outBitmap->setInfo(info); 185 return Bitmap::allocateHeapBitmap(outBitmap, nullptr); 186 } 187 188 static sp<DeferredLayerUpdater> createTextureLayerUpdater( 189 renderthread::RenderThread& renderThread); 190 191 static sp<DeferredLayerUpdater> createTextureLayerUpdater( 192 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, 193 const SkMatrix& transform); 194 195 template<class CanvasType> 196 static std::unique_ptr<DisplayList> createDisplayList(int width, int height, 197 std::function<void(CanvasType& canvas)> canvasCallback) { 198 CanvasType canvas(width, height); 199 canvasCallback(canvas); 200 return std::unique_ptr<DisplayList>(canvas.finishRecording()); 201 } 202 203 static sp<RenderNode> createNode(int left, int top, int right, int bottom, 204 std::function<void(RenderProperties& props, Canvas& canvas)> setup) { 205#if HWUI_NULL_GPU 206 // if RenderNodes are being sync'd/used, device info will be needed, since 207 // DeviceInfo::maxTextureSize() affects layer property 208 DeviceInfo::initialize(); 209#endif 210 211 sp<RenderNode> node = new RenderNode(); 212 RenderProperties& props = node->mutateStagingProperties(); 213 props.setLeftTopRightBottom(left, top, right, bottom); 214 if (setup) { 215 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(props.getWidth(), 216 props.getHeight())); 217 setup(props, *canvas.get()); 218 node->setStagingDisplayList(canvas->finishRecording(), nullptr); 219 } 220 node->setPropertyFieldsDirty(0xFFFFFFFF); 221 return node; 222 } 223 224 template<class RecordingCanvasType> 225 static sp<RenderNode> createNode(int left, int top, int right, int bottom, 226 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) { 227#if HWUI_NULL_GPU 228 // if RenderNodes are being sync'd/used, device info will be needed, since 229 // DeviceInfo::maxTextureSize() affects layer property 230 DeviceInfo::initialize(); 231#endif 232 233 sp<RenderNode> node = new RenderNode(); 234 RenderProperties& props = node->mutateStagingProperties(); 235 props.setLeftTopRightBottom(left, top, right, bottom); 236 if (setup) { 237 RecordingCanvasType canvas(props.getWidth(), props.getHeight()); 238 setup(props, canvas); 239 node->setStagingDisplayList(canvas.finishRecording(), nullptr); 240 } 241 node->setPropertyFieldsDirty(0xFFFFFFFF); 242 return node; 243 } 244 245 static void recordNode(RenderNode& node, 246 std::function<void(Canvas&)> contentCallback) { 247 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas( 248 node.stagingProperties().getWidth(), node.stagingProperties().getHeight())); 249 contentCallback(*canvas.get()); 250 node.setStagingDisplayList(canvas->finishRecording(), nullptr); 251 } 252 253 static sp<RenderNode> createSkiaNode(int left, int top, int right, int bottom, 254 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)> setup, 255 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) { 256 #if HWUI_NULL_GPU 257 // if RenderNodes are being sync'd/used, device info will be needed, since 258 // DeviceInfo::maxTextureSize() affects layer property 259 DeviceInfo::initialize(); 260 #endif 261 sp<RenderNode> node = new RenderNode(); 262 if (name) { 263 node->setName(name); 264 } 265 RenderProperties& props = node->mutateStagingProperties(); 266 props.setLeftTopRightBottom(left, top, right, bottom); 267 if (displayList) { 268 node->setStagingDisplayList(displayList, nullptr); 269 } 270 if (setup) { 271 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas( 272 new skiapipeline::SkiaRecordingCanvas(nullptr, 273 props.getWidth(), props.getHeight())); 274 setup(props, *canvas.get()); 275 node->setStagingDisplayList(canvas->finishRecording(), nullptr); 276 } 277 node->setPropertyFieldsDirty(0xFFFFFFFF); 278 TestUtils::syncHierarchyPropertiesAndDisplayList(node); 279 return node; 280 } 281 282 /** 283 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging 284 * properties and DisplayList moved to the render copies. 285 * 286 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded. 287 * For this reason, this should generally only be called once on a tree. 288 */ 289 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) { 290 syncHierarchyPropertiesAndDisplayListImpl(node.get()); 291 } 292 293 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) { 294 syncHierarchyPropertiesAndDisplayList(node); 295 return node; 296 } 297 298 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback; 299 300 class TestTask : public renderthread::RenderTask { 301 public: 302 explicit TestTask(RtCallback rtCallback) 303 : rtCallback(rtCallback) {} 304 virtual ~TestTask() {} 305 virtual void run() override; 306 RtCallback rtCallback; 307 }; 308 309 /** 310 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely. 311 */ 312 static void runOnRenderThread(RtCallback rtCallback) { 313 TestTask task(rtCallback); 314 renderthread::RenderThread::getInstance().queueAndWait(&task); 315 } 316 317 static bool isRenderThreadRunning() { 318 return renderthread::RenderThread::hasInstance(); 319 } 320 321 static SkColor interpolateColor(float fraction, SkColor start, SkColor end); 322 323 static void layoutTextUnscaled(const SkPaint& paint, const char* text, 324 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, 325 float* outTotalAdvance, Rect* outBounds); 326 327 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, 328 const SkPaint& paint, float x, float y); 329 330 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, 331 const SkPaint& paint, const SkPath& path); 332 333 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str); 334 335 class MockFunctor : public Functor { 336 public: 337 virtual status_t operator ()(int what, void* data) { 338 mLastMode = what; 339 return DrawGlInfo::kStatusDone; 340 } 341 int getLastMode() const { return mLastMode; } 342 private: 343 int mLastMode = -1; 344 }; 345 346 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y); 347 348 static SkRect getClipBounds(const SkCanvas* canvas); 349 static SkRect getLocalClipBounds(const SkCanvas* canvas); 350 351private: 352 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) { 353 node->syncProperties(); 354 node->syncDisplayList(nullptr); 355 auto displayList = node->getDisplayList(); 356 if (displayList) { 357 for (auto&& childOp : displayList->getChildren()) { 358 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode); 359 } 360 } 361 } 362 363}; // class TestUtils 364 365} /* namespace uirenderer */ 366} /* namespace android */ 367