Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 20 | #include <Rect.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 21 | #include <RenderNode.h> |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 22 | #include <renderstate/RenderState.h> |
| 23 | #include <renderthread/RenderThread.h> |
| 24 | #include <Snapshot.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 25 | |
| 26 | #include <memory> |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | #define EXPECT_MATRIX_APPROX_EQ(a, b) \ |
| 32 | EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) |
| 33 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 34 | #define EXPECT_RECT_APPROX_EQ(a, b) \ |
| 35 | EXPECT_TRUE(MathUtils::areEqual(a.left, b.left) \ |
| 36 | && MathUtils::areEqual(a.top, b.top) \ |
| 37 | && MathUtils::areEqual(a.right, b.right) \ |
| 38 | && MathUtils::areEqual(a.bottom, b.bottom)); |
| 39 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 40 | class TestUtils { |
| 41 | public: |
Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 42 | class SignalingDtor { |
| 43 | public: |
| 44 | SignalingDtor() |
| 45 | : mSignal(nullptr) {} |
| 46 | SignalingDtor(int* signal) |
| 47 | : mSignal(signal) {} |
| 48 | void setSignal(int* signal) { |
| 49 | mSignal = signal; |
| 50 | } |
| 51 | ~SignalingDtor() { |
| 52 | if (mSignal) { |
| 53 | (*mSignal)++; |
| 54 | } |
| 55 | } |
| 56 | private: |
| 57 | int* mSignal; |
| 58 | }; |
| 59 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 60 | static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) { |
| 61 | for (int i = 0; i < 16; i++) { |
| 62 | if (!MathUtils::areEqual(a[i], b[i])) { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) { |
| 70 | std::unique_ptr<Snapshot> snapshot(new Snapshot()); |
| 71 | snapshot->clip(clip.left, clip.top, clip.right, clip.bottom, SkRegion::kReplace_Op); |
| 72 | *(snapshot->transform) = transform; |
| 73 | return snapshot; |
| 74 | } |
| 75 | |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 76 | static SkBitmap createSkBitmap(int width, int height) { |
| 77 | SkBitmap bitmap; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 78 | SkImageInfo info = SkImageInfo::MakeUnknown(width, height); |
| 79 | bitmap.setInfo(info); |
| 80 | bitmap.allocPixels(info); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 81 | return bitmap; |
| 82 | } |
| 83 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 84 | template<class CanvasType> |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 85 | static std::unique_ptr<DisplayList> createDisplayList(int width, int height, |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 86 | std::function<void(CanvasType& canvas)> canvasCallback) { |
| 87 | CanvasType canvas(width, height); |
| 88 | canvasCallback(canvas); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 89 | return std::unique_ptr<DisplayList>(canvas.finishRecording()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 92 | static sp<RenderNode> createNode(int left, int top, int right, int bottom, bool onLayer = false) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 93 | sp<RenderNode> node = new RenderNode(); |
| 94 | node->mutateStagingProperties().setLeftTopRightBottom(left, top, right, bottom); |
| 95 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 96 | if (onLayer) { |
| 97 | node->mutateStagingProperties().mutateLayerProperties().setType(LayerType::RenderLayer); |
| 98 | node->setPropertyFieldsDirty(RenderNode::GENERIC); |
| 99 | } |
| 100 | return node; |
| 101 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 102 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 103 | template<class CanvasType> |
| 104 | static sp<RenderNode> createNode(int left, int top, int right, int bottom, |
| 105 | std::function<void(CanvasType& canvas)> canvasCallback, bool onLayer = false) { |
| 106 | sp<RenderNode> node = createNode(left, top, right, bottom, onLayer); |
| 107 | |
| 108 | auto&& props = node->stagingProperties(); // staging, since not sync'd yet |
| 109 | CanvasType canvas(props.getWidth(), props.getHeight()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 110 | canvasCallback(canvas); |
| 111 | node->setStagingDisplayList(canvas.finishRecording()); |
| 112 | return node; |
| 113 | } |
| 114 | |
| 115 | static void syncNodePropertiesAndDisplayList(sp<RenderNode>& node) { |
| 116 | node->syncProperties(); |
| 117 | node->syncDisplayList(); |
| 118 | } |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 119 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 120 | typedef std::function<void(renderthread::RenderThread& thread)> RtCallback; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 121 | |
| 122 | class TestTask : public renderthread::RenderTask { |
| 123 | public: |
| 124 | TestTask(RtCallback rtCallback) |
| 125 | : rtCallback(rtCallback) {} |
| 126 | virtual ~TestTask() {} |
| 127 | virtual void run() override { |
| 128 | // RenderState only valid once RenderThread is running, so queried here |
| 129 | RenderState& renderState = renderthread::RenderThread::getInstance().renderState(); |
| 130 | |
| 131 | renderState.onGLContextCreated(); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 132 | rtCallback(renderthread::RenderThread::getInstance()); |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 133 | renderState.onGLContextDestroyed(); |
| 134 | }; |
| 135 | RtCallback rtCallback; |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely. |
| 140 | */ |
| 141 | static void runOnRenderThread(RtCallback rtCallback) { |
| 142 | TestTask task(rtCallback); |
| 143 | renderthread::RenderThread::getInstance().queueAndWait(&task); |
| 144 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 145 | }; // class TestUtils |
| 146 | |
| 147 | } /* namespace uirenderer */ |
| 148 | } /* namespace android */ |
| 149 | |
| 150 | #endif /* TEST_UTILS_H */ |