blob: 90e74b85b398a4abbd0456897c5960235f1b9544 [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/vk/GrVkVulkan.h"
Greg Daniel54bfb182018-11-20 17:12:36 -050015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040017#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrContextPriv.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 "tools/gpu/GrContextFactory.h"
Robert Phillips646e4292017-06-13 12:44:56 -040022
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/gpu/vk/GrVkTypes.h"
24#include "src/gpu/vk/GrVkCaps.h"
25#include "src/gpu/vk/GrVkGpu.h"
26#include "src/gpu/vk/GrVkMemory.h"
27#include "tests/Test.h"
Greg Danielc1ad77c2020-05-06 11:40:03 -040028#include "tests/TestUtils.h"
jvanverthfd359ca2016-03-18 11:57:24 -070029
kkinnunen2ae4b2e2016-03-31 08:08:20 -070030using sk_gpu_test::GrContextFactory;
31
jvanverthfd359ca2016-03-18 11:57:24 -070032const int kW = 1024;
33const int kH = 1024;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040034const SkColorType kColorType = SkColorType::kRGBA_8888_SkColorType;
jvanverthfd359ca2016-03-18 11:57:24 -070035
Robert Phillipseffd13f2020-07-20 15:00:36 -040036void wrap_tex_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
jvanverthfd359ca2016-03-18 11:57:24 -070037
Robert Phillipseffd13f2020-07-20 15:00:36 -040038 GrGpu* gpu = dContext->priv().getGpu();
jvanverthfd359ca2016-03-18 11:57:24 -070039
Greg Danielc1ad77c2020-05-06 11:40:03 -040040 GrBackendTexture origBackendTex;
Robert Phillipseffd13f2020-07-20 15:00:36 -040041 CreateBackendTexture(dContext, &origBackendTex, kW, kH, kColorType, SkColors::kTransparent,
42 GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Greg Danielc1ad77c2020-05-06 11:40:03 -040043
Greg Daniel52e16d92018-04-10 09:34:07 -040044 GrVkImageInfo imageInfo;
45 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070046
Brian Salomon8a78e9c2020-03-27 10:42:15 -040047 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership,
48 GrWrapCacheable::kNo, kRead_GrIOType);
jvanverthfd359ca2016-03-18 11:57:24 -070049 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -070050
51 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050052 {
Greg Daniel52e16d92018-04-10 09:34:07 -040053 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050054 backendCopy.fImage = VK_NULL_HANDLE;
55 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -040056 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
57 kRead_GrIOType);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050058 REPORTER_ASSERT(reporter, !tex);
Brian Salomon8a78e9c2020-03-27 10:42:15 -040059 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
60 kRead_GrIOType);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050061 REPORTER_ASSERT(reporter, !tex);
62 }
jvanverthfd359ca2016-03-18 11:57:24 -070063
64 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050065 {
Greg Daniel52e16d92018-04-10 09:34:07 -040066 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050067 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050068 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -040069 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
70 kRead_GrIOType);
Jim Van Verth658d4992019-07-11 14:07:53 -040071 REPORTER_ASSERT(reporter, tex);
Brian Salomon8a78e9c2020-03-27 10:42:15 -040072 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
73 kRead_GrIOType);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050074 REPORTER_ASSERT(reporter, !tex);
75 }
76
Stan Iliev7fa5c312017-04-19 00:23:39 +000077 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050078 {
Greg Daniel52e16d92018-04-10 09:34:07 -040079 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050080 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -040081 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
82 kRead_GrIOType);
Greg Daniel7ef28f32017-04-20 16:41:55 +000083
Robert Phillipsd21b2a52017-12-12 13:01:25 -050084 REPORTER_ASSERT(reporter, tex);
85 }
jvanverthfd359ca2016-03-18 11:57:24 -070086}
87
Robert Phillipseffd13f2020-07-20 15:00:36 -040088void wrap_rt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
89 GrGpu* gpu = dContext->priv().getGpu();
jvanverthfd359ca2016-03-18 11:57:24 -070090
Greg Danielc1ad77c2020-05-06 11:40:03 -040091 GrBackendTexture origBackendTex;
Robert Phillipseffd13f2020-07-20 15:00:36 -040092 CreateBackendTexture(dContext, &origBackendTex, kW, kH, kColorType, SkColors::kTransparent,
Greg Danielc1ad77c2020-05-06 11:40:03 -040093 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Greg Daniel108bb232018-07-03 16:18:29 -040094
Greg Daniel52e16d92018-04-10 09:34:07 -040095 GrVkImageInfo imageInfo;
96 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070097
Greg Daniel52e16d92018-04-10 09:34:07 -040098 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
Greg Danielbcf612b2017-05-01 13:50:58 +000099
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400100 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
jvanverthfd359ca2016-03-18 11:57:24 -0700101 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -0700102
103 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500104 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400105 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500106 backendCopy.fImage = VK_NULL_HANDLE;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500107 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400108 rt = gpu->wrapBackendRenderTarget(backendRT);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500109 REPORTER_ASSERT(reporter, !rt);
110 }
jvanverthfd359ca2016-03-18 11:57:24 -0700111
112 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500113 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400114 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500115 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500116 // can wrap null alloc
Brian Salomonbdecacf2018-02-02 20:32:49 -0500117 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400118 rt = gpu->wrapBackendRenderTarget(backendRT);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500119 REPORTER_ASSERT(reporter, rt);
120 }
jvanverthfd359ca2016-03-18 11:57:24 -0700121
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000122 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
123 // resource when we're done.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400124 dContext->deleteBackendTexture(origBackendTex);
jvanverthfd359ca2016-03-18 11:57:24 -0700125}
126
Robert Phillipseffd13f2020-07-20 15:00:36 -0400127void wrap_trt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
128 GrGpu* gpu = dContext->priv().getGpu();
jvanverthfd359ca2016-03-18 11:57:24 -0700129
Greg Danielc1ad77c2020-05-06 11:40:03 -0400130 GrBackendTexture origBackendTex;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400131 CreateBackendTexture(dContext, &origBackendTex, kW, kH, kColorType, SkColors::kTransparent,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400132 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
133
Greg Daniel52e16d92018-04-10 09:34:07 -0400134 GrVkImageInfo imageInfo;
135 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -0700136
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500137 sk_sp<GrTexture> tex = gpu->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400138 origBackendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
jvanverthfd359ca2016-03-18 11:57:24 -0700139 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700140
141 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500142 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400143 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500144 backendCopy.fImage = VK_NULL_HANDLE;
145 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400146 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
147 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500148 REPORTER_ASSERT(reporter, !tex);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400149 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
150 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500151 REPORTER_ASSERT(reporter, !tex);
152 }
jvanverthfd359ca2016-03-18 11:57:24 -0700153
154 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500155 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400156 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500157 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500158 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400159 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
160 GrWrapCacheable::kNo);
Jim Van Verth658d4992019-07-11 14:07:53 -0400161 REPORTER_ASSERT(reporter, tex);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400162 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
163 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500164 REPORTER_ASSERT(reporter, !tex);
165 }
jvanverthfd359ca2016-03-18 11:57:24 -0700166
167 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500168 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400169 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500170 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400171 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
172 GrWrapCacheable::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500173 REPORTER_ASSERT(reporter, tex);
174 }
jvanverthfd359ca2016-03-18 11:57:24 -0700175}
176
bsalomondc0fcd42016-04-11 14:21:33 -0700177DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400178 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -0400179
Robert Phillipseffd13f2020-07-20 15:00:36 -0400180 wrap_tex_test(reporter, dContext);
181 wrap_rt_test(reporter, dContext);
182 wrap_trt_test(reporter, dContext);
jvanverthfd359ca2016-03-18 11:57:24 -0700183}
184
185#endif