Flutter Engine
The Flutter Engine
VkDrawableTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 Google Inc.
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// This is a GPU-backend specific test. It relies on static initializers to work
9
11
12#if defined(SK_GANESH) && defined(SK_VULKAN)
22#include "include/core/SkRect.h"
30#include "include/gpu/GrTypes.h"
39#include "tests/Test.h"
41
42#include <vulkan/vulkan_core.h>
43#include <cstdint>
44#include <memory>
45
46namespace skgpu { struct VulkanInterface; }
47
49
50static const int DEV_W = 16, DEV_H = 16;
51
52class TestDrawable : public SkDrawable {
53public:
54 TestDrawable(const skgpu::VulkanInterface* interface, GrDirectContext* dContext,
55 int32_t width, int32_t height)
56 : INHERITED()
57 , fInterface(interface)
58 , fDContext(dContext)
59 , fWidth(width)
60 , fHeight(height) {}
61
62 ~TestDrawable() override {}
63
64 class DrawHandlerBasic : public GpuDrawHandler {
65 public:
66 DrawHandlerBasic(const skgpu::VulkanInterface* interface, int32_t width, int32_t height)
67 : INHERITED()
68 , fInterface(interface)
69 , fWidth(width)
70 , fHeight(height) {}
71 ~DrawHandlerBasic() override {}
72
73 void draw(const GrBackendDrawableInfo& info) override {
74 GrVkDrawableInfo vkInfo;
75 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
76
77 // Clear to Red
78 VkClearColorValue vkColor;
79 vkColor.float32[0] = 1.0f; // r
80 vkColor.float32[1] = 0.0f; // g
81 vkColor.float32[2] = 0.0f; // b
82 vkColor.float32[3] = 1.0f; // a
83
84 // Clear right half of render target
85 VkClearRect clearRect;
86 clearRect.rect.offset = { fWidth / 2, 0 };
87 clearRect.rect.extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
88 clearRect.baseArrayLayer = 0;
89 clearRect.layerCount = 1;
90
91 VkClearAttachment attachment;
93 attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
94 attachment.clearValue.color = vkColor;
95
96 GR_VK_CALL(fInterface, CmdClearAttachments(vkInfo.fSecondaryCommandBuffer,
97 1,
98 &attachment,
99 1,
100 &clearRect));
101 vkInfo.fDrawBounds->offset = { fWidth / 2, 0 };
102 vkInfo.fDrawBounds->extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
103 }
104 private:
105 const skgpu::VulkanInterface* fInterface;
106 int32_t fWidth;
107 int32_t fHeight;
108
109 using INHERITED = GpuDrawHandler;
110 };
111
112 typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&,
113 const SkImageInfo&, const GrVkDrawableInfo&);
114 typedef void (*SubmitProc)(TestDrawable*);
115
116 // Exercises the exporting of a secondary command buffer from one context and then importing
117 // it into a second context. We then draw to the secondary command buffer from the second
118 // context.
119 class DrawHandlerImport : public GpuDrawHandler {
120 public:
121 DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc,
122 const SkMatrix& matrix,
123 const SkIRect& clipBounds,
124 const SkImageInfo& bufferInfo)
125 : INHERITED()
126 , fTestDrawable(td)
127 , fDrawProc(drawProc)
128 , fSubmitProc(submitProc)
129 , fMatrix(matrix)
130 , fClipBounds(clipBounds)
131 , fBufferInfo(bufferInfo) {}
132 ~DrawHandlerImport() override {
133 fSubmitProc(fTestDrawable);
134 }
135
136 void draw(const GrBackendDrawableInfo& info) override {
137 GrVkDrawableInfo vkInfo;
138 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
139
140 fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo);
141 }
142 private:
143 TestDrawable* fTestDrawable;
144 DrawProc fDrawProc;
145 SubmitProc fSubmitProc;
146 const SkMatrix fMatrix;
147 const SkIRect fClipBounds;
148 const SkImageInfo fBufferInfo;
149
150 using INHERITED = GpuDrawHandler;
151 };
152
153 // Helper function to test drawing to a secondary command buffer that we imported into the
154 // context using a GrVkSecondaryCBDrawContext.
155 static void ImportDraw(TestDrawable* td, const SkMatrix& matrix, const SkIRect& clipBounds,
156 const SkImageInfo& bufferInfo, const GrVkDrawableInfo& info) {
157 td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fDContext, bufferInfo,
158 info, nullptr);
159 if (!td->fDrawContext) {
160 return;
161 }
162
163 SkCanvas* canvas = td->fDrawContext->getCanvas();
164 canvas->clipRect(SkRect::Make(clipBounds));
165 canvas->setMatrix(matrix);
166
167 SkIRect rect = SkIRect::MakeXYWH(td->fWidth/2, 0, td->fWidth/4, td->fHeight);
169 paint.setColor(SK_ColorRED);
170 canvas->drawIRect(rect, paint);
171
172 // Draw to an offscreen target so that we end up with a mix of "real" secondary command
173 // buffers and the imported secondary command buffer. Also we do two separate offscreen
174 // draws to test that we don't share scratch textures. The GrResourceAllocator would think
175 // that the two offscreens can share a texture here since we draw them to the SCB before
176 // drawing the 2nd offscreen. However, since the SCB ends up not being submitted the GPU
177 // immediately we need to make sure to not share offscreen textures or else the second will
178 // overwrite the first. This test makes sure this non sharing logic is workng correctly.
179 sk_sp<SkSurface> surf =
180 SkSurfaces::RenderTarget(td->fDContext, skgpu::Budgeted::kYes, bufferInfo);
181 surf->getCanvas()->clear(SK_ColorBLUE);
182
183 SkRect dstRect = SkRect::MakeXYWH(td->fWidth/4, 0, td->fWidth/4, td->fHeight);
184 SkRect srcRect = SkRect::MakeIWH(td->fWidth/4, td->fHeight);
185 canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, SkSamplingOptions(),
187
188 surf = SkSurfaces::RenderTarget(td->fDContext, skgpu::Budgeted::kYes, bufferInfo);
189 surf->getCanvas()->clear(SK_ColorRED);
190
191 dstRect = SkRect::MakeXYWH(3*td->fWidth/4, 0, td->fWidth/4, td->fHeight);
192 canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, SkSamplingOptions(),
194
195 surf.reset();
196
197 td->fDrawContext->flush();
198 }
199
200 // Helper function to test waiting for the imported secondary command buffer to be submitted on
201 // its original context and then cleaning up the GrVkSecondaryCBDrawContext from this context.
202 static void ImportSubmitted(TestDrawable* td) {
203 // Typical use case here would be to create a fence that we submit to the gpu and then wait
204 // on before releasing the GrVkSecondaryCBDrawContext resources. To simulate that for this
205 // test (and since we are running single threaded anyways), we will just force a sync of
206 // the gpu and cpu here.
207 td->fDContext->submit(GrSyncCpu::kYes);
208
209 td->fDrawContext->releaseResources();
210 // We release the context here manually to test that we waited long enough before
211 // releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
212 // the context it used to imported the secondary command buffer. If we had released the
213 // context's resources earlier (before waiting on the gpu above), we would get vulkan
214 // validation layer errors saying we freed some vulkan objects while they were still in use
215 // on the GPU.
216 td->fDContext->releaseResourcesAndAbandonContext();
217 }
218
219
220 std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
221 const SkMatrix& matrix,
222 const SkIRect& clipBounds,
223 const SkImageInfo& bufferInfo) override {
224 if (backendApi != GrBackendApi::kVulkan) {
225 return nullptr;
226 }
227 std::unique_ptr<GpuDrawHandler> draw;
228 if (fDContext) {
229 draw = std::make_unique<DrawHandlerImport>(this, ImportDraw, ImportSubmitted, matrix,
230 clipBounds, bufferInfo);
231 } else {
232 draw = std::make_unique<DrawHandlerBasic>(fInterface, fWidth, fHeight);
233 }
234 return draw;
235 }
236
237 SkRect onGetBounds() override {
238 return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
239 }
240
241 void onDraw(SkCanvas*) override {
242 SkASSERT(false);
243 }
244
245private:
246 const skgpu::VulkanInterface* fInterface;
249 int32_t fWidth;
250 int32_t fHeight;
251
252 using INHERITED = SkDrawable;
253};
254
255void draw_drawable_test(skiatest::Reporter* reporter,
256 GrDirectContext* dContext,
257 GrDirectContext* childDContext) {
258 GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
259
263 dContext, skgpu::Budgeted::kNo, ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
264 SkCanvas* canvas = surface->getCanvas();
265 canvas->clear(SK_ColorBLUE);
266
267 sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childDContext, DEV_W, DEV_H));
268 canvas->drawDrawable(drawable.get());
269
271 paint.setColor(SK_ColorGREEN);
273 canvas->drawIRect(rect, paint);
274
275 // read pixels
277 bitmap.allocPixels(ii);
278 canvas->readPixels(bitmap, 0, 0);
279
280 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
281 bool failureFound = false;
282 SkPMColor expectedPixel;
283 for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
284 for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
285 SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
286 if (cy < DEV_H / 2) {
287 if (cx < DEV_W / 2) {
288 expectedPixel = 0xFFFF0000; // Blue
289 } else {
290 expectedPixel = 0xFF0000FF; // Red
291 }
292 } else {
293 expectedPixel = 0xFF00FF00; // Green
294 }
295 if (expectedPixel != canvasPixel) {
296 failureFound = true;
297 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
298 cx, cy, canvasPixel, expectedPixel);
299 }
300 }
301 }
302}
303
305 draw_drawable_test(reporter, ctxInfo.directContext(), nullptr);
306}
307
309 for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) {
310 skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt);
311 if (contextType != skgpu::ContextType::kVulkan) {
312 continue;
313 }
314 GrContextOptions childOptions = options;
315 // Part of our testing of secondary command buffers here is that we don't recycle scratch
316 // textures that are drawing into the SCB. The reason being that the SCB gets played back
317 // later and thus logically gets reordered to the end. This can mess up our resource
318 // allocator which thinks it is safe to reuse things. To test this behavior we need to make
319 // sure we aren't pre-emptively reordering draws.
321 sk_gpu_test::GrContextFactory factory(childOptions);
322 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
324 if (ctxInfo.directContext()) {
326 factory.getSharedContextInfo(ctxInfo.directContext(), 0);
327 if (!child.directContext()) {
328 continue;
329 }
330
331 draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext());
332 }
333 }
334}
335
336#endif
const char *(* DrawProc)(const BezierRec *, int)
Definition: BezierBench.cpp:21
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkMatrix fMatrix
Definition: FillRRectOp.cpp:74
static const int DEV_H
static const int DEV_W
reporter
Definition: FontMgrTest.cpp:39
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
GrBackendApi
Definition: GrTypes.h:95
#define GR_VK_CALL(IFACE, X)
Definition: GrVkUtil.h:24
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#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
uint32_t SkPMColor
Definition: SkColor.h:205
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
GrDirectContext * fDContext
#define DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(name, reporter, context_info, ctsEnforcement)
Definition: Test.h:458
#define DEF_GANESH_TEST(name, reporter, options, ctsEnforcement)
Definition: Test.h:393
#define ERRORF(r,...)
Definition: Test.h:293
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
GrDirectContextPriv priv()
const skgpu::VulkanInterface * vkInterface() const
Definition: GrVkGpu.h:60
static sk_sp< GrVkSecondaryCBDrawContext > Make(GrRecordingContext *, const SkImageInfo &, const GrVkDrawableInfo &, const SkSurfaceProps *props)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
Definition: SkCanvas.cpp:2574
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1542
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Definition: SkCanvas.h:1358
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
void setMatrix(const SkM44 &matrix)
Definition: SkCanvas.cpp:1349
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY)
Definition: SkCanvas.cpp:382
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
GrDirectContext * directContext() const
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
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)
Definition: bitmap.py:1
Definition: GpuTools.h:21
static const int kContextTypeCount
Definition: ContextType.h:42
ContextType
Definition: ContextType.h:19
@ kVulkan
ANGLE on Metal ES 3 context.
const char * ContextTypeName(skgpu::ContextType type)
Definition: ContextType.cpp:13
int32_t height
int32_t width
Enable fReduceOpsTaskSplitting
VkRect2D * fDrawBounds
Definition: GrVkTypes.h:89
uint32_t fColorAttachmentIndex
Definition: GrVkTypes.h:86
VkCommandBuffer fSecondaryCommandBuffer
Definition: GrVkTypes.h:85
Definition: SkRect.h:32
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
VkImageAspectFlags aspectMask
Definition: vulkan_core.h:3936
VkClearValue clearValue
Definition: vulkan_core.h:3938
uint32_t colorAttachment
Definition: vulkan_core.h:3937
VkRect2D rect
Definition: vulkan_core.h:3942
uint32_t layerCount
Definition: vulkan_core.h:3944
uint32_t baseArrayLayer
Definition: vulkan_core.h:3943
VkExtent2D extent
Definition: vulkan_core.h:2881
VkOffset2D offset
Definition: vulkan_core.h:2880
VkClearColorValue color
Definition: vulkan_core.h:3931
@ VK_IMAGE_ASPECT_COLOR_BIT
Definition: vulkan_core.h:2238