blob: 9c1c0b9d2c080e0b77d0bbdebcd018262d4c85cc [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 Craikddf22152015-10-14 17:42:47 -0700106 static SkBitmap createSkBitmap(int width, int height) {
107 SkBitmap bitmap;
John Reck16c9d6a2015-11-17 15:51:08 -0800108 SkImageInfo info = SkImageInfo::Make(width, height,
109 kN32_SkColorType, kPremul_SkAlphaType);
Chris Craik0a24b142015-10-19 17:10:19 -0700110 bitmap.setInfo(info);
111 bitmap.allocPixels(info);
Chris Craikddf22152015-10-14 17:42:47 -0700112 return bitmap;
113 }
114
Chris Craikb565df12015-10-05 13:00:52 -0700115 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -0700116 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -0700117 std::function<void(CanvasType& canvas)> canvasCallback) {
118 CanvasType canvas(width, height);
119 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700120 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700121 }
122
Chris Craikd3daa312015-11-06 10:59:56 -0800123 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
Chris Craik8d1f2122015-11-24 16:40:09 -0800124 std::function<void(RenderProperties& props, TestCanvas& canvas)> setup) {
Chris Craik9fded232015-11-11 16:42:34 -0800125#if HWUI_NULL_GPU
Chris Craik76caecf2015-11-02 19:17:45 -0800126 // if RenderNodes are being sync'd/used, device info will be needed, since
127 // DeviceInfo::maxTextureSize() affects layer property
128 DeviceInfo::initialize();
Chris Craik9fded232015-11-11 16:42:34 -0800129#endif
Chris Craik76caecf2015-11-02 19:17:45 -0800130
Chris Craikb565df12015-10-05 13:00:52 -0700131 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800132 RenderProperties& props = node->mutateStagingProperties();
133 props.setLeftTopRightBottom(left, top, right, bottom);
134 if (setup) {
135 TestCanvas canvas(props.getWidth(), props.getHeight());
136 setup(props, canvas);
137 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700138 }
John Reck16c9d6a2015-11-17 15:51:08 -0800139 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700140 return node;
141 }
Chris Craikb565df12015-10-05 13:00:52 -0700142
John Reck16c9d6a2015-11-17 15:51:08 -0800143 static void recordNode(RenderNode& node,
144 std::function<void(TestCanvas&)> contentCallback) {
145 TestCanvas canvas(node.stagingProperties().getWidth(),
146 node.stagingProperties().getHeight());
147 contentCallback(canvas);
148 node.setStagingDisplayList(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700149 }
150
Chris Craik8d1f2122015-11-24 16:40:09 -0800151 /**
152 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
153 * properties and DisplayList moved to the render copies.
154 *
155 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
156 * For this reason, this should generally only be called once on a tree.
157 */
Chris Craik161f54b2015-11-05 11:08:52 -0800158 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
159 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700160 }
Chris Craik0a24b142015-10-19 17:10:19 -0700161
Chris Craik0b7e8242015-10-28 16:50:44 -0700162 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700163
164 class TestTask : public renderthread::RenderTask {
165 public:
166 TestTask(RtCallback rtCallback)
167 : rtCallback(rtCallback) {}
168 virtual ~TestTask() {}
169 virtual void run() override {
170 // RenderState only valid once RenderThread is running, so queried here
171 RenderState& renderState = renderthread::RenderThread::getInstance().renderState();
172
173 renderState.onGLContextCreated();
Chris Craik0b7e8242015-10-28 16:50:44 -0700174 rtCallback(renderthread::RenderThread::getInstance());
Chris Craik0a24b142015-10-19 17:10:19 -0700175 renderState.onGLContextDestroyed();
176 };
177 RtCallback rtCallback;
178 };
179
180 /**
181 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
182 */
183 static void runOnRenderThread(RtCallback rtCallback) {
184 TestTask task(rtCallback);
185 renderthread::RenderThread::getInstance().queueAndWait(&task);
186 }
John Reck16c9d6a2015-11-17 15:51:08 -0800187
188 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
189
Chris Craika1717272015-11-19 13:02:43 -0800190 static void drawTextToCanvas(TestCanvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -0800191 const SkPaint& paint, float x, float y);
Chris Craika1717272015-11-19 13:02:43 -0800192
Chris Craik161f54b2015-11-05 11:08:52 -0800193private:
194 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
195 node->syncProperties();
196 node->syncDisplayList();
197 auto displayList = node->getDisplayList();
198 if (displayList) {
199 for (auto&& childOp : displayList->getChildren()) {
200 syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode);
201 }
202 }
203 }
204
Chris Craikb565df12015-10-05 13:00:52 -0700205}; // class TestUtils
206
207} /* namespace uirenderer */
208} /* namespace android */
209
210#endif /* TEST_UTILS_H */