blob: 0ac75e3857e0adbd6ad207a1a490c3df71a9a3b1 [file] [log] [blame]
jvanverthfd359ca2016-03-18 11:57:24 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
jvanverthfd359ca2016-03-18 11:57:24 -070011
Brian Osmanc7ad40f2018-05-31 14:27:17 -040012#if defined(SK_VULKAN)
jvanverthfd359ca2016-03-18 11:57:24 -070013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040015#include "include/gpu/GrDirectContext.h"
Brian Salomon72050802020-10-12 20:45:06 -040016#include "include/gpu/vk/GrVkTypes.h"
17#include "include/gpu/vk/GrVkVulkan.h"
Adlai Hollera0693042020-10-14 11:23:11 -040018#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040019#include "src/gpu/GrRenderTarget.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/vk/GrVkCaps.h"
22#include "src/gpu/vk/GrVkGpu.h"
23#include "src/gpu/vk/GrVkMemory.h"
24#include "tests/Test.h"
Brian Salomon72050802020-10-12 20:45:06 -040025#include "tools/gpu/GrContextFactory.h"
26#include "tools/gpu/ManagedBackendTexture.h"
jvanverthfd359ca2016-03-18 11:57:24 -070027
kkinnunen2ae4b2e2016-03-31 08:08:20 -070028using sk_gpu_test::GrContextFactory;
29
jvanverthfd359ca2016-03-18 11:57:24 -070030const int kW = 1024;
31const int kH = 1024;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040032const SkColorType kColorType = SkColorType::kRGBA_8888_SkColorType;
jvanverthfd359ca2016-03-18 11:57:24 -070033
Robert Phillipseffd13f2020-07-20 15:00:36 -040034void wrap_tex_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
Robert Phillipseffd13f2020-07-20 15:00:36 -040035 GrGpu* gpu = dContext->priv().getGpu();
jvanverthfd359ca2016-03-18 11:57:24 -070036
Brian Salomon72050802020-10-12 20:45:06 -040037 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
38 dContext, kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
39 if (!mbet) {
40 ERRORF(reporter, "Could not create backend texture.");
41 return;
42 }
43
44 GrBackendTexture origBackendTex = mbet->texture();
Greg Danielc1ad77c2020-05-06 11:40:03 -040045
Greg Daniel52e16d92018-04-10 09:34:07 -040046 GrVkImageInfo imageInfo;
47 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070048
Brian Salomon72050802020-10-12 20:45:06 -040049 {
50 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership,
51 GrWrapCacheable::kNo, kRead_GrIOType);
52 REPORTER_ASSERT(reporter, tex);
53 }
jvanverthfd359ca2016-03-18 11:57:24 -070054
55 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050056 {
Greg Daniel52e16d92018-04-10 09:34:07 -040057 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050058 backendCopy.fImage = VK_NULL_HANDLE;
59 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon72050802020-10-12 20:45:06 -040060 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
61 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050062 REPORTER_ASSERT(reporter, !tex);
Brian Salomon72050802020-10-12 20:45:06 -040063 tex = gpu->wrapBackendTexture(
64 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050065 REPORTER_ASSERT(reporter, !tex);
66 }
jvanverthfd359ca2016-03-18 11:57:24 -070067
68 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050069 {
Greg Daniel52e16d92018-04-10 09:34:07 -040070 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050071 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050072 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon72050802020-10-12 20:45:06 -040073 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
74 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth658d4992019-07-11 14:07:53 -040075 REPORTER_ASSERT(reporter, tex);
Brian Salomon72050802020-10-12 20:45:06 -040076 tex = gpu->wrapBackendTexture(
77 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050078 REPORTER_ASSERT(reporter, !tex);
79 }
80
Stan Iliev7fa5c312017-04-19 00:23:39 +000081 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050082 {
Greg Daniel52e16d92018-04-10 09:34:07 -040083 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050084 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon72050802020-10-12 20:45:06 -040085 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
86 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
Greg Daniel7ef28f32017-04-20 16:41:55 +000087
Robert Phillipsd21b2a52017-12-12 13:01:25 -050088 REPORTER_ASSERT(reporter, tex);
Brian Salomon72050802020-10-12 20:45:06 -040089 if (tex) {
90 mbet->wasAdopted();
91 }
Brian Salomon57fd9232020-09-28 17:02:49 -040092 }
jvanverthfd359ca2016-03-18 11:57:24 -070093}
94
Robert Phillipseffd13f2020-07-20 15:00:36 -040095void wrap_rt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
96 GrGpu* gpu = dContext->priv().getGpu();
Brian Salomon72050802020-10-12 20:45:06 -040097 GrColorType ct = SkColorTypeToGrColorType(kColorType);
jvanverthfd359ca2016-03-18 11:57:24 -070098
Brian Salomon72050802020-10-12 20:45:06 -040099 for (int sampleCnt : {1, 4}) {
100 GrBackendFormat format = gpu->caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
101 if (sampleCnt > gpu->caps()->maxRenderTargetSampleCount(format)) {
102 continue;
103 }
Greg Daniel108bb232018-07-03 16:18:29 -0400104
Brian Salomon72050802020-10-12 20:45:06 -0400105 GrBackendRenderTarget origBackendRT =
106 gpu->createTestingOnlyBackendRenderTarget({kW, kH}, ct, sampleCnt);
107 if (!origBackendRT.isValid()) {
108 ERRORF(reporter, "Could not create backend render target.");
109 }
jvanverthfd359ca2016-03-18 11:57:24 -0700110
Brian Salomon72050802020-10-12 20:45:06 -0400111 GrVkImageInfo imageInfo;
112 REPORTER_ASSERT(reporter, origBackendRT.getVkImageInfo(&imageInfo));
Greg Danielbcf612b2017-05-01 13:50:58 +0000113
Brian Salomon72050802020-10-12 20:45:06 -0400114 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500115 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -0700116
Brian Salomon72050802020-10-12 20:45:06 -0400117 // image is null
118 {
119 GrVkImageInfo backendCopy = imageInfo;
120 backendCopy.fImage = VK_NULL_HANDLE;
121 GrBackendRenderTarget backendRT(kW, kH, 1, backendCopy);
122 rt = gpu->wrapBackendRenderTarget(backendRT);
123 REPORTER_ASSERT(reporter, !rt);
124 }
125
126 // alloc is null
127 {
128 GrVkImageInfo backendCopy = imageInfo;
129 backendCopy.fAlloc = GrVkAlloc();
130 // can wrap null alloc
131 GrBackendRenderTarget backendRT(kW, kH, 1, backendCopy);
Brian Salomon72c7b982020-10-06 10:07:38 -0400132 rt = gpu->wrapBackendRenderTarget(backendRT);
133 REPORTER_ASSERT(reporter, rt);
Brian Salomon72c7b982020-10-06 10:07:38 -0400134 }
Brian Salomon4efaf5e2020-10-12 21:30:00 +0000135
Brian Salomon72050802020-10-12 20:45:06 -0400136 gpu->deleteTestingOnlyBackendRenderTarget(origBackendRT);
137 }
jvanverthfd359ca2016-03-18 11:57:24 -0700138}
139
Robert Phillipseffd13f2020-07-20 15:00:36 -0400140void wrap_trt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
141 GrGpu* gpu = dContext->priv().getGpu();
jvanverthfd359ca2016-03-18 11:57:24 -0700142
Brian Salomon72050802020-10-12 20:45:06 -0400143 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
144 dContext, kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kYes);
145 if (!mbet) {
146 ERRORF(reporter, "Could not create renderable backend texture.");
147 return;
148 }
149 GrBackendTexture origBackendTex = mbet->texture();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400150
Greg Daniel52e16d92018-04-10 09:34:07 -0400151 GrVkImageInfo imageInfo;
152 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -0700153
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500154 sk_sp<GrTexture> tex = gpu->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400155 origBackendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
jvanverthfd359ca2016-03-18 11:57:24 -0700156 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700157
158 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500159 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400160 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500161 backendCopy.fImage = VK_NULL_HANDLE;
162 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400163 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
164 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500165 REPORTER_ASSERT(reporter, !tex);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400166 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
167 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500168 REPORTER_ASSERT(reporter, !tex);
169 }
jvanverthfd359ca2016-03-18 11:57:24 -0700170
171 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500172 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400173 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500174 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500175 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400176 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
177 GrWrapCacheable::kNo);
Jim Van Verth658d4992019-07-11 14:07:53 -0400178 REPORTER_ASSERT(reporter, tex);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400179 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
180 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500181 REPORTER_ASSERT(reporter, !tex);
182 }
jvanverthfd359ca2016-03-18 11:57:24 -0700183
Brian Salomon57fd9232020-09-28 17:02:49 -0400184 // check rendering with MSAA
185 {
186 int maxSamples = dContext->priv().caps()->maxRenderTargetSampleCount(
187 origBackendTex.getBackendFormat());
188 bool shouldSucceed = maxSamples > 1;
189 tex = gpu->wrapRenderableBackendTexture(origBackendTex, 2, kBorrow_GrWrapOwnership,
190 GrWrapCacheable::kNo);
191 REPORTER_ASSERT(reporter, SkToBool(tex) == shouldSucceed);
192 }
193
Brian Salomon72050802020-10-12 20:45:06 -0400194 // check adopt creation
Brian Salomon57fd9232020-09-28 17:02:49 -0400195 {
196 GrVkImageInfo backendCopy = imageInfo;
Brian Salomon57fd9232020-09-28 17:02:49 -0400197 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
198 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
199 GrWrapCacheable::kNo);
Brian Salomon72050802020-10-12 20:45:06 -0400200 REPORTER_ASSERT(reporter, tex);
201 if (tex) {
202 mbet->wasAdopted();
203 }
Brian Salomon57fd9232020-09-28 17:02:49 -0400204 }
jvanverthfd359ca2016-03-18 11:57:24 -0700205}
206
bsalomondc0fcd42016-04-11 14:21:33 -0700207DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400208 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -0400209
Robert Phillipseffd13f2020-07-20 15:00:36 -0400210 wrap_tex_test(reporter, dContext);
211 wrap_rt_test(reporter, dContext);
212 wrap_trt_test(reporter, dContext);
jvanverthfd359ca2016-03-18 11:57:24 -0700213}
214
215#endif