blob: 154c089f95016285185dce168ae1066c86701636 [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 Reed99da77a2021-01-29 12:27:49 -0500161 SkRect srcRect = SkRect::MakeIWH(td->fWidth/4, td->fHeight);
162 canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, SkSamplingOptions(),
163 &paint, SkCanvas::kStrict_SrcRectConstraint);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500164
165 td->fDrawContext->flush();
166 }
167
168 // Helper function to test waiting for the imported secondary command buffer to be submitted on
Robert Phillipse94b4e12020-07-23 13:54:35 -0400169 // its original context and then cleaning up the GrVkSecondaryCBDrawContext from this context.
Greg Daniel070cbaf2019-01-03 17:35:54 -0500170 static void ImportSubmitted(TestDrawable* td) {
171 // Typical use case here would be to create a fence that we submit to the gpu and then wait
172 // on before releasing the GrVkSecondaryCBDrawContext resources. To simulate that for this
173 // test (and since we are running single threaded anyways), we will just force a sync of
174 // the gpu and cpu here.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400175 td->fDContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500176
177 td->fDrawContext->releaseResources();
Robert Phillipse94b4e12020-07-23 13:54:35 -0400178 // We release the context here manually to test that we waited long enough before
Greg Daniel070cbaf2019-01-03 17:35:54 -0500179 // releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
Robert Phillipse94b4e12020-07-23 13:54:35 -0400180 // the context it used to imported the secondary command buffer. If we had released the
181 // context's resources earlier (before waiting on the gpu above), we would get vulkan
Greg Daniel070cbaf2019-01-03 17:35:54 -0500182 // validation layer errors saying we freed some vulkan objects while they were still in use
183 // on the GPU.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400184 td->fDContext->releaseResourcesAndAbandonContext();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500185 }
186
187
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400188 std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
Greg Danielb2a259c2018-12-17 10:28:47 -0500189 const SkMatrix& matrix,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500190 const SkIRect& clipBounds,
191 const SkImageInfo& bufferInfo) override {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400192 if (backendApi != GrBackendApi::kVulkan) {
193 return nullptr;
194 }
Greg Daniel070cbaf2019-01-03 17:35:54 -0500195 std::unique_ptr<GpuDrawHandler> draw;
Robert Phillipsb25e0652020-07-22 09:35:49 -0400196 if (fDContext) {
John Stilesfbd050b2020-08-03 13:21:46 -0400197 draw = std::make_unique<DrawHandlerImport>(this, ImportDraw, ImportSubmitted, matrix,
198 clipBounds, bufferInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500199 } else {
John Stilesfbd050b2020-08-03 13:21:46 -0400200 draw = std::make_unique<DrawHandlerBasic>(fInterface, fWidth, fHeight);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500201 }
202 return draw;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400203 }
204
205 SkRect onGetBounds() override {
206 return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
207 }
208
209 void onDraw(SkCanvas*) override {
210 SkASSERT(false);
211 }
212
213private:
214 const GrVkInterface* fInterface;
Robert Phillipsb25e0652020-07-22 09:35:49 -0400215 GrDirectContext* fDContext;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500216 sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
217 int32_t fWidth;
218 int32_t fHeight;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400219
John Stiles7571f9e2020-09-02 22:42:33 -0400220 using INHERITED = SkDrawable;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400221};
222
Robert Phillipsb25e0652020-07-22 09:35:49 -0400223void draw_drawable_test(skiatest::Reporter* reporter,
224 GrDirectContext* dContext,
225 GrDirectContext* childDContext) {
226 GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400227
228 const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
229 kPremul_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400230 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo,
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400231 ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
232 SkCanvas* canvas = surface->getCanvas();
233 canvas->clear(SK_ColorBLUE);
234
Robert Phillipsb25e0652020-07-22 09:35:49 -0400235 sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childDContext, DEV_W, DEV_H));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400236 canvas->drawDrawable(drawable.get());
237
238 SkPaint paint;
239 paint.setColor(SK_ColorGREEN);
240 SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H);
241 canvas->drawIRect(rect, paint);
242
243 // read pixels
244 SkBitmap bitmap;
245 bitmap.allocPixels(ii);
246 canvas->readPixels(bitmap, 0, 0);
247
248 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
249 bool failureFound = false;
250 SkPMColor expectedPixel;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500251 for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
252 for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400253 SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
254 if (cy < DEV_H / 2) {
255 if (cx < DEV_W / 2) {
256 expectedPixel = 0xFFFF0000; // Blue
257 } else {
258 expectedPixel = 0xFF0000FF; // Red
259 }
260 } else {
261 expectedPixel = 0xFF00FF00; // Green
262 }
263 if (expectedPixel != canvasPixel) {
264 failureFound = true;
265 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
266 cx, cy, canvasPixel, expectedPixel);
267 }
268 }
269 }
270}
271
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400272DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400273 draw_drawable_test(reporter, ctxInfo.directContext(), nullptr);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500274}
275
276DEF_GPUTEST(VkDrawableImportTest, reporter, options) {
277 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
278 sk_gpu_test::GrContextFactory::ContextType contextType =
279 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
280 if (contextType != sk_gpu_test::GrContextFactory::kVulkan_ContextType) {
281 continue;
282 }
283 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600284 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500285 skiatest::ReporterContext ctx(
286 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
Robert Phillips6d344c32020-07-06 10:56:46 -0400287 if (ctxInfo.directContext()) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500288 sk_gpu_test::ContextInfo child =
Robert Phillips00f78de2020-07-01 16:09:43 -0400289 factory.getSharedContextInfo(ctxInfo.directContext(), 0);
Robert Phillips6d344c32020-07-06 10:56:46 -0400290 if (!child.directContext()) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500291 continue;
292 }
293
Robert Phillips6d344c32020-07-06 10:56:46 -0400294 draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext());
Greg Daniel070cbaf2019-01-03 17:35:54 -0500295 }
296 }
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400297}
298
299#endif