blob: 80d83a2e47a13e23a6f74e686bd2874332da2a3e [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
19#include <Matrix.h>
Chris Craik0a24b142015-10-19 17:10:19 -070020#include <Rect.h>
Chris Craikb565df12015-10-05 13:00:52 -070021#include <RenderNode.h>
Chris Craik0a24b142015-10-19 17:10:19 -070022#include <renderstate/RenderState.h>
23#include <renderthread/RenderThread.h>
24#include <Snapshot.h>
Chris Craikb565df12015-10-05 13:00:52 -070025
26#include <memory>
27
28namespace android {
29namespace uirenderer {
30
31#define EXPECT_MATRIX_APPROX_EQ(a, b) \
32 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
33
34class TestUtils {
35public:
36 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
37 for (int i = 0; i < 16; i++) {
38 if (!MathUtils::areEqual(a[i], b[i])) {
39 return false;
40 }
41 }
42 return true;
43 }
44
45 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
46 std::unique_ptr<Snapshot> snapshot(new Snapshot());
47 snapshot->clip(clip.left, clip.top, clip.right, clip.bottom, SkRegion::kReplace_Op);
48 *(snapshot->transform) = transform;
49 return snapshot;
50 }
51
Chris Craikddf22152015-10-14 17:42:47 -070052 static SkBitmap createSkBitmap(int width, int height) {
53 SkBitmap bitmap;
Chris Craik0a24b142015-10-19 17:10:19 -070054 SkImageInfo info = SkImageInfo::MakeUnknown(width, height);
55 bitmap.setInfo(info);
56 bitmap.allocPixels(info);
Chris Craikddf22152015-10-14 17:42:47 -070057 return bitmap;
58 }
59
Chris Craikb565df12015-10-05 13:00:52 -070060 template<class CanvasType>
Chris Craik003cc3d2015-10-16 10:24:55 -070061 static std::unique_ptr<DisplayList> createDisplayList(int width, int height,
Chris Craikb565df12015-10-05 13:00:52 -070062 std::function<void(CanvasType& canvas)> canvasCallback) {
63 CanvasType canvas(width, height);
64 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -070065 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -070066 }
67
68 template<class CanvasType>
69 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
70 std::function<void(CanvasType& canvas)> canvasCallback) {
71 sp<RenderNode> node = new RenderNode();
72 node->mutateStagingProperties().setLeftTopRightBottom(left, top, right, bottom);
73 node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
74
75 CanvasType canvas(
76 node->stagingProperties().getWidth(), node->stagingProperties().getHeight());
77 canvasCallback(canvas);
78 node->setStagingDisplayList(canvas.finishRecording());
79 return node;
80 }
81
82 static void syncNodePropertiesAndDisplayList(sp<RenderNode>& node) {
83 node->syncProperties();
84 node->syncDisplayList();
85 }
Chris Craik0a24b142015-10-19 17:10:19 -070086
87 typedef std::function<void(RenderState& state, Caches& caches)> RtCallback;
88
89 class TestTask : public renderthread::RenderTask {
90 public:
91 TestTask(RtCallback rtCallback)
92 : rtCallback(rtCallback) {}
93 virtual ~TestTask() {}
94 virtual void run() override {
95 // RenderState only valid once RenderThread is running, so queried here
96 RenderState& renderState = renderthread::RenderThread::getInstance().renderState();
97
98 renderState.onGLContextCreated();
99 rtCallback(renderState, Caches::getInstance());
100 renderState.onGLContextDestroyed();
101 };
102 RtCallback rtCallback;
103 };
104
105 /**
106 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
107 */
108 static void runOnRenderThread(RtCallback rtCallback) {
109 TestTask task(rtCallback);
110 renderthread::RenderThread::getInstance().queueAndWait(&task);
111 }
Chris Craikb565df12015-10-05 13:00:52 -0700112}; // class TestUtils
113
114} /* namespace uirenderer */
115} /* namespace android */
116
117#endif /* TEST_UTILS_H */