blob: 16bc6f7825b33de973ac874e23b69ed2b13ed81a [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>
sergeyvc1c54062016-10-19 18:47:26 -070025#include <hwui/Bitmap.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040026#include <pipeline/skia/SkiaRecordingCanvas.h>
Chris Craik0a24b142015-10-19 17:10:19 -070027#include <renderstate/RenderState.h>
28#include <renderthread/RenderThread.h>
29#include <Snapshot.h>
Chris Craikb565df12015-10-05 13:00:52 -070030
Chris Craik161f54b2015-11-05 11:08:52 -080031#include <RecordedOp.h>
John Reck16c9d6a2015-11-17 15:51:08 -080032#include <RecordingCanvas.h>
Chris Craik161f54b2015-11-05 11:08:52 -080033
Chris Craikb565df12015-10-05 13:00:52 -070034#include <memory>
35
36namespace android {
37namespace uirenderer {
38
39#define EXPECT_MATRIX_APPROX_EQ(a, b) \
40 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
41
Chris Craik6fe991e52015-10-20 09:39:42 -070042#define EXPECT_RECT_APPROX_EQ(a, b) \
Chih-Hung Hsieh474081e2016-08-26 15:19:47 -070043 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));
Chris Craik6fe991e52015-10-20 09:39:42 -070047
Chris Craik7435eb12016-01-07 17:41:40 -080048#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 }
Greg Daniel98c78dad2017-01-04 14:45:56 -050055
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
Chris Craik98787e62015-11-13 10:55:30 -080080/**
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 }; \
Greg Daniel98c78dad2017-01-04 14:45:56 -050089 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); \
Chris Craik98787e62015-11-13 10:55:30 -0800101 }; \
Greg Daniel98c78dad2017-01-04 14:45:56 -0500102 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); \
Chris Craik98787e62015-11-13 10:55:30 -0800115 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread)
116
Chris Craik37413282016-05-12 17:48:51 -0700117/**
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
Chris Craikb565df12015-10-05 13:00:52 -0700139class TestUtils {
140public:
Chris Craik76ace112015-10-29 12:46:19 -0700141 class SignalingDtor {
142 public:
143 SignalingDtor()
144 : mSignal(nullptr) {}
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700145 explicit SignalingDtor(int* signal)
Chris Craik76ace112015-10-29 12:46:19 -0700146 : 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
John Reck2de950d2017-01-25 10:58:30 -0800159 class MockTreeObserver : public TreeObserver {
160 public:
161 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
162 };
163
Chris Craikb565df12015-10-05 13:00:52 -0700164 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
165 for (int i = 0; i < 16; i++) {
166 if (!MathUtils::areEqual(a[i], b[i])) {
167 return false;
168 }
169 }
170 return true;
171 }
172
173 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
174 std::unique_ptr<Snapshot> snapshot(new Snapshot());
Mike Reed6e49c9f2016-12-02 15:36:59 -0500175 // store clip first, so it isn't transformed
176 snapshot->setClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craikb565df12015-10-05 13:00:52 -0700177 *(snapshot->transform) = transform;
178 return snapshot;
179 }
180
sergeyvaed7f582016-10-14 16:30:21 -0700181 static sk_sp<Bitmap> createBitmap(int width, int height,
182 SkColorType colorType = kN32_SkColorType) {
183 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700184 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700185 }
186
187 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
188 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
189 outBitmap->setInfo(info);
190 return Bitmap::allocateHeapBitmap(outBitmap, nullptr);
191 }
192
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800193 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500194 renderthread::RenderThread& renderThread);
195
196 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800197 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700198 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800199
Chris Craikb565df12015-10-05 13:00:52 -0700200 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -0700201 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -0700202 std::function<void(CanvasType& canvas)> canvasCallback) {
203 CanvasType canvas(width, height);
204 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700205 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700206 }
207
Chris Craikd3daa312015-11-06 10:59:56 -0800208 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400209 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800210#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800211 // if RenderNodes are being sync'd/used, device info will be needed, since
212 // DeviceInfo::maxTextureSize() affects layer property
213 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800214#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800215
Chris Craikb565df12015-10-05 13:00:52 -0700216 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800217 RenderProperties& props = node->mutateStagingProperties();
218 props.setLeftTopRightBottom(left, top, right, bottom);
219 if (setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400220 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(props.getWidth(),
221 props.getHeight()));
222 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800223 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev06152cd2016-07-27 17:55:43 -0400224 }
225 node->setPropertyFieldsDirty(0xFFFFFFFF);
226 return node;
227 }
228
229 template<class RecordingCanvasType>
230 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
231 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
232#if HWUI_NULL_GPU
233 // if RenderNodes are being sync'd/used, device info will be needed, since
234 // DeviceInfo::maxTextureSize() affects layer property
235 DeviceInfo::initialize();
236#endif
237
238 sp<RenderNode> node = new RenderNode();
239 RenderProperties& props = node->mutateStagingProperties();
240 props.setLeftTopRightBottom(left, top, right, bottom);
241 if (setup) {
242 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800243 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800244 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700245 }
John Reck16c9d6a2015-11-17 15:51:08 -0800246 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700247 return node;
248 }
Chris Craikb565df12015-10-05 13:00:52 -0700249
John Reck16c9d6a2015-11-17 15:51:08 -0800250 static void recordNode(RenderNode& node,
Stan Iliev06152cd2016-07-27 17:55:43 -0400251 std::function<void(Canvas&)> contentCallback) {
252 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
253 node.stagingProperties().getWidth(), node.stagingProperties().getHeight()));
254 contentCallback(*canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800255 node.setStagingDisplayList(canvas->finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700256 }
257
Stan Iliev500a0c32016-10-26 10:30:09 -0400258 static sp<RenderNode> createSkiaNode(int left, int top, int right, int bottom,
259 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)> setup,
260 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
261 #if HWUI_NULL_GPU
262 // if RenderNodes are being sync'd/used, device info will be needed, since
263 // DeviceInfo::maxTextureSize() affects layer property
264 DeviceInfo::initialize();
265 #endif
266 sp<RenderNode> node = new RenderNode();
267 if (name) {
268 node->setName(name);
269 }
270 RenderProperties& props = node->mutateStagingProperties();
271 props.setLeftTopRightBottom(left, top, right, bottom);
272 if (displayList) {
John Reck2de950d2017-01-25 10:58:30 -0800273 node->setStagingDisplayList(displayList);
Stan Iliev500a0c32016-10-26 10:30:09 -0400274 }
275 if (setup) {
276 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
277 new skiapipeline::SkiaRecordingCanvas(nullptr,
278 props.getWidth(), props.getHeight()));
279 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800280 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev500a0c32016-10-26 10:30:09 -0400281 }
282 node->setPropertyFieldsDirty(0xFFFFFFFF);
283 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
284 return node;
285 }
286
Chris Craik8d1f2122015-11-24 16:40:09 -0800287 /**
288 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
289 * properties and DisplayList moved to the render copies.
290 *
291 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
292 * For this reason, this should generally only be called once on a tree.
293 */
Chris Craik161f54b2015-11-05 11:08:52 -0800294 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
295 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700296 }
Chris Craik0a24b142015-10-19 17:10:19 -0700297
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700298 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
299 syncHierarchyPropertiesAndDisplayList(node);
300 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800301 }
302
Chris Craik0b7e8242015-10-28 16:50:44 -0700303 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700304
305 class TestTask : public renderthread::RenderTask {
306 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700307 explicit TestTask(RtCallback rtCallback)
Chris Craik0a24b142015-10-19 17:10:19 -0700308 : rtCallback(rtCallback) {}
309 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800310 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700311 RtCallback rtCallback;
312 };
313
314 /**
315 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
316 */
317 static void runOnRenderThread(RtCallback rtCallback) {
318 TestTask task(rtCallback);
319 renderthread::RenderThread::getInstance().queueAndWait(&task);
320 }
John Reck16c9d6a2015-11-17 15:51:08 -0800321
John Reck38e0c322015-11-10 12:19:17 -0800322 static bool isRenderThreadRunning() {
323 return renderthread::RenderThread::hasInstance();
324 }
325
John Reck16c9d6a2015-11-17 15:51:08 -0800326 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
327
Chris Craike8c3c812016-02-05 20:10:50 -0800328 static void layoutTextUnscaled(const SkPaint& paint, const char* text,
329 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
330 float* outTotalAdvance, Rect* outBounds);
331
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400332 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -0800333 const SkPaint& paint, float x, float y);
Chris Craika1717272015-11-19 13:02:43 -0800334
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400335 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -0800336 const SkPaint& paint, const SkPath& path);
337
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400338 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700339
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400340 class MockFunctor : public Functor {
341 public:
342 virtual status_t operator ()(int what, void* data) {
343 mLastMode = what;
344 return DrawGlInfo::kStatusDone;
345 }
346 int getLastMode() const { return mLastMode; }
347 private:
348 int mLastMode = -1;
349 };
350
Stan Iliev021693b2016-10-17 16:26:15 -0400351 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
352
Stan Iliev52771272016-11-17 09:54:38 -0500353 static SkRect getClipBounds(const SkCanvas* canvas);
354 static SkRect getLocalClipBounds(const SkCanvas* canvas);
355
Chris Craik161f54b2015-11-05 11:08:52 -0800356private:
357 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800358 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800359 node->syncProperties();
John Reck2de950d2017-01-25 10:58:30 -0800360 node->syncDisplayList(observer, nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800361 auto displayList = node->getDisplayList();
362 if (displayList) {
363 for (auto&& childOp : displayList->getChildren()) {
364 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode);
365 }
366 }
367 }
368
Chris Craikb565df12015-10-05 13:00:52 -0700369}; // class TestUtils
370
371} /* namespace uirenderer */
372} /* namespace android */