| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include <AutoBackendTextureRelease.h> |
| 20 |
#include <DisplayList.h> |
| 21 |
#include <Matrix.h> |
| 22 |
#include <Properties.h> |
| 23 |
#include <Rect.h> |
| 24 |
#include <RenderNode.h> |
| 25 |
#include <hwui/Bitmap.h> |
| 26 |
#include <pipeline/skia/SkiaRecordingCanvas.h> |
| 27 |
#include <private/hwui/DrawGlInfo.h> |
| 28 |
#include <renderstate/RenderState.h> |
| 29 |
#include <renderthread/RenderThread.h> |
| 30 |
|
| 31 |
#include <gtest/gtest.h> |
| 32 |
#include <memory> |
| 33 |
#include <unordered_map> |
| 34 |
|
| 35 |
namespace android { |
| 36 |
namespace uirenderer { |
| 37 |
|
| 38 |
#define EXPECT_MATRIX_APPROX_EQ(a, b) EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) |
| 39 |
|
| 40 |
#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)); |
| 45 |
|
| 46 |
#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 |
} |
| 53 |
|
| 54 |
#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); \ |
| 60 |
}; |
| 61 |
|
| 62 |
#define INNER_PIPELINE_RENDERTHREAD_TEST(test_case_name, test_name, pipeline) \ |
| 63 |
INNER_PIPELINE_TEST(test_case_name, test_name, pipeline, \ |
| 64 |
TestUtils::runOnRenderThread( \ |
| 65 |
test_case_name##_##test_name##_RenderThreadTest::doTheThing)) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#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( \ |
| 79 |
renderthread::RenderThread& renderThread) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#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( \ |
| 92 |
renderthread::RenderThread& renderThread) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
template <typename T> |
| 101 |
class ScopedProperty { |
| 102 |
public: |
| 103 |
ScopedProperty(T& property, T newValue) : mPropertyPtr(&property), mOldValue(property) { |
| 104 |
property = newValue; |
| 105 |
} |
| 106 |
~ScopedProperty() { *mPropertyPtr = mOldValue; } |
| 107 |
|
| 108 |
private: |
| 109 |
T* mPropertyPtr; |
| 110 |
T mOldValue; |
| 111 |
}; |
| 112 |
|
| 113 |
class TestUtils { |
| 114 |
public: |
| 115 |
class SignalingDtor { |
| 116 |
public: |
| 117 |
SignalingDtor() : mSignal(nullptr) {} |
| 118 |
explicit SignalingDtor(int* signal) : mSignal(signal) {} |
| 119 |
void setSignal(int* signal) { mSignal = signal; } |
| 120 |
~SignalingDtor() { |
| 121 |
if (mSignal) { |
| 122 |
(*mSignal)++; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
private: |
| 127 |
int* mSignal; |
| 128 |
}; |
| 129 |
|
| 130 |
class MockTreeObserver : public TreeObserver { |
| 131 |
public: |
| 132 |
virtual void onMaybeRemovedFromTree(RenderNode* node) {} |
| 133 |
}; |
| 134 |
|
| 135 |
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 |
|
| 144 |
static sk_sp<Bitmap> createBitmap(int width, int height, |
| 145 |
SkColorType colorType = kN32_SkColorType) { |
| 146 |
SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType); |
| 147 |
return Bitmap::allocateHeapBitmap(info); |
| 148 |
} |
| 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); |
| 153 |
return Bitmap::allocateHeapBitmap(outBitmap); |
| 154 |
} |
| 155 |
|
| 156 |
static sp<DeferredLayerUpdater> createTextureLayerUpdater( |
| 157 |
renderthread::RenderThread& renderThread); |
| 158 |
|
| 159 |
static sp<DeferredLayerUpdater> createTextureLayerUpdater( |
| 160 |
renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
| 161 |
const SkMatrix& transform); |
| 162 |
|
| 163 |
static sp<RenderNode> createNode( |
| 164 |
int left, int top, int right, int bottom, |
| 165 |
std::function<void(RenderProperties& props, Canvas& canvas)> setup) { |
| 166 |
sp<RenderNode> node = new RenderNode(); |
| 167 |
RenderProperties& props = node->mutateStagingProperties(); |
| 168 |
props.setLeftTopRightBottom(left, top, right, bottom); |
| 169 |
if (setup) { |
| 170 |
std::unique_ptr<Canvas> canvas( |
| 171 |
Canvas::create_recording_canvas(props.getWidth(), props.getHeight())); |
| 172 |
setup(props, *canvas.get()); |
| 173 |
canvas->finishRecording(node.get()); |
| 174 |
} |
| 175 |
node->setPropertyFieldsDirty(0xFFFFFFFF); |
| 176 |
return node; |
| 177 |
} |
| 178 |
|
| 179 |
template <class RecordingCanvasType> |
| 180 |
static sp<RenderNode> createNode( |
| 181 |
int left, int top, int right, int bottom, |
| 182 |
std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) { |
| 183 |
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()); |
| 188 |
setup(props, canvas); |
| 189 |
node->setStagingDisplayList(canvas.finishRecording()); |
| 190 |
} |
| 191 |
node->setPropertyFieldsDirty(0xFFFFFFFF); |
| 192 |
return node; |
| 193 |
} |
| 194 |
|
| 195 |
static void recordNode(RenderNode& node, std::function<void(Canvas&)> contentCallback) { |
| 196 |
std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas( |
| 197 |
node.stagingProperties().getWidth(), node.stagingProperties().getHeight(), &node)); |
| 198 |
contentCallback(*canvas.get()); |
| 199 |
canvas->finishRecording(&node); |
| 200 |
} |
| 201 |
|
| 202 |
static sp<RenderNode> createSkiaNode( |
| 203 |
int left, int top, int right, int bottom, |
| 204 |
std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)> |
| 205 |
setup, |
| 206 |
const char* name = nullptr, |
| 207 |
std::unique_ptr<skiapipeline::SkiaDisplayList> displayList = nullptr) { |
| 208 |
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) { |
| 215 |
node->setStagingDisplayList(DisplayList(std::move(displayList))); |
| 216 |
} |
| 217 |
if (setup) { |
| 218 |
std::unique_ptr<skiapipeline::SkiaRecordingCanvas> canvas( |
| 219 |
new skiapipeline::SkiaRecordingCanvas(nullptr, props.getWidth(), |
| 220 |
props.getHeight())); |
| 221 |
setup(props, *canvas.get()); |
| 222 |
canvas->finishRecording(node.get()); |
| 223 |
} |
| 224 |
node->setPropertyFieldsDirty(0xFFFFFFFF); |
| 225 |
TestUtils::syncHierarchyPropertiesAndDisplayList(node); |
| 226 |
return node; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
static void syncHierarchyPropertiesAndDisplayList(sp<RenderNode>& node) { |
| 237 |
syncHierarchyPropertiesAndDisplayListImpl(node.get()); |
| 238 |
} |
| 239 |
|
| 240 |
static sp<RenderNode>& getSyncedNode(sp<RenderNode>& node) { |
| 241 |
syncHierarchyPropertiesAndDisplayList(node); |
| 242 |
return node; |
| 243 |
} |
| 244 |
|
| 245 |
typedef std::function<void(renderthread::RenderThread& thread)> RtCallback; |
| 246 |
|
| 247 |
class TestTask : public renderthread::RenderTask { |
| 248 |
public: |
| 249 |
explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {} |
| 250 |
virtual ~TestTask() {} |
| 251 |
virtual void run() override; |
| 252 |
RtCallback rtCallback; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static void runOnRenderThread(RtCallback rtCallback) { |
| 259 |
TestTask task(rtCallback); |
| 260 |
renderthread::RenderThread::getInstance().queue().runSync([&]() { task.run(); }); |
| 261 |
} |
| 262 |
|
| 263 |
static void runOnRenderThreadUnmanaged(RtCallback rtCallback) { |
| 264 |
auto& rt = renderthread::RenderThread::getInstance(); |
| 265 |
rt.queue().runSync([&]() { rtCallback(rt); }); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
static bool isRenderThreadRunning() { return renderthread::RenderThread::hasInstance(); } |
| 270 |
static pid_t getRenderThreadTid() { return renderthread::RenderThread::getInstance().getTid(); } |
| 271 |
|
| 272 |
static SkColor interpolateColor(float fraction, SkColor start, SkColor end); |
| 273 |
|
| 274 |
static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x, |
| 275 |
float y); |
| 276 |
|
| 277 |
static void drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, |
| 278 |
const SkPath& path); |
| 279 |
|
| 280 |
static std::unique_ptr<uint16_t[]> asciiToUtf16(const char* str); |
| 281 |
|
| 282 |
static SkColor getColor(const sk_sp<SkSurface>& surface, int x, int y); |
| 283 |
|
| 284 |
static SkRect getClipBounds(const SkCanvas* canvas); |
| 285 |
static SkRect getLocalClipBounds(const SkCanvas* canvas); |
| 286 |
|
| 287 |
static int getUsageCount(const AutoBackendTextureRelease* textureRelease) { |
| 288 |
EXPECT_NE(nullptr, textureRelease); |
| 289 |
return textureRelease->mUsageCount; |
| 290 |
} |
| 291 |
|
| 292 |
struct CallCounts { |
| 293 |
int sync = 0; |
| 294 |
int contextDestroyed = 0; |
| 295 |
int destroyed = 0; |
| 296 |
int removeOverlays = 0; |
| 297 |
int glesDraw = 0; |
| 298 |
}; |
| 299 |
|
| 300 |
static void expectOnRenderThread(const std::string_view& function = "unknown") { |
| 301 |
EXPECT_EQ(gettid(), TestUtils::getRenderThreadTid()) << "Called on wrong thread: " << function; |
| 302 |
} |
| 303 |
|
| 304 |
static WebViewFunctorCallbacks createMockFunctor(RenderMode mode) { |
| 305 |
auto callbacks = WebViewFunctorCallbacks{ |
| 306 |
.onSync = |
| 307 |
[](int functor, void* client_data, const WebViewSyncData& data) { |
| 308 |
expectOnRenderThread("onSync"); |
| 309 |
sMockFunctorCounts[functor].sync++; |
| 310 |
}, |
| 311 |
.onContextDestroyed = |
| 312 |
[](int functor, void* client_data) { |
| 313 |
expectOnRenderThread("onContextDestroyed"); |
| 314 |
sMockFunctorCounts[functor].contextDestroyed++; |
| 315 |
}, |
| 316 |
.onDestroyed = |
| 317 |
[](int functor, void* client_data) { |
| 318 |
expectOnRenderThread("onDestroyed"); |
| 319 |
sMockFunctorCounts[functor].destroyed++; |
| 320 |
}, |
| 321 |
.removeOverlays = |
| 322 |
[](int functor, void* data, |
| 323 |
void (*mergeTransaction)(ASurfaceTransaction*)) { |
| 324 |
expectOnRenderThread("removeOverlays"); |
| 325 |
sMockFunctorCounts[functor].removeOverlays++; |
| 326 |
}, |
| 327 |
}; |
| 328 |
switch (mode) { |
| 329 |
case RenderMode::OpenGL_ES: |
| 330 |
callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params, |
| 331 |
const WebViewOverlayData& overlay_params) { |
| 332 |
expectOnRenderThread("draw"); |
| 333 |
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 |
|
| 345 |
private: |
| 346 |
static std::unordered_map<int, CallCounts> sMockFunctorCounts; |
| 347 |
|
| 348 |
static void syncHierarchyPropertiesAndDisplayListImpl(RenderNode* node) { |
| 349 |
MarkAndSweepRemoved observer(nullptr); |
| 350 |
node->syncProperties(); |
| 351 |
if (node->mNeedsDisplayListSync) { |
| 352 |
node->mNeedsDisplayListSync = false; |
| 353 |
node->syncDisplayList(observer, nullptr); |
| 354 |
} |
| 355 |
auto& displayList = node->getDisplayList(); |
| 356 |
if (displayList) { |
| 357 |
displayList.updateChildren([](RenderNode* child) { |
| 358 |
syncHierarchyPropertiesAndDisplayListImpl(child); |
| 359 |
}); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
}; |
| 364 |
|
| 365 |
} |
| 366 |
} |