blob: 662b322ef629dd84c075723f04ee0c020bdcf76b [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 Reedac9f0c92020-12-23 10:11:33 -050016#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkDrawable.h"
18#include "include/core/SkSurface.h"
19#include "include/gpu/GrBackendDrawableInfo.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040020#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/vk/GrVkGpu.h"
23#include "src/gpu/vk/GrVkInterface.h"
24#include "src/gpu/vk/GrVkMemory.h"
25#include "src/gpu/vk/GrVkSecondaryCBDrawContext.h"
26#include "src/gpu/vk/GrVkUtil.h"
27#include "tests/Test.h"
28#include "tools/gpu/GrContextFactory.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040029
30using sk_gpu_test::GrContextFactory;
31
32static const int DEV_W = 16, DEV_H = 16;
33
34class TestDrawable : public SkDrawable {
35public:
Robert Phillipsb25e0652020-07-22 09:35:49 -040036 TestDrawable(const GrVkInterface* interface, GrDirectContext* dContext,
37 int32_t width, int32_t height)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040038 : INHERITED()
39 , fInterface(interface)
Robert Phillipsb25e0652020-07-22 09:35:49 -040040 , fDContext(dContext)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040041 , fWidth(width)
42 , fHeight(height) {}
43
44 ~TestDrawable() override {}
45
Greg Daniel070cbaf2019-01-03 17:35:54 -050046 class DrawHandlerBasic : public GpuDrawHandler {
Greg Daniel64cc9aa2018-10-19 13:54:56 -040047 public:
Greg Daniel070cbaf2019-01-03 17:35:54 -050048 DrawHandlerBasic(const GrVkInterface* interface, int32_t width, int32_t height)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040049 : INHERITED()
50 , fInterface(interface)
51 , fWidth(width)
52 , fHeight(height) {}
Greg Daniel070cbaf2019-01-03 17:35:54 -050053 ~DrawHandlerBasic() override {}
Greg Daniel64cc9aa2018-10-19 13:54:56 -040054
55 void draw(const GrBackendDrawableInfo& info) override {
56 GrVkDrawableInfo vkInfo;
57 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
58
59 // Clear to Red
60 VkClearColorValue vkColor;
61 vkColor.float32[0] = 1.0f; // r
62 vkColor.float32[1] = 0.0f; // g
63 vkColor.float32[2] = 0.0f; // b
64 vkColor.float32[3] = 1.0f; // a
65
66 // Clear right half of render target
67 VkClearRect clearRect;
68 clearRect.rect.offset = { fWidth / 2, 0 };
69 clearRect.rect.extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
70 clearRect.baseArrayLayer = 0;
71 clearRect.layerCount = 1;
72
73 VkClearAttachment attachment;
74 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Greg Danielb353eeb2018-12-05 11:01:58 -050075 attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
Greg Daniel64cc9aa2018-10-19 13:54:56 -040076 attachment.clearValue.color = vkColor;
77
78 GR_VK_CALL(fInterface, CmdClearAttachments(vkInfo.fSecondaryCommandBuffer,
79 1,
80 &attachment,
81 1,
82 &clearRect));
83 vkInfo.fDrawBounds->offset = { fWidth / 2, 0 };
84 vkInfo.fDrawBounds->extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
85 }
86 private:
87 const GrVkInterface* fInterface;
88 int32_t fWidth;
89 int32_t fHeight;
90
John Stiles7571f9e2020-09-02 22:42:33 -040091 using INHERITED = GpuDrawHandler;
Greg Daniel64cc9aa2018-10-19 13:54:56 -040092 };
93
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -050094 typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&,
95 const SkImageInfo&, const GrVkDrawableInfo&);
Greg Daniel070cbaf2019-01-03 17:35:54 -050096 typedef void (*SubmitProc)(TestDrawable*);
97
Robert Phillipse94b4e12020-07-23 13:54:35 -040098 // Exercises the exporting of a secondary command buffer from one context and then importing
99 // it into a second context. We then draw to the secondary command buffer from the second
100 // context.
Greg Daniel070cbaf2019-01-03 17:35:54 -0500101 class DrawHandlerImport : public GpuDrawHandler {
102 public:
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500103 DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc,
104 const SkMatrix& matrix,
105 const SkIRect& clipBounds,
106 const SkImageInfo& bufferInfo)
Greg Daniel070cbaf2019-01-03 17:35:54 -0500107 : INHERITED()
108 , fTestDrawable(td)
109 , fDrawProc(drawProc)
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500110 , fSubmitProc(submitProc)
111 , fMatrix(matrix)
112 , fClipBounds(clipBounds)
113 , fBufferInfo(bufferInfo) {}
Greg Daniel070cbaf2019-01-03 17:35:54 -0500114 ~DrawHandlerImport() override {
115 fSubmitProc(fTestDrawable);
116 }
117
118 void draw(const GrBackendDrawableInfo& info) override {
119 GrVkDrawableInfo vkInfo;
120 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
121
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500122 fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500123 }
124 private:
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500125 TestDrawable* fTestDrawable;
126 DrawProc fDrawProc;
127 SubmitProc fSubmitProc;
128 const SkMatrix fMatrix;
129 const SkIRect fClipBounds;
130 const SkImageInfo fBufferInfo;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500131
John Stiles7571f9e2020-09-02 22:42:33 -0400132 using INHERITED = GpuDrawHandler;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500133 };
134
135 // Helper function to test drawing to a secondary command buffer that we imported into the
Robert Phillipse94b4e12020-07-23 13:54:35 -0400136 // context using a GrVkSecondaryCBDrawContext.
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500137 static void ImportDraw(TestDrawable* td, const SkMatrix& matrix, const SkIRect& clipBounds,
138 const SkImageInfo& bufferInfo, const GrVkDrawableInfo& info) {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400139 td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fDContext, bufferInfo,
140 info, nullptr);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500141 if (!td->fDrawContext) {
142 return;
143 }
144
145 SkCanvas* canvas = td->fDrawContext->getCanvas();
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500146 canvas->clipRect(SkRect::Make(clipBounds));
147 canvas->setMatrix(matrix);
148
Greg Daniel070cbaf2019-01-03 17:35:54 -0500149 SkIRect rect = SkIRect::MakeXYWH(td->fWidth/2, 0, td->fWidth/4, td->fHeight);
150 SkPaint paint;
151 paint.setColor(SK_ColorRED);
152 canvas->drawIRect(rect, paint);
153
154 // Draw to an offscreen target so that we end up with a mix of "real" secondary command
155 // buffers and the imported secondary command buffer.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400156 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(td->fDContext, SkBudgeted::kYes,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500157 bufferInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500158 surf->getCanvas()->clear(SK_ColorRED);
159
160 SkRect dstRect = SkRect::MakeXYWH(3*td->fWidth/4, 0, td->fWidth/4, td->fHeight);
Mike Reed0ab283a2021-01-29 17:25:20 +0000161 SkIRect srcRect = SkIRect::MakeWH(td->fWidth/4, td->fHeight);
162 canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, &paint);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500163
164 td->fDrawContext->flush();
165 }
166
167 // Helper function to test waiting for the imported secondary command buffer to be submitted on
Robert Phillipse94b4e12020-07-23 13:54:35 -0400168 // its original context and then cleaning up the GrVkSecondaryCBDrawContext from this context.
Greg Daniel070cbaf2019-01-03 17:35:54 -0500169 static void ImportSubmitted(TestDrawable* td) {
170 // Typical use case here would be to create a fence that we submit to the gpu and then wait
171 // on before releasing the GrVkSecondaryCBDrawContext resources. To simulate that for this
172 // test (and since we are running single threaded anyways), we will just force a sync of
173 // the gpu and cpu here.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400174 td->fDContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500175
176 td->fDrawContext->releaseResources();
Robert Phillipse94b4e12020-07-23 13:54:35 -0400177 // We release the context here manually to test that we waited long enough before
Greg Daniel070cbaf2019-01-03 17:35:54 -0500178 // releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
Robert Phillipse94b4e12020-07-23 13:54:35 -0400179 // the context it used to imported the secondary command buffer. If we had released the
180 // context's resources earlier (before waiting on the gpu above), we would get vulkan
Greg Daniel070cbaf2019-01-03 17:35:54 -0500181 // validation layer errors saying we freed some vulkan objects while they were still in use
182 // on the GPU.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400183 td->fDContext->releaseResourcesAndAbandonContext();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500184 }
185
186
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400187 std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
Greg Danielb2a259c2018-12-17 10:28:47 -0500188 const SkMatrix& matrix,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500189 const SkIRect& clipBounds,
190 const SkImageInfo& bufferInfo) override {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400191 if (backendApi != GrBackendApi::kVulkan) {
192 return nullptr;
193 }
Greg Daniel070cbaf2019-01-03 17:35:54 -0500194 std::unique_ptr<GpuDrawHandler> draw;
Robert Phillipsb25e0652020-07-22 09:35:49 -0400195 if (fDContext) {
John Stilesfbd050b2020-08-03 13:21:46 -0400196 draw = std::make_unique<DrawHandlerImport>(this, ImportDraw, ImportSubmitted, matrix,
197 clipBounds, bufferInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500198 } else {
John Stilesfbd050b2020-08-03 13:21:46 -0400199 draw = std::make_unique<DrawHandlerBasic>(fInterface, fWidth, fHeight);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500200 }
201 return draw;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400202 }
203
204 SkRect onGetBounds() override {
205 return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
206 }
207
208 void onDraw(SkCanvas*) override {
209 SkASSERT(false);
210 }
211
212private:
213 const GrVkInterface* fInterface;
Robert Phillipsb25e0652020-07-22 09:35:49 -0400214 GrDirectContext* fDContext;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500215 sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
216 int32_t fWidth;
217 int32_t fHeight;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400218
John Stiles7571f9e2020-09-02 22:42:33 -0400219 using INHERITED = SkDrawable;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400220};
221
Robert Phillipsb25e0652020-07-22 09:35:49 -0400222void draw_drawable_test(skiatest::Reporter* reporter,
223 GrDirectContext* dContext,
224 GrDirectContext* childDContext) {
225 GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400226
227 const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
228 kPremul_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400229 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo,
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400230 ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
231 SkCanvas* canvas = surface->getCanvas();
232 canvas->clear(SK_ColorBLUE);
233
Robert Phillipsb25e0652020-07-22 09:35:49 -0400234 sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childDContext, DEV_W, DEV_H));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400235 canvas->drawDrawable(drawable.get());
236
237 SkPaint paint;
238 paint.setColor(SK_ColorGREEN);
239 SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H);
240 canvas->drawIRect(rect, paint);
241
242 // read pixels
243 SkBitmap bitmap;
244 bitmap.allocPixels(ii);
245 canvas->readPixels(bitmap, 0, 0);
246
247 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
248 bool failureFound = false;
249 SkPMColor expectedPixel;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500250 for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
251 for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400252 SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
253 if (cy < DEV_H / 2) {
254 if (cx < DEV_W / 2) {
255 expectedPixel = 0xFFFF0000; // Blue
256 } else {
257 expectedPixel = 0xFF0000FF; // Red
258 }
259 } else {
260 expectedPixel = 0xFF00FF00; // Green
261 }
262 if (expectedPixel != canvasPixel) {
263 failureFound = true;
264 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
265 cx, cy, canvasPixel, expectedPixel);
266 }
267 }
268 }
269}
270
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400271DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400272 draw_drawable_test(reporter, ctxInfo.directContext(), nullptr);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500273}
274
275DEF_GPUTEST(VkDrawableImportTest, reporter, options) {
276 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
277 sk_gpu_test::GrContextFactory::ContextType contextType =
278 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
279 if (contextType != sk_gpu_test::GrContextFactory::kVulkan_ContextType) {
280 continue;
281 }
282 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600283 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500284 skiatest::ReporterContext ctx(
285 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
Robert Phillips6d344c32020-07-06 10:56:46 -0400286 if (ctxInfo.directContext()) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500287 sk_gpu_test::ContextInfo child =
Robert Phillips00f78de2020-07-01 16:09:43 -0400288 factory.getSharedContextInfo(ctxInfo.directContext(), 0);
Robert Phillips6d344c32020-07-06 10:56:46 -0400289 if (!child.directContext()) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500290 continue;
291 }
292
Robert Phillips6d344c32020-07-06 10:56:46 -0400293 draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext());
Greg Daniel070cbaf2019-01-03 17:35:54 -0500294 }
295 }
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400296}
297
298#endif