blob: a139742f25caf4f7cbe683285e1888ad4b4f44bb [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"
19#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/vk/GrVkGpu.h"
21#include "src/gpu/vk/GrVkInterface.h"
22#include "src/gpu/vk/GrVkMemory.h"
23#include "src/gpu/vk/GrVkSecondaryCBDrawContext.h"
24#include "src/gpu/vk/GrVkUtil.h"
25#include "tests/Test.h"
26#include "tools/gpu/GrContextFactory.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040027
28using sk_gpu_test::GrContextFactory;
29
30static const int DEV_W = 16, DEV_H = 16;
31
32class TestDrawable : public SkDrawable {
33public:
Greg Daniel070cbaf2019-01-03 17:35:54 -050034 TestDrawable(const GrVkInterface* interface, GrContext* context, int32_t width, int32_t height)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040035 : INHERITED()
36 , fInterface(interface)
Greg Daniel070cbaf2019-01-03 17:35:54 -050037 , fContext(context)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040038 , fWidth(width)
39 , fHeight(height) {}
40
41 ~TestDrawable() override {}
42
Greg Daniel070cbaf2019-01-03 17:35:54 -050043 class DrawHandlerBasic : public GpuDrawHandler {
Greg Daniel64cc9aa2018-10-19 13:54:56 -040044 public:
Greg Daniel070cbaf2019-01-03 17:35:54 -050045 DrawHandlerBasic(const GrVkInterface* interface, int32_t width, int32_t height)
Greg Daniel64cc9aa2018-10-19 13:54:56 -040046 : INHERITED()
47 , fInterface(interface)
48 , fWidth(width)
49 , fHeight(height) {}
Greg Daniel070cbaf2019-01-03 17:35:54 -050050 ~DrawHandlerBasic() override {}
Greg Daniel64cc9aa2018-10-19 13:54:56 -040051
52 void draw(const GrBackendDrawableInfo& info) override {
53 GrVkDrawableInfo vkInfo;
54 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
55
56 // Clear to Red
57 VkClearColorValue vkColor;
58 vkColor.float32[0] = 1.0f; // r
59 vkColor.float32[1] = 0.0f; // g
60 vkColor.float32[2] = 0.0f; // b
61 vkColor.float32[3] = 1.0f; // a
62
63 // Clear right half of render target
64 VkClearRect clearRect;
65 clearRect.rect.offset = { fWidth / 2, 0 };
66 clearRect.rect.extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
67 clearRect.baseArrayLayer = 0;
68 clearRect.layerCount = 1;
69
70 VkClearAttachment attachment;
71 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Greg Danielb353eeb2018-12-05 11:01:58 -050072 attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
Greg Daniel64cc9aa2018-10-19 13:54:56 -040073 attachment.clearValue.color = vkColor;
74
75 GR_VK_CALL(fInterface, CmdClearAttachments(vkInfo.fSecondaryCommandBuffer,
76 1,
77 &attachment,
78 1,
79 &clearRect));
80 vkInfo.fDrawBounds->offset = { fWidth / 2, 0 };
81 vkInfo.fDrawBounds->extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
82 }
83 private:
84 const GrVkInterface* fInterface;
85 int32_t fWidth;
86 int32_t fHeight;
87
88 typedef GpuDrawHandler INHERITED;
89 };
90
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -050091 typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&,
92 const SkImageInfo&, const GrVkDrawableInfo&);
Greg Daniel070cbaf2019-01-03 17:35:54 -050093 typedef void (*SubmitProc)(TestDrawable*);
94
95 // Exercises the exporting of a secondary command buffer from one GrContext and then importing
96 // it into a second GrContext. We then draw to the secondary command buffer from the second
97 // GrContext.
98 class DrawHandlerImport : public GpuDrawHandler {
99 public:
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500100 DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc,
101 const SkMatrix& matrix,
102 const SkIRect& clipBounds,
103 const SkImageInfo& bufferInfo)
Greg Daniel070cbaf2019-01-03 17:35:54 -0500104 : INHERITED()
105 , fTestDrawable(td)
106 , fDrawProc(drawProc)
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500107 , fSubmitProc(submitProc)
108 , fMatrix(matrix)
109 , fClipBounds(clipBounds)
110 , fBufferInfo(bufferInfo) {}
Greg Daniel070cbaf2019-01-03 17:35:54 -0500111 ~DrawHandlerImport() override {
112 fSubmitProc(fTestDrawable);
113 }
114
115 void draw(const GrBackendDrawableInfo& info) override {
116 GrVkDrawableInfo vkInfo;
117 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
118
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500119 fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500120 }
121 private:
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500122 TestDrawable* fTestDrawable;
123 DrawProc fDrawProc;
124 SubmitProc fSubmitProc;
125 const SkMatrix fMatrix;
126 const SkIRect fClipBounds;
127 const SkImageInfo fBufferInfo;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500128
129 typedef GpuDrawHandler INHERITED;
130 };
131
132 // Helper function to test drawing to a secondary command buffer that we imported into the
133 // GrContext using a GrVkSecondaryCBDrawContext.
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500134 static void ImportDraw(TestDrawable* td, const SkMatrix& matrix, const SkIRect& clipBounds,
135 const SkImageInfo& bufferInfo, const GrVkDrawableInfo& info) {
136 td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fContext, bufferInfo, info, nullptr);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500137 if (!td->fDrawContext) {
138 return;
139 }
140
141 SkCanvas* canvas = td->fDrawContext->getCanvas();
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500142 canvas->clipRect(SkRect::Make(clipBounds));
143 canvas->setMatrix(matrix);
144
Greg Daniel070cbaf2019-01-03 17:35:54 -0500145 SkIRect rect = SkIRect::MakeXYWH(td->fWidth/2, 0, td->fWidth/4, td->fHeight);
146 SkPaint paint;
147 paint.setColor(SK_ColorRED);
148 canvas->drawIRect(rect, paint);
149
150 // Draw to an offscreen target so that we end up with a mix of "real" secondary command
151 // buffers and the imported secondary command buffer.
152 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(td->fContext, SkBudgeted::kYes,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500153 bufferInfo);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500154 surf->getCanvas()->clear(SK_ColorRED);
155
156 SkRect dstRect = SkRect::MakeXYWH(3*td->fWidth/4, 0, td->fWidth/4, td->fHeight);
157 SkIRect srcRect = SkIRect::MakeWH(td->fWidth/4, td->fHeight);
158 canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, &paint);
159
160 td->fDrawContext->flush();
161 }
162
163 // Helper function to test waiting for the imported secondary command buffer to be submitted on
164 // its original context and then cleaning up the GrVkSecondaryCBDrawContext from this GrContext.
165 static void ImportSubmitted(TestDrawable* td) {
166 // Typical use case here would be to create a fence that we submit to the gpu and then wait
167 // on before releasing the GrVkSecondaryCBDrawContext resources. To simulate that for this
168 // test (and since we are running single threaded anyways), we will just force a sync of
169 // the gpu and cpu here.
Robert Phillips9da87e02019-02-04 13:26:26 -0500170 td->fContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500171
172 td->fDrawContext->releaseResources();
173 // We release the GrContext here manually to test that we waited long enough before
174 // releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
175 // the GrContext it used to imported the secondary command buffer. If we had released the
176 // GrContext's resources earlier (before waiting on the gpu above), we would get vulkan
177 // validation layer errors saying we freed some vulkan objects while they were still in use
178 // on the GPU.
179 td->fContext->releaseResourcesAndAbandonContext();
180 }
181
182
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400183 std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
Greg Danielb2a259c2018-12-17 10:28:47 -0500184 const SkMatrix& matrix,
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500185 const SkIRect& clipBounds,
186 const SkImageInfo& bufferInfo) override {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400187 if (backendApi != GrBackendApi::kVulkan) {
188 return nullptr;
189 }
Greg Daniel070cbaf2019-01-03 17:35:54 -0500190 std::unique_ptr<GpuDrawHandler> draw;
191 if (fContext) {
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -0500192 draw.reset(new DrawHandlerImport(this, ImportDraw, ImportSubmitted, matrix,
193 clipBounds, bufferInfo));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500194 } else {
195 draw.reset(new DrawHandlerBasic(fInterface, fWidth, fHeight));
196 }
197 return draw;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400198 }
199
200 SkRect onGetBounds() override {
201 return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
202 }
203
204 void onDraw(SkCanvas*) override {
205 SkASSERT(false);
206 }
207
208private:
209 const GrVkInterface* fInterface;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500210 GrContext* fContext;
211 sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
212 int32_t fWidth;
213 int32_t fHeight;
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400214
215 typedef SkDrawable INHERITED;
216};
217
Greg Daniel070cbaf2019-01-03 17:35:54 -0500218void draw_drawable_test(skiatest::Reporter* reporter, GrContext* context, GrContext* childContext) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500219 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->priv().getGpu());
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400220
221 const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
222 kPremul_SkAlphaType);
223 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
224 ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
225 SkCanvas* canvas = surface->getCanvas();
226 canvas->clear(SK_ColorBLUE);
227
Greg Daniel070cbaf2019-01-03 17:35:54 -0500228 sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childContext, DEV_W, DEV_H));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400229 canvas->drawDrawable(drawable.get());
230
231 SkPaint paint;
232 paint.setColor(SK_ColorGREEN);
233 SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H);
234 canvas->drawIRect(rect, paint);
235
236 // read pixels
237 SkBitmap bitmap;
238 bitmap.allocPixels(ii);
239 canvas->readPixels(bitmap, 0, 0);
240
241 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
242 bool failureFound = false;
243 SkPMColor expectedPixel;
Greg Daniel070cbaf2019-01-03 17:35:54 -0500244 for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
245 for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400246 SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
247 if (cy < DEV_H / 2) {
248 if (cx < DEV_W / 2) {
249 expectedPixel = 0xFFFF0000; // Blue
250 } else {
251 expectedPixel = 0xFF0000FF; // Red
252 }
253 } else {
254 expectedPixel = 0xFF00FF00; // Green
255 }
256 if (expectedPixel != canvasPixel) {
257 failureFound = true;
258 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
259 cx, cy, canvasPixel, expectedPixel);
260 }
261 }
262 }
263}
264
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400265DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500266 draw_drawable_test(reporter, ctxInfo.grContext(), nullptr);
267}
268
269DEF_GPUTEST(VkDrawableImportTest, reporter, options) {
270 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
271 sk_gpu_test::GrContextFactory::ContextType contextType =
272 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
273 if (contextType != sk_gpu_test::GrContextFactory::kVulkan_ContextType) {
274 continue;
275 }
276 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600277 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500278 skiatest::ReporterContext ctx(
279 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
280 if (ctxInfo.grContext()) {
281 sk_gpu_test::ContextInfo child =
282 factory.getSharedContextInfo(ctxInfo.grContext(), 0);
283 if (!child.grContext()) {
284 continue;
285 }
286
287 draw_drawable_test(reporter, ctxInfo.grContext(), child.grContext());
288 }
289 }
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400290}
291
292#endif