blob: 367fdb5c5324f7f13b635580a7b70efafab5000e [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
Jim Van Verth91589312017-06-22 12:52:46 -040012#if SK_SUPPORT_GPU && defined(SK_VULKAN)
jvanverthfd359ca2016-03-18 11:57:24 -070013
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050014#include "GrContextPriv.h"
jvanverthfd359ca2016-03-18 11:57:24 -070015#include "GrContextFactory.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040016#include "GrRenderTarget.h"
jvanverthfd359ca2016-03-18 11:57:24 -070017#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018#include "GrTexture.h"
19
jvanverthfd359ca2016-03-18 11:57:24 -070020#include "Test.h"
21#include "vk/GrVkCaps.h"
22#include "vk/GrVkGpu.h"
23#include "vk/GrVkMemory.h"
24#include "vk/GrVkTypes.h"
25
kkinnunen2ae4b2e2016-03-31 08:08:20 -070026using sk_gpu_test::GrContextFactory;
27
jvanverthfd359ca2016-03-18 11:57:24 -070028const int kW = 1024;
29const int kH = 1024;
30const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig;
31
32void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
33
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050034 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070035
Robert Phillipsd21b2a52017-12-12 13:01:25 -050036 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
37 kPixelConfig, false,
38 GrMipMapped::kNo);
39 const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
jvanverthfd359ca2016-03-18 11:57:24 -070040
Robert Phillipsd21b2a52017-12-12 13:01:25 -050041 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -070042 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -070043
44 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050045 {
46 GrVkImageInfo backendCopy = *imageInfo;
47 backendCopy.fImage = VK_NULL_HANDLE;
48 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
49 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
50 REPORTER_ASSERT(reporter, !tex);
51 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
52 REPORTER_ASSERT(reporter, !tex);
53 }
jvanverthfd359ca2016-03-18 11:57:24 -070054
55 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050056 {
57 GrVkImageInfo backendCopy = *imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050058 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050059 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
60 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
61 REPORTER_ASSERT(reporter, !tex);
62 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
63 REPORTER_ASSERT(reporter, !tex);
64 }
65
Stan Iliev7fa5c312017-04-19 00:23:39 +000066 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050067 {
68 GrVkImageInfo backendCopy = *imageInfo;
69 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
70 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000071
Robert Phillipsd21b2a52017-12-12 13:01:25 -050072 REPORTER_ASSERT(reporter, tex);
73 }
jvanverthfd359ca2016-03-18 11:57:24 -070074}
75
76void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050077 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070078
Robert Phillipsd21b2a52017-12-12 13:01:25 -050079 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
80 kPixelConfig, true,
81 GrMipMapped::kNo);
82 const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
jvanverthfd359ca2016-03-18 11:57:24 -070083
Brian Salomonbdecacf2018-02-02 20:32:49 -050084 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, *imageInfo);
Greg Danielbcf612b2017-05-01 13:50:58 +000085
Robert Phillipsd21b2a52017-12-12 13:01:25 -050086 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
jvanverthfd359ca2016-03-18 11:57:24 -070087 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -070088
89 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050090 {
91 GrVkImageInfo backendCopy = *imageInfo;
92 backendCopy.fImage = VK_NULL_HANDLE;
Brian Salomonbdecacf2018-02-02 20:32:49 -050093 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050094 rt = gpu->wrapBackendRenderTarget(backendRT);
95 REPORTER_ASSERT(reporter, !rt);
96 }
jvanverthfd359ca2016-03-18 11:57:24 -070097
98 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050099 {
100 GrVkImageInfo backendCopy = *imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500101 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500102 // can wrap null alloc
Brian Salomonbdecacf2018-02-02 20:32:49 -0500103 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500104 rt = gpu->wrapBackendRenderTarget(backendRT);
105 REPORTER_ASSERT(reporter, rt);
106 }
jvanverthfd359ca2016-03-18 11:57:24 -0700107
Greg Danielaf401272017-03-27 16:18:03 -0400108 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
109 // resource when we're done.
Brian Salomon26102cb2018-03-09 09:33:19 -0500110 gpu->deleteTestingOnlyBackendTexture(origBackendTex);
jvanverthfd359ca2016-03-18 11:57:24 -0700111}
112
113void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500114 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -0700115
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500116 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
117 kPixelConfig, true,
118 GrMipMapped::kNo);
119 const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
jvanverthfd359ca2016-03-18 11:57:24 -0700120
Brian Salomonbdecacf2018-02-02 20:32:49 -0500121 sk_sp<GrTexture> tex =
122 gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -0700123 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700124
125 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500126 {
127 GrVkImageInfo backendCopy = *imageInfo;
128 backendCopy.fImage = VK_NULL_HANDLE;
129 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500130 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500131 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500132 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500133 REPORTER_ASSERT(reporter, !tex);
134 }
jvanverthfd359ca2016-03-18 11:57:24 -0700135
136 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500137 {
138 GrVkImageInfo backendCopy = *imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500139 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500140 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500142 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500144 REPORTER_ASSERT(reporter, !tex);
145 }
jvanverthfd359ca2016-03-18 11:57:24 -0700146
147 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500148 {
149 GrVkImageInfo backendCopy = *imageInfo;
150 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500151 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500152 REPORTER_ASSERT(reporter, tex);
153 }
jvanverthfd359ca2016-03-18 11:57:24 -0700154}
155
bsalomondc0fcd42016-04-11 14:21:33 -0700156DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700157 wrap_tex_test(reporter, ctxInfo.grContext());
158 wrap_rt_test(reporter, ctxInfo.grContext());
159 wrap_trt_test(reporter, ctxInfo.grContext());
jvanverthfd359ca2016-03-18 11:57:24 -0700160}
161
162#endif