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