blob: 5a4ab99012b9dd89a20f8b3da71a43f3608a8f3d [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 */
16#ifndef TEST_UTILS_H
17#define TEST_UTILS_H
18
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>
Chris Craik0a24b142015-10-19 17:10:19 -070024#include <renderstate/RenderState.h>
25#include <renderthread/RenderThread.h>
26#include <Snapshot.h>
Chris Craikb565df12015-10-05 13:00:52 -070027
Chris Craik161f54b2015-11-05 11:08:52 -080028#if HWUI_NEW_OPS
29#include <RecordedOp.h>
John Reck16c9d6a2015-11-17 15:51:08 -080030#include <RecordingCanvas.h>
Chris Craik161f54b2015-11-05 11:08:52 -080031#else
32#include <DisplayListOp.h>
John Reck16c9d6a2015-11-17 15:51:08 -080033#include <DisplayListCanvas.h>
Chris Craik161f54b2015-11-05 11:08:52 -080034#endif
35
Chris Craikb565df12015-10-05 13:00:52 -070036#include <memory>
37
38namespace android {
39namespace uirenderer {
40
John Reck16c9d6a2015-11-17 15:51:08 -080041#if HWUI_NEW_OPS
42typedef RecordingCanvas TestCanvas;
43#else
44typedef DisplayListCanvas TestCanvas;
45#endif
46
Chris Craikb565df12015-10-05 13:00:52 -070047#define EXPECT_MATRIX_APPROX_EQ(a, b) \
48 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
49
Chris Craik6fe991e52015-10-20 09:39:42 -070050#define EXPECT_RECT_APPROX_EQ(a, b) \
Chih-Hung Hsieh474081e2016-08-26 15:19:47 -070051 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) \
52 && MathUtils::areEqual((a).top, (b).top) \
53 && MathUtils::areEqual((a).right, (b).right) \
54 && MathUtils::areEqual((a).bottom, (b).bottom));
Chris Craik6fe991e52015-10-20 09:39:42 -070055
Chris Craik7435eb12016-01-07 17:41:40 -080056#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \
57 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \
58 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \
59 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \
60 } else { \
61 ADD_FAILURE() << "ClipState not a rect"; \
62 }
Chris Craik98787e62015-11-13 10:55:30 -080063/**
64 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
65 * (for e.g. accessing its RenderState)
66 */
67#define RENDERTHREAD_TEST(test_case_name, test_name) \
68 class test_case_name##_##test_name##_RenderThreadTest { \
69 public: \
70 static void doTheThing(renderthread::RenderThread& renderThread); \
71 }; \
72 TEST(test_case_name, test_name) { \
73 TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing); \
74 }; \
75 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread)
76
Chris Craik37413282016-05-12 17:48:51 -070077/**
78 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
79 * value when going out of scope.
80 *
81 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
82 */
83template <typename T>
84class ScopedProperty {
85public:
86 ScopedProperty(T& property, T newValue)
87 : mPropertyPtr(&property)
88 , mOldValue(property) {
89 property = newValue;
90 }
91 ~ScopedProperty() {
92 *mPropertyPtr = mOldValue;
93 }
94private:
95 T* mPropertyPtr;
96 T mOldValue;
97};
98
Chris Craikb565df12015-10-05 13:00:52 -070099class TestUtils {
100public:
Chris Craik76ace112015-10-29 12:46:19 -0700101 class SignalingDtor {
102 public:
103 SignalingDtor()
104 : mSignal(nullptr) {}
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700105 explicit SignalingDtor(int* signal)
Chris Craik76ace112015-10-29 12:46:19 -0700106 : mSignal(signal) {}
107 void setSignal(int* signal) {
108 mSignal = signal;
109 }
110 ~SignalingDtor() {
111 if (mSignal) {
112 (*mSignal)++;
113 }
114 }
115 private:
116 int* mSignal;
117 };
118
Chris Craikb565df12015-10-05 13:00:52 -0700119 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
120 for (int i = 0; i < 16; i++) {
121 if (!MathUtils::areEqual(a[i], b[i])) {
122 return false;
123 }
124 }
125 return true;
126 }
127
128 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
129 std::unique_ptr<Snapshot> snapshot(new Snapshot());
Chris Craika2a70722015-12-17 12:58:24 -0800130 snapshot->clip(clip, SkRegion::kReplace_Op); // store clip first, so it isn't transformed
Chris Craikb565df12015-10-05 13:00:52 -0700131 *(snapshot->transform) = transform;
132 return snapshot;
133 }
134
Chris Craik15c3f192015-12-03 12:16:56 -0800135 static SkBitmap createSkBitmap(int width, int height,
136 SkColorType colorType = kN32_SkColorType) {
Chris Craikddf22152015-10-14 17:42:47 -0700137 SkBitmap bitmap;
John Reck16c9d6a2015-11-17 15:51:08 -0800138 SkImageInfo info = SkImageInfo::Make(width, height,
Chris Craik15c3f192015-12-03 12:16:56 -0800139 colorType, kPremul_SkAlphaType);
Chris Craik0a24b142015-10-19 17:10:19 -0700140 bitmap.setInfo(info);
141 bitmap.allocPixels(info);
Chris Craikddf22152015-10-14 17:42:47 -0700142 return bitmap;
143 }
144
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800145 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
146 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700147 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800148
Chris Craikb565df12015-10-05 13:00:52 -0700149 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -0700150 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -0700151 std::function<void(CanvasType& canvas)> canvasCallback) {
152 CanvasType canvas(width, height);
153 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700154 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700155 }
156
Chris Craikd3daa312015-11-06 10:59:56 -0800157 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
Chris Craik8d1f2122015-11-24 16:40:09 -0800158 std::function<void(RenderProperties& props, TestCanvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800159#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800160 // if RenderNodes are being sync'd/used, device info will be needed, since
161 // DeviceInfo::maxTextureSize() affects layer property
162 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800163#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800164
Chris Craikb565df12015-10-05 13:00:52 -0700165 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800166 RenderProperties& props = node->mutateStagingProperties();
167 props.setLeftTopRightBottom(left, top, right, bottom);
168 if (setup) {
169 TestCanvas canvas(props.getWidth(), props.getHeight());
170 setup(props, canvas);
John Reck51f2d602016-04-06 07:50:47 -0700171 node->setStagingDisplayList(canvas.finishRecording(), nullptr);
Chris Craik0b7e8242015-10-28 16:50:44 -0700172 }
John Reck16c9d6a2015-11-17 15:51:08 -0800173 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700174 return node;
175 }
Chris Craikb565df12015-10-05 13:00:52 -0700176
John Reck16c9d6a2015-11-17 15:51:08 -0800177 static void recordNode(RenderNode& node,
178 std::function<void(TestCanvas&)> contentCallback) {
179 TestCanvas canvas(node.stagingProperties().getWidth(),
180 node.stagingProperties().getHeight());
181 contentCallback(canvas);
John Reck51f2d602016-04-06 07:50:47 -0700182 node.setStagingDisplayList(canvas.finishRecording(), nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700183 }
184
Chris Craik8d1f2122015-11-24 16:40:09 -0800185 /**
186 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
187 * properties and DisplayList moved to the render copies.
188 *
189 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
190 * For this reason, this should generally only be called once on a tree.
191 */
Chris Craik161f54b2015-11-05 11:08:52 -0800192 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
193 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700194 }
Chris Craik0a24b142015-10-19 17:10:19 -0700195
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700196 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
197 syncHierarchyPropertiesAndDisplayList(node);
198 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800199 }
200
Chris Craik0b7e8242015-10-28 16:50:44 -0700201 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700202
203 class TestTask : public renderthread::RenderTask {
204 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700205 explicit TestTask(RtCallback rtCallback)
Chris Craik0a24b142015-10-19 17:10:19 -0700206 : rtCallback(rtCallback) {}
207 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800208 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700209 RtCallback rtCallback;
210 };
211
212 /**
213 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
214 */
215 static void runOnRenderThread(RtCallback rtCallback) {
216 TestTask task(rtCallback);
217 renderthread::RenderThread::getInstance().queueAndWait(&task);
218 }
John Reck16c9d6a2015-11-17 15:51:08 -0800219
John Reck38e0c322015-11-10 12:19:17 -0800220 static bool isRenderThreadRunning() {
221 return renderthread::RenderThread::hasInstance();
222 }
223
John Reck16c9d6a2015-11-17 15:51:08 -0800224 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
225
Chris Craike8c3c812016-02-05 20:10:50 -0800226 static void layoutTextUnscaled(const SkPaint& paint, const char* text,
227 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
228 float* outTotalAdvance, Rect* outBounds);
229
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400230 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -0800231 const SkPaint& paint, float x, float y);
Chris Craika1717272015-11-19 13:02:43 -0800232
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400233 static void drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -0800234 const SkPaint& paint, const SkPath& path);
235
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400236 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700237
Chris Craik161f54b2015-11-05 11:08:52 -0800238private:
239 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
240 node->syncProperties();
John Reck44b49f02016-03-25 14:29:48 -0700241 node->syncDisplayList(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800242 auto displayList = node->getDisplayList();
243 if (displayList) {
244 for (auto&& childOp : displayList->getChildren()) {
245 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode);
246 }
247 }
248 }
249
Chris Craikb565df12015-10-05 13:00:52 -0700250}; // class TestUtils
251
252} /* namespace uirenderer */
253} /* namespace android */
254
255#endif /* TEST_UTILS_H */