Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 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 intializers to work |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkTypes.h" |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 11 | |
| 12 | #if SK_SUPPORT_GPU && defined(SK_VULKAN) |
| 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/vk/GrVkVulkan.h" |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkDrawable.h" |
| 17 | #include "include/core/SkSurface.h" |
| 18 | #include "include/gpu/GrBackendDrawableInfo.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #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 Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 28 | |
| 29 | using sk_gpu_test::GrContextFactory; |
| 30 | |
| 31 | static const int DEV_W = 16, DEV_H = 16; |
| 32 | |
| 33 | class TestDrawable : public SkDrawable { |
| 34 | public: |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 35 | TestDrawable(const GrVkInterface* interface, GrContext* context, int32_t width, int32_t height) |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 36 | : INHERITED() |
| 37 | , fInterface(interface) |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 38 | , fContext(context) |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 39 | , fWidth(width) |
| 40 | , fHeight(height) {} |
| 41 | |
| 42 | ~TestDrawable() override {} |
| 43 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 44 | class DrawHandlerBasic : public GpuDrawHandler { |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 45 | public: |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 46 | DrawHandlerBasic(const GrVkInterface* interface, int32_t width, int32_t height) |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 47 | : INHERITED() |
| 48 | , fInterface(interface) |
| 49 | , fWidth(width) |
| 50 | , fHeight(height) {} |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 51 | ~DrawHandlerBasic() override {} |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 52 | |
| 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 Daniel | b353eeb | 2018-12-05 11:01:58 -0500 | [diff] [blame] | 73 | attachment.colorAttachment = vkInfo.fColorAttachmentIndex; |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 74 | 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 Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 92 | typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&, |
| 93 | const SkImageInfo&, const GrVkDrawableInfo&); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 94 | 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 Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 101 | DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc, |
| 102 | const SkMatrix& matrix, |
| 103 | const SkIRect& clipBounds, |
| 104 | const SkImageInfo& bufferInfo) |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 105 | : INHERITED() |
| 106 | , fTestDrawable(td) |
| 107 | , fDrawProc(drawProc) |
Derek Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 108 | , fSubmitProc(submitProc) |
| 109 | , fMatrix(matrix) |
| 110 | , fClipBounds(clipBounds) |
| 111 | , fBufferInfo(bufferInfo) {} |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 112 | ~DrawHandlerImport() override { |
| 113 | fSubmitProc(fTestDrawable); |
| 114 | } |
| 115 | |
| 116 | void draw(const GrBackendDrawableInfo& info) override { |
| 117 | GrVkDrawableInfo vkInfo; |
| 118 | SkAssertResult(info.getVkDrawableInfo(&vkInfo)); |
| 119 | |
Derek Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 120 | fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 121 | } |
| 122 | private: |
Derek Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 123 | TestDrawable* fTestDrawable; |
| 124 | DrawProc fDrawProc; |
| 125 | SubmitProc fSubmitProc; |
| 126 | const SkMatrix fMatrix; |
| 127 | const SkIRect fClipBounds; |
| 128 | const SkImageInfo fBufferInfo; |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 129 | |
| 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 Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 135 | 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 Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 138 | if (!td->fDrawContext) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | SkCanvas* canvas = td->fDrawContext->getCanvas(); |
Derek Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 143 | canvas->clipRect(SkRect::Make(clipBounds)); |
| 144 | canvas->setMatrix(matrix); |
| 145 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 146 | 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 Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 154 | bufferInfo); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 155 | 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 Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 171 | td->fContext->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 172 | |
| 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 Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 184 | std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi, |
Greg Daniel | b2a259c | 2018-12-17 10:28:47 -0500 | [diff] [blame] | 185 | const SkMatrix& matrix, |
Derek Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 186 | const SkIRect& clipBounds, |
| 187 | const SkImageInfo& bufferInfo) override { |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 188 | if (backendApi != GrBackendApi::kVulkan) { |
| 189 | return nullptr; |
| 190 | } |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 191 | std::unique_ptr<GpuDrawHandler> draw; |
| 192 | if (fContext) { |
Derek Sollenberger | e6fb76b | 2019-01-10 13:19:06 -0500 | [diff] [blame] | 193 | draw.reset(new DrawHandlerImport(this, ImportDraw, ImportSubmitted, matrix, |
| 194 | clipBounds, bufferInfo)); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 195 | } else { |
| 196 | draw.reset(new DrawHandlerBasic(fInterface, fWidth, fHeight)); |
| 197 | } |
| 198 | return draw; |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 199 | } |
| 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 | |
| 209 | private: |
| 210 | const GrVkInterface* fInterface; |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 211 | GrContext* fContext; |
| 212 | sk_sp<GrVkSecondaryCBDrawContext> fDrawContext; |
| 213 | int32_t fWidth; |
| 214 | int32_t fHeight; |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 215 | |
| 216 | typedef SkDrawable INHERITED; |
| 217 | }; |
| 218 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 219 | void draw_drawable_test(skiatest::Reporter* reporter, GrContext* context, GrContext* childContext) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 220 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->priv().getGpu()); |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 221 | |
| 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 Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 229 | sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childContext, DEV_W, DEV_H)); |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 230 | 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 Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 245 | for (int cy = 0; cy < DEV_H && !failureFound; ++cy) { |
| 246 | for (int cx = 0; cx < DEV_W && !failureFound; ++cx) { |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 247 | 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 Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 266 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 267 | draw_drawable_test(reporter, ctxInfo.directContext(), nullptr); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | DEF_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 Dalton | b3c9745 | 2019-06-25 20:07:56 -0600 | [diff] [blame] | 278 | sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 279 | skiatest::ReporterContext ctx( |
| 280 | reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType))); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 281 | if (ctxInfo.directContext()) { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 282 | sk_gpu_test::ContextInfo child = |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 283 | factory.getSharedContextInfo(ctxInfo.directContext(), 0); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 284 | if (!child.directContext()) { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 285 | continue; |
| 286 | } |
| 287 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 288 | draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext()); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 289 | } |
| 290 | } |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | #endif |