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 | |
| 34 | class TestUtils { |
| 35 | public: |
| 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 Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 52 | static SkBitmap createSkBitmap(int width, int height) { |
| 53 | SkBitmap bitmap; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 54 | SkImageInfo info = SkImageInfo::MakeUnknown(width, height); |
| 55 | bitmap.setInfo(info); |
| 56 | bitmap.allocPixels(info); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 57 | return bitmap; |
| 58 | } |
| 59 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 60 | template<class CanvasType> |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 61 | static std::unique_ptr<DisplayList> createDisplayList(int width, int height, |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 62 | std::function<void(CanvasType& canvas)> canvasCallback) { |
| 63 | CanvasType canvas(width, height); |
| 64 | canvasCallback(canvas); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 65 | return std::unique_ptr<DisplayList>(canvas.finishRecording()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 66 | } |
| 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 Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 86 | |
| 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 112 | }; // class TestUtils |
| 113 | |
| 114 | } /* namespace uirenderer */ |
| 115 | } /* namespace android */ |
| 116 | |
| 117 | #endif /* TEST_UTILS_H */ |