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 | |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 19 | #include <DeviceInfo.h> |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 20 | #include <DisplayList.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 21 | #include <Matrix.h> |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 22 | #include <Rect.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 23 | #include <RenderNode.h> |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 24 | #include <renderstate/RenderState.h> |
| 25 | #include <renderthread/RenderThread.h> |
| 26 | #include <Snapshot.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 27 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 28 | #if HWUI_NEW_OPS |
| 29 | #include <RecordedOp.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 30 | #include <RecordingCanvas.h> |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 31 | #else |
| 32 | #include <DisplayListOp.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 33 | #include <DisplayListCanvas.h> |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 34 | #endif |
| 35 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 36 | #include <memory> |
| 37 | |
| 38 | namespace android { |
| 39 | namespace uirenderer { |
| 40 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 41 | #if HWUI_NEW_OPS |
| 42 | typedef RecordingCanvas TestCanvas; |
| 43 | #else |
| 44 | typedef DisplayListCanvas TestCanvas; |
| 45 | #endif |
| 46 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 47 | #define EXPECT_MATRIX_APPROX_EQ(a, b) \ |
| 48 | EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) |
| 49 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 50 | #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 Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 56 | #define EXPECT_CLIP_RECT(expRect, clipStatePtr) \ |
| 57 | EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \ |
| 58 | if ((clipStatePtr)->mode == ClipMode::Rectangle) { \ |
| 59 | EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \ |
| 60 | } else { \ |
| 61 | ADD_FAILURE() << "ClipState not a rect"; \ |
| 62 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope |
| 65 | * (for e.g. accessing its RenderState) |
| 66 | */ |
| 67 | #define RENDERTHREAD_TEST(test_case_name, test_name) \ |
| 68 | class test_case_name##_##test_name##_RenderThreadTest { \ |
| 69 | public: \ |
| 70 | static void doTheThing(renderthread::RenderThread& renderThread); \ |
| 71 | }; \ |
| 72 | TEST(test_case_name, test_name) { \ |
| 73 | TestUtils::runOnRenderThread(test_case_name##_##test_name##_RenderThreadTest::doTheThing); \ |
| 74 | }; \ |
| 75 | void test_case_name##_##test_name##_RenderThreadTest::doTheThing(renderthread::RenderThread& renderThread) |
| 76 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 77 | class TestUtils { |
| 78 | public: |
Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 79 | class SignalingDtor { |
| 80 | public: |
| 81 | SignalingDtor() |
| 82 | : mSignal(nullptr) {} |
| 83 | SignalingDtor(int* signal) |
| 84 | : mSignal(signal) {} |
| 85 | void setSignal(int* signal) { |
| 86 | mSignal = signal; |
| 87 | } |
| 88 | ~SignalingDtor() { |
| 89 | if (mSignal) { |
| 90 | (*mSignal)++; |
| 91 | } |
| 92 | } |
| 93 | private: |
| 94 | int* mSignal; |
| 95 | }; |
| 96 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 97 | static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) { |
| 98 | for (int i = 0; i < 16; i++) { |
| 99 | if (!MathUtils::areEqual(a[i], b[i])) { |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) { |
| 107 | std::unique_ptr<Snapshot> snapshot(new Snapshot()); |
Chris Craik | a2a7072 | 2015-12-17 12:58:24 -0800 | [diff] [blame] | 108 | snapshot->clip(clip, SkRegion::kReplace_Op); // store clip first, so it isn't transformed |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 109 | *(snapshot->transform) = transform; |
| 110 | return snapshot; |
| 111 | } |
| 112 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 113 | static SkBitmap createSkBitmap(int width, int height, |
| 114 | SkColorType colorType = kN32_SkColorType) { |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 115 | SkBitmap bitmap; |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 116 | SkImageInfo info = SkImageInfo::Make(width, height, |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 117 | colorType, kPremul_SkAlphaType); |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 118 | bitmap.setInfo(info); |
| 119 | bitmap.allocPixels(info); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 120 | return bitmap; |
| 121 | } |
| 122 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 123 | static sp<DeferredLayerUpdater> createTextureLayerUpdater( |
| 124 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
| 125 | std::function<void(Matrix4*)> transformSetupCallback); |
| 126 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 127 | template<class CanvasType> |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 128 | static std::unique_ptr<DisplayList> createDisplayList(int width, int height, |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 129 | std::function<void(CanvasType& canvas)> canvasCallback) { |
| 130 | CanvasType canvas(width, height); |
| 131 | canvasCallback(canvas); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 132 | return std::unique_ptr<DisplayList>(canvas.finishRecording()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 135 | static sp<RenderNode> createNode(int left, int top, int right, int bottom, |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 136 | std::function<void(RenderProperties& props, TestCanvas& canvas)> setup) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 137 | #if HWUI_NULL_GPU |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 138 | // if RenderNodes are being sync'd/used, device info will be needed, since |
| 139 | // DeviceInfo::maxTextureSize() affects layer property |
| 140 | DeviceInfo::initialize(); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 141 | #endif |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 142 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 143 | sp<RenderNode> node = new RenderNode(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 144 | RenderProperties& props = node->mutateStagingProperties(); |
| 145 | props.setLeftTopRightBottom(left, top, right, bottom); |
| 146 | if (setup) { |
| 147 | TestCanvas canvas(props.getWidth(), props.getHeight()); |
| 148 | setup(props, canvas); |
| 149 | node->setStagingDisplayList(canvas.finishRecording()); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 150 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 151 | node->setPropertyFieldsDirty(0xFFFFFFFF); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 152 | return node; |
| 153 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 154 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 155 | static void recordNode(RenderNode& node, |
| 156 | std::function<void(TestCanvas&)> contentCallback) { |
| 157 | TestCanvas canvas(node.stagingProperties().getWidth(), |
| 158 | node.stagingProperties().getHeight()); |
| 159 | contentCallback(canvas); |
| 160 | node.setStagingDisplayList(canvas.finishRecording()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 163 | /** |
| 164 | * Forces a sync of a tree of RenderNode, such that every descendant will have its staging |
| 165 | * properties and DisplayList moved to the render copies. |
| 166 | * |
| 167 | * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded. |
| 168 | * For this reason, this should generally only be called once on a tree. |
| 169 | */ |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 170 | static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) { |
| 171 | syncHierarchyPropertiesAndDisplayListImpl(node.get()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 172 | } |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 173 | |
John Reck | 7db5ffb | 2016-01-15 13:17:09 -0800 | [diff] [blame] | 174 | static std::vector<sp<RenderNode>> createSyncedNodeList(sp<RenderNode>& node) { |
| 175 | TestUtils::syncHierarchyPropertiesAndDisplayList(node); |
| 176 | std::vector<sp<RenderNode>> vec; |
| 177 | vec.emplace_back(node); |
| 178 | return vec; |
| 179 | } |
| 180 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 181 | typedef std::function<void(renderthread::RenderThread& thread)> RtCallback; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 182 | |
| 183 | class TestTask : public renderthread::RenderTask { |
| 184 | public: |
| 185 | TestTask(RtCallback rtCallback) |
| 186 | : rtCallback(rtCallback) {} |
| 187 | virtual ~TestTask() {} |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 188 | virtual void run() override; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 189 | RtCallback rtCallback; |
| 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely. |
| 194 | */ |
| 195 | static void runOnRenderThread(RtCallback rtCallback) { |
| 196 | TestTask task(rtCallback); |
| 197 | renderthread::RenderThread::getInstance().queueAndWait(&task); |
| 198 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 199 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 200 | static bool isRenderThreadRunning() { |
| 201 | return renderthread::RenderThread::hasInstance(); |
| 202 | } |
| 203 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 204 | static SkColor interpolateColor(float fraction, SkColor start, SkColor end); |
| 205 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 206 | static void layoutTextUnscaled(const SkPaint& paint, const char* text, |
| 207 | std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, |
| 208 | float* outTotalAdvance, Rect* outBounds); |
| 209 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 210 | static void drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 211 | const SkPaint& paint, float x, float y); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 212 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 213 | static void drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 214 | const SkPaint& paint, const SkPath& path); |
| 215 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 216 | static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 217 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 218 | private: |
| 219 | static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) { |
| 220 | node->syncProperties(); |
| 221 | node->syncDisplayList(); |
| 222 | auto displayList = node->getDisplayList(); |
| 223 | if (displayList) { |
| 224 | for (auto&& childOp : displayList->getChildren()) { |
| 225 | syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 230 | }; // class TestUtils |
| 231 | |
| 232 | } /* namespace uirenderer */ |
| 233 | } /* namespace android */ |
| 234 | |
| 235 | #endif /* TEST_UTILS_H */ |