blob: 13fb5285bc31a54e84ffad4f93ab9530cbddf7e9 [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
Chris Craik161f54b2015-11-05 11:08:52 -080031#include <RecordedOp.h>
Chris Craik161f54b2015-11-05 11:08:52 -080032
Chris Craikb565df12015-10-05 13:00:52 -070033#include <memory>
34
35namespace android {
36namespace uirenderer {
37
John Reck1bcacfd2017-11-03 10:12:19 -070038#define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
Chris Craikb565df12015-10-05 13:00:52 -070039
John Reck1bcacfd2017-11-03 10:12:19 -070040#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));
Chris Craik6fe991e52015-10-20 09:39:42 -070045
John Reck1bcacfd2017-11-03 10:12:19 -070046#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 }
Greg Daniel98c78dad2017-01-04 14:45:56 -050053
54#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \
John Reck1bcacfd2017-11-03 10:12:19 -070055 TEST(test_case_name, test_name##_##pipeline) { \
56 RenderPipelineType oldType = Properties::getRenderPipelineType(); \
57 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline); \
58 functionCall; \
59 Properties::overrideRenderPipelineType(oldType); \
Greg Daniel98c78dad2017-01-04 14:45:56 -050060 };
61
Greg Daniel98c78dad2017-01-04 14:45:56 -050062#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \
John Reck1bcacfd2017-11-03 10:12:19 -070063 INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \
64 TestUtils::runOnRenderThread( \
65 test_case_name##_##test_name##_RenderThreadTest::doTheThing))
Greg Daniel98c78dad2017-01-04 14:45:56 -050066
Chris Craik98787e62015-11-13 10:55:30 -080067/**
68 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
69 * (for e.g. accessing its RenderState)
70 */
John Reck1bcacfd2017-11-03 10:12:19 -070071#define RENDERTHREAD_TEST(test_case_name, test_name) \
72 class test_case_name##_##test_name##_RenderThreadTest { \
73 public: \
74 static void doTheThing(renderthread::RenderThread& renderThread); \
75 }; \
John Reck1bcacfd2017-11-03 10:12:19 -070076 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
Derek Sollenberger0f895392017-04-27 11:30:20 -040077 /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \
John Reck1bcacfd2017-11-03 10:12:19 -070078 /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \
79 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
80 renderthread::RenderThread& renderThread)
Greg Daniel98c78dad2017-01-04 14:45:56 -050081
82/**
Greg Daniel98c78dad2017-01-04 14:45:56 -050083 * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes
84 */
John Reck1bcacfd2017-11-03 10:12:19 -070085#define RENDERTHREAD_SKIA_PIPELINE_TEST(test_case_name, test_name) \
86 class test_case_name##_##test_name##_RenderThreadTest { \
87 public: \
88 static void doTheThing(renderthread::RenderThread& renderThread); \
89 }; \
90 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
Derek Sollenberger0f895392017-04-27 11:30:20 -040091 /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \
John Reck1bcacfd2017-11-03 10:12:19 -070092 /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \
93 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
94 renderthread::RenderThread& renderThread)
Chris Craik98787e62015-11-13 10:55:30 -080095
Chris Craik37413282016-05-12 17:48:51 -070096/**
97 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
98 * value when going out of scope.
99 *
100 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
101 */
102template <typename T>
103class ScopedProperty {
104public:
John Reck1bcacfd2017-11-03 10:12:19 -0700105 ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) {
Chris Craik37413282016-05-12 17:48:51 -0700106 property = newValue;
107 }
John Reck1bcacfd2017-11-03 10:12:19 -0700108 ~ScopedProperty() { *mPropertyPtr = mOldValue; }
109
Chris Craik37413282016-05-12 17:48:51 -0700110private:
111 T* mPropertyPtr;
112 T mOldValue;
113};
114
Chris Craikb565df12015-10-05 13:00:52 -0700115class TestUtils {
116public:
Chris Craik76ace112015-10-29 12:46:19 -0700117 class SignalingDtor {
118 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700119 SignalingDtor() : mSignal(nullptr) {}
120 explicit SignalingDtor(int* signal) : mSignal(signal) {}
121 void setSignal(int* signal) { mSignal = signal; }
Chris Craik76ace112015-10-29 12:46:19 -0700122 ~SignalingDtor() {
123 if (mSignal) {
124 (*mSignal)++;
125 }
126 }
John Reck1bcacfd2017-11-03 10:12:19 -0700127
Chris Craik76ace112015-10-29 12:46:19 -0700128 private:
129 int* mSignal;
130 };
131
John Reck2de950d2017-01-25 10:58:30 -0800132 class MockTreeObserver : public TreeObserver {
133 public:
134 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
135 };
136
Chris Craikb565df12015-10-05 13:00:52 -0700137 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
138 for (int i = 0; i < 16; i++) {
139 if (!MathUtils::areEqual(a[i], b[i])) {
140 return false;
141 }
142 }
143 return true;
144 }
145
146 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
147 std::unique_ptr<Snapshot> snapshot(new Snapshot());
Mike Reed6e49c9f2016-12-02 15:36:59 -0500148 // store clip first, so it isn't transformed
149 snapshot->setClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craikb565df12015-10-05 13:00:52 -0700150 *(snapshot->transform) = transform;
151 return snapshot;
152 }
153
sergeyvaed7f582016-10-14 16:30:21 -0700154 static sk_sp<Bitmap> createBitmap(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -0700155 SkColorType colorType = kN32_SkColorType) {
sergeyvaed7f582016-10-14 16:30:21 -0700156 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700157 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700158 }
159
160 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
161 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
162 outBitmap->setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400163 return Bitmap::allocateHeapBitmap(outBitmap);
sergeyvaed7f582016-10-14 16:30:21 -0700164 }
165
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800166 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500167 renderthread::RenderThread& renderThread);
168
169 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800170 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700171 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800172
John Reck1bcacfd2017-11-03 10:12:19 -0700173 template <class CanvasType>
174 static std::unique_ptr<DisplayList> createDisplayList(
175 int width, int height, std::function<void(CanvasType& canvas)> canvasCallback) {
Chris Craikb565df12015-10-05 13:00:52 -0700176 CanvasType canvas(width, height);
177 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700178 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700179 }
180
John Reck1bcacfd2017-11-03 10:12:19 -0700181 static sp<RenderNode> createNode(
182 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400183 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800184#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800185 // if RenderNodes are being sync'd/used, device info will be needed, since
186 // DeviceInfo::maxTextureSize() affects layer property
187 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800188#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800189
Chris Craikb565df12015-10-05 13:00:52 -0700190 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800191 RenderProperties& props = node->mutateStagingProperties();
192 props.setLeftTopRightBottom(left, top, right, bottom);
193 if (setup) {
John Reck1bcacfd2017-11-03 10:12:19 -0700194 std::unique_ptr<Canvas> canvas(
195 Canvas::create_recording_canvas(props.getWidth(), props.getHeight()));
Stan Iliev06152cd2016-07-27 17:55:43 -0400196 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800197 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev06152cd2016-07-27 17:55:43 -0400198 }
199 node->setPropertyFieldsDirty(0xFFFFFFFF);
200 return node;
201 }
202
John Reck1bcacfd2017-11-03 10:12:19 -0700203 template <class RecordingCanvasType>
204 static sp<RenderNode> createNode(
205 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400206 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
207#if HWUI_NULL_GPU
208 // if RenderNodes are being sync'd/used, device info will be needed, since
209 // DeviceInfo::maxTextureSize() affects layer property
210 DeviceInfo::initialize();
211#endif
212
213 sp<RenderNode> node = new RenderNode();
214 RenderProperties& props = node->mutateStagingProperties();
215 props.setLeftTopRightBottom(left, top, right, bottom);
216 if (setup) {
217 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800218 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800219 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700220 }
John Reck16c9d6a2015-11-17 15:51:08 -0800221 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700222 return node;
223 }
Chris Craikb565df12015-10-05 13:00:52 -0700224
John Reck1bcacfd2017-11-03 10:12:19 -0700225 static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) {
226 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
Stan Iliev4f901c82018-04-13 10:21:26 -0400227 node.stagingProperties().getWidth(), node.stagingProperties().getHeight(),
228 &node));
John Reck1bcacfd2017-11-03 10:12:19 -0700229 contentCallback(*canvas.get());
230 node.setStagingDisplayList(canvas->finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700231 }
232
John Reck1bcacfd2017-11-03 10:12:19 -0700233 static sp<RenderNode> createSkiaNode(
234 int left, int top, int right, int bottom,
235 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
236 setup,
Stan Iliev500a0c32016-10-26 10:30:09 -0400237 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
John Reck1bcacfd2017-11-03 10:12:19 -0700238#if HWUI_NULL_GPU
Stan Iliev500a0c32016-10-26 10:30:09 -0400239 // if RenderNodes are being sync'd/used, device info will be needed, since
240 // DeviceInfo::maxTextureSize() affects layer property
241 DeviceInfo::initialize();
John Reck1bcacfd2017-11-03 10:12:19 -0700242#endif
Stan Iliev500a0c32016-10-26 10:30:09 -0400243 sp<RenderNode> node = new RenderNode();
244 if (name) {
245 node->setName(name);
246 }
247 RenderProperties& props = node->mutateStagingProperties();
248 props.setLeftTopRightBottom(left, top, right, bottom);
249 if (displayList) {
John Reck2de950d2017-01-25 10:58:30 -0800250 node->setStagingDisplayList(displayList);
Stan Iliev500a0c32016-10-26 10:30:09 -0400251 }
252 if (setup) {
253 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700254 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
255 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400256 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800257 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev500a0c32016-10-26 10:30:09 -0400258 }
259 node->setPropertyFieldsDirty(0xFFFFFFFF);
260 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
261 return node;
262 }
263
Chris Craik8d1f2122015-11-24 16:40:09 -0800264 /**
265 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
266 * properties and DisplayList moved to the render copies.
267 *
268 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
269 * For this reason, this should generally only be called once on a tree.
270 */
Chris Craik161f54b2015-11-05 11:08:52 -0800271 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
272 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700273 }
Chris Craik0a24b142015-10-19 17:10:19 -0700274
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700275 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
276 syncHierarchyPropertiesAndDisplayList(node);
277 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800278 }
279
Chris Craik0b7e8242015-10-28 16:50:44 -0700280 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700281
282 class TestTask : public renderthread::RenderTask {
283 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700284 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700285 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800286 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700287 RtCallback rtCallback;
288 };
289
290 /**
291 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
292 */
293 static void runOnRenderThread(RtCallback rtCallback) {
294 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700295 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700296 }
John Reck16c9d6a2015-11-17 15:51:08 -0800297
John Reck1bcacfd2017-11-03 10:12:19 -0700298 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck38e0c322015-11-10 12:19:17 -0800299
John Reck16c9d6a2015-11-17 15:51:08 -0800300 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
301
John Reck1bcacfd2017-11-03 10:12:19 -0700302 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, float x,
303 float y);
Chris Craika1717272015-11-19 13:02:43 -0800304
John Reck1bcacfd2017-11-03 10:12:19 -0700305 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint,
306 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800307
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400308 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700309
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400310 class MockFunctor : public Functor {
John Reck1bcacfd2017-11-03 10:12:19 -0700311 public:
312 virtual status_t operator()(int what, void* data) {
313 mLastMode = what;
314 return DrawGlInfo::kStatusDone;
315 }
316 int getLastMode() const { return mLastMode; }
317
318 private:
319 int mLastMode = -1;
320 };
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400321
Stan Iliev021693b2016-10-17 16:26:15 -0400322 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
323
Stan Iliev52771272016-11-17 09:54:38 -0500324 static SkRect getClipBounds(const SkCanvas* canvas);
325 static SkRect getLocalClipBounds(const SkCanvas* canvas);
326
Chris Craik161f54b2015-11-05 11:08:52 -0800327private:
328 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800329 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800330 node->syncProperties();
John Reck3afd6372017-01-30 10:15:48 -0800331 if (node->mNeedsDisplayListSync) {
332 node->mNeedsDisplayListSync = false;
333 node->syncDisplayList(observer, nullptr);
334 }
Chris Craik161f54b2015-11-05 11:08:52 -0800335 auto displayList = node->getDisplayList();
336 if (displayList) {
John Reckd9d7f122018-05-03 14:40:56 -0700337 for (auto&& childDr : static_cast<skiapipeline::SkiaDisplayList*>(
338 const_cast<DisplayList*>(displayList))
339 ->mChildNodes) {
340 syncHierarchyPropertiesAndDisplayListImpl(childDr.getRenderNode());
Chris Craik161f54b2015-11-05 11:08:52 -0800341 }
342 }
343 }
344
John Reck1bcacfd2017-11-03 10:12:19 -0700345}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700346
347} /* namespace uirenderer */
348} /* namespace android */