blob: 9723d5763bfd2cb7fbd10b29e8df1e5818b419a2 [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;
58 backendCopy.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
59 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
Robert Phillipsd21b2a52017-12-12 13:01:25 -050075 gpu->deleteTestingOnlyBackendTexture(&origBackendTex, true);
jvanverthfd359ca2016-03-18 11:57:24 -070076}
77
78void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050079 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070080
Robert Phillipsd21b2a52017-12-12 13:01:25 -050081 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
82 kPixelConfig, true,
83 GrMipMapped::kNo);
84 const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
jvanverthfd359ca2016-03-18 11:57:24 -070085
Brian Salomonbdecacf2018-02-02 20:32:49 -050086 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 {
93 GrVkImageInfo backendCopy = *imageInfo;
94 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 {
102 GrVkImageInfo backendCopy = *imageInfo;
103 backendCopy.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
104 // 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 Danielaf401272017-03-27 16:18:03 -0400110 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
111 // resource when we're done.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500112 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);
121 const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
jvanverthfd359ca2016-03-18 11:57:24 -0700122
Brian Salomonbdecacf2018-02-02 20:32:49 -0500123 sk_sp<GrTexture> tex =
124 gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -0700125 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700126
127 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500128 {
129 GrVkImageInfo backendCopy = *imageInfo;
130 backendCopy.fImage = VK_NULL_HANDLE;
131 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500132 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500133 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500134 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500135 REPORTER_ASSERT(reporter, !tex);
136 }
jvanverthfd359ca2016-03-18 11:57:24 -0700137
138 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500139 {
140 GrVkImageInfo backendCopy = *imageInfo;
141 backendCopy.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
142 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500144 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500145 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500146 REPORTER_ASSERT(reporter, !tex);
147 }
jvanverthfd359ca2016-03-18 11:57:24 -0700148
149 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500150 {
151 GrVkImageInfo backendCopy = *imageInfo;
152 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500153 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500154 REPORTER_ASSERT(reporter, tex);
155 }
jvanverthfd359ca2016-03-18 11:57:24 -0700156
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500157 gpu->deleteTestingOnlyBackendTexture(&origBackendTex, true);
jvanverthfd359ca2016-03-18 11:57:24 -0700158}
159
bsalomondc0fcd42016-04-11 14:21:33 -0700160DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700161 wrap_tex_test(reporter, ctxInfo.grContext());
162 wrap_rt_test(reporter, ctxInfo.grContext());
163 wrap_trt_test(reporter, ctxInfo.grContext());
jvanverthfd359ca2016-03-18 11:57:24 -0700164}
165
166#endif