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