• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "include/core/SkTypes.h"
11 
12 #if SK_SUPPORT_GPU && defined(SK_VULKAN)
13 
14 #include "include/gpu/vk/GrVkVulkan.h"
15 
16 #include "include/core/SkBitmap.h"
17 #include "include/core/SkDrawable.h"
18 #include "include/core/SkSurface.h"
19 #include "include/gpu/GrBackendDrawableInfo.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "src/gpu/GrDirectContextPriv.h"
22 #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"
29 
30 using sk_gpu_test::GrContextFactory;
31 
32 static const int DEV_W = 16, DEV_H = 16;
33 
34 class TestDrawable : public SkDrawable {
35 public:
TestDrawable(const GrVkInterface * interface,GrDirectContext * dContext,int32_t width,int32_t height)36     TestDrawable(const GrVkInterface* interface, GrDirectContext* dContext,
37                  int32_t width, int32_t height)
38             : INHERITED()
39             , fInterface(interface)
40             , fDContext(dContext)
41             , fWidth(width)
42             , fHeight(height) {}
43 
~TestDrawable()44     ~TestDrawable() override {}
45 
46     class DrawHandlerBasic : public GpuDrawHandler {
47     public:
DrawHandlerBasic(const GrVkInterface * interface,int32_t width,int32_t height)48         DrawHandlerBasic(const GrVkInterface* interface, int32_t width, int32_t height)
49             : INHERITED()
50             , fInterface(interface)
51             , fWidth(width)
52             , fHeight(height) {}
~DrawHandlerBasic()53         ~DrawHandlerBasic() override {}
54 
draw(const GrBackendDrawableInfo & info)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;
75             attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
76             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 
91         using INHERITED = GpuDrawHandler;
92     };
93 
94     typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&,
95                              const SkImageInfo&, const GrVkDrawableInfo&);
96     typedef void (*SubmitProc)(TestDrawable*);
97 
98     // 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.
101     class DrawHandlerImport : public GpuDrawHandler {
102     public:
DrawHandlerImport(TestDrawable * td,DrawProc drawProc,SubmitProc submitProc,const SkMatrix & matrix,const SkIRect & clipBounds,const SkImageInfo & bufferInfo)103         DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc,
104                           const SkMatrix& matrix,
105                           const SkIRect& clipBounds,
106                           const SkImageInfo& bufferInfo)
107             : INHERITED()
108             , fTestDrawable(td)
109             , fDrawProc(drawProc)
110             , fSubmitProc(submitProc)
111             , fMatrix(matrix)
112             , fClipBounds(clipBounds)
113             , fBufferInfo(bufferInfo) {}
~DrawHandlerImport()114         ~DrawHandlerImport() override {
115             fSubmitProc(fTestDrawable);
116         }
117 
draw(const GrBackendDrawableInfo & info)118         void draw(const GrBackendDrawableInfo& info) override {
119             GrVkDrawableInfo vkInfo;
120             SkAssertResult(info.getVkDrawableInfo(&vkInfo));
121 
122             fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo);
123         }
124     private:
125         TestDrawable*     fTestDrawable;
126         DrawProc          fDrawProc;
127         SubmitProc        fSubmitProc;
128         const SkMatrix    fMatrix;
129         const SkIRect     fClipBounds;
130         const SkImageInfo fBufferInfo;
131 
132         using INHERITED = GpuDrawHandler;
133     };
134 
135     // Helper function to test drawing to a secondary command buffer that we imported into the
136     // context using a GrVkSecondaryCBDrawContext.
ImportDraw(TestDrawable * td,const SkMatrix & matrix,const SkIRect & clipBounds,const SkImageInfo & bufferInfo,const GrVkDrawableInfo & info)137     static void ImportDraw(TestDrawable* td, const SkMatrix& matrix, const SkIRect& clipBounds,
138                            const SkImageInfo& bufferInfo, const GrVkDrawableInfo& info) {
139         td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fDContext, bufferInfo,
140                                                             info, nullptr);
141         if (!td->fDrawContext) {
142             return;
143         }
144 
145         SkCanvas* canvas = td->fDrawContext->getCanvas();
146         canvas->clipRect(SkRect::Make(clipBounds));
147         canvas->setMatrix(matrix);
148 
149         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.
156         sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(td->fDContext, SkBudgeted::kYes,
157                                                             bufferInfo);
158         surf->getCanvas()->clear(SK_ColorRED);
159 
160         SkRect dstRect = SkRect::MakeXYWH(3*td->fWidth/4, 0, td->fWidth/4, td->fHeight);
161         SkRect srcRect = SkRect::MakeIWH(td->fWidth/4, td->fHeight);
162         canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, SkSamplingOptions(),
163                               &paint, SkCanvas::kStrict_SrcRectConstraint);
164 
165         td->fDrawContext->flush();
166     }
167 
168     // Helper function to test waiting for the imported secondary command buffer to be submitted on
169     // its original context and then cleaning up the GrVkSecondaryCBDrawContext from this context.
ImportSubmitted(TestDrawable * td)170     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.
175         td->fDContext->submit(true);
176 
177         td->fDrawContext->releaseResources();
178         // We release the context here manually to test that we waited long enough before
179         // releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
180         // 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
182         // validation layer errors saying we freed some vulkan objects while they were still in use
183         // on the GPU.
184         td->fDContext->releaseResourcesAndAbandonContext();
185     }
186 
187 
onSnapGpuDrawHandler(GrBackendApi backendApi,const SkMatrix & matrix,const SkIRect & clipBounds,const SkImageInfo & bufferInfo)188     std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
189                                                          const SkMatrix& matrix,
190                                                          const SkIRect& clipBounds,
191                                                          const SkImageInfo& bufferInfo) override {
192         if (backendApi != GrBackendApi::kVulkan) {
193             return nullptr;
194         }
195         std::unique_ptr<GpuDrawHandler> draw;
196         if (fDContext) {
197             draw = std::make_unique<DrawHandlerImport>(this, ImportDraw, ImportSubmitted, matrix,
198                                                        clipBounds, bufferInfo);
199         } else {
200             draw = std::make_unique<DrawHandlerBasic>(fInterface, fWidth, fHeight);
201         }
202         return draw;
203     }
204 
onGetBounds()205     SkRect onGetBounds() override {
206         return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
207     }
208 
onDraw(SkCanvas *)209     void onDraw(SkCanvas*) override {
210         SkASSERT(false);
211     }
212 
213 private:
214     const GrVkInterface* fInterface;
215     GrDirectContext*     fDContext;
216     sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
217     int32_t              fWidth;
218     int32_t              fHeight;
219 
220     using INHERITED = SkDrawable;
221 };
222 
draw_drawable_test(skiatest::Reporter * reporter,GrDirectContext * dContext,GrDirectContext * childDContext)223 void draw_drawable_test(skiatest::Reporter* reporter,
224                         GrDirectContext* dContext,
225                         GrDirectContext* childDContext) {
226     GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
227 
228     const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
229                                              kPremul_SkAlphaType);
230     sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo,
231                                                          ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
232     SkCanvas* canvas = surface->getCanvas();
233     canvas->clear(SK_ColorBLUE);
234 
235     sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childDContext, DEV_W, DEV_H));
236     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;
251     for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
252         for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
253             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 
DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest,reporter,ctxInfo)272 DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
273     draw_drawable_test(reporter, ctxInfo.directContext(), nullptr);
274 }
275 
DEF_GPUTEST(VkDrawableImportTest,reporter,options)276 DEF_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);
284         sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
285         skiatest::ReporterContext ctx(
286                    reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
287         if (ctxInfo.directContext()) {
288             sk_gpu_test::ContextInfo child =
289                     factory.getSharedContextInfo(ctxInfo.directContext(), 0);
290             if (!child.directContext()) {
291                 continue;
292             }
293 
294             draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext());
295         }
296     }
297 }
298 
299 #endif
300