Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RecordingSurfacesTest.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
19
20namespace skgpu::graphite {
21
22using DrawCallback = std::function<void(SkCanvas*)>;
23
24struct Expectation {
25 int fX;
26 int fY;
28};
29
31 Context* context,
32 SkISize surfaceSize,
33 SkISize recordingSize,
34 SkISize replayOffset,
36 const std::vector<Expectation>& expectations) {
37 const SkImageInfo surfaceImageInfo = SkImageInfo::Make(
39
40 std::unique_ptr<Recorder> surfaceRecorder = context->makeRecorder();
41 sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(surfaceRecorder.get(), surfaceImageInfo);
42 Surface* graphiteSurface = static_cast<Surface*>(surface.get());
43 const TextureInfo& textureInfo = graphiteSurface->backingTextureProxy()->textureInfo();
44
45 // Flush the initial clear added by MakeGraphite.
46 std::unique_ptr<skgpu::graphite::Recording> surfaceRecording = surfaceRecorder->snap();
47 context->insertRecording({surfaceRecording.get()});
48
49 // Snap a recording without a bound target.
50 const SkImageInfo recordingImageInfo = surfaceImageInfo.makeDimensions(recordingSize);
51 std::unique_ptr<Recorder> recorder = context->makeRecorder();
52 SkCanvas* canvas = recorder->makeDeferredCanvas(recordingImageInfo, textureInfo);
53 draw(canvas);
54
55 // Can't make another canvas before snapping.
57 recorder->makeDeferredCanvas(recordingImageInfo, textureInfo) == nullptr);
58 std::unique_ptr<Recording> recording = recorder->snap();
59
60 // Play back recording.
61 context->insertRecording(
62 {recording.get(), surface.get(), {replayOffset.fWidth, replayOffset.fHeight}});
63
64 // Read pixels.
66 SkPixmap pixmap;
67 bitmap.allocPixels(surfaceImageInfo);
68 SkAssertResult(bitmap.peekPixels(&pixmap));
69 if (!surface->readPixels(pixmap, 0, 0)) {
70 ERRORF(reporter, "readPixels failed");
71 return;
72 }
73
74 // Veryify expectations are met and recording is uninstantiated.
75 REPORTER_ASSERT(reporter, !recording->priv().isTargetProxyInstantiated());
76 for (const Expectation& e : expectations) {
77 SkColor4f color = pixmap.getColor4f(e.fX, e.fY);
78#ifdef SK_DEBUG
79 if (color != e.fColor) {
80 SkDebugf("Wrong color\n\texpected: %f %f %f %f\n\tactual: %f %f %f %f",
81 e.fColor.fR,
82 e.fColor.fG,
83 e.fColor.fB,
84 e.fColor.fA,
85 color.fR,
86 color.fG,
87 color.fB,
88 color.fA);
89 }
90#endif
91 REPORTER_ASSERT(reporter, color == e.fColor);
92 }
93}
94
95// Tests that clear does not clear an entire replayed-to surface if recorded onto a smaller surface.
96DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(RecordingSurfacesTestClear, reporter, context,
98 SkISize surfaceSize = SkISize::Make(8, 4);
99 SkISize recordingSize = SkISize::Make(4, 4);
100 SkISize replayOffset = SkISize::Make(0, 0);
101
102 auto draw = [](SkCanvas* canvas) { canvas->clear(SkColors::kRed); };
103
104 std::vector<Expectation> expectations = {{0, 0, SkColors::kRed},
105 {4, 0, SkColors::kTransparent}};
106
107 run_test(reporter, context, surfaceSize, recordingSize, replayOffset, draw, expectations);
108}
109
110// Tests that writePixels is translated correctly when replayed with an offset.
111DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(RecordingSurfacesTestWritePixels, reporter, context,
114 bitmap.allocN32Pixels(4, 4, true);
115 SkCanvas bitmapCanvas(bitmap);
117 paint.setColor(SkColors::kRed);
118 bitmapCanvas.drawIRect(SkIRect::MakeXYWH(0, 0, 4, 4), paint);
119
120 SkISize surfaceSize = SkISize::Make(8, 4);
121 SkISize recordingSize = SkISize::Make(4, 4);
122 SkISize replayOffset = SkISize::Make(4, 0);
123
124 auto draw = [&bitmap](SkCanvas* canvas) { canvas->writePixels(bitmap, 0, 0); };
125
126 std::vector<Expectation> expectations = {{0, 0, SkColors::kTransparent},
127 {4, 0, SkColors::kRed}};
128
129 run_test(reporter, context, surfaceSize, recordingSize, replayOffset, draw, expectations);
130}
131
132// Tests that the result of writePixels is cropped correctly when offscreen.
133DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(RecordingSurfacesTestWritePixelsOffscreen, reporter, context,
136 bitmap.allocN32Pixels(4, 4, true);
137 SkCanvas bitmapCanvas(bitmap);
139 paint.setColor(SkColors::kRed);
140 bitmapCanvas.drawIRect(SkIRect::MakeXYWH(0, 0, 4, 4), paint);
141 paint.setColor(SkColors::kGreen);
142 bitmapCanvas.drawIRect(SkIRect::MakeXYWH(2, 2, 2, 2), paint);
143
144 SkISize surfaceSize = SkISize::Make(4, 4);
145 SkISize recordingSize = SkISize::Make(4, 4);
146 SkISize replayOffset = SkISize::Make(-2, -2);
147
148 auto draw = [&bitmap](SkCanvas* canvas) { canvas->writePixels(bitmap, 0, 0); };
149
150 std::vector<Expectation> expectations = {{0, 0, SkColors::kGreen}};
151
152 run_test(reporter, context, surfaceSize, recordingSize, replayOffset, draw, expectations);
153}
154
155} // namespace skgpu::graphite
reporter
SkColor4f color
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(name, reporter, graphite_ctx, ctsEnforcement)
Definition Test.h:373
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Definition SkCanvas.h:1358
SkColor4f getColor4f(int x, int y) const
Definition SkPixmap.cpp:388
std::unique_ptr< Recorder > makeRecorder(const RecorderOptions &={})
Definition Context.cpp:130
bool insertRecording(const InsertRecordingInfo &)
Definition Context.cpp:142
TextureProxy * backingTextureProxy() const
const TextureInfo & textureInfo() const
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
constexpr SkColor4f kGreen
Definition SkColor.h:441
constexpr SkColor4f kRed
Definition SkColor.h:440
constexpr SkColor4f kTransparent
Definition SkColor.h:434
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)
std::function< void(SkCanvas *)> DrawCallback
void run_test(skiatest::Reporter *reporter, Context *context, SkISize surfaceSize, SkISize recordingSize, SkISize replayOffset, DrawCallback draw, const std::vector< Expectation > &expectations)
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
SkImageInfo makeDimensions(SkISize newSize) const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)