blob: 0ce598d4dba451dd39714b1c6ff531af851dbd9f [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>
Chris Craik0a24b142015-10-19 17:10:19 -070022#include <Rect.h>
Chris Craikb565df12015-10-05 13:00:52 -070023#include <RenderNode.h>
sergeyvc1c54062016-10-19 18:47:26 -070024#include <hwui/Bitmap.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040025#include <pipeline/skia/SkiaRecordingCanvas.h>
Chris Craik0a24b142015-10-19 17:10:19 -070026#include <renderstate/RenderState.h>
27#include <renderthread/RenderThread.h>
28#include <Snapshot.h>
Chris Craikb565df12015-10-05 13:00:52 -070029
Chris Craik161f54b2015-11-05 11:08:52 -080030#include <RecordedOp.h>
John Reck16c9d6a2015-11-17 15:51:08 -080031#include <RecordingCanvas.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
38#define EXPECT_MATRIX_APPROX_EQ(a, b) \
39 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
40
Chris Craik6fe991e52015-10-20 09:39:42 -070041#define EXPECT_RECT_APPROX_EQ(a, b) \
Chih-Hung Hsieh474081e2016-08-26 15:19:47 -070042 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) \
43 && MathUtils::areEqual((a).top, (b).top) \
44 && MathUtils::areEqual((a).right, (b).right) \
45 && MathUtils::areEqual((a).bottom, (b).bottom));
Chris Craik6fe991e52015-10-20 09:39:42 -070046
Chris Craik7435eb12016-01-07 17:41:40 -080047#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \
48 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \
49 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \
50 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \
51 } else { \
52 ADD_FAILURE() << "ClipState not a rect"; \
53 }
Chris Craik98787e62015-11-13 10:55:30 -080054/**
55 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
56 * (for e.g. accessing its RenderState)
57 */
58#define RENDERTHREAD_TEST(test_case_name, test_name) \
59 class test_case_name##_##test_name##_RenderThreadTest { \
60 public: \
61 static void doTheThing(renderthread::RenderThread& renderThread); \
62 }; \
63 TEST(test_case_name, test_name) { \
64 TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing); \
65 }; \
66 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread)
67
Chris Craik37413282016-05-12 17:48:51 -070068/**
69 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
70 * value when going out of scope.
71 *
72 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
73 */
74template <typename T>
75class ScopedProperty {
76public:
77 ScopedProperty(T& property, T newValue)
78 : mPropertyPtr(&property)
79 , mOldValue(property) {
80 property = newValue;
81 }
82 ~ScopedProperty() {
83 *mPropertyPtr = mOldValue;
84 }
85private:
86 T* mPropertyPtr;
87 T mOldValue;
88};
89
Chris Craikb565df12015-10-05 13:00:52 -070090class TestUtils {
91public:
Chris Craik76ace112015-10-29 12:46:19 -070092 class SignalingDtor {
93 public:
94 SignalingDtor()
95 : mSignal(nullptr) {}
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -070096 explicit SignalingDtor(int* signal)
Chris Craik76ace112015-10-29 12:46:19 -070097 : mSignal(signal) {}
98 void setSignal(int* signal) {
99 mSignal = signal;
100 }
101 ~SignalingDtor() {
102 if (mSignal) {
103 (*mSignal)++;
104 }
105 }
106 private:
107 int* mSignal;
108 };
109
Chris Craikb565df12015-10-05 13:00:52 -0700110 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
111 for (int i = 0; i < 16; i++) {
112 if (!MathUtils::areEqual(a[i], b[i])) {
113 return false;
114 }
115 }
116 return true;
117 }
118
119 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
120 std::unique_ptr<Snapshot> snapshot(new Snapshot());
Chris Craika2a70722015-12-17 12:58:24 -0800121 snapshot->clip(clip, SkRegion::kReplace_Op); // store clip first, so it isn't transformed
Chris Craikb565df12015-10-05 13:00:52 -0700122 *(snapshot->transform) = transform;
123 return snapshot;
124 }
125
sergeyvaed7f582016-10-14 16:30:21 -0700126 static sk_sp<Bitmap> createBitmap(int width, int height,
127 SkColorType colorType = kN32_SkColorType) {
128 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700129 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700130 }
131
132 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
133 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
134 outBitmap->setInfo(info);
135 return Bitmap::allocateHeapBitmap(outBitmap, nullptr);
136 }
137
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800138 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
139 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700140 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800141
Chris Craikb565df12015-10-05 13:00:52 -0700142 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -0700143 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -0700144 std::function<void(CanvasType& canvas)> canvasCallback) {
145 CanvasType canvas(width, height);
146 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700147 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700148 }
149
Chris Craikd3daa312015-11-06 10:59:56 -0800150 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400151 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800152#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800153 // if RenderNodes are being sync'd/used, device info will be needed, since
154 // DeviceInfo::maxTextureSize() affects layer property
155 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800156#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800157
Chris Craikb565df12015-10-05 13:00:52 -0700158 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800159 RenderProperties& props = node->mutateStagingProperties();
160 props.setLeftTopRightBottom(left, top, right, bottom);
161 if (setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400162 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(props.getWidth(),
163 props.getHeight()));
164 setup(props, *canvas.get());
165 node->setStagingDisplayList(canvas->finishRecording(), nullptr);
166 }
167 node->setPropertyFieldsDirty(0xFFFFFFFF);
168 return node;
169 }
170
171 template<class RecordingCanvasType>
172 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
173 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
174#if HWUI_NULL_GPU
175 // if RenderNodes are being sync'd/used, device info will be needed, since
176 // DeviceInfo::maxTextureSize() affects layer property
177 DeviceInfo::initialize();
178#endif
179
180 sp<RenderNode> node = new RenderNode();
181 RenderProperties& props = node->mutateStagingProperties();
182 props.setLeftTopRightBottom(left, top, right, bottom);
183 if (setup) {
184 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800185 setup(props, canvas);
John Reck51f2d602016-04-06 07:50:47 -0700186 node->setStagingDisplayList(canvas.finishRecording(), nullptr);
Chris Craik0b7e8242015-10-28 16:50:44 -0700187 }
John Reck16c9d6a2015-11-17 15:51:08 -0800188 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700189 return node;
190 }
Chris Craikb565df12015-10-05 13:00:52 -0700191
John Reck16c9d6a2015-11-17 15:51:08 -0800192 static void recordNode(RenderNode& node,
Stan Iliev06152cd2016-07-27 17:55:43 -0400193 std::function<void(Canvas&)> contentCallback) {
194 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
195 node.stagingProperties().getWidth(), node.stagingProperties().getHeight()));
196 contentCallback(*canvas.get());
197 node.setStagingDisplayList(canvas->finishRecording(), nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700198 }
199
Stan Iliev500a0c32016-10-26 10:30:09 -0400200 static sp<RenderNode> createSkiaNode(int left, int top, int right, int bottom,
201 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)> setup,
202 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
203 #if HWUI_NULL_GPU
204 // if RenderNodes are being sync'd/used, device info will be needed, since
205 // DeviceInfo::maxTextureSize() affects layer property
206 DeviceInfo::initialize();
207 #endif
208 sp<RenderNode> node = new RenderNode();
209 if (name) {
210 node->setName(name);
211 }
212 RenderProperties& props = node->mutateStagingProperties();
213 props.setLeftTopRightBottom(left, top, right, bottom);
214 if (displayList) {
215 node->setStagingDisplayList(displayList, nullptr);
216 }
217 if (setup) {
218 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
219 new skiapipeline::SkiaRecordingCanvas(nullptr,
220 props.getWidth(), props.getHeight()));
221 setup(props, *canvas.get());
222 node->setStagingDisplayList(canvas->finishRecording(), nullptr);
223 }
224 node->setPropertyFieldsDirty(0xFFFFFFFF);
225 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
226 return node;
227 }
228
Chris Craik8d1f2122015-11-24 16:40:09 -0800229 /**
230 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
231 * properties and DisplayList moved to the render copies.
232 *
233 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
234 * For this reason, this should generally only be called once on a tree.
235 */
Chris Craik161f54b2015-11-05 11:08:52 -0800236 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
237 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700238 }
Chris Craik0a24b142015-10-19 17:10:19 -0700239
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700240 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
241 syncHierarchyPropertiesAndDisplayList(node);
242 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800243 }
244
Chris Craik0b7e8242015-10-28 16:50:44 -0700245 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700246
247 class TestTask : public renderthread::RenderTask {
248 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700249 explicit TestTask(RtCallback rtCallback)
Chris Craik0a24b142015-10-19 17:10:19 -0700250 : rtCallback(rtCallback) {}
251 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800252 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700253 RtCallback rtCallback;
254 };
255
256 /**
257 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
258 */
259 static void runOnRenderThread(RtCallback rtCallback) {
260 TestTask task(rtCallback);
261 renderthread::RenderThread::getInstance().queueAndWait(&task);
262 }
John Reck16c9d6a2015-11-17 15:51:08 -0800263
John Reck38e0c322015-11-10 12:19:17 -0800264 static bool isRenderThreadRunning() {
265 return renderthread::RenderThread::hasInstance();
266 }
267
John Reck16c9d6a2015-11-17 15:51:08 -0800268 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
269
Chris Craike8c3c812016-02-05 20:10:50 -0800270 static void layoutTextUnscaled(const SkPaint& paint, const char* text,
271 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
272 float* outTotalAdvance, Rect* outBounds);
273
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400274 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -0800275 const SkPaint& paint, float x, float y);
Chris Craika1717272015-11-19 13:02:43 -0800276
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400277 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -0800278 const SkPaint& paint, const SkPath& path);
279
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400280 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700281
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400282 class MockFunctor : public Functor {
283 public:
284 virtual status_t operator ()(int what, void* data) {
285 mLastMode = what;
286 return DrawGlInfo::kStatusDone;
287 }
288 int getLastMode() const { return mLastMode; }
289 private:
290 int mLastMode = -1;
291 };
292
Stan Iliev021693b2016-10-17 16:26:15 -0400293 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
294
Chris Craik161f54b2015-11-05 11:08:52 -0800295private:
296 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
297 node->syncProperties();
John Reck44b49f02016-03-25 14:29:48 -0700298 node->syncDisplayList(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800299 auto displayList = node->getDisplayList();
300 if (displayList) {
301 for (auto&& childOp : displayList->getChildren()) {
302 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode);
303 }
304 }
305 }
306
Chris Craikb565df12015-10-05 13:00:52 -0700307}; // class TestUtils
308
309} /* namespace uirenderer */
310} /* namespace android */