Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RecordingOrderTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "tests/Test.h"
9
11#include "include/core/SkFont.h"
17#include "tests/TestUtils.h"
18#include "tools/ToolUtils.h"
20
21using namespace skgpu::graphite;
22
23namespace {
24
25const SkISize kSurfaceSize = { 64, 64 };
26
28
30 Context* context,
31 Recorder* recorder) {
33
34 SkBitmap result0, result1;
35 result0.allocPixels(ii);
36 result1.allocPixels(ii);
37 SkPixmap pm0, pm1;
38
39 SkAssertResult(result0.peekPixels(&pm0));
40 SkAssertResult(result1.peekPixels(&pm1));
41
43 if (!surface) {
44 ERRORF(reporter, "Surface creation failed");
45 return false;
46 }
47
48 SkCanvas* canvas = surface->getCanvas();
49
50 // Set up a recording to clear the surface
51 canvas->clear(kBackgroundColor);
52 std::unique_ptr<Recording> clearRecording = recorder->snap();
53 if (!clearRecording) {
54 ERRORF(reporter, "Recording creation failed");
55 return false;
56 }
57
58 // Draw some text and get recording
60 paint.setAntiAlias(true);
61
63 font.setSubpixel(true);
64 font.setSize(12);
65
66 const char* text0 = "Hamburge";
67 const size_t text0Len = strlen(text0);
68
69 canvas->drawSimpleText(text0, text0Len, SkTextEncoding::kUTF8, 3, 20, font, paint);
70 std::unique_ptr<Recording> text0Recording = recorder->snap();
71
72 // Draw some more text and get recording
73 const char* text1 = "burgefons";
74 const size_t text1Len = strlen(text1);
75
76 canvas->drawSimpleText(text1, text1Len, SkTextEncoding::kUTF8, 3, 40, font, paint);
77 std::unique_ptr<Recording> text1Recording = recorder->snap();
78
79 // Playback 0, then 1, and read pixels
81 info.fRecording = clearRecording.get();
82 context->insertRecording(info);
83 info.fRecording = text0Recording.get();
84 context->insertRecording(info);
85 info.fRecording = text1Recording.get();
86 context->insertRecording(info);
87 context->submit();
88
89 if (!surface->readPixels(pm0, 0, 0)) {
90 ERRORF(reporter, "readPixels failed");
91 return false;
92 }
93
94 // Playback 1, then 0, and read pixels
95 info.fRecording = clearRecording.get();
96 context->insertRecording(info);
97 info.fRecording = text1Recording.get();
98 context->insertRecording(info);
99 info.fRecording = text0Recording.get();
100 context->insertRecording(info);
101 context->submit();
102
103 if (!surface->readPixels(pm1, 0, 0)) {
104 ERRORF(reporter, "readPixels failed");
105 return false;
106 }
107
108 // Compare and contrast
109 float tol = 1.f/256;
110 const float tols[4] = {tol, tol, tol, tol};
111 auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
112 const float diffs[4]) {
113 SkASSERT(x >= 0 && y >= 0);
115 "Error at %d, %d. Diff in floats: (%f, %f, %f, %f)",
116 x, y, diffs[0], diffs[1], diffs[2], diffs[3]);
117 });
118 ComparePixels(pm0, pm1, tols, error);
119
120 return true;
121}
122
123} // anonymous namespace
124
125// This test captures two recordings A and B, plays them back as A then B, and B then A,
126// and verifies that the result is the same.
127DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(RecordingOrderTest_Graphite, reporter, context,
129 std::unique_ptr<Recorder> recorder = context->makeRecorder();
130
131 (void) run_test(reporter, context, recorder.get());
132}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
reporter
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kUTF8
uses bytes to represent UTF-8 or ASCII
bool ComparePixels(const GrCPixmap &a, const GrCPixmap &b, const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
#define DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(name, reporter, graphite_context, ctsEnforcement)
Definition Test.h:377
#define ERRORF(r,...)
Definition Test.h:293
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
bool peekPixels(SkPixmap *pixmap) const
Definition SkBitmap.cpp:635
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
std::unique_ptr< Recorder > makeRecorder(const RecorderOptions &={})
Definition Context.cpp:130
bool submit(SyncToCpu=SyncToCpu::kNo)
Definition Context.cpp:148
bool insertRecording(const InsertRecordingInfo &)
Definition Context.cpp:142
std::unique_ptr< Recording > snap()
Definition Recorder.cpp:149
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
const uint8_t uint32_t uint32_t GError ** error
double y
double x
constexpr SkColor4f kWhite
Definition SkColor.h:439
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
font
Font Metadata and Metrics.
void run_test(skiatest::Reporter *reporter, Context *context, SkISize surfaceSize, SkISize recordingSize, SkISize replayOffset, DrawCallback draw, const std::vector< Expectation > &expectations)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)