blob: f6fe7d267354ee4a781e94d1d2c1bc3957b4dacf [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>
Chris Craik0a24b142015-10-19 17:10:19 -070025#include <renderstate/RenderState.h>
26#include <renderthread/RenderThread.h>
27#include <Snapshot.h>
Chris Craikb565df12015-10-05 13:00:52 -070028
Chris Craik161f54b2015-11-05 11:08:52 -080029#include <RecordedOp.h>
John Reck16c9d6a2015-11-17 15:51:08 -080030#include <RecordingCanvas.h>
Chris Craik161f54b2015-11-05 11:08:52 -080031
Chris Craikb565df12015-10-05 13:00:52 -070032#include <memory>
33
34namespace android {
35namespace uirenderer {
36
37#define EXPECT_MATRIX_APPROX_EQ(a, b) \
38 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
39
Chris Craik6fe991e52015-10-20 09:39:42 -070040#define EXPECT_RECT_APPROX_EQ(a, b) \
Chih-Hung Hsieh474081e2016-08-26 15:19:47 -070041 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
Chris Craik7435eb12016-01-07 17:41:40 -080046#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 }
Chris Craik98787e62015-11-13 10:55:30 -080053/**
54 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
55 * (for e.g. accessing its RenderState)
56 */
57#define RENDERTHREAD_TEST(test_case_name, test_name) \
58 class test_case_name##_##test_name##_RenderThreadTest { \
59 public: \
60 static void doTheThing(renderthread::RenderThread& renderThread); \
61 }; \
62 TEST(test_case_name, test_name) { \
63 TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing); \
64 }; \
65 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread)
66
Chris Craik37413282016-05-12 17:48:51 -070067/**
68 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
69 * value when going out of scope.
70 *
71 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
72 */
73template <typename T>
74class ScopedProperty {
75public:
76 ScopedProperty(T& property, T newValue)
77 : mPropertyPtr(&property)
78 , mOldValue(property) {
79 property = newValue;
80 }
81 ~ScopedProperty() {
82 *mPropertyPtr = mOldValue;
83 }
84private:
85 T* mPropertyPtr;
86 T mOldValue;
87};
88
Chris Craikb565df12015-10-05 13:00:52 -070089class TestUtils {
90public:
Chris Craik76ace112015-10-29 12:46:19 -070091 class SignalingDtor {
92 public:
93 SignalingDtor()
94 : mSignal(nullptr) {}
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -070095 explicit SignalingDtor(int* signal)
Chris Craik76ace112015-10-29 12:46:19 -070096 : mSignal(signal) {}
97 void setSignal(int* signal) {
98 mSignal = signal;
99 }
100 ~SignalingDtor() {
101 if (mSignal) {
102 (*mSignal)++;
103 }
104 }
105 private:
106 int* mSignal;
107 };
108
Chris Craikb565df12015-10-05 13:00:52 -0700109 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
110 for (int i = 0; i < 16; i++) {
111 if (!MathUtils::areEqual(a[i], b[i])) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
119 std::unique_ptr<Snapshot> snapshot(new Snapshot());
Chris Craika2a70722015-12-17 12:58:24 -0800120 snapshot->clip(clip, SkRegion::kReplace_Op); // store clip first, so it isn't transformed
Chris Craikb565df12015-10-05 13:00:52 -0700121 *(snapshot->transform) = transform;
122 return snapshot;
123 }
124
sergeyvaed7f582016-10-14 16:30:21 -0700125 static sk_sp<Bitmap> createBitmap(int width, int height,
126 SkColorType colorType = kN32_SkColorType) {
127 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700128 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700129 }
130
131 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
132 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
133 outBitmap->setInfo(info);
134 return Bitmap::allocateHeapBitmap(outBitmap, nullptr);
135 }
136
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800137 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
138 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700139 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800140
Chris Craikb565df12015-10-05 13:00:52 -0700141 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -0700142 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -0700143 std::function<void(CanvasType& canvas)> canvasCallback) {
144 CanvasType canvas(width, height);
145 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700146 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700147 }
148
Chris Craikd3daa312015-11-06 10:59:56 -0800149 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400150 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800151#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800152 // if RenderNodes are being sync'd/used, device info will be needed, since
153 // DeviceInfo::maxTextureSize() affects layer property
154 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800155#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800156
Chris Craikb565df12015-10-05 13:00:52 -0700157 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800158 RenderProperties& props = node->mutateStagingProperties();
159 props.setLeftTopRightBottom(left, top, right, bottom);
160 if (setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400161 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(props.getWidth(),
162 props.getHeight()));
163 setup(props, *canvas.get());
164 node->setStagingDisplayList(canvas->finishRecording(), nullptr);
165 }
166 node->setPropertyFieldsDirty(0xFFFFFFFF);
167 return node;
168 }
169
170 template<class RecordingCanvasType>
171 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
172 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
173#if HWUI_NULL_GPU
174 // if RenderNodes are being sync'd/used, device info will be needed, since
175 // DeviceInfo::maxTextureSize() affects layer property
176 DeviceInfo::initialize();
177#endif
178
179 sp<RenderNode> node = new RenderNode();
180 RenderProperties& props = node->mutateStagingProperties();
181 props.setLeftTopRightBottom(left, top, right, bottom);
182 if (setup) {
183 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800184 setup(props, canvas);
John Reck51f2d602016-04-06 07:50:47 -0700185 node->setStagingDisplayList(canvas.finishRecording(), nullptr);
Chris Craik0b7e8242015-10-28 16:50:44 -0700186 }
John Reck16c9d6a2015-11-17 15:51:08 -0800187 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700188 return node;
189 }
Chris Craikb565df12015-10-05 13:00:52 -0700190
John Reck16c9d6a2015-11-17 15:51:08 -0800191 static void recordNode(RenderNode& node,
Stan Iliev06152cd2016-07-27 17:55:43 -0400192 std::function<void(Canvas&)> contentCallback) {
193 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
194 node.stagingProperties().getWidth(), node.stagingProperties().getHeight()));
195 contentCallback(*canvas.get());
196 node.setStagingDisplayList(canvas->finishRecording(), nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700197 }
198
Chris Craik8d1f2122015-11-24 16:40:09 -0800199 /**
200 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
201 * properties and DisplayList moved to the render copies.
202 *
203 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
204 * For this reason, this should generally only be called once on a tree.
205 */
Chris Craik161f54b2015-11-05 11:08:52 -0800206 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
207 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700208 }
Chris Craik0a24b142015-10-19 17:10:19 -0700209
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700210 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
211 syncHierarchyPropertiesAndDisplayList(node);
212 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800213 }
214
Chris Craik0b7e8242015-10-28 16:50:44 -0700215 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700216
217 class TestTask : public renderthread::RenderTask {
218 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700219 explicit TestTask(RtCallback rtCallback)
Chris Craik0a24b142015-10-19 17:10:19 -0700220 : rtCallback(rtCallback) {}
221 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800222 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700223 RtCallback rtCallback;
224 };
225
226 /**
227 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
228 */
229 static void runOnRenderThread(RtCallback rtCallback) {
230 TestTask task(rtCallback);
231 renderthread::RenderThread::getInstance().queueAndWait(&task);
232 }
John Reck16c9d6a2015-11-17 15:51:08 -0800233
John Reck38e0c322015-11-10 12:19:17 -0800234 static bool isRenderThreadRunning() {
235 return renderthread::RenderThread::hasInstance();
236 }
237
John Reck16c9d6a2015-11-17 15:51:08 -0800238 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
239
Chris Craike8c3c812016-02-05 20:10:50 -0800240 static void layoutTextUnscaled(const SkPaint& paint, const char* text,
241 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
242 float* outTotalAdvance, Rect* outBounds);
243
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400244 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -0800245 const SkPaint& paint, float x, float y);
Chris Craika1717272015-11-19 13:02:43 -0800246
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400247 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -0800248 const SkPaint& paint, const SkPath& path);
249
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400250 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700251
Derek Sollenberger835b3a692016-10-28 13:57:14 -0400252 class MockFunctor : public Functor {
253 public:
254 virtual status_t operator ()(int what, void* data) {
255 mLastMode = what;
256 return DrawGlInfo::kStatusDone;
257 }
258 int getLastMode() const { return mLastMode; }
259 private:
260 int mLastMode = -1;
261 };
262
Chris Craik161f54b2015-11-05 11:08:52 -0800263private:
264 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
265 node->syncProperties();
John Reck44b49f02016-03-25 14:29:48 -0700266 node->syncDisplayList(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800267 auto displayList = node->getDisplayList();
268 if (displayList) {
269 for (auto&& childOp : displayList->getChildren()) {
270 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode);
271 }
272 }
273 }
274
Chris Craikb565df12015-10-05 13:00:52 -0700275}; // class TestUtils
276
277} /* namespace uirenderer */
278} /* namespace android */