blob: 5092675a8104157fc569a73f6a3aaf9a4efdcf61 [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
John Reck283bb462018-12-13 16:40:14 -080030#include <gtest/gtest.h>
Chris Craikb565df12015-10-05 13:00:52 -070031#include <memory>
Stan Ilievaaa9e832019-09-17 14:07:23 -040032#include <unordered_map>
Chris Craikb565df12015-10-05 13:00:52 -070033
34namespace android {
35namespace uirenderer {
36
John Reck1bcacfd2017-11-03 10:12:19 -070037#define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
Chris Craikb565df12015-10-05 13:00:52 -070038
John Reck1bcacfd2017-11-03 10:12:19 -070039#define EXPECT_RECT_APPROX_EQ(a, b) \
40 EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) && \
41 MathUtils::areEqual((a).top, (b).top) && \
42 MathUtils::areEqual((a).right, (b).right) && \
43 MathUtils::areEqual((a).bottom, (b).bottom));
Chris Craik6fe991e52015-10-20 09:39:42 -070044
John Reck1bcacfd2017-11-03 10:12:19 -070045#define EXPECT_CLIP_RECT(expRect, clipStatePtr) \
46 EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \
47 if ((clipStatePtr)->mode == ClipMode::Rectangle) { \
48 EXPECT_EQ((expRect), reinterpret_cast<const ClipRect*>(clipStatePtr)->rect); \
49 } else { \
50 ADD_FAILURE() << "ClipState not a rect"; \
51 }
Greg Daniel98c78dad2017-01-04 14:45:56 -050052
Alec Mouriaa3e4982020-12-14 14:47:57 -080053#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \
54 TEST(test_case_name, test_name##_##pipeline) { \
55 RenderPipelineType oldType = Properties::getRenderPipelineType(); \
56 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline, true); \
57 functionCall; \
58 Properties::overrideRenderPipelineType(oldType, true); \
Greg Daniel98c78dad2017-01-04 14:45:56 -050059 };
60
Greg Daniel98c78dad2017-01-04 14:45:56 -050061#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \
John Reck1bcacfd2017-11-03 10:12:19 -070062 INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \
63 TestUtils::runOnRenderThread( \
64 test_case_name##_##test_name##_RenderThreadTest::doTheThing))
Greg Daniel98c78dad2017-01-04 14:45:56 -050065
Chris Craik98787e62015-11-13 10:55:30 -080066/**
67 * Like gtest's TEST, but runs on the RenderThread, and 'renderThread' is passed, in top level scope
68 * (for e.g. accessing its RenderState)
69 */
Alec Mouriaa3e4982020-12-14 14:47:57 -080070#define RENDERTHREAD_TEST(test_case_name, test_name) \
71 class test_case_name##_##test_name##_RenderThreadTest { \
72 public: \
73 static void doTheThing(renderthread::RenderThread& renderThread); \
74 }; \
75 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
76 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \
77 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
John Reck1bcacfd2017-11-03 10:12:19 -070078 renderthread::RenderThread& renderThread)
Greg Daniel98c78dad2017-01-04 14:45:56 -050079
80/**
Greg Daniel98c78dad2017-01-04 14:45:56 -050081 * Like RENDERTHREAD_TEST, but only runs with the Skia RenderPipelineTypes
82 */
Alec Mouriaa3e4982020-12-14 14:47:57 -080083#define RENDERTHREAD_SKIA_PIPELINE_TEST(test_case_name, test_name) \
84 class test_case_name##_##test_name##_RenderThreadTest { \
85 public: \
86 static void doTheThing(renderthread::RenderThread& renderThread); \
87 }; \
88 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
89 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); \
90 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
John Reck1bcacfd2017-11-03 10:12:19 -070091 renderthread::RenderThread& renderThread)
Chris Craik98787e62015-11-13 10:55:30 -080092
Chris Craik37413282016-05-12 17:48:51 -070093/**
94 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
95 * value when going out of scope.
96 *
97 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
98 */
99template <typename T>
100class ScopedProperty {
101public:
John Reck1bcacfd2017-11-03 10:12:19 -0700102 ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) {
Chris Craik37413282016-05-12 17:48:51 -0700103 property = newValue;
104 }
John Reck1bcacfd2017-11-03 10:12:19 -0700105 ~ScopedProperty() { *mPropertyPtr = mOldValue; }
106
Chris Craik37413282016-05-12 17:48:51 -0700107private:
108 T* mPropertyPtr;
109 T mOldValue;
110};
111
Chris Craikb565df12015-10-05 13:00:52 -0700112class TestUtils {
113public:
Chris Craik76ace112015-10-29 12:46:19 -0700114 class SignalingDtor {
115 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700116 SignalingDtor() : mSignal(nullptr) {}
117 explicit SignalingDtor(int* signal) : mSignal(signal) {}
118 void setSignal(int* signal) { mSignal = signal; }
Chris Craik76ace112015-10-29 12:46:19 -0700119 ~SignalingDtor() {
120 if (mSignal) {
121 (*mSignal)++;
122 }
123 }
John Reck1bcacfd2017-11-03 10:12:19 -0700124
Chris Craik76ace112015-10-29 12:46:19 -0700125 private:
126 int* mSignal;
127 };
128
John Reck2de950d2017-01-25 10:58:30 -0800129 class MockTreeObserver : public TreeObserver {
130 public:
131 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
132 };
133
Chris Craikb565df12015-10-05 13:00:52 -0700134 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
135 for (int i = 0; i < 16; i++) {
136 if (!MathUtils::areEqual(a[i], b[i])) {
137 return false;
138 }
139 }
140 return true;
141 }
142
sergeyvaed7f582016-10-14 16:30:21 -0700143 static sk_sp<Bitmap> createBitmap(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -0700144 SkColorType colorType = kN32_SkColorType) {
sergeyvaed7f582016-10-14 16:30:21 -0700145 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700146 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700147 }
148
149 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
150 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
151 outBitmap->setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400152 return Bitmap::allocateHeapBitmap(outBitmap);
sergeyvaed7f582016-10-14 16:30:21 -0700153 }
154
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800155 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500156 renderthread::RenderThread& renderThread);
157
158 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800159 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700160 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800161
John Reck1bcacfd2017-11-03 10:12:19 -0700162 static sp<RenderNode> createNode(
163 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400164 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craikb565df12015-10-05 13:00:52 -0700165 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800166 RenderProperties& props = node->mutateStagingProperties();
167 props.setLeftTopRightBottom(left, top, right, bottom);
168 if (setup) {
John Reck1bcacfd2017-11-03 10:12:19 -0700169 std::unique_ptr<Canvas> canvas(
170 Canvas::create_recording_canvas(props.getWidth(), props.getHeight()));
Stan Iliev06152cd2016-07-27 17:55:43 -0400171 setup(props, *canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500172 canvas->finishRecording(node.get());
Stan Iliev06152cd2016-07-27 17:55:43 -0400173 }
174 node->setPropertyFieldsDirty(0xFFFFFFFF);
175 return node;
176 }
177
John Reck1bcacfd2017-11-03 10:12:19 -0700178 template <class RecordingCanvasType>
179 static sp<RenderNode> createNode(
180 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400181 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400182 sp<RenderNode> node = new RenderNode();
183 RenderProperties& props = node->mutateStagingProperties();
184 props.setLeftTopRightBottom(left, top, right, bottom);
185 if (setup) {
186 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800187 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800188 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700189 }
John Reck16c9d6a2015-11-17 15:51:08 -0800190 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700191 return node;
192 }
Chris Craikb565df12015-10-05 13:00:52 -0700193
John Reck1bcacfd2017-11-03 10:12:19 -0700194 static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) {
195 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
John Reck283bb462018-12-13 16:40:14 -0800196 node.stagingProperties().getWidth(), node.stagingProperties().getHeight(), &node));
John Reck1bcacfd2017-11-03 10:12:19 -0700197 contentCallback(*canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500198 canvas->finishRecording(&node);
Chris Craikb565df12015-10-05 13:00:52 -0700199 }
200
John Reck1bcacfd2017-11-03 10:12:19 -0700201 static sp<RenderNode> createSkiaNode(
202 int left, int top, int right, int bottom,
203 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
204 setup,
John Reckbe671952021-01-13 22:39:32 -0500205 const char* name = nullptr,
206 std::unique_ptr<skiapipeline::SkiaDisplayList> displayList = nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400207 sp<RenderNode> node = new RenderNode();
208 if (name) {
209 node->setName(name);
210 }
211 RenderProperties& props = node->mutateStagingProperties();
212 props.setLeftTopRightBottom(left, top, right, bottom);
213 if (displayList) {
John Reckbe671952021-01-13 22:39:32 -0500214 node->setStagingDisplayList(DisplayList(std::move(displayList)));
Stan Iliev500a0c32016-10-26 10:30:09 -0400215 }
216 if (setup) {
217 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700218 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
219 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400220 setup(props, *canvas.get());
John Reck2e48df32021-01-19 18:06:05 -0500221 canvas->finishRecording(node.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400222 }
223 node->setPropertyFieldsDirty(0xFFFFFFFF);
224 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
225 return node;
226 }
227
Chris Craik8d1f2122015-11-24 16:40:09 -0800228 /**
229 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
230 * properties and DisplayList moved to the render copies.
231 *
232 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
233 * For this reason, this should generally only be called once on a tree.
234 */
Chris Craik161f54b2015-11-05 11:08:52 -0800235 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
236 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700237 }
Chris Craik0a24b142015-10-19 17:10:19 -0700238
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700239 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
240 syncHierarchyPropertiesAndDisplayList(node);
241 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800242 }
243
Chris Craik0b7e8242015-10-28 16:50:44 -0700244 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700245
246 class TestTask : public renderthread::RenderTask {
247 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700248 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700249 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800250 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700251 RtCallback rtCallback;
252 };
253
254 /**
255 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
256 */
257 static void runOnRenderThread(RtCallback rtCallback) {
258 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700259 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700260 }
John Reck16c9d6a2015-11-17 15:51:08 -0800261
John Reck283bb462018-12-13 16:40:14 -0800262 static void runOnRenderThreadUnmanaged(RtCallback rtCallback) {
263 auto& rt = renderthread::RenderThread::getInstance();
264 rt.queue().runSync([&]() { rtCallback(rt); });
265 }
266
267
John Reck1bcacfd2017-11-03 10:12:19 -0700268 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck283bb462018-12-13 16:40:14 -0800269 static pid_t getRenderThreadTid() { return renderthread::RenderThread::getInstance().getTid(); }
John Reck38e0c322015-11-10 12:19:17 -0800270
John Reck16c9d6a2015-11-17 15:51:08 -0800271 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
272
Mike Reedf6d86ac2019-01-18 14:13:23 -0500273 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700274 float y);
Chris Craika1717272015-11-19 13:02:43 -0800275
Mike Reedf6d86ac2019-01-18 14:13:23 -0500276 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700277 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800278
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400279 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700280
Stan Iliev021693b2016-10-17 16:26:15 -0400281 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
282
Stan Iliev52771272016-11-17 09:54:38 -0500283 static SkRect getClipBounds(const SkCanvas* canvas);
284 static SkRect getLocalClipBounds(const SkCanvas* canvas);
285
John Reck283bb462018-12-13 16:40:14 -0800286 struct CallCounts {
287 int sync = 0;
288 int contextDestroyed = 0;
289 int destroyed = 0;
John Reckfaa1b0a2021-05-13 10:28:38 -0400290 int removeOverlays = 0;
John Reck283bb462018-12-13 16:40:14 -0800291 int glesDraw = 0;
292 };
293
John Reckaa4c9822020-06-25 17:14:13 -0700294 static void expectOnRenderThread(const std::string_view& function = "unknown") {
295 EXPECT_EQ(gettid(), TestUtils::getRenderThreadTid()) << "Called on wrong thread: " << function;
296 }
John Reck283bb462018-12-13 16:40:14 -0800297
298 static WebViewFunctorCallbacks createMockFunctor(RenderMode mode) {
299 auto callbacks = WebViewFunctorCallbacks{
300 .onSync =
Bo Liud6668e72018-12-14 19:37:41 -0800301 [](int functor, void* client_data, const WebViewSyncData& data) {
John Reckaa4c9822020-06-25 17:14:13 -0700302 expectOnRenderThread("onSync");
John Reck283bb462018-12-13 16:40:14 -0800303 sMockFunctorCounts[functor].sync++;
304 },
305 .onContextDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800306 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700307 expectOnRenderThread("onContextDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800308 sMockFunctorCounts[functor].contextDestroyed++;
309 },
310 .onDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800311 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700312 expectOnRenderThread("onDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800313 sMockFunctorCounts[functor].destroyed++;
314 },
John Reckfaa1b0a2021-05-13 10:28:38 -0400315 .removeOverlays =
316 [](int functor, void* data,
317 void (*mergeTransaction)(ASurfaceTransaction*)) {
318 expectOnRenderThread("removeOverlays");
319 sMockFunctorCounts[functor].removeOverlays++;
320 },
John Reck283bb462018-12-13 16:40:14 -0800321 };
322 switch (mode) {
323 case RenderMode::OpenGL_ES:
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500324 callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params,
325 const WebViewOverlayData& overlay_params) {
John Reckaa4c9822020-06-25 17:14:13 -0700326 expectOnRenderThread("draw");
John Reck283bb462018-12-13 16:40:14 -0800327 sMockFunctorCounts[functor].glesDraw++;
328 };
329 break;
330 default:
331 ADD_FAILURE();
332 return WebViewFunctorCallbacks{};
333 }
334 return callbacks;
335 }
336
337 static CallCounts& countsForFunctor(int functor) { return sMockFunctorCounts[functor]; }
338
Chris Craik161f54b2015-11-05 11:08:52 -0800339private:
John Reck283bb462018-12-13 16:40:14 -0800340 static std::unordered_map<int, CallCounts> sMockFunctorCounts;
341
Chris Craik161f54b2015-11-05 11:08:52 -0800342 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800343 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800344 node->syncProperties();
John Reck3afd6372017-01-30 10:15:48 -0800345 if (node->mNeedsDisplayListSync) {
346 node->mNeedsDisplayListSync = false;
347 node->syncDisplayList(observer, nullptr);
348 }
John Reckbe671952021-01-13 22:39:32 -0500349 auto& displayList = node->getDisplayList();
Chris Craik161f54b2015-11-05 11:08:52 -0800350 if (displayList) {
John Reckbe671952021-01-13 22:39:32 -0500351 displayList.updateChildren([](RenderNode* child) {
352 syncHierarchyPropertiesAndDisplayListImpl(child);
353 });
Chris Craik161f54b2015-11-05 11:08:52 -0800354 }
355 }
356
John Reck1bcacfd2017-11-03 10:12:19 -0700357}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700358
359} /* namespace uirenderer */
360} /* namespace android */