blob: 36c5a8c1b3de0658f92368d8bd9714eb9be53c76 [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
53#define INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, functionCall) \
John Reck1bcacfd2017-11-03 10:12:19 -070054 TEST(test_case_name, test_name##_##pipeline) { \
55 RenderPipelineType oldType = Properties::getRenderPipelineType(); \
56 Properties::overrideRenderPipelineType(RenderPipelineType::pipeline); \
57 functionCall; \
58 Properties::overrideRenderPipelineType(oldType); \
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 */
John Reck1bcacfd2017-11-03 10:12:19 -070070#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 }; \
John Reck1bcacfd2017-11-03 10:12:19 -070075 INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaGL); \
Derek Sollenberger0f895392017-04-27 11:30:20 -040076 /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \
John Reck1bcacfd2017-11-03 10:12:19 -070077 /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \
78 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
79 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 */
John Reck1bcacfd2017-11-03 10:12:19 -070084#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); \
Derek Sollenberger0f895392017-04-27 11:30:20 -040090 /* Temporarily disabling Vulkan until we can figure out a way to stub out the driver */ \
John Reck1bcacfd2017-11-03 10:12:19 -070091 /* INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, SkiaVulkan); */ \
92 void test_case_name##_##test_name##_RenderThreadTest::doTheThing( \
93 renderthread::RenderThread& renderThread)
Chris Craik98787e62015-11-13 10:55:30 -080094
Chris Craik37413282016-05-12 17:48:51 -070095/**
96 * Sets a property value temporarily, generally for the duration of a test, restoring the previous
97 * value when going out of scope.
98 *
99 * Can be used e.g. to test behavior only active while Properties::debugOverdraw is enabled.
100 */
101template <typename T>
102class ScopedProperty {
103public:
John Reck1bcacfd2017-11-03 10:12:19 -0700104 ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) {
Chris Craik37413282016-05-12 17:48:51 -0700105 property = newValue;
106 }
John Reck1bcacfd2017-11-03 10:12:19 -0700107 ~ScopedProperty() { *mPropertyPtr = mOldValue; }
108
Chris Craik37413282016-05-12 17:48:51 -0700109private:
110 T* mPropertyPtr;
111 T mOldValue;
112};
113
Chris Craikb565df12015-10-05 13:00:52 -0700114class TestUtils {
115public:
Chris Craik76ace112015-10-29 12:46:19 -0700116 class SignalingDtor {
117 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700118 SignalingDtor() : mSignal(nullptr) {}
119 explicit SignalingDtor(int* signal) : mSignal(signal) {}
120 void setSignal(int* signal) { mSignal = signal; }
Chris Craik76ace112015-10-29 12:46:19 -0700121 ~SignalingDtor() {
122 if (mSignal) {
123 (*mSignal)++;
124 }
125 }
John Reck1bcacfd2017-11-03 10:12:19 -0700126
Chris Craik76ace112015-10-29 12:46:19 -0700127 private:
128 int* mSignal;
129 };
130
John Reck2de950d2017-01-25 10:58:30 -0800131 class MockTreeObserver : public TreeObserver {
132 public:
133 virtual void onMaybeRemovedFromTree(RenderNode* node) {}
134 };
135
Chris Craikb565df12015-10-05 13:00:52 -0700136 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
137 for (int i = 0; i < 16; i++) {
138 if (!MathUtils::areEqual(a[i], b[i])) {
139 return false;
140 }
141 }
142 return true;
143 }
144
sergeyvaed7f582016-10-14 16:30:21 -0700145 static sk_sp<Bitmap> createBitmap(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -0700146 SkColorType colorType = kN32_SkColorType) {
sergeyvaed7f582016-10-14 16:30:21 -0700147 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700148 return Bitmap::allocateHeapBitmap(info);
sergeyvaed7f582016-10-14 16:30:21 -0700149 }
150
151 static sk_sp<Bitmap> createBitmap(int width, int height, SkBitmap* outBitmap) {
152 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
153 outBitmap->setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400154 return Bitmap::allocateHeapBitmap(outBitmap);
sergeyvaed7f582016-10-14 16:30:21 -0700155 }
156
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800157 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -0500158 renderthread::RenderThread& renderThread);
159
160 static sp<DeferredLayerUpdater> createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800161 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -0700162 const SkMatrix& transform);
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800163
John Reck1bcacfd2017-11-03 10:12:19 -0700164 template <class CanvasType>
165 static std::unique_ptr<DisplayList> createDisplayList(
166 int width, int height, std::function<void(CanvasType& canvas)> canvasCallback) {
Chris Craikb565df12015-10-05 13:00:52 -0700167 CanvasType canvas(width, height);
168 canvasCallback(canvas);
Chris Craik003cc3d2015-10-16 10:24:55 -0700169 return std::unique_ptr<DisplayList>(canvas.finishRecording());
Chris Craikb565df12015-10-05 13:00:52 -0700170 }
171
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 Reck2de950d2017-01-25 10:58:30 -0800182 node->setStagingDisplayList(canvas->finishRecording());
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());
208 node.setStagingDisplayList(canvas->finishRecording());
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,
Stan Iliev500a0c32016-10-26 10:30:09 -0400215 const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400216 sp<RenderNode> node = new RenderNode();
217 if (name) {
218 node->setName(name);
219 }
220 RenderProperties& props = node->mutateStagingProperties();
221 props.setLeftTopRightBottom(left, top, right, bottom);
222 if (displayList) {
John Reck2de950d2017-01-25 10:58:30 -0800223 node->setStagingDisplayList(displayList);
Stan Iliev500a0c32016-10-26 10:30:09 -0400224 }
225 if (setup) {
226 std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas(
John Reck1bcacfd2017-11-03 10:12:19 -0700227 new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(),
228 props.getHeight()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400229 setup(props, *canvas.get());
John Reck2de950d2017-01-25 10:58:30 -0800230 node->setStagingDisplayList(canvas->finishRecording());
Stan Iliev500a0c32016-10-26 10:30:09 -0400231 }
232 node->setPropertyFieldsDirty(0xFFFFFFFF);
233 TestUtils::syncHierarchyPropertiesAndDisplayList(node);
234 return node;
235 }
236
Chris Craik8d1f2122015-11-24 16:40:09 -0800237 /**
238 * Forces a sync of a tree of RenderNode, such that every descendant will have its staging
239 * properties and DisplayList moved to the render copies.
240 *
241 * Note: does not check dirtiness bits, so any non-staging DisplayLists will be discarded.
242 * For this reason, this should generally only be called once on a tree.
243 */
Chris Craik161f54b2015-11-05 11:08:52 -0800244 static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) {
245 syncHierarchyPropertiesAndDisplayListImpl(node.get());
Chris Craikb565df12015-10-05 13:00:52 -0700246 }
Chris Craik0a24b142015-10-19 17:10:19 -0700247
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700248 static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) {
249 syncHierarchyPropertiesAndDisplayList(node);
250 return node;
John Reck7db5ffb2016-01-15 13:17:09 -0800251 }
252
Chris Craik0b7e8242015-10-28 16:50:44 -0700253 typedef std::function<void(renderthread::RenderThread& thread)> RtCallback;
Chris Craik0a24b142015-10-19 17:10:19 -0700254
255 class TestTask : public renderthread::RenderTask {
256 public:
John Reck1bcacfd2017-11-03 10:12:19 -0700257 explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {}
Chris Craik0a24b142015-10-19 17:10:19 -0700258 virtual ~TestTask() {}
John Recke5da4ef2016-01-14 12:34:46 -0800259 virtual void run() override;
Chris Craik0a24b142015-10-19 17:10:19 -0700260 RtCallback rtCallback;
261 };
262
263 /**
264 * NOTE: requires surfaceflinger to run, otherwise this method will wait indefinitely.
265 */
266 static void runOnRenderThread(RtCallback rtCallback) {
267 TestTask task(rtCallback);
John Reck1bcacfd2017-11-03 10:12:19 -0700268 renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); });
Chris Craik0a24b142015-10-19 17:10:19 -0700269 }
John Reck16c9d6a2015-11-17 15:51:08 -0800270
John Reck283bb462018-12-13 16:40:14 -0800271 static void runOnRenderThreadUnmanaged(RtCallback rtCallback) {
272 auto& rt = renderthread::RenderThread::getInstance();
273 rt.queue().runSync([&]() { rtCallback(rt); });
274 }
275
276
John Reck1bcacfd2017-11-03 10:12:19 -0700277 static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); }
John Reck283bb462018-12-13 16:40:14 -0800278 static pid_t getRenderThreadTid() { return renderthread::RenderThread::getInstance().getTid(); }
John Reck38e0c322015-11-10 12:19:17 -0800279
John Reck16c9d6a2015-11-17 15:51:08 -0800280 static SkColor interpolateColor(float fraction, SkColor start, SkColor end);
281
Mike Reedf6d86ac2019-01-18 14:13:23 -0500282 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700283 float y);
Chris Craika1717272015-11-19 13:02:43 -0800284
Mike Reedf6d86ac2019-01-18 14:13:23 -0500285 static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700286 const SkPath& path);
Chris Craikd7448e62015-12-15 10:34:36 -0800287
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400288 static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str);
sergeyvdccca442016-03-21 15:38:21 -0700289
Stan Iliev021693b2016-10-17 16:26:15 -0400290 static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y);
291
Stan Iliev52771272016-11-17 09:54:38 -0500292 static SkRect getClipBounds(const SkCanvas* canvas);
293 static SkRect getLocalClipBounds(const SkCanvas* canvas);
294
John Reck283bb462018-12-13 16:40:14 -0800295 struct CallCounts {
296 int sync = 0;
297 int contextDestroyed = 0;
298 int destroyed = 0;
299 int glesDraw = 0;
300 };
301
John Reckaa4c9822020-06-25 17:14:13 -0700302 static void expectOnRenderThread(const std::string_view& function = "unknown") {
303 EXPECT_EQ(gettid(), TestUtils::getRenderThreadTid()) << "Called on wrong thread: " << function;
304 }
John Reck283bb462018-12-13 16:40:14 -0800305
306 static WebViewFunctorCallbacks createMockFunctor(RenderMode mode) {
307 auto callbacks = WebViewFunctorCallbacks{
308 .onSync =
Bo Liud6668e72018-12-14 19:37:41 -0800309 [](int functor, void* client_data, const WebViewSyncData& data) {
John Reckaa4c9822020-06-25 17:14:13 -0700310 expectOnRenderThread("onSync");
John Reck283bb462018-12-13 16:40:14 -0800311 sMockFunctorCounts[functor].sync++;
312 },
313 .onContextDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800314 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700315 expectOnRenderThread("onContextDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800316 sMockFunctorCounts[functor].contextDestroyed++;
317 },
318 .onDestroyed =
Bo Liud6668e72018-12-14 19:37:41 -0800319 [](int functor, void* client_data) {
John Reckaa4c9822020-06-25 17:14:13 -0700320 expectOnRenderThread("onDestroyed");
John Reck283bb462018-12-13 16:40:14 -0800321 sMockFunctorCounts[functor].destroyed++;
322 },
323 };
324 switch (mode) {
325 case RenderMode::OpenGL_ES:
Bo Liud6668e72018-12-14 19:37:41 -0800326 callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params) {
John Reckaa4c9822020-06-25 17:14:13 -0700327 expectOnRenderThread("draw");
John Reck283bb462018-12-13 16:40:14 -0800328 sMockFunctorCounts[functor].glesDraw++;
329 };
330 break;
331 default:
332 ADD_FAILURE();
333 return WebViewFunctorCallbacks{};
334 }
335 return callbacks;
336 }
337
338 static CallCounts& countsForFunctor(int functor) { return sMockFunctorCounts[functor]; }
339
Chris Craik161f54b2015-11-05 11:08:52 -0800340private:
John Reck283bb462018-12-13 16:40:14 -0800341 static std::unordered_map<int, CallCounts> sMockFunctorCounts;
342
Chris Craik161f54b2015-11-05 11:08:52 -0800343 static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) {
John Reck2de950d2017-01-25 10:58:30 -0800344 MarkAndSweepRemoved observer(nullptr);
Chris Craik161f54b2015-11-05 11:08:52 -0800345 node->syncProperties();
John Reck3afd6372017-01-30 10:15:48 -0800346 if (node->mNeedsDisplayListSync) {
347 node->mNeedsDisplayListSync = false;
348 node->syncDisplayList(observer, nullptr);
349 }
Chris Craik161f54b2015-11-05 11:08:52 -0800350 auto displayList = node->getDisplayList();
351 if (displayList) {
John Reck283bb462018-12-13 16:40:14 -0800352 for (auto&& childDr :
353 static_cast<skiapipeline::SkiaDisplayList*>(const_cast<DisplayList*>(displayList))
354 ->mChildNodes) {
John Reckd9d7f122018-05-03 14:40:56 -0700355 syncHierarchyPropertiesAndDisplayListImpl(childDr.getRenderNode());
Chris Craik161f54b2015-11-05 11:08:52 -0800356 }
357 }
358 }
359
John Reck1bcacfd2017-11-03 10:12:19 -0700360}; // class TestUtils
Chris Craikb565df12015-10-05 13:00:52 -0700361
362} /* namespace uirenderer */
363} /* namespace android */