blob: 75865c751d52fdbad53c5b19fd04427f58236326 [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
Chris Craik161f54b2015-11-05 11:08:52 -080019#include <DisplayList.h>
Chris Craikb565df12015-10-05 13:00:52 -070020#include <Matrix.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050021#include <Properties.h>
Chris Craik0a24b142015-10-19 17:10:19 -070022#include <Rect.h>
Chris Craikb565df12015-10-05 13:00:52 -070023#include <RenderNode.h>
sergeyvc1c54062016-10-19 18:47:26 -070024#include <hwui/Bitmap.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040025#include <pipeline/skia/SkiaRecordingCanvas.h>
Derek Sollenberger28a4d992018-09-20 13:37:24 -040026#include <private/hwui/DrawGlInfo.h>
Chris Craik0a24b142015-10-19 17:10:19 -070027#include <renderstate/RenderState.h>
28#include <renderthread/RenderThread.h>
Chris Craikb565df12015-10-05 13:00:52 -070029
Kevin Lubick1175dc02022-02-28 12:41:27 -050030#include <SkBitmap.h>
31#include <SkColor.h>
32#include <SkImageInfo.h>
33#include <SkRefCnt.h>
34
John Reck283bb462018-12-13 16:40:14 -080035#include <gtest/gtest.h>
Chris Craikb565df12015-10-05 13:00:52 -070036#include <memory>
Stan Ilievaaa9e832019-09-17 14:07:23 -040037#include <unordered_map>
Chris Craikb565df12015-10-05 13:00:52 -070038
Kevin Lubick1175dc02022-02-28 12:41:27 -050039class SkCanvas;
40class SkMatrix;
41class SkPath;
42struct SkRect;
43
Chris Craikb565df12015-10-05 13:00:52 -070044namespace android {
45namespace uirenderer {
46
John Reck1bcacfd2017-11-03 10:12:19 -070047#define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
Chris Craikb565df12015-10-05 13:00:52 -070048
John Reck1bcacfd2017-11-03 10:12:19 -070049#define EXPECT_RECT_APPROX_EQ(a, b) \
50 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) && \
51 MathUtils::areEqual((a).top, (b).top) && \
52 MathUtils::areEqual((a).right, (b).right) && \
53 MathUtils::areEqual((a).bottom, (b).bottom));
Chris Craik6fe991e52015-10-20 09:39:42 -070054
John Reck1bcacfd2017-11-03 10:12:19 -070055#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \
56 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \
57 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \
58 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \
59 } else { \
60 ADD_FAILURE() << "ClipState not a rect"; \
61 }
Greg Daniel98c78dad2017-01-04 14:45:56 -050062
Alec Mouriaa3e4982020-12-14 14:47:57 -080063#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \
64 TEST(test_case_name, test_name##_##pipeline) { \
65 RenderPipelineType oldType = Properties::getRenderPipelineType(); \
66 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline, true); \
67 functionCall; \
68 Properties::overrideRenderPipelineType(oldType, true); \
Greg Daniel98c78dad2017-01-04 14:45:56 -050069 };
70
Greg Daniel98c78dad2017-01-04 14:45:56 -050071#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \
John Reck1bcacfd2017-11-03 10:12:19 -070072 INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \
73 TestUtils::runOnRenderThread( \
74 test_case_name##_##test_name##_RenderThreadTest::doTheThing))
Greg Daniel98c78dad2017-01-04 14:45:56 -050075
Chris Craik98787e62015-11-13 10:55:30 -080076/**
77 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
78 * (for e.g. accessing its RenderState)
79 */
Alec Mouriaa3e4982020-12-14 14:47:57 -080080#define RENDERTHREAD_TEST(test_case_name, test_name) \
81 class test_case_name##_##test_name##_RenderThreadTest { \
82 public: \
83 static void doTheThing(renderthread::RenderThread& renderThread); \
84 }; \
85 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
86 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \
87 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
John Reck1bcacfd2017-11-03 10:12:19 -070088 renderthread::RenderThread& renderThread)
Greg Daniel98c78dad2017-01-04 14:45:56 -050089
90/**
Greg Daniel98c78dad2017-01-04 14:45:56 -050091 * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes
92 */
Alec Mouriaa3e4982020-12-14 14:47:57 -080093#define RENDERTHREAD_SKIA_PIPELINE_TEST(test_case_name, test_name) \
94 class test_case_name##_##test_name##_RenderThreadTest { \
95 public: \
96 static void doTheThing(renderthread::RenderThread& renderThread); \
97 }; \
98 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
99 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \
100 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
John Reck1bcacfd2017-11-03 10:12:19 -0700101 renderthread::RenderThread& renderThread)
Chris Craik98787e62015-11-13 10:55:30 -0800102
Chris Craik37413282016-05-12 17:48:51 -0700103/**
104 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
105 * value when going out of scope.
106 *
107 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
108 */
109template <typename T>
110class ScopedProperty {
111public:
John Reck1bcacfd2017-11-03 10:12:19 -0700112 ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) {
Chris Craik37413282016-05-12 17:48:51 -0700113 property = newValue;
114 }
John Reck1bcacfd2017-11-03 10:12:19 -0700115 ~ScopedProperty() { *mPropertyPtr = mOldValue; }
116
Chris Craik37413282016-05-12 17:48:51 -0700117private:
118 T* mPropertyPtr;
119 T mOldValue;
120};
121
Chris Craikb565df12015-10-05 13:00:52 -0700122class TestUtils {
123public:
Chris Craik76ace112015-10-29 12:46:19 -0700124 class SignalingDtor {
125 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700126 SignalingDtor() : mSignal(nullptr) {}
127 explicit SignalingDtor(int* signal) : mSignal(signal) {}
128 void setSignal(int* signal) { mSignal = signal; }
Chris Craik76ace112015-10-29 12:46:19 -0700129 ~SignalingDtor() {
130 if (mSignal) {
131 (*mSignal)++;
132 }
133 }
John Reck1bcacfd2017-11-03 10:12:19 -0700134
Chris Craik76ace112015-10-29 12:46:19 -0700135 private:
136 int* mSignal;
137 };
138
John Reck2de950d2017-01-25 10:58:30 -0800139 class MockTreeObserver : public TreeObserver {
140 public:
141 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
142 };
143
Chris Craikb565df12015-10-05 13:00:52 -0700144 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
145 for (int i = 0; i < 16; i++) {
146 if (!MathUtils::areEqual(a[i], b[i])) {
147 return false;
148 }
149 }
150 return true;
151 }
152
sergeyvaed7f582016-10-14 16:30:21 -0700153 static sk_sp<Bitmap> createBitmap(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -0700154 SkColorType colorType = kN32_SkColorType) {
sergeyvaed7f582016-10-14 16:30:21 -0700155 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700156 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700157 }
158
159 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
160 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
161 outBitmap->setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400162 return Bitmap::allocateHeapBitmap(outBitmap);
sergeyvaed7f582016-10-14 16:30:21 -0700163 }
164
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800165 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500166 renderthread::RenderThread& renderThread);
167
168 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800169 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700170 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800171
John Reck1bcacfd2017-11-03 10:12:19 -0700172 static sp<RenderNode> createNode(
173 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400174 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craikb565df12015-10-05 13:00:52 -0700175 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800176 RenderProperties& props = node->mutateStagingProperties();
177 props.setLeftTopRightBottom(left, top, right, bottom);
178 if (setup) {
John Reck1bcacfd2017-11-03 10:12:19 -0700179 std::unique_ptr<Canvas> canvas(
180 Canvas::create_recording_canvas(props.getWidth(), props.getHeight()));
Stan Iliev06152cd2016-07-27 17:55:43 -0400181 setup(props, *canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500182 canvas->finishRecording(node.get());
Stan Iliev06152cd2016-07-27 17:55:43 -0400183 }
184 node->setPropertyFieldsDirty(0xFFFFFFFF);
185 return node;
186 }
187
John Reck1bcacfd2017-11-03 10:12:19 -0700188 template <class RecordingCanvasType>
189 static sp<RenderNode> createNode(
190 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400191 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400192 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 Reck16c9d6a2015-11-17 15:51:08 -0800197 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800198 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700199 }
John Reck16c9d6a2015-11-17 15:51:08 -0800200 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700201 return node;
202 }
Chris Craikb565df12015-10-05 13:00:52 -0700203
John Reck1bcacfd2017-11-03 10:12:19 -0700204 static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) {
205 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
John Reck283bb462018-12-13 16:40:14 -0800206 node.stagingProperties().getWidth(), node.stagingProperties().getHeight(), &node));
John Reck1bcacfd2017-11-03 10:12:19 -0700207 contentCallback(*canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500208 canvas->finishRecording(&node);
Chris Craikb565df12015-10-05 13:00:52 -0700209 }
210
John Reck1bcacfd2017-11-03 10:12:19 -0700211 static sp<RenderNode> createSkiaNode(
212 int left, int top, int right, int bottom,
213 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
214 setup,
John Reckbe671952021-01-13 22:39:32 -0500215 const char* name = nullptr,
216 std::unique_ptr<skiapipeline::SkiaDisplayList> displayList = nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400217 sp<RenderNode> node = new RenderNode();
218 if (name) {
219 node->setName(name);
220 }
221 RenderProperties& props = node->mutateStagingProperties();
222 props.setLeftTopRightBottom(left, top, right, bottom);
223 if (displayList) {
John Reckbe671952021-01-13 22:39:32 -0500224 node->setStagingDisplayList(DisplayList(std::move(displayList)));
Stan Iliev500a0c32016-10-26 10:30:09 -0400225 }
226 if (setup) {
227 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700228 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
229 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400230 setup(props, *canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500231 canvas->finishRecording(node.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400232 }
233 node->setPropertyFieldsDirty(0xFFFFFFFF);
234 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
235 return node;
236 }
237
Chris Craik8d1f2122015-11-24 16:40:09 -0800238 /**
239 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
240 * properties and DisplayList moved to the render copies.
241 *
242 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
243 * For this reason, this should generally only be called once on a tree.
244 */
Chris Craik161f54b2015-11-05 11:08:52 -0800245 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
246 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700247 }
Chris Craik0a24b142015-10-19 17:10:19 -0700248
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700249 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
250 syncHierarchyPropertiesAndDisplayList(node);
251 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800252 }
253
Chris Craik0b7e8242015-10-28 16:50:44 -0700254 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700255
256 class TestTask : public renderthread::RenderTask {
257 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700258 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700259 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800260 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700261 RtCallback rtCallback;
262 };
263
264 /**
265 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
266 */
267 static void runOnRenderThread(RtCallback rtCallback) {
268 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700269 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700270 }
John Reck16c9d6a2015-11-17 15:51:08 -0800271
John Reck283bb462018-12-13 16:40:14 -0800272 static void runOnRenderThreadUnmanaged(RtCallback rtCallback) {
273 auto& rt = renderthread::RenderThread::getInstance();
274 rt.queue().runSync([&]() { rtCallback(rt); });
275 }
276
277
John Reck1bcacfd2017-11-03 10:12:19 -0700278 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck283bb462018-12-13 16:40:14 -0800279 static pid_t getRenderThreadTid() { return renderthread::RenderThread::getInstance().getTid(); }
John Reck38e0c322015-11-10 12:19:17 -0800280
John Reck16c9d6a2015-11-17 15:51:08 -0800281 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
282
Mike Reedf6d86ac2019-01-18 14:13:23 -0500283 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700284 float y);
Chris Craika1717272015-11-19 13:02:43 -0800285
Mike Reedf6d86ac2019-01-18 14:13:23 -0500286 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700287 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800288
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400289 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700290
Stan Iliev021693b2016-10-17 16:26:15 -0400291 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
292
Stan Iliev52771272016-11-17 09:54:38 -0500293 static SkRect getClipBounds(const SkCanvas* canvas);
294 static SkRect getLocalClipBounds(const SkCanvas* canvas);
295
John Reck283bb462018-12-13 16:40:14 -0800296 struct CallCounts {
297 int sync = 0;
298 int contextDestroyed = 0;
299 int destroyed = 0;
John Reckfaa1b0a2021-05-13 10:28:38 -0400300 int removeOverlays = 0;
John Reck283bb462018-12-13 16:40:14 -0800301 int glesDraw = 0;
302 };
303
John Reckaa4c9822020-06-25 17:14:13 -0700304 static void expectOnRenderThread(const std::string_view& function = "unknown") {
305 EXPECT_EQ(gettid(), TestUtils::getRenderThreadTid()) << "Called on wrong thread: " << function;
306 }
John Reck283bb462018-12-13 16:40:14 -0800307
308 static WebViewFunctorCallbacks createMockFunctor(RenderMode mode) {
309 auto callbacks = WebViewFunctorCallbacks{
310 .onSync =
Bo Liud6668e72018-12-14 19:37:41 -0800311 [](int functor, void* client_data, const WebViewSyncData& data) {
John Reckaa4c9822020-06-25 17:14:13 -0700312 expectOnRenderThread("onSync");
John Reck283bb462018-12-13 16:40:14 -0800313 sMockFunctorCounts[functor].sync++;
314 },
315 .onContextDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800316 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700317 expectOnRenderThread("onContextDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800318 sMockFunctorCounts[functor].contextDestroyed++;
319 },
320 .onDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800321 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700322 expectOnRenderThread("onDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800323 sMockFunctorCounts[functor].destroyed++;
324 },
John Reckfaa1b0a2021-05-13 10:28:38 -0400325 .removeOverlays =
326 [](int functor, void* data,
327 void (*mergeTransaction)(ASurfaceTransaction*)) {
328 expectOnRenderThread("removeOverlays");
329 sMockFunctorCounts[functor].removeOverlays++;
330 },
John Reck283bb462018-12-13 16:40:14 -0800331 };
332 switch (mode) {
333 case RenderMode::OpenGL_ES:
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500334 callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params,
335 const WebViewOverlayData& overlay_params) {
John Reckaa4c9822020-06-25 17:14:13 -0700336 expectOnRenderThread("draw");
John Reck283bb462018-12-13 16:40:14 -0800337 sMockFunctorCounts[functor].glesDraw++;
338 };
339 break;
340 default:
341 ADD_FAILURE();
342 return WebViewFunctorCallbacks{};
343 }
344 return callbacks;
345 }
346
347 static CallCounts& countsForFunctor(int functor) { return sMockFunctorCounts[functor]; }
348
Chris Craik161f54b2015-11-05 11:08:52 -0800349private:
John Reck283bb462018-12-13 16:40:14 -0800350 static std::unordered_map<int, CallCounts> sMockFunctorCounts;
351
Chris Craik161f54b2015-11-05 11:08:52 -0800352 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800353 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800354 node->syncProperties();
John Reck3afd6372017-01-30 10:15:48 -0800355 if (node->mNeedsDisplayListSync) {
356 node->mNeedsDisplayListSync = false;
357 node->syncDisplayList(observer, nullptr);
358 }
John Reckbe671952021-01-13 22:39:32 -0500359 auto& displayList = node->getDisplayList();
Chris Craik161f54b2015-11-05 11:08:52 -0800360 if (displayList) {
John Reckbe671952021-01-13 22:39:32 -0500361 displayList.updateChildren([](RenderNode* child) {
362 syncHierarchyPropertiesAndDisplayListImpl(child);
363 });
Chris Craik161f54b2015-11-05 11:08:52 -0800364 }
365 }
366
John Reck1bcacfd2017-11-03 10:12:19 -0700367}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700368
369} /* namespace uirenderer */
370} /* namespace android */