blob: 771c3452bf84676243d61f33dbeb22baaf1d746e [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 template <class CanvasType>
163 static std::unique_ptr<DisplayList> createDisplayList(
164 int width, int height, std::function<void(CanvasType& canvas)> canvasCallback) {
Chris Craikb565df12015-10-05 13:00:52 -0700165 CanvasType canvas(width, height);
166 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700167 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700168 }
169
John Reck1bcacfd2017-11-03 10:12:19 -0700170 static sp<RenderNode> createNode(
171 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400172 std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
Chris Craikb565df12015-10-05 13:00:52 -0700173 sp<RenderNode> node = new RenderNode();
John Reck16c9d6a2015-11-17 15:51:08 -0800174 RenderProperties& props = node->mutateStagingProperties();
175 props.setLeftTopRightBottom(left, top, right, bottom);
176 if (setup) {
John Reck1bcacfd2017-11-03 10:12:19 -0700177 std::unique_ptr<Canvas> canvas(
178 Canvas::create_recording_canvas(props.getWidth(), props.getHeight()));
Stan Iliev06152cd2016-07-27 17:55:43 -0400179 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800180 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev06152cd2016-07-27 17:55:43 -0400181 }
182 node->setPropertyFieldsDirty(0xFFFFFFFF);
183 return node;
184 }
185
John Reck1bcacfd2017-11-03 10:12:19 -0700186 template <class RecordingCanvasType>
187 static sp<RenderNode> createNode(
188 int left, int top, int right, int bottom,
Stan Iliev06152cd2016-07-27 17:55:43 -0400189 std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
Stan Iliev06152cd2016-07-27 17:55:43 -0400190 sp<RenderNode> node = new RenderNode();
191 RenderProperties& props = node->mutateStagingProperties();
192 props.setLeftTopRightBottom(left, top, right, bottom);
193 if (setup) {
194 RecordingCanvasType canvas(props.getWidth(), props.getHeight());
John Reck16c9d6a2015-11-17 15:51:08 -0800195 setup(props, canvas);
John Reck2de950d2017-01-25 10:58:30 -0800196 node->setStagingDisplayList(canvas.finishRecording());
Chris Craik0b7e8242015-10-28 16:50:44 -0700197 }
John Reck16c9d6a2015-11-17 15:51:08 -0800198 node->setPropertyFieldsDirty(0xFFFFFFFF);
Chris Craik0b7e8242015-10-28 16:50:44 -0700199 return node;
200 }
Chris Craikb565df12015-10-05 13:00:52 -0700201
John Reck1bcacfd2017-11-03 10:12:19 -0700202 static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) {
203 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
John Reck283bb462018-12-13 16:40:14 -0800204 node.stagingProperties().getWidth(), node.stagingProperties().getHeight(), &node));
John Reck1bcacfd2017-11-03 10:12:19 -0700205 contentCallback(*canvas.get());
206 node.setStagingDisplayList(canvas->finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700207 }
208
John Reck1bcacfd2017-11-03 10:12:19 -0700209 static sp<RenderNode> createSkiaNode(
210 int left, int top, int right, int bottom,
211 std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
212 setup,
Stan Iliev500a0c32016-10-26 10:30:09 -0400213 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400214 sp<RenderNode> node = new RenderNode();
215 if (name) {
216 node->setName(name);
217 }
218 RenderProperties& props = node->mutateStagingProperties();
219 props.setLeftTopRightBottom(left, top, right, bottom);
220 if (displayList) {
John Reck2de950d2017-01-25 10:58:30 -0800221 node->setStagingDisplayList(displayList);
Stan Iliev500a0c32016-10-26 10:30:09 -0400222 }
223 if (setup) {
224 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700225 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
226 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400227 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800228 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev500a0c32016-10-26 10:30:09 -0400229 }
230 node->setPropertyFieldsDirty(0xFFFFFFFF);
231 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
232 return node;
233 }
234
Chris Craik8d1f2122015-11-24 16:40:09 -0800235 /**
236 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
237 * properties and DisplayList moved to the render copies.
238 *
239 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
240 * For this reason, this should generally only be called once on a tree.
241 */
Chris Craik161f54b2015-11-05 11:08:52 -0800242 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
243 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700244 }
Chris Craik0a24b142015-10-19 17:10:19 -0700245
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700246 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
247 syncHierarchyPropertiesAndDisplayList(node);
248 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800249 }
250
Chris Craik0b7e8242015-10-28 16:50:44 -0700251 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700252
253 class TestTask : public renderthread::RenderTask {
254 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700255 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700256 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800257 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700258 RtCallback rtCallback;
259 };
260
261 /**
262 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
263 */
264 static void runOnRenderThread(RtCallback rtCallback) {
265 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700266 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700267 }
John Reck16c9d6a2015-11-17 15:51:08 -0800268
John Reck283bb462018-12-13 16:40:14 -0800269 static void runOnRenderThreadUnmanaged(RtCallback rtCallback) {
270 auto& rt = renderthread::RenderThread::getInstance();
271 rt.queue().runSync([&]() { rtCallback(rt); });
272 }
273
274
John Reck1bcacfd2017-11-03 10:12:19 -0700275 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck283bb462018-12-13 16:40:14 -0800276 static pid_t getRenderThreadTid() { return renderthread::RenderThread::getInstance().getTid(); }
John Reck38e0c322015-11-10 12:19:17 -0800277
John Reck16c9d6a2015-11-17 15:51:08 -0800278 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
279
Mike Reedf6d86ac2019-01-18 14:13:23 -0500280 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700281 float y);
Chris Craika1717272015-11-19 13:02:43 -0800282
Mike Reedf6d86ac2019-01-18 14:13:23 -0500283 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700284 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800285
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400286 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700287
Stan Iliev021693b2016-10-17 16:26:15 -0400288 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
289
Stan Iliev52771272016-11-17 09:54:38 -0500290 static SkRect getClipBounds(const SkCanvas* canvas);
291 static SkRect getLocalClipBounds(const SkCanvas* canvas);
292
John Reck283bb462018-12-13 16:40:14 -0800293 struct CallCounts {
294 int sync = 0;
295 int contextDestroyed = 0;
296 int destroyed = 0;
297 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 },
321 };
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 }
Chris Craik161f54b2015-11-05 11:08:52 -0800349 auto displayList = node->getDisplayList();
350 if (displayList) {
John Reck283bb462018-12-13 16:40:14 -0800351 for (auto&& childDr :
352 static_cast<skiapipeline::SkiaDisplayList*>(const_cast<DisplayList*>(displayList))
353 ->mChildNodes) {
John Reckd9d7f122018-05-03 14:40:56 -0700354 syncHierarchyPropertiesAndDisplayListImpl(childDr.getRenderNode());
Chris Craik161f54b2015-11-05 11:08:52 -0800355 }
356 }
357 }
358
John Reck1bcacfd2017-11-03 10:12:19 -0700359}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700360
361} /* namespace uirenderer */
362} /* namespace android */