blob: 0af99398b5f5332b937a5c192a99b1681c87532f [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) \
51 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));
55
Chris Craik98787e62015-11-13 10:55:30 -080056/**
57 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
58 * (for e.g. accessing its RenderState)
59 */
60#define RENDERTHREAD_TEST(test_case_name, test_name) \
61 class test_case_name##_##test_name##_RenderThreadTest { \
62 public: \
63 static void doTheThing(renderthread::RenderThread& renderThread); \
64 }; \
65 TEST(test_case_name, test_name) { \
66 TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing); \
67 }; \
68 void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread)
69
Chris Craikb565df12015-10-05 13:00:52 -070070class TestUtils {
71public:
Chris Craik76ace112015-10-29 12:46:19 -070072 class SignalingDtor {
73 public:
74 SignalingDtor()
75 : mSignal(nullptr) {}
76 SignalingDtor(int* signal)
77 : mSignal(signal) {}
78 void setSignal(int* signal) {
79 mSignal = signal;
80 }
81 ~SignalingDtor() {
82 if (mSignal) {
83 (*mSignal)++;
84 }
85 }
86 private:
87 int* mSignal;
88 };
89
Chris Craikb565df12015-10-05 13:00:52 -070090 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
91 for (int i = 0; i < 16; i++) {
92 if (!MathUtils::areEqual(a[i], b[i])) {
93 return false;
94 }
95 }
96 return true;
97 }
98
99 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
100 std::unique_ptr<Snapshot> snapshot(new Snapshot());
101 snapshot->clip(clip.left, clip.top, clip.right, clip.bottom, SkRegion::kReplace_Op);
102 *(snapshot->transform) = transform;
103 return snapshot;
104 }
105
Chris Craik15c3f192015-12-03 12:16:56 -0800106 static SkBitmap createSkBitmap(int width, int height,
107 SkColorType colorType = kN32_SkColorType) {
Chris Craikddf22152015-10-14 17:42:47 -0700108 SkBitmap bitmap;
John Reck16c9d6a2015-11-17 15:51:08 -0800109 SkImageInfo info = SkImageInfo::Make(width, height,
Chris Craik15c3f192015-12-03 12:16:56 -0800110 colorType, kPremul_SkAlphaType);
Chris Craik0a24b142015-10-19 17:10:19 -0700111 bitmap.setInfo(info);
112 bitmap.allocPixels(info);
Chris Craikddf22152015-10-14 17:42:47 -0700113 return bitmap;
114 }
115
Chris Craikb565df12015-10-05 13:00:52 -0700116 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -0700117 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -0700118 std::function<void(CanvasType& canvas)> canvasCallback) {
119 CanvasType canvas(width, height);
120 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700121 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700122 }
123
Chris Craikd3daa312015-11-06 10:59:56 -0800124 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
Chris Craik8d1f2122015-11-24 16:40:09 -0800125 std::function<void(RenderProperties& props, TestCanvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800126#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800127 // if RenderNodes are being sync'd/used, device info will be needed, since
128 // DeviceInfo::maxTextureSize() affects layer property
129 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800130#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800131
Chris Craikb565df12015-10-05 13:00:52 -0700132 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800133 RenderProperties& props = node->mutateStagingProperties();
134 props.setLeftTopRightBottom(left, top, right, bottom);
135 if (setup) {
136 TestCanvas canvas(props.getWidth(), props.getHeight());
137 setup(props, canvas);
138 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700139 }
John Reck16c9d6a2015-11-17 15:51:08 -0800140 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700141 return node;
142 }
Chris Craikb565df12015-10-05 13:00:52 -0700143
John Reck16c9d6a2015-11-17 15:51:08 -0800144 static void recordNode(RenderNode& node,
145 std::function<void(TestCanvas&)> contentCallback) {
146 TestCanvas canvas(node.stagingProperties().getWidth(),
147 node.stagingProperties().getHeight());
148 contentCallback(canvas);
149 node.setStagingDisplayList(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700150 }
151
Chris Craik8d1f2122015-11-24 16:40:09 -0800152 /**
153 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
154 * properties and DisplayList moved to the render copies.
155 *
156 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
157 * For this reason, this should generally only be called once on a tree.
158 */
Chris Craik161f54b2015-11-05 11:08:52 -0800159 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
160 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700161 }
Chris Craik0a24b142015-10-19 17:10:19 -0700162
Chris Craik0b7e8242015-10-28 16:50:44 -0700163 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700164
165 class TestTask : public renderthread::RenderTask {
166 public:
167 TestTask(RtCallback rtCallback)
168 : rtCallback(rtCallback) {}
169 virtual ~TestTask() {}
170 virtual void run() override {
171 // RenderState only valid once RenderThread is running, so queried here
172 RenderState& renderState = renderthread::RenderThread::getInstance().renderState();
173
174 renderState.onGLContextCreated();
Chris Craik0b7e8242015-10-28 16:50:44 -0700175 rtCallback(renderthread::RenderThread::getInstance());
Chris Craik0a24b142015-10-19 17:10:19 -0700176 renderState.onGLContextDestroyed();
177 };
178 RtCallback rtCallback;
179 };
180
181 /**
182 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
183 */
184 static void runOnRenderThread(RtCallback rtCallback) {
185 TestTask task(rtCallback);
186 renderthread::RenderThread::getInstance().queueAndWait(&task);
187 }
John Reck16c9d6a2015-11-17 15:51:08 -0800188
189 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
190
Chris Craika1717272015-11-19 13:02:43 -0800191 static void drawTextToCanvas(TestCanvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -0800192 const SkPaint& paint, float x, float y);
Chris Craika1717272015-11-19 13:02:43 -0800193
Chris Craik161f54b2015-11-05 11:08:52 -0800194private:
195 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
196 node->syncProperties();
197 node->syncDisplayList();
198 auto displayList = node->getDisplayList();
199 if (displayList) {
200 for (auto&& childOp : displayList->getChildren()) {
201 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode);
202 }
203 }
204 }
205
Chris Craikb565df12015-10-05 13:00:52 -0700206}; // class TestUtils
207
208} /* namespace uirenderer */
209} /* namespace android */
210
211#endif /* TEST_UTILS_H */