blob: 0e6582c59a364187bf29dbd1652fb91d038c40b7 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
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 */
Chris Craik5e00c7c2016-07-06 16:10:09 -070016
17#pragma once
Chris Craikb565df12015-10-05 13:00:52 -070018
Chris Craik76caecf2015-11-02 19:17:45 -080019#include <DeviceInfo.h>
Chris Craik161f54b2015-11-05 11:08:52 -080020#include <DisplayList.h>
Chris Craikb565df12015-10-05 13:00:52 -070021#include <Matrix.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050022#include <Properties.h>
Chris Craik0a24b142015-10-19 17:10:19 -070023#include <Rect.h>
Chris Craikb565df12015-10-05 13:00:52 -070024#include <RenderNode.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <Snapshot.h>
sergeyvc1c54062016-10-19 18:47:26 -070026#include <hwui/Bitmap.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040027#include <pipeline/skia/SkiaRecordingCanvas.h>
Chris Craik0a24b142015-10-19 17:10:19 -070028#include <renderstate/RenderState.h>
29#include <renderthread/RenderThread.h>
Chris Craikb565df12015-10-05 13:00:52 -070030
31#include <memory>
32
33namespace android {
34namespace uirenderer {
35
John Reck1bcacfd2017-11-03 10:12:19 -070036#define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
Chris Craikb565df12015-10-05 13:00:52 -070037
John Reck1bcacfd2017-11-03 10:12:19 -070038#define EXPECT_RECT_APPROX_EQ(a, b) \
39 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) && \
40 MathUtils::areEqual((a).top, (b).top) && \
41 MathUtils::areEqual((a).right, (b).right) && \
42 MathUtils::areEqual((a).bottom, (b).bottom));
Chris Craik6fe991e52015-10-20 09:39:42 -070043
John Reck1bcacfd2017-11-03 10:12:19 -070044#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \
45 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \
46 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \
47 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \
48 } else { \
49 ADD_FAILURE() << "ClipState not a rect"; \
50 }
Greg Daniel98c78dad2017-01-04 14:45:56 -050051
52#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \
John Reck1bcacfd2017-11-03 10:12:19 -070053 TEST(test_case_name, test_name##_##pipeline) { \
54 RenderPipelineType oldType = Properties::getRenderPipelineType(); \
55 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline); \
56 functionCall; \
57 Properties::overrideRenderPipelineType(oldType); \
Greg Daniel98c78dad2017-01-04 14:45:56 -050058 };
59
Greg Daniel98c78dad2017-01-04 14:45:56 -050060#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \
John Reck1bcacfd2017-11-03 10:12:19 -070061 INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \
62 TestUtils::runOnRenderThread( \
63 test_case_name##_##test_name##_RenderThreadTest::doTheThing))
Greg Daniel98c78dad2017-01-04 14:45:56 -050064
Chris Craik98787e62015-11-13 10:55:30 -080065/**
66 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
67 * (for e.g. accessing its RenderState)
68 */
John Reck1bcacfd2017-11-03 10:12:19 -070069#define RENDERTHREAD_TEST(test_case_name, test_name) \
70 class test_case_name##_##test_name##_RenderThreadTest { \
71 public: \
72 static void doTheThing(renderthread::RenderThread& renderThread); \
73 }; \
John Reck1bcacfd2017-11-03 10:12:19 -070074 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
Derek Sollenberger0f895392017-04-27 11:30:20 -040075 /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \
John Reck1bcacfd2017-11-03 10:12:19 -070076 /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \
77 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
78 renderthread::RenderThread& renderThread)
Greg Daniel98c78dad2017-01-04 14:45:56 -050079
80/**
Greg Daniel98c78dad2017-01-04 14:45:56 -050081 * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes
82 */
John Reck1bcacfd2017-11-03 10:12:19 -070083#define RENDERTHREAD_SKIA_PIPELINE_TEST(test_case_name, test_name) \
84 class test_case_name##_##test_name##_RenderThreadTest { \
85 public: \
86 static void doTheThing(renderthread::RenderThread& renderThread); \
87 }; \
88 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
Derek Sollenberger0f895392017-04-27 11:30:20 -040089 /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \
John Reck1bcacfd2017-11-03 10:12:19 -070090 /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \
91 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
92 renderthread::RenderThread& renderThread)
Chris Craik98787e62015-11-13 10:55:30 -080093
Chris Craik37413282016-05-12 17:48:51 -070094/**
95 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
96 * value when going out of scope.
97 *
98 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
99 */
100template <typename T>
101class ScopedProperty {
102public:
John Reck1bcacfd2017-11-03 10:12:19 -0700103 ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) {
Chris Craik37413282016-05-12 17:48:51 -0700104 property = newValue;
105 }
John Reck1bcacfd2017-11-03 10:12:19 -0700106 ~ScopedProperty() { *mPropertyPtr = mOldValue; }
107
Chris Craik37413282016-05-12 17:48:51 -0700108private:
109 T* mPropertyPtr;
110 T mOldValue;
111};
112
Chris Craikb565df12015-10-05 13:00:52 -0700113class TestUtils {
114public:
Chris Craik76ace112015-10-29 12:46:19 -0700115 class SignalingDtor {
116 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700117 SignalingDtor() : mSignal(nullptr) {}
118 explicit SignalingDtor(int* signal) : mSignal(signal) {}
119 void setSignal(int* signal) { mSignal = signal; }
Chris Craik76ace112015-10-29 12:46:19 -0700120 ~SignalingDtor() {
121 if (mSignal) {
122 (*mSignal)++;
123 }
124 }
John Reck1bcacfd2017-11-03 10:12:19 -0700125
Chris Craik76ace112015-10-29 12:46:19 -0700126 private:
127 int* mSignal;
128 };
129
John Reck2de950d2017-01-25 10:58:30 -0800130 class MockTreeObserver : public TreeObserver {
131 public:
132 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
133 };
134
Chris Craikb565df12015-10-05 13:00:52 -0700135 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
136 for (int i = 0; i < 16; i++) {
137 if (!MathUtils::areEqual(a[i], b[i])) {
138 return false;
139 }
140 }
141 return true;
142 }
143
144 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
145 std::unique_ptr<Snapshot> snapshot(new Snapshot());
Mike Reed6e49c9f2016-12-02 15:36:59 -0500146 // store clip first, so it isn't transformed
147 snapshot->setClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craikb565df12015-10-05 13:00:52 -0700148 *(snapshot->transform) = transform;
149 return snapshot;
150 }
151
sergeyvaed7f582016-10-14 16:30:21 -0700152 static sk_sp<Bitmap> createBitmap(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -0700153 SkColorType colorType = kN32_SkColorType) {
sergeyvaed7f582016-10-14 16:30:21 -0700154 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700155 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700156 }
157
158 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
159 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
160 outBitmap->setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400161 return Bitmap::allocateHeapBitmap(outBitmap);
sergeyvaed7f582016-10-14 16:30:21 -0700162 }
163
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800164 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500165 renderthread::RenderThread& renderThread);
166
167 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800168 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700169 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800170
John Reck1bcacfd2017-11-03 10:12:19 -0700171 template <class CanvasType>
172 static std::unique_ptr<DisplayList> createDisplayList(
173 int width, int height, std::function<void(CanvasType& canvas)> canvasCallback) {
Chris Craikb565df12015-10-05 13:00:52 -0700174 CanvasType canvas(width, height);
175 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700176 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700177 }
178
John Reck1bcacfd2017-11-03 10:12:19 -0700179 static sp<RenderNode> createNode(
180 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400181 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800182#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800183 // if RenderNodes are being sync'd/used, device info will be needed, since
184 // DeviceInfo::maxTextureSize() affects layer property
185 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800186#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800187
Chris Craikb565df12015-10-05 13:00:52 -0700188 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800189 RenderProperties& props = node->mutateStagingProperties();
190 props.setLeftTopRightBottom(left, top, right, bottom);
191 if (setup) {
John Reck1bcacfd2017-11-03 10:12:19 -0700192 std::unique_ptr<Canvas> canvas(
193 Canvas::create_recording_canvas(props.getWidth(), props.getHeight()));
Stan Iliev06152cd2016-07-27 17:55:43 -0400194 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800195 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev06152cd2016-07-27 17:55:43 -0400196 }
197 node->setPropertyFieldsDirty(0xFFFFFFFF);
198 return node;
199 }
200
John Reck1bcacfd2017-11-03 10:12:19 -0700201 template <class RecordingCanvasType>
202 static sp<RenderNode> createNode(
203 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400204 std::function<void(RenderProperties& props, RecordingCanvasType& 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 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800216 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800217 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700218 }
John Reck16c9d6a2015-11-17 15:51:08 -0800219 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700220 return node;
221 }
Chris Craikb565df12015-10-05 13:00:52 -0700222
John Reck1bcacfd2017-11-03 10:12:19 -0700223 static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) {
224 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
Stan Iliev4f901c82018-04-13 10:21:26 -0400225 node.stagingProperties().getWidth(), node.stagingProperties().getHeight(),
226 &node));
John Reck1bcacfd2017-11-03 10:12:19 -0700227 contentCallback(*canvas.get());
228 node.setStagingDisplayList(canvas->finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700229 }
230
John Reck1bcacfd2017-11-03 10:12:19 -0700231 static sp<RenderNode> createSkiaNode(
232 int left, int top, int right, int bottom,
233 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
234 setup,
Stan Iliev500a0c32016-10-26 10:30:09 -0400235 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
John Reck1bcacfd2017-11-03 10:12:19 -0700236#if HWUI_NULL_GPU
Stan Iliev500a0c32016-10-26 10:30:09 -0400237 // if RenderNodes are being sync'd/used, device info will be needed, since
238 // DeviceInfo::maxTextureSize() affects layer property
239 DeviceInfo::initialize();
John Reck1bcacfd2017-11-03 10:12:19 -0700240#endif
Stan Iliev500a0c32016-10-26 10:30:09 -0400241 sp<RenderNode> node = new RenderNode();
242 if (name) {
243 node->setName(name);
244 }
245 RenderProperties& props = node->mutateStagingProperties();
246 props.setLeftTopRightBottom(left, top, right, bottom);
247 if (displayList) {
John Reck2de950d2017-01-25 10:58:30 -0800248 node->setStagingDisplayList(displayList);
Stan Iliev500a0c32016-10-26 10:30:09 -0400249 }
250 if (setup) {
251 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700252 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
253 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400254 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800255 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev500a0c32016-10-26 10:30:09 -0400256 }
257 node->setPropertyFieldsDirty(0xFFFFFFFF);
258 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
259 return node;
260 }
261
Chris Craik8d1f2122015-11-24 16:40:09 -0800262 /**
263 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
264 * properties and DisplayList moved to the render copies.
265 *
266 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
267 * For this reason, this should generally only be called once on a tree.
268 */
Chris Craik161f54b2015-11-05 11:08:52 -0800269 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
270 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700271 }
Chris Craik0a24b142015-10-19 17:10:19 -0700272
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700273 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
274 syncHierarchyPropertiesAndDisplayList(node);
275 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800276 }
277
Chris Craik0b7e8242015-10-28 16:50:44 -0700278 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700279
280 class TestTask : public renderthread::RenderTask {
281 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700282 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700283 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800284 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700285 RtCallback rtCallback;
286 };
287
288 /**
289 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
290 */
291 static void runOnRenderThread(RtCallback rtCallback) {
292 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700293 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700294 }
John Reck16c9d6a2015-11-17 15:51:08 -0800295
John Reck1bcacfd2017-11-03 10:12:19 -0700296 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck38e0c322015-11-10 12:19:17 -0800297
John Reck16c9d6a2015-11-17 15:51:08 -0800298 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
299
John Reck1bcacfd2017-11-03 10:12:19 -0700300 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, float x,
301 float y);
Chris Craika1717272015-11-19 13:02:43 -0800302
John Reck1bcacfd2017-11-03 10:12:19 -0700303 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint,
304 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800305
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400306 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700307
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400308 class MockFunctor : public Functor {
John Reck1bcacfd2017-11-03 10:12:19 -0700309 public:
310 virtual status_t operator()(int what, void* data) {
311 mLastMode = what;
312 return DrawGlInfo::kStatusDone;
313 }
314 int getLastMode() const { return mLastMode; }
315
316 private:
317 int mLastMode = -1;
318 };
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400319
Stan Iliev021693b2016-10-17 16:26:15 -0400320 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
321
Stan Iliev52771272016-11-17 09:54:38 -0500322 static SkRect getClipBounds(const SkCanvas* canvas);
323 static SkRect getLocalClipBounds(const SkCanvas* canvas);
324
Chris Craik161f54b2015-11-05 11:08:52 -0800325private:
326 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800327 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800328 node->syncProperties();
John Reck3afd6372017-01-30 10:15:48 -0800329 if (node->mNeedsDisplayListSync) {
330 node->mNeedsDisplayListSync = false;
331 node->syncDisplayList(observer, nullptr);
332 }
Chris Craik161f54b2015-11-05 11:08:52 -0800333 auto displayList = node->getDisplayList();
334 if (displayList) {
John Reckd9d7f122018-05-03 14:40:56 -0700335 for (auto&& childDr : static_cast<skiapipeline::SkiaDisplayList*>(
336 const_cast<DisplayList*>(displayList))
337 ->mChildNodes) {
338 syncHierarchyPropertiesAndDisplayListImpl(childDr.getRenderNode());
Chris Craik161f54b2015-11-05 11:08:52 -0800339 }
340 }
341 }
342
John Reck1bcacfd2017-11-03 10:12:19 -0700343}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700344
345} /* namespace uirenderer */
346} /* namespace android */