blob: 2bfcd4abd62e937fe9e0e0a834cb5f3ca0a9904a [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
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);
Greg Daniel52e16d92018-04-10 09:34:07 -040039 GrVkImageInfo imageInfo;
40 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070041
Robert Phillipsd21b2a52017-12-12 13:01:25 -050042 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -070043 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -070044
45 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050046 {
Greg Daniel52e16d92018-04-10 09:34:07 -040047 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050048 backendCopy.fImage = VK_NULL_HANDLE;
49 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
50 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
51 REPORTER_ASSERT(reporter, !tex);
52 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
53 REPORTER_ASSERT(reporter, !tex);
54 }
jvanverthfd359ca2016-03-18 11:57:24 -070055
56 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050057 {
Greg Daniel52e16d92018-04-10 09:34:07 -040058 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050059 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050060 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
61 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
62 REPORTER_ASSERT(reporter, !tex);
63 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
64 REPORTER_ASSERT(reporter, !tex);
65 }
66
Stan Iliev7fa5c312017-04-19 00:23:39 +000067 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050068 {
Greg Daniel52e16d92018-04-10 09:34:07 -040069 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050070 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
71 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000072
Robert Phillipsd21b2a52017-12-12 13:01:25 -050073 REPORTER_ASSERT(reporter, tex);
74 }
jvanverthfd359ca2016-03-18 11:57:24 -070075}
76
77void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050078 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070079
Greg Daniel8a3f55c2018-03-14 17:32:12 +000080 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
81 kPixelConfig, true,
82 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -040083 GrVkImageInfo imageInfo;
84 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070085
Greg Daniel52e16d92018-04-10 09:34:07 -040086 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
Greg Danielbcf612b2017-05-01 13:50:58 +000087
Robert Phillipsd21b2a52017-12-12 13:01:25 -050088 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
jvanverthfd359ca2016-03-18 11:57:24 -070089 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -070090
91 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050092 {
Greg Daniel52e16d92018-04-10 09:34:07 -040093 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050094 backendCopy.fImage = VK_NULL_HANDLE;
Brian Salomonbdecacf2018-02-02 20:32:49 -050095 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050096 rt = gpu->wrapBackendRenderTarget(backendRT);
97 REPORTER_ASSERT(reporter, !rt);
98 }
jvanverthfd359ca2016-03-18 11:57:24 -070099
100 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500101 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400102 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500103 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500104 // can wrap null alloc
Brian Salomonbdecacf2018-02-02 20:32:49 -0500105 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500106 rt = gpu->wrapBackendRenderTarget(backendRT);
107 REPORTER_ASSERT(reporter, rt);
108 }
jvanverthfd359ca2016-03-18 11:57:24 -0700109
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000110 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
111 // resource when we're done.
112 gpu->deleteTestingOnlyBackendTexture(origBackendTex);
jvanverthfd359ca2016-03-18 11:57:24 -0700113}
114
115void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500116 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -0700117
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500118 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
119 kPixelConfig, true,
120 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -0400121 GrVkImageInfo imageInfo;
122 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -0700123
Brian Salomonbdecacf2018-02-02 20:32:49 -0500124 sk_sp<GrTexture> tex =
125 gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -0700126 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700127
128 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500129 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400130 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500131 backendCopy.fImage = VK_NULL_HANDLE;
132 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500134 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500136 REPORTER_ASSERT(reporter, !tex);
137 }
jvanverthfd359ca2016-03-18 11:57:24 -0700138
139 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500140 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400141 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500142 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500143 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
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 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500151 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400152 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500153 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500154 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500155 REPORTER_ASSERT(reporter, tex);
156 }
jvanverthfd359ca2016-03-18 11:57:24 -0700157}
158
bsalomondc0fcd42016-04-11 14:21:33 -0700159DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700160 wrap_tex_test(reporter, ctxInfo.grContext());
161 wrap_rt_test(reporter, ctxInfo.grContext());
162 wrap_trt_test(reporter, ctxInfo.grContext());
jvanverthfd359ca2016-03-18 11:57:24 -0700163}
164
165#endif