blob: fcaa745e9fc6ba53e67dc8a61240aac7aac99dba [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
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 Craik5e00c7c2016-07-06 16:10:09 -070016
17#pragma once
Chris Craikb565df12015-10-05 13:00:52 -070018
Nolan Scobie892cac22022-10-24 19:57:43 -040019#include <AutoBackendTextureRelease.h>
Chris Craik161f54b2015-11-05 11:08:52 -080020#include <DisplayList.h>
Chris Craikb565df12015-10-05 13:00:52 -070021#include <Matrix.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050022#include <Properties.h>
Chris Craik0a24b142015-10-19 17:10:19 -070023#include <Rect.h>
Chris Craikb565df12015-10-05 13:00:52 -070024#include <RenderNode.h>
sergeyvc1c54062016-10-19 18:47:26 -070025#include <hwui/Bitmap.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040026#include <pipeline/skia/SkiaRecordingCanvas.h>
Derek Sollenberger28a4d992018-09-20 13:37:24 -040027#include <private/hwui/DrawGlInfo.h>
Chris Craik0a24b142015-10-19 17:10:19 -070028#include <renderstate/RenderState.h>
29#include <renderthread/RenderThread.h>
Chris Craikb565df12015-10-05 13:00:52 -070030
John Reck283bb462018-12-13 16:40:14 -080031#include <gtest/gtest.h>
Chris Craikb565df12015-10-05 13:00:52 -070032#include <memory>
Stan Ilievaaa9e832019-09-17 14:07:23 -040033#include <unordered_map>
Chris Craikb565df12015-10-05 13:00:52 -070034
35namespace android {
36namespace uirenderer {
37
John Reck1bcacfd2017-11-03 10:12:19 -070038#define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
Chris Craikb565df12015-10-05 13:00:52 -070039
John Reck1bcacfd2017-11-03 10:12:19 -070040#define EXPECT_RECT_APPROX_EQ(a, b) \
41 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) && \
42 MathUtils::areEqual((a).top, (b).top) && \
43 MathUtils::areEqual((a).right, (b).right) && \
44 MathUtils::areEqual((a).bottom, (b).bottom));
Chris Craik6fe991e52015-10-20 09:39:42 -070045
John Reck1bcacfd2017-11-03 10:12:19 -070046#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \
47 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \
48 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \
49 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \
50 } else { \
51 ADD_FAILURE() << "ClipState not a rect"; \
52 }
Greg Daniel98c78dad2017-01-04 14:45:56 -050053
Alec Mouriaa3e4982020-12-14 14:47:57 -080054#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \
55 TEST(test_case_name, test_name##_##pipeline) { \
56 RenderPipelineType oldType = Properties::getRenderPipelineType(); \
57 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline, true); \
58 functionCall; \
59 Properties::overrideRenderPipelineType(oldType, true); \
Greg Daniel98c78dad2017-01-04 14:45:56 -050060 };
61
Greg Daniel98c78dad2017-01-04 14:45:56 -050062#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \
John Reck1bcacfd2017-11-03 10:12:19 -070063 INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \
64 TestUtils::runOnRenderThread( \
65 test_case_name##_##test_name##_RenderThreadTest::doTheThing))
Greg Daniel98c78dad2017-01-04 14:45:56 -050066
Chris Craik98787e62015-11-13 10:55:30 -080067/**
68 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
69 * (for e.g. accessing its RenderState)
70 */
Alec Mouriaa3e4982020-12-14 14:47:57 -080071#define RENDERTHREAD_TEST(test_case_name, test_name) \
72 class test_case_name##_##test_name##_RenderThreadTest { \
73 public: \
74 static void doTheThing(renderthread::RenderThread& renderThread); \
75 }; \
76 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
77 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \
78 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
John Reck1bcacfd2017-11-03 10:12:19 -070079 renderthread::RenderThread& renderThread)
Greg Daniel98c78dad2017-01-04 14:45:56 -050080
81/**
Greg Daniel98c78dad2017-01-04 14:45:56 -050082 * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes
83 */
Alec Mouriaa3e4982020-12-14 14:47:57 -080084#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); \
90 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \
91 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
John Reck1bcacfd2017-11-03 10:12:19 -070092 renderthread::RenderThread& renderThread)
Chris Craik98787e62015-11-13 10:55:30 -080093
Chris Craik37413282016-05-12 17:48:51 -070094/**
95 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
96 * value when going out of scope.
97 *
98 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
99 */
100template <typename T>
101class ScopedProperty {
102public:
John Reck1bcacfd2017-11-03 10:12:19 -0700103 ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) {
Chris Craik37413282016-05-12 17:48:51 -0700104 property = newValue;
105 }
John Reck1bcacfd2017-11-03 10:12:19 -0700106 ~ScopedProperty() { *mPropertyPtr = mOldValue; }
107
Chris Craik37413282016-05-12 17:48:51 -0700108private:
109 T* mPropertyPtr;
110 T mOldValue;
111};
112
Chris Craikb565df12015-10-05 13:00:52 -0700113class TestUtils {
114public:
Chris Craik76ace112015-10-29 12:46:19 -0700115 class SignalingDtor {
116 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700117 SignalingDtor() : mSignal(nullptr) {}
118 explicit SignalingDtor(int* signal) : mSignal(signal) {}
119 void setSignal(int* signal) { mSignal = signal; }
Chris Craik76ace112015-10-29 12:46:19 -0700120 ~SignalingDtor() {
121 if (mSignal) {
122 (*mSignal)++;
123 }
124 }
John Reck1bcacfd2017-11-03 10:12:19 -0700125
Chris Craik76ace112015-10-29 12:46:19 -0700126 private:
127 int* mSignal;
128 };
129
John Reck2de950d2017-01-25 10:58:30 -0800130 class MockTreeObserver : public TreeObserver {
131 public:
132 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
133 };
134
Chris Craikb565df12015-10-05 13:00:52 -0700135 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
136 for (int i = 0; i < 16; i++) {
137 if (!MathUtils::areEqual(a[i], b[i])) {
138 return false;
139 }
140 }
141 return true;
142 }
143
sergeyvaed7f582016-10-14 16:30:21 -0700144 static sk_sp<Bitmap> createBitmap(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -0700145 SkColorType colorType = kN32_SkColorType) {
sergeyvaed7f582016-10-14 16:30:21 -0700146 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700147 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700148 }
149
150 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
151 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
152 outBitmap->setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400153 return Bitmap::allocateHeapBitmap(outBitmap);
sergeyvaed7f582016-10-14 16:30:21 -0700154 }
155
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800156 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500157 renderthread::RenderThread& renderThread);
158
159 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800160 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700161 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800162
John Reck1bcacfd2017-11-03 10:12:19 -0700163 static sp<RenderNode> createNode(
164 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400165 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craikb565df12015-10-05 13:00:52 -0700166 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800167 RenderProperties& props = node->mutateStagingProperties();
168 props.setLeftTopRightBottom(left, top, right, bottom);
169 if (setup) {
John Reck1bcacfd2017-11-03 10:12:19 -0700170 std::unique_ptr<Canvas> canvas(
171 Canvas::create_recording_canvas(props.getWidth(), props.getHeight()));
Stan Iliev06152cd2016-07-27 17:55:43 -0400172 setup(props, *canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500173 canvas->finishRecording(node.get());
Stan Iliev06152cd2016-07-27 17:55:43 -0400174 }
175 node->setPropertyFieldsDirty(0xFFFFFFFF);
176 return node;
177 }
178
John Reck1bcacfd2017-11-03 10:12:19 -0700179 template <class RecordingCanvasType>
180 static sp<RenderNode> createNode(
181 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400182 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400183 sp<RenderNode> node = new RenderNode();
184 RenderProperties& props = node->mutateStagingProperties();
185 props.setLeftTopRightBottom(left, top, right, bottom);
186 if (setup) {
187 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800188 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800189 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700190 }
John Reck16c9d6a2015-11-17 15:51:08 -0800191 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700192 return node;
193 }
Chris Craikb565df12015-10-05 13:00:52 -0700194
John Reck1bcacfd2017-11-03 10:12:19 -0700195 static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) {
196 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
John Reck283bb462018-12-13 16:40:14 -0800197 node.stagingProperties().getWidth(), node.stagingProperties().getHeight(), &node));
John Reck1bcacfd2017-11-03 10:12:19 -0700198 contentCallback(*canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500199 canvas->finishRecording(&node);
Chris Craikb565df12015-10-05 13:00:52 -0700200 }
201
John Reck1bcacfd2017-11-03 10:12:19 -0700202 static sp<RenderNode> createSkiaNode(
203 int left, int top, int right, int bottom,
204 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
205 setup,
John Reckbe671952021-01-13 22:39:32 -0500206 const char* name = nullptr,
207 std::unique_ptr<skiapipeline::SkiaDisplayList> displayList = nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400208 sp<RenderNode> node = new RenderNode();
209 if (name) {
210 node->setName(name);
211 }
212 RenderProperties& props = node->mutateStagingProperties();
213 props.setLeftTopRightBottom(left, top, right, bottom);
214 if (displayList) {
John Reckbe671952021-01-13 22:39:32 -0500215 node->setStagingDisplayList(DisplayList(std::move(displayList)));
Stan Iliev500a0c32016-10-26 10:30:09 -0400216 }
217 if (setup) {
218 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700219 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
220 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400221 setup(props, *canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500222 canvas->finishRecording(node.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400223 }
224 node->setPropertyFieldsDirty(0xFFFFFFFF);
225 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
226 return node;
227 }
228
Chris Craik8d1f2122015-11-24 16:40:09 -0800229 /**
230 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
231 * properties and DisplayList moved to the render copies.
232 *
233 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
234 * For this reason, this should generally only be called once on a tree.
235 */
Chris Craik161f54b2015-11-05 11:08:52 -0800236 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
237 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700238 }
Chris Craik0a24b142015-10-19 17:10:19 -0700239
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700240 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
241 syncHierarchyPropertiesAndDisplayList(node);
242 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800243 }
244
Chris Craik0b7e8242015-10-28 16:50:44 -0700245 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700246
247 class TestTask : public renderthread::RenderTask {
248 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700249 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700250 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800251 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700252 RtCallback rtCallback;
253 };
254
255 /**
256 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
257 */
258 static void runOnRenderThread(RtCallback rtCallback) {
259 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700260 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700261 }
John Reck16c9d6a2015-11-17 15:51:08 -0800262
John Reck283bb462018-12-13 16:40:14 -0800263 static void runOnRenderThreadUnmanaged(RtCallback rtCallback) {
264 auto& rt = renderthread::RenderThread::getInstance();
265 rt.queue().runSync([&]() { rtCallback(rt); });
266 }
267
268
John Reck1bcacfd2017-11-03 10:12:19 -0700269 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck283bb462018-12-13 16:40:14 -0800270 static pid_t getRenderThreadTid() { return renderthread::RenderThread::getInstance().getTid(); }
John Reck38e0c322015-11-10 12:19:17 -0800271
John Reck16c9d6a2015-11-17 15:51:08 -0800272 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
273
Mike Reedf6d86ac2019-01-18 14:13:23 -0500274 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700275 float y);
Chris Craika1717272015-11-19 13:02:43 -0800276
Mike Reedf6d86ac2019-01-18 14:13:23 -0500277 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700278 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800279
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400280 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700281
Stan Iliev021693b2016-10-17 16:26:15 -0400282 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
283
Stan Iliev52771272016-11-17 09:54:38 -0500284 static SkRect getClipBounds(const SkCanvas* canvas);
285 static SkRect getLocalClipBounds(const SkCanvas* canvas);
286
Nolan Scobie892cac22022-10-24 19:57:43 -0400287 static int getUsageCount(const AutoBackendTextureRelease* textureRelease) {
288 EXPECT_NE(nullptr, textureRelease);
289 return textureRelease->mUsageCount;
290 }
291
John Reck283bb462018-12-13 16:40:14 -0800292 struct CallCounts {
293 int sync = 0;
294 int contextDestroyed = 0;
295 int destroyed = 0;
John Reckfaa1b0a2021-05-13 10:28:38 -0400296 int removeOverlays = 0;
John Reck283bb462018-12-13 16:40:14 -0800297 int glesDraw = 0;
298 };
299
John Reckaa4c9822020-06-25 17:14:13 -0700300 static void expectOnRenderThread(const std::string_view& function = "unknown") {
301 EXPECT_EQ(gettid(), TestUtils::getRenderThreadTid()) << "Called on wrong thread: " << function;
302 }
John Reck283bb462018-12-13 16:40:14 -0800303
304 static WebViewFunctorCallbacks createMockFunctor(RenderMode mode) {
305 auto callbacks = WebViewFunctorCallbacks{
306 .onSync =
Bo Liud6668e72018-12-14 19:37:41 -0800307 [](int functor, void* client_data, const WebViewSyncData& data) {
John Reckaa4c9822020-06-25 17:14:13 -0700308 expectOnRenderThread("onSync");
John Reck283bb462018-12-13 16:40:14 -0800309 sMockFunctorCounts[functor].sync++;
310 },
311 .onContextDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800312 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700313 expectOnRenderThread("onContextDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800314 sMockFunctorCounts[functor].contextDestroyed++;
315 },
316 .onDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800317 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700318 expectOnRenderThread("onDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800319 sMockFunctorCounts[functor].destroyed++;
320 },
John Reckfaa1b0a2021-05-13 10:28:38 -0400321 .removeOverlays =
322 [](int functor, void* data,
323 void (*mergeTransaction)(ASurfaceTransaction*)) {
324 expectOnRenderThread("removeOverlays");
325 sMockFunctorCounts[functor].removeOverlays++;
326 },
John Reck283bb462018-12-13 16:40:14 -0800327 };
328 switch (mode) {
329 case RenderMode::OpenGL_ES:
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500330 callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params,
331 const WebViewOverlayData& overlay_params) {
John Reckaa4c9822020-06-25 17:14:13 -0700332 expectOnRenderThread("draw");
John Reck283bb462018-12-13 16:40:14 -0800333 sMockFunctorCounts[functor].glesDraw++;
334 };
335 break;
336 default:
337 ADD_FAILURE();
338 return WebViewFunctorCallbacks{};
339 }
340 return callbacks;
341 }
342
343 static CallCounts& countsForFunctor(int functor) { return sMockFunctorCounts[functor]; }
344
Chris Craik161f54b2015-11-05 11:08:52 -0800345private:
John Reck283bb462018-12-13 16:40:14 -0800346 static std::unordered_map<int, CallCounts> sMockFunctorCounts;
347
Chris Craik161f54b2015-11-05 11:08:52 -0800348 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800349 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800350 node->syncProperties();
John Reck3afd6372017-01-30 10:15:48 -0800351 if (node->mNeedsDisplayListSync) {
352 node->mNeedsDisplayListSync = false;
353 node->syncDisplayList(observer, nullptr);
354 }
John Reckbe671952021-01-13 22:39:32 -0500355 auto& displayList = node->getDisplayList();
Chris Craik161f54b2015-11-05 11:08:52 -0800356 if (displayList) {
John Reckbe671952021-01-13 22:39:32 -0500357 displayList.updateChildren([](RenderNode* child) {
358 syncHierarchyPropertiesAndDisplayListImpl(child);
359 });
Chris Craik161f54b2015-11-05 11:08:52 -0800360 }
361 }
362
John Reck1bcacfd2017-11-03 10:12:19 -0700363}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700364
365} /* namespace uirenderer */
366} /* namespace android */