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 | */ |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 16 | |
| 17 | #pragma once |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 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> |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 22 | #include <Properties.h> |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 23 | #include <Rect.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 24 | #include <RenderNode.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 25 | #include <Snapshot.h> |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 26 | #include <hwui/Bitmap.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 27 | #include <pipeline/skia/SkiaRecordingCanvas.h> |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 28 | #include <renderstate/RenderState.h> |
| 29 | #include <renderthread/RenderThread.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 30 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 31 | #include <RecordedOp.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 32 | #include <RecordingCanvas.h> |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 33 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
| 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 39 | #define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 40 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 41 | #define EXPECT_RECT_APPROX_EQ(a, b) \ |
| 42 | EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) && \ |
| 43 | MathUtils::areEqual((a).top, (b).top) && \ |
| 44 | MathUtils::areEqual((a).right, (b).right) && \ |
| 45 | MathUtils::areEqual((a).bottom, (b).bottom)); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 46 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 47 | #define EXPECT_CLIP_RECT(expRect, clipStatePtr) \ |
| 48 | EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \ |
| 49 | if ((clipStatePtr)->mode == ClipMode::Rectangle) { \ |
| 50 | EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \ |
| 51 | } else { \ |
| 52 | ADD_FAILURE() << "ClipState not a rect"; \ |
| 53 | } |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 54 | |
| 55 | #define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 56 | TEST(test_case_name, test_name##_##pipeline) { \ |
| 57 | RenderPipelineType oldType = Properties::getRenderPipelineType(); \ |
| 58 | Properties::overrideRenderPipelineType(RenderPipelineType::pipeline); \ |
| 59 | functionCall; \ |
| 60 | Properties::overrideRenderPipelineType(oldType); \ |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Like gtests' TEST, but only runs with the OpenGL RenderPipelineType |
| 65 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 66 | #define OPENGL_PIPELINE_TEST(test_case_name, test_name) \ |
| 67 | class test_case_name##_##test_name##_HwuiTest { \ |
| 68 | public: \ |
| 69 | static void doTheThing(); \ |
| 70 | }; \ |
| 71 | INNER_PIPELINE_TEST(test_case_name, test_name, OpenGL, \ |
| 72 | test_case_name##_##test_name##_HwuiTest::doTheThing()) \ |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 73 | void test_case_name##_##test_name##_HwuiTest::doTheThing() |
| 74 | |
| 75 | #define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 76 | INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \ |
| 77 | TestUtils::runOnRenderThread( \ |
| 78 | test_case_name##_##test_name##_RenderThreadTest::doTheThing)) |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 79 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope |
| 82 | * (for e.g. accessing its RenderState) |
| 83 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 84 | #define RENDERTHREAD_TEST(test_case_name, test_name) \ |
| 85 | class test_case_name##_##test_name##_RenderThreadTest { \ |
| 86 | public: \ |
| 87 | static void doTheThing(renderthread::RenderThread& renderThread); \ |
| 88 | }; \ |
| 89 | INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, OpenGL); \ |
| 90 | INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \ |
Derek Sollenberger | 0f89539 | 2017-04-27 11:30:20 -0400 | [diff] [blame] | 91 | /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 92 | /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \ |
| 93 | void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \ |
| 94 | renderthread::RenderThread& renderThread) |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Like RENDERTHREAD_TEST, but only runs with the OpenGL RenderPipelineType |
| 98 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 99 | #define RENDERTHREAD_OPENGL_PIPELINE_TEST(test_case_name, test_name) \ |
| 100 | class test_case_name##_##test_name##_RenderThreadTest { \ |
| 101 | public: \ |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 102 | static void doTheThing(renderthread::RenderThread& renderThread); \ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 103 | }; \ |
| 104 | INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, OpenGL); \ |
| 105 | void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \ |
| 106 | renderthread::RenderThread& renderThread) |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes |
| 110 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 111 | #define RENDERTHREAD_SKIA_PIPELINE_TEST(test_case_name, test_name) \ |
| 112 | class test_case_name##_##test_name##_RenderThreadTest { \ |
| 113 | public: \ |
| 114 | static void doTheThing(renderthread::RenderThread& renderThread); \ |
| 115 | }; \ |
| 116 | INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \ |
Derek Sollenberger | 0f89539 | 2017-04-27 11:30:20 -0400 | [diff] [blame] | 117 | /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 118 | /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \ |
| 119 | void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \ |
| 120 | renderthread::RenderThread& renderThread) |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 121 | |
Chris Craik | 3741328 | 2016-05-12 17:48:51 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Sets a property value temporarily, generally for the duration of a test, restoring the previous |
| 124 | * value when going out of scope. |
| 125 | * |
| 126 | * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled. |
| 127 | */ |
| 128 | template <typename T> |
| 129 | class ScopedProperty { |
| 130 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 131 | ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) { |
Chris Craik | 3741328 | 2016-05-12 17:48:51 -0700 | [diff] [blame] | 132 | property = newValue; |
| 133 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 134 | ~ScopedProperty() { *mPropertyPtr = mOldValue; } |
| 135 | |
Chris Craik | 3741328 | 2016-05-12 17:48:51 -0700 | [diff] [blame] | 136 | private: |
| 137 | T* mPropertyPtr; |
| 138 | T mOldValue; |
| 139 | }; |
| 140 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 141 | class TestUtils { |
| 142 | public: |
Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 143 | class SignalingDtor { |
| 144 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 145 | SignalingDtor() : mSignal(nullptr) {} |
| 146 | explicit SignalingDtor(int* signal) : mSignal(signal) {} |
| 147 | void setSignal(int* signal) { mSignal = signal; } |
Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 148 | ~SignalingDtor() { |
| 149 | if (mSignal) { |
| 150 | (*mSignal)++; |
| 151 | } |
| 152 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 153 | |
Chris Craik | 76ace11 | 2015-10-29 12:46:19 -0700 | [diff] [blame] | 154 | private: |
| 155 | int* mSignal; |
| 156 | }; |
| 157 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 158 | class MockTreeObserver : public TreeObserver { |
| 159 | public: |
| 160 | virtual void onMaybeRemovedFromTree(RenderNode* node) {} |
| 161 | }; |
| 162 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 163 | static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) { |
| 164 | for (int i = 0; i < 16; i++) { |
| 165 | if (!MathUtils::areEqual(a[i], b[i])) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) { |
| 173 | std::unique_ptr<Snapshot> snapshot(new Snapshot()); |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 174 | // store clip first, so it isn't transformed |
| 175 | snapshot->setClip(clip.left, clip.top, clip.right, clip.bottom); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 176 | *(snapshot->transform) = transform; |
| 177 | return snapshot; |
| 178 | } |
| 179 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 180 | static sk_sp<Bitmap> createBitmap(int width, int height, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 181 | SkColorType colorType = kN32_SkColorType) { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 182 | SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 183 | return Bitmap::allocateHeapBitmap(info); |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) { |
| 187 | SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType); |
| 188 | outBitmap->setInfo(info); |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 189 | return Bitmap::allocateHeapBitmap(outBitmap); |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 192 | static sp<DeferredLayerUpdater> createTextureLayerUpdater( |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 193 | renderthread::RenderThread& renderThread); |
| 194 | |
| 195 | static sp<DeferredLayerUpdater> createTextureLayerUpdater( |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 196 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 197 | const SkMatrix& transform); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 198 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 199 | template <class CanvasType> |
| 200 | static std::unique_ptr<DisplayList> createDisplayList( |
| 201 | int width, int height, std::function<void(CanvasType& canvas)> canvasCallback) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 202 | CanvasType canvas(width, height); |
| 203 | canvasCallback(canvas); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 204 | return std::unique_ptr<DisplayList>(canvas.finishRecording()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 205 | } |
| 206 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 207 | static sp<RenderNode> createNode( |
| 208 | int left, int top, int right, int bottom, |
Stan Iliev | 06152cd | 2016-07-27 17:55:43 -0400 | [diff] [blame] | 209 | std::function<void(RenderProperties& props, Canvas& canvas)> setup) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 210 | #if HWUI_NULL_GPU |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 211 | // if RenderNodes are being sync'd/used, device info will be needed, since |
| 212 | // DeviceInfo::maxTextureSize() affects layer property |
| 213 | DeviceInfo::initialize(); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 214 | #endif |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 215 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 216 | sp<RenderNode> node = new RenderNode(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 217 | RenderProperties& props = node->mutateStagingProperties(); |
| 218 | props.setLeftTopRightBottom(left, top, right, bottom); |
| 219 | if (setup) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 220 | std::unique_ptr<Canvas> canvas( |
| 221 | Canvas::create_recording_canvas(props.getWidth(), props.getHeight())); |
Stan Iliev | 06152cd | 2016-07-27 17:55:43 -0400 | [diff] [blame] | 222 | setup(props, *canvas.get()); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 223 | node->setStagingDisplayList(canvas->finishRecording()); |
Stan Iliev | 06152cd | 2016-07-27 17:55:43 -0400 | [diff] [blame] | 224 | } |
| 225 | node->setPropertyFieldsDirty(0xFFFFFFFF); |
| 226 | return node; |
| 227 | } |
| 228 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 229 | template <class RecordingCanvasType> |
| 230 | static sp<RenderNode> createNode( |
| 231 | int left, int top, int right, int bottom, |
Stan Iliev | 06152cd | 2016-07-27 17:55:43 -0400 | [diff] [blame] | 232 | std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) { |
| 233 | #if HWUI_NULL_GPU |
| 234 | // if RenderNodes are being sync'd/used, device info will be needed, since |
| 235 | // DeviceInfo::maxTextureSize() affects layer property |
| 236 | DeviceInfo::initialize(); |
| 237 | #endif |
| 238 | |
| 239 | sp<RenderNode> node = new RenderNode(); |
| 240 | RenderProperties& props = node->mutateStagingProperties(); |
| 241 | props.setLeftTopRightBottom(left, top, right, bottom); |
| 242 | if (setup) { |
| 243 | RecordingCanvasType canvas(props.getWidth(), props.getHeight()); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 244 | setup(props, canvas); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 245 | node->setStagingDisplayList(canvas.finishRecording()); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 246 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 247 | node->setPropertyFieldsDirty(0xFFFFFFFF); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 248 | return node; |
| 249 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 250 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 251 | static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) { |
| 252 | std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas( |
| 253 | node.stagingProperties().getWidth(), node.stagingProperties().getHeight())); |
| 254 | contentCallback(*canvas.get()); |
| 255 | node.setStagingDisplayList(canvas->finishRecording()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 256 | } |
| 257 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 258 | static sp<RenderNode> createSkiaNode( |
| 259 | int left, int top, int right, int bottom, |
| 260 | std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)> |
| 261 | setup, |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 262 | const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 263 | #if HWUI_NULL_GPU |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 264 | // if RenderNodes are being sync'd/used, device info will be needed, since |
| 265 | // DeviceInfo::maxTextureSize() affects layer property |
| 266 | DeviceInfo::initialize(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 267 | #endif |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 268 | sp<RenderNode> node = new RenderNode(); |
| 269 | if (name) { |
| 270 | node->setName(name); |
| 271 | } |
| 272 | RenderProperties& props = node->mutateStagingProperties(); |
| 273 | props.setLeftTopRightBottom(left, top, right, bottom); |
| 274 | if (displayList) { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 275 | node->setStagingDisplayList(displayList); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 276 | } |
| 277 | if (setup) { |
| 278 | std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 279 | new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(), |
| 280 | props.getHeight())); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 281 | setup(props, *canvas.get()); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 282 | node->setStagingDisplayList(canvas->finishRecording()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 283 | } |
| 284 | node->setPropertyFieldsDirty(0xFFFFFFFF); |
| 285 | TestUtils::syncHierarchyPropertiesAndDisplayList(node); |
| 286 | return node; |
| 287 | } |
| 288 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 289 | /** |
| 290 | * Forces a sync of a tree of RenderNode, such that every descendant will have its staging |
| 291 | * properties and DisplayList moved to the render copies. |
| 292 | * |
| 293 | * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded. |
| 294 | * For this reason, this should generally only be called once on a tree. |
| 295 | */ |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 296 | static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) { |
| 297 | syncHierarchyPropertiesAndDisplayListImpl(node.get()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 298 | } |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 299 | |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 300 | static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) { |
| 301 | syncHierarchyPropertiesAndDisplayList(node); |
| 302 | return node; |
John Reck | 7db5ffb | 2016-01-15 13:17:09 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 305 | typedef std::function<void(renderthread::RenderThread& thread)> RtCallback; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 306 | |
| 307 | class TestTask : public renderthread::RenderTask { |
| 308 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 309 | explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {} |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 310 | virtual ~TestTask() {} |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 311 | virtual void run() override; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 312 | RtCallback rtCallback; |
| 313 | }; |
| 314 | |
| 315 | /** |
| 316 | * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely. |
| 317 | */ |
| 318 | static void runOnRenderThread(RtCallback rtCallback) { |
| 319 | TestTask task(rtCallback); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 320 | renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); }); |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 321 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 322 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 323 | static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 324 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 325 | static SkColor interpolateColor(float fraction, SkColor start, SkColor end); |
| 326 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 327 | static void layoutTextUnscaled(const SkPaint& paint, const char* text, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 328 | std::vector<glyph_t>* outGlyphs, |
| 329 | std::vector<float>* outPositions, float* outTotalAdvance, |
| 330 | Rect* outBounds); |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 331 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 332 | static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, float x, |
| 333 | float y); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 334 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 335 | static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, |
| 336 | const SkPath& path); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 337 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 338 | static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 339 | |
Derek Sollenberger | 835b3a69 | 2016-10-28 13:57:14 -0400 | [diff] [blame] | 340 | class MockFunctor : public Functor { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 341 | public: |
| 342 | virtual status_t operator()(int what, void* data) { |
| 343 | mLastMode = what; |
| 344 | return DrawGlInfo::kStatusDone; |
| 345 | } |
| 346 | int getLastMode() const { return mLastMode; } |
| 347 | |
| 348 | private: |
| 349 | int mLastMode = -1; |
| 350 | }; |
Derek Sollenberger | 835b3a69 | 2016-10-28 13:57:14 -0400 | [diff] [blame] | 351 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 352 | static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y); |
| 353 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 354 | static SkRect getClipBounds(const SkCanvas* canvas); |
| 355 | static SkRect getLocalClipBounds(const SkCanvas* canvas); |
| 356 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 357 | private: |
| 358 | static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 359 | MarkAndSweepRemoved observer(nullptr); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 360 | node->syncProperties(); |
John Reck | 3afd637 | 2017-01-30 10:15:48 -0800 | [diff] [blame] | 361 | if (node->mNeedsDisplayListSync) { |
| 362 | node->mNeedsDisplayListSync = false; |
| 363 | node->syncDisplayList(observer, nullptr); |
| 364 | } |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 365 | auto displayList = node->getDisplayList(); |
| 366 | if (displayList) { |
Stan Iliev | 25912c7 | 2017-03-31 14:00:20 -0400 | [diff] [blame] | 367 | if (displayList->isSkiaDL()) { |
| 368 | for (auto&& childDr : static_cast<skiapipeline::SkiaDisplayList*>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 369 | const_cast<DisplayList*>(displayList)) |
| 370 | ->mChildNodes) { |
Stan Iliev | 25912c7 | 2017-03-31 14:00:20 -0400 | [diff] [blame] | 371 | syncHierarchyPropertiesAndDisplayListImpl(childDr.getRenderNode()); |
| 372 | } |
| 373 | } else { |
| 374 | for (auto&& childOp : displayList->getChildren()) { |
| 375 | syncHierarchyPropertiesAndDisplayListImpl(childOp->renderNode); |
| 376 | } |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 381 | }; // class TestUtils |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 382 | |
| 383 | } /* namespace uirenderer */ |
| 384 | } /* namespace android */ |