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