1 /*
2 * Copyright 2016 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 "SkTypes.h"
11
12 #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_VULKAN)
13
14 #include "GrContextFactory.h"
15 #include "GrTest.h"
16 #include "Test.h"
17 #include "vk/GrVkCaps.h"
18 #include "vk/GrVkGpu.h"
19 #include "vk/GrVkMemory.h"
20 #include "vk/GrVkTypes.h"
21
22 using sk_gpu_test::GrContextFactory;
23
24 const int kW = 1024;
25 const int kH = 1024;
26 const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig;
27
wrap_tex_test(skiatest::Reporter * reporter,GrContext * context)28 void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
29
30 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu());
31
32 GrBackendObject backendObj = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kPixelConfig,
33 false);
34 const GrVkImageInfo* backendTex = reinterpret_cast<const GrVkImageInfo*>(backendObj);
35
36 // check basic borrowed creation
37 GrBackendTextureDesc desc;
38 desc.fConfig = kPixelConfig;
39 desc.fWidth = kW;
40 desc.fHeight = kH;
41 desc.fTextureHandle = backendObj;
42 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership);
43 REPORTER_ASSERT(reporter, tex);
44
45 // image is null
46 GrVkImageInfo backendCopy = *backendTex;
47 backendCopy.fImage = VK_NULL_HANDLE;
48 desc.fTextureHandle = (GrBackendObject) &backendCopy;
49 tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership);
50 REPORTER_ASSERT(reporter, !tex);
51 tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership);
52 REPORTER_ASSERT(reporter, !tex);
53
54 // alloc is null
55 backendCopy.fImage = backendTex->fImage;
56 backendCopy.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
57 tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership);
58 REPORTER_ASSERT(reporter, !tex);
59 tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership);
60 REPORTER_ASSERT(reporter, !tex);
61
62 // check adopt creation
63 backendCopy.fAlloc = backendTex->fAlloc;
64 tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership);
65 REPORTER_ASSERT(reporter, tex);
66
67 gpu->deleteTestingOnlyBackendTexture(backendObj, true);
68 }
69
wrap_rt_test(skiatest::Reporter * reporter,GrContext * context)70 void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
71 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu());
72
73 GrBackendObject backendObj = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kPixelConfig,
74 true);
75 const GrVkImageInfo* backendTex = reinterpret_cast<const GrVkImageInfo*>(backendObj);
76
77 // check basic borrowed creation
78 GrBackendRenderTargetDesc desc;
79 desc.fWidth = kW;
80 desc.fHeight = kH;
81 desc.fConfig = kPixelConfig;
82 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
83 desc.fSampleCnt = 0;
84 desc.fStencilBits = 0;
85 desc.fRenderTargetHandle = backendObj;
86 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(desc);
87 REPORTER_ASSERT(reporter, rt);
88
89 // image is null
90 GrVkImageInfo backendCopy = *backendTex;
91 backendCopy.fImage = VK_NULL_HANDLE;
92 desc.fRenderTargetHandle = (GrBackendObject)&backendCopy;
93 rt = gpu->wrapBackendRenderTarget(desc);
94 REPORTER_ASSERT(reporter, !rt);
95
96 // alloc is null
97 backendCopy.fImage = backendTex->fImage;
98 backendCopy.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
99 // can wrap null alloc
100 rt = gpu->wrapBackendRenderTarget(desc);
101 REPORTER_ASSERT(reporter, rt);
102
103 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
104 // resource when we're done.
105 gpu->deleteTestingOnlyBackendTexture(backendObj, false);
106 }
107
wrap_trt_test(skiatest::Reporter * reporter,GrContext * context)108 void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
109 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu());
110
111 GrBackendObject backendObj = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kPixelConfig,
112 true);
113 const GrVkImageInfo* backendTex = reinterpret_cast<const GrVkImageInfo*>(backendObj);
114
115 // check basic borrowed creation
116 GrBackendTextureDesc desc;
117 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
118 desc.fConfig = kPixelConfig;
119 desc.fWidth = kW;
120 desc.fHeight = kH;
121 desc.fTextureHandle = backendObj;
122 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership);
123 REPORTER_ASSERT(reporter, tex);
124
125 // image is null
126 GrVkImageInfo backendCopy = *backendTex;
127 backendCopy.fImage = VK_NULL_HANDLE;
128 desc.fTextureHandle = (GrBackendObject)&backendCopy;
129 tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership);
130 REPORTER_ASSERT(reporter, !tex);
131 tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership);
132 REPORTER_ASSERT(reporter, !tex);
133
134 // alloc is null
135 backendCopy.fImage = backendTex->fImage;
136 backendCopy.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
137 tex = gpu->wrapBackendTexture(desc, kBorrow_GrWrapOwnership);
138 REPORTER_ASSERT(reporter, !tex);
139 tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership);
140 REPORTER_ASSERT(reporter, !tex);
141
142 // check adopt creation
143 backendCopy.fAlloc = backendTex->fAlloc;
144 tex = gpu->wrapBackendTexture(desc, kAdopt_GrWrapOwnership);
145 REPORTER_ASSERT(reporter, tex);
146
147 gpu->deleteTestingOnlyBackendTexture(backendObj, true);
148 }
149
DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests,reporter,ctxInfo)150 DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
151 wrap_tex_test(reporter, ctxInfo.grContext());
152 wrap_rt_test(reporter, ctxInfo.grContext());
153 wrap_trt_test(reporter, ctxInfo.grContext());
154 }
155
156 #endif
157