blob: 155333473cd7328612c0605f6ed60f5b4e65d12f [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
10#include "SkTypes.h"
11
Brian Osmanc7ad40f2018-05-31 14:27:17 -040012#if defined(SK_VULKAN)
jvanverthfd359ca2016-03-18 11:57:24 -070013
Greg Daniel54bfb182018-11-20 17:12:36 -050014#include "vk/GrVkVulkan.h"
15
Herb Derbyd3895d82018-09-04 13:27:00 -040016#include "GrBackendSurface.h"
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050017#include "GrContextPriv.h"
jvanverthfd359ca2016-03-18 11:57:24 -070018#include "GrContextFactory.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040019#include "GrRenderTarget.h"
Robert Phillips646e4292017-06-13 12:44:56 -040020#include "GrTexture.h"
21
jvanverthfd359ca2016-03-18 11:57:24 -070022#include "Test.h"
23#include "vk/GrVkCaps.h"
24#include "vk/GrVkGpu.h"
25#include "vk/GrVkMemory.h"
26#include "vk/GrVkTypes.h"
27
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;
32const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips646f6372018-09-25 09:31:10 -040033const GrColorType kColorType = GrColorType::kRGBA_8888;
jvanverthfd359ca2016-03-18 11:57:24 -070034
35void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
36
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050037 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070038
Robert Phillipsd21b2a52017-12-12 13:01:25 -050039 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -040040 kColorType, false,
Robert Phillipsd21b2a52017-12-12 13:01:25 -050041 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -040042 GrVkImageInfo imageInfo;
43 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070044
Greg Daniel2268ad22018-11-15 09:27:38 -050045 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership, false);
jvanverthfd359ca2016-03-18 11:57:24 -070046 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -070047
48 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050049 {
Greg Daniel52e16d92018-04-10 09:34:07 -040050 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050051 backendCopy.fImage = VK_NULL_HANDLE;
52 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040053 backendTex.setPixelConfig(kPixelConfig);
Greg Daniel2268ad22018-11-15 09:27:38 -050054 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050055 REPORTER_ASSERT(reporter, !tex);
Greg Daniel2268ad22018-11-15 09:27:38 -050056 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050057 REPORTER_ASSERT(reporter, !tex);
58 }
jvanverthfd359ca2016-03-18 11:57:24 -070059
60 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050061 {
Greg Daniel52e16d92018-04-10 09:34:07 -040062 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050063 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050064 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040065 backendTex.setPixelConfig(kPixelConfig);
Greg Daniel2268ad22018-11-15 09:27:38 -050066 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050067 REPORTER_ASSERT(reporter, !tex);
Greg Daniel2268ad22018-11-15 09:27:38 -050068 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050069 REPORTER_ASSERT(reporter, !tex);
70 }
71
Stan Iliev7fa5c312017-04-19 00:23:39 +000072 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050073 {
Greg Daniel52e16d92018-04-10 09:34:07 -040074 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050075 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040076 backendTex.setPixelConfig(kPixelConfig);
Greg Daniel2268ad22018-11-15 09:27:38 -050077 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, false);
Greg Daniel7ef28f32017-04-20 16:41:55 +000078
Robert Phillipsd21b2a52017-12-12 13:01:25 -050079 REPORTER_ASSERT(reporter, tex);
80 }
jvanverthfd359ca2016-03-18 11:57:24 -070081}
82
83void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050084 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070085
Greg Daniel8a3f55c2018-03-14 17:32:12 +000086 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -040087 kColorType, true,
Greg Daniel8a3f55c2018-03-14 17:32:12 +000088 GrMipMapped::kNo);
Greg Daniel108bb232018-07-03 16:18:29 -040089
Greg Daniel52e16d92018-04-10 09:34:07 -040090 GrVkImageInfo imageInfo;
91 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070092
Greg Daniel52e16d92018-04-10 09:34:07 -040093 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
Greg Daniel108bb232018-07-03 16:18:29 -040094 origBackendRT.setPixelConfig(kPixelConfig);
Greg Danielbcf612b2017-05-01 13:50:58 +000095
Robert Phillipsd21b2a52017-12-12 13:01:25 -050096 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
jvanverthfd359ca2016-03-18 11:57:24 -070097 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -070098
99 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500100 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400101 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500102 backendCopy.fImage = VK_NULL_HANDLE;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500103 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400104 backendRT.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500105 rt = gpu->wrapBackendRenderTarget(backendRT);
106 REPORTER_ASSERT(reporter, !rt);
107 }
jvanverthfd359ca2016-03-18 11:57:24 -0700108
109 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500110 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400111 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500112 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500113 // can wrap null alloc
Brian Salomonbdecacf2018-02-02 20:32:49 -0500114 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400115 backendRT.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500116 rt = gpu->wrapBackendRenderTarget(backendRT);
117 REPORTER_ASSERT(reporter, rt);
118 }
jvanverthfd359ca2016-03-18 11:57:24 -0700119
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000120 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
121 // resource when we're done.
122 gpu->deleteTestingOnlyBackendTexture(origBackendTex);
jvanverthfd359ca2016-03-18 11:57:24 -0700123}
124
125void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500126 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -0700127
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500128 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -0400129 kColorType, true,
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500130 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -0400131 GrVkImageInfo imageInfo;
132 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -0700133
Brian Salomonbdecacf2018-02-02 20:32:49 -0500134 sk_sp<GrTexture> tex =
135 gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -0700136 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700137
138 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500139 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400140 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500141 backendCopy.fImage = VK_NULL_HANDLE;
142 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400143 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500144 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500145 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500146 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500147 REPORTER_ASSERT(reporter, !tex);
148 }
jvanverthfd359ca2016-03-18 11:57:24 -0700149
150 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500151 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400152 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500153 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500154 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400155 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500156 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500157 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500158 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500159 REPORTER_ASSERT(reporter, !tex);
160 }
jvanverthfd359ca2016-03-18 11:57:24 -0700161
162 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500163 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400164 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500165 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400166 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500167 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500168 REPORTER_ASSERT(reporter, tex);
169 }
jvanverthfd359ca2016-03-18 11:57:24 -0700170}
171
bsalomondc0fcd42016-04-11 14:21:33 -0700172DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700173 wrap_tex_test(reporter, ctxInfo.grContext());
174 wrap_rt_test(reporter, ctxInfo.grContext());
175 wrap_trt_test(reporter, ctxInfo.grContext());
jvanverthfd359ca2016-03-18 11:57:24 -0700176}
177
178#endif