blob: d3e6e8fd36196aed922cb06ed5e258afc893c6dd [file] [log] [blame]
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001/*
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 intializers to work
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040011
12#if SK_SUPPORT_GPU && defined(SK_VULKAN)
13
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/vk/GrVkVulkan.h"
Greg Daniel54bfb182018-11-20 17:12:36 -050015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkDrawable.h"
17#include "include/core/SkSurface.h"
18#include "include/gpu/GrBackendDrawableInfo.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040019#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrContextPriv.h"
21#include "src/gpu/vk/GrVkGpu.h"
22#include "src/gpu/vk/GrVkInterface.h"
23#include "src/gpu/vk/GrVkMemory.h"
24#include "src/gpu/vk/GrVkSecondaryCBDrawContext.h"
25#include "src/gpu/vk/GrVkUtil.h"
26#include "tests/Test.h"
27#include "tools/gpu/GrContextFactory.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040028
29using sk_gpu_test::GrContextFactory;
30
31static const int DEV_W = 16, DEV_H = 16;
32
33class TestDrawable : public SkDrawable {
34public:
Greg Daniel070cbaf2019-01-03 17:35:54 -050035 TestDrawable(const GrVkInterface* interface, GrContext* context, int32_t width, int32_t height)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040036 : INHERITED()
37 , fInterface(interface)
Greg Daniel070cbaf2019-01-03 17:35:54 -050038 , fContext(context)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040039 , fWidth(width)
40 , fHeight(height) {}
41
42 ~TestDrawable() override {}
43
Greg Daniel070cbaf2019-01-03 17:35:54 -050044 class DrawHandlerBasic : public GpuDrawHandler {
Greg Daniel64cc9aa2018-10-19 13:54:56 -040045 public:
Greg Daniel070cbaf2019-01-03 17:35:54 -050046 DrawHandlerBasic(const GrVkInterface* interface, int32_t width, int32_t height)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040047 : INHERITED()
48 , fInterface(interface)
49 , fWidth(width)
50 , fHeight(height) {}
Greg Daniel070cbaf2019-01-03 17:35:54 -050051 ~DrawHandlerBasic() override {}
Greg Daniel64cc9aa2018-10-19 13:54:56 -040052
53 void draw(const GrBackendDrawableInfo& info) override {
54 GrVkDrawableInfo vkInfo;
55 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
56
57 // Clear to Red
58 VkClearColorValue vkColor;
59 vkColor.float32[0] = 1.0f; // r
60 vkColor.float32[1] = 0.0f; // g
61 vkColor.float32[2] = 0.0f; // b
62 vkColor.float32[3] = 1.0f; // a
63
64 // Clear right half of render target
65 VkClearRect clearRect;
66 clearRect.rect.offset = { fWidth / 2, 0 };
67 clearRect.rect.extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
68 clearRect.baseArrayLayer = 0;
69 clearRect.layerCount = 1;
70
71 VkClearAttachment attachment;
72 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Greg Danielb353eeb2018-12-05 11:01:58 -050073 attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
Greg Daniel64cc9aa2018-10-19 13:54:56 -040074 attachment.clearValue.color = vkColor;
75
76 GR_VK_CALL(fInterface, CmdClearAttachments(vkInfo.fSecondaryCommandBuffer,
77 1,
78 &attachment,
79 1,
80 &clearRect));
81 vkInfo.fDrawBounds->offset = { fWidth / 2, 0 };
82 vkInfo.fDrawBounds->extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
83 }
84 private:
85 const GrVkInterface* fInterface;
86 int32_t fWidth;
87 int32_t fHeight;
88
89 typedef GpuDrawHandler INHERITED;
90 };
91
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -050092 typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&,
93 const SkImageInfo&, const GrVkDrawableInfo&);
Greg Daniel070cbaf2019-01-03 17:35:54 -050094 typedef void (*SubmitProc)(TestDrawable*);
95
96 // Exercises the exporting of a secondary command buffer from one GrContext and then importing
97 // it into a second GrContext. We then draw to the secondary command buffer from the second
98 // GrContext.
99 class DrawHandlerImport : public GpuDrawHandler {
100 public:
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500101 DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc,
102 const SkMatrix& matrix,
103 const SkIRect& clipBounds,
104 const SkImageInfo& bufferInfo)
Greg Daniel070cbaf2019-01-03 17:35:54 -0500105 : INHERITED()
106 , fTestDrawable(td)
107 , fDrawProc(drawProc)
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500108 , fSubmitProc(submitProc)
109 , fMatrix(matrix)
110 , fClipBounds(clipBounds)
111 , fBufferInfo(bufferInfo) {}
Greg Daniel070cbaf2019-01-03 17:35:54 -0500112 ~DrawHandlerImport() override {
113 fSubmitProc(fTestDrawable);
114 }
115
116 void draw(const GrBackendDrawableInfo& info) override {
117 GrVkDrawableInfo vkInfo;
118 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
119
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500120 fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500121 }
122 private:
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500123 TestDrawable* fTestDrawable;
124 DrawProc fDrawProc;
125 SubmitProc fSubmitProc;
126 const SkMatrix fMatrix;
127 const SkIRect fClipBounds;
128 const SkImageInfo fBufferInfo;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500129
130 typedef GpuDrawHandler INHERITED;
131 };
132
133 // Helper function to test drawing to a secondary command buffer that we imported into the
134 // GrContext using a GrVkSecondaryCBDrawContext.
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500135 static void ImportDraw(TestDrawable* td, const SkMatrix& matrix, const SkIRect& clipBounds,
136 const SkImageInfo& bufferInfo, const GrVkDrawableInfo& info) {
137 td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fContext, bufferInfo, info, nullptr);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500138 if (!td->fDrawContext) {
139 return;
140 }
141
142 SkCanvas* canvas = td->fDrawContext->getCanvas();
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500143 canvas->clipRect(SkRect::Make(clipBounds));
144 canvas->setMatrix(matrix);
145
Greg Daniel070cbaf2019-01-03 17:35:54 -0500146 SkIRect rect = SkIRect::MakeXYWH(td->fWidth/2, 0, td->fWidth/4, td->fHeight);
147 SkPaint paint;
148 paint.setColor(SK_ColorRED);
149 canvas->drawIRect(rect, paint);
150
151 // Draw to an offscreen target so that we end up with a mix of "real" secondary command
152 // buffers and the imported secondary command buffer.
153 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(td->fContext, SkBudgeted::kYes,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500154 bufferInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500155 surf->getCanvas()->clear(SK_ColorRED);
156
157 SkRect dstRect = SkRect::MakeXYWH(3*td->fWidth/4, 0, td->fWidth/4, td->fHeight);
158 SkIRect srcRect = SkIRect::MakeWH(td->fWidth/4, td->fHeight);
159 canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, &paint);
160
161 td->fDrawContext->flush();
162 }
163
164 // Helper function to test waiting for the imported secondary command buffer to be submitted on
165 // its original context and then cleaning up the GrVkSecondaryCBDrawContext from this GrContext.
166 static void ImportSubmitted(TestDrawable* td) {
167 // Typical use case here would be to create a fence that we submit to the gpu and then wait
168 // on before releasing the GrVkSecondaryCBDrawContext resources. To simulate that for this
169 // test (and since we are running single threaded anyways), we will just force a sync of
170 // the gpu and cpu here.
Robert Phillips9da87e02019-02-04 13:26:26 -0500171 td->fContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500172
173 td->fDrawContext->releaseResources();
174 // We release the GrContext here manually to test that we waited long enough before
175 // releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
176 // the GrContext it used to imported the secondary command buffer. If we had released the
177 // GrContext's resources earlier (before waiting on the gpu above), we would get vulkan
178 // validation layer errors saying we freed some vulkan objects while they were still in use
179 // on the GPU.
180 td->fContext->releaseResourcesAndAbandonContext();
181 }
182
183
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400184 std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
Greg Danielb2a259c2018-12-17 10:28:47 -0500185 const SkMatrix& matrix,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500186 const SkIRect& clipBounds,
187 const SkImageInfo& bufferInfo) override {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400188 if (backendApi != GrBackendApi::kVulkan) {
189 return nullptr;
190 }
Greg Daniel070cbaf2019-01-03 17:35:54 -0500191 std::unique_ptr<GpuDrawHandler> draw;
192 if (fContext) {
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500193 draw.reset(new DrawHandlerImport(this, ImportDraw, ImportSubmitted, matrix,
194 clipBounds, bufferInfo));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500195 } else {
196 draw.reset(new DrawHandlerBasic(fInterface, fWidth, fHeight));
197 }
198 return draw;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400199 }
200
201 SkRect onGetBounds() override {
202 return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
203 }
204
205 void onDraw(SkCanvas*) override {
206 SkASSERT(false);
207 }
208
209private:
210 const GrVkInterface* fInterface;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500211 GrContext* fContext;
212 sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
213 int32_t fWidth;
214 int32_t fHeight;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400215
216 typedef SkDrawable INHERITED;
217};
218
Greg Daniel070cbaf2019-01-03 17:35:54 -0500219void draw_drawable_test(skiatest::Reporter* reporter, GrContext* context, GrContext* childContext) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500220 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->priv().getGpu());
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400221
222 const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
223 kPremul_SkAlphaType);
224 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
225 ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
226 SkCanvas* canvas = surface->getCanvas();
227 canvas->clear(SK_ColorBLUE);
228
Greg Daniel070cbaf2019-01-03 17:35:54 -0500229 sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childContext, DEV_W, DEV_H));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400230 canvas->drawDrawable(drawable.get());
231
232 SkPaint paint;
233 paint.setColor(SK_ColorGREEN);
234 SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H);
235 canvas->drawIRect(rect, paint);
236
237 // read pixels
238 SkBitmap bitmap;
239 bitmap.allocPixels(ii);
240 canvas->readPixels(bitmap, 0, 0);
241
242 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
243 bool failureFound = false;
244 SkPMColor expectedPixel;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500245 for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
246 for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400247 SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
248 if (cy < DEV_H / 2) {
249 if (cx < DEV_W / 2) {
250 expectedPixel = 0xFFFF0000; // Blue
251 } else {
252 expectedPixel = 0xFF0000FF; // Red
253 }
254 } else {
255 expectedPixel = 0xFF00FF00; // Green
256 }
257 if (expectedPixel != canvasPixel) {
258 failureFound = true;
259 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
260 cx, cy, canvasPixel, expectedPixel);
261 }
262 }
263 }
264}
265
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400266DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400267 draw_drawable_test(reporter, ctxInfo.directContext(), nullptr);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500268}
269
270DEF_GPUTEST(VkDrawableImportTest, reporter, options) {
271 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
272 sk_gpu_test::GrContextFactory::ContextType contextType =
273 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
274 if (contextType != sk_gpu_test::GrContextFactory::kVulkan_ContextType) {
275 continue;
276 }
277 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600278 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500279 skiatest::ReporterContext ctx(
280 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
Robert Phillips6d344c32020-07-06 10:56:46 -0400281 if (ctxInfo.directContext()) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500282 sk_gpu_test::ContextInfo child =
Robert Phillips00f78de2020-07-01 16:09:43 -0400283 factory.getSharedContextInfo(ctxInfo.directContext(), 0);
Robert Phillips6d344c32020-07-06 10:56:46 -0400284 if (!child.directContext()) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500285 continue;
286 }
287
Robert Phillips6d344c32020-07-06 10:56:46 -0400288 draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext());
Greg Daniel070cbaf2019-01-03 17:35:54 -0500289 }
290 }
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400291}
292
293#endif