blob: 5b2f2096a70348eea11290a325eaf75adc4cdb6e [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
Herb Derbyd3895d82018-09-04 13:27:00 -040014#include "GrBackendSurface.h"
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050015#include "GrContextPriv.h"
jvanverthfd359ca2016-03-18 11:57:24 -070016#include "GrContextFactory.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040017#include "GrRenderTarget.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;
Robert Phillips646f6372018-09-25 09:31:10 -040031const GrColorType kColorType = GrColorType::kRGBA_8888;
jvanverthfd359ca2016-03-18 11:57:24 -070032
33void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
34
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050035 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070036
Robert Phillipsd21b2a52017-12-12 13:01:25 -050037 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -040038 kColorType, false,
Robert Phillipsd21b2a52017-12-12 13:01:25 -050039 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -040040 GrVkImageInfo imageInfo;
41 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070042
Robert Phillipsd21b2a52017-12-12 13:01:25 -050043 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -070044 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -070045
46 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050047 {
Greg Daniel52e16d92018-04-10 09:34:07 -040048 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050049 backendCopy.fImage = VK_NULL_HANDLE;
50 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040051 backendTex.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050052 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
53 REPORTER_ASSERT(reporter, !tex);
54 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
55 REPORTER_ASSERT(reporter, !tex);
56 }
jvanverthfd359ca2016-03-18 11:57:24 -070057
58 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050059 {
Greg Daniel52e16d92018-04-10 09:34:07 -040060 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050061 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050062 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040063 backendTex.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050064 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
65 REPORTER_ASSERT(reporter, !tex);
66 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
67 REPORTER_ASSERT(reporter, !tex);
68 }
69
Stan Iliev7fa5c312017-04-19 00:23:39 +000070 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050071 {
Greg Daniel52e16d92018-04-10 09:34:07 -040072 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050073 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040074 backendTex.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050075 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000076
Robert Phillipsd21b2a52017-12-12 13:01:25 -050077 REPORTER_ASSERT(reporter, tex);
78 }
jvanverthfd359ca2016-03-18 11:57:24 -070079}
80
81void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050082 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070083
Greg Daniel8a3f55c2018-03-14 17:32:12 +000084 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -040085 kColorType, true,
Greg Daniel8a3f55c2018-03-14 17:32:12 +000086 GrMipMapped::kNo);
Greg Daniel108bb232018-07-03 16:18:29 -040087
Greg Daniel52e16d92018-04-10 09:34:07 -040088 GrVkImageInfo imageInfo;
89 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070090
Greg Daniel52e16d92018-04-10 09:34:07 -040091 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
Greg Daniel108bb232018-07-03 16:18:29 -040092 origBackendRT.setPixelConfig(kPixelConfig);
Greg Danielbcf612b2017-05-01 13:50:58 +000093
Robert Phillipsd21b2a52017-12-12 13:01:25 -050094 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
jvanverthfd359ca2016-03-18 11:57:24 -070095 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -070096
97 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050098 {
Greg Daniel52e16d92018-04-10 09:34:07 -040099 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500100 backendCopy.fImage = VK_NULL_HANDLE;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500101 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400102 backendRT.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500103 rt = gpu->wrapBackendRenderTarget(backendRT);
104 REPORTER_ASSERT(reporter, !rt);
105 }
jvanverthfd359ca2016-03-18 11:57:24 -0700106
107 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500108 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400109 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500110 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500111 // can wrap null alloc
Brian Salomonbdecacf2018-02-02 20:32:49 -0500112 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400113 backendRT.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500114 rt = gpu->wrapBackendRenderTarget(backendRT);
115 REPORTER_ASSERT(reporter, rt);
116 }
jvanverthfd359ca2016-03-18 11:57:24 -0700117
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000118 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
119 // resource when we're done.
120 gpu->deleteTestingOnlyBackendTexture(origBackendTex);
jvanverthfd359ca2016-03-18 11:57:24 -0700121}
122
123void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500124 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -0700125
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500126 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -0400127 kColorType, true,
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500128 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -0400129 GrVkImageInfo imageInfo;
130 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -0700131
Brian Salomonbdecacf2018-02-02 20:32:49 -0500132 sk_sp<GrTexture> tex =
133 gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -0700134 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700135
136 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500137 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400138 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500139 backendCopy.fImage = VK_NULL_HANDLE;
140 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400141 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500142 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500143 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500144 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500145 REPORTER_ASSERT(reporter, !tex);
146 }
jvanverthfd359ca2016-03-18 11:57:24 -0700147
148 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500149 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400150 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500151 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500152 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400153 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500154 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500155 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500156 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500157 REPORTER_ASSERT(reporter, !tex);
158 }
jvanverthfd359ca2016-03-18 11:57:24 -0700159
160 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500161 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400162 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500163 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400164 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500165 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500166 REPORTER_ASSERT(reporter, tex);
167 }
jvanverthfd359ca2016-03-18 11:57:24 -0700168}
169
bsalomondc0fcd42016-04-11 14:21:33 -0700170DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700171 wrap_tex_test(reporter, ctxInfo.grContext());
172 wrap_rt_test(reporter, ctxInfo.grContext());
173 wrap_trt_test(reporter, ctxInfo.grContext());
jvanverthfd359ca2016-03-18 11:57:24 -0700174}
175
176#endif