blob: 7c846adb7c7564179a1f564e4cb287803529888a [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
Brian Salomonc67c31c2018-12-06 10:00:03 -050045 sk_sp<GrTexture> tex =
46 gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership, kRead_GrIOType, false);
jvanverthfd359ca2016-03-18 11:57:24 -070047 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -070048
49 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050050 {
Greg Daniel52e16d92018-04-10 09:34:07 -040051 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050052 backendCopy.fImage = VK_NULL_HANDLE;
53 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040054 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonc67c31c2018-12-06 10:00:03 -050055 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, kRead_GrIOType, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050056 REPORTER_ASSERT(reporter, !tex);
Brian Salomonc67c31c2018-12-06 10:00:03 -050057 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, kRead_GrIOType, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050058 REPORTER_ASSERT(reporter, !tex);
59 }
jvanverthfd359ca2016-03-18 11:57:24 -070060
61 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -050062 {
Greg Daniel52e16d92018-04-10 09:34:07 -040063 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -050064 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -050065 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040066 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonc67c31c2018-12-06 10:00:03 -050067 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, kRead_GrIOType, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050068 REPORTER_ASSERT(reporter, !tex);
Brian Salomonc67c31c2018-12-06 10:00:03 -050069 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, kRead_GrIOType, false);
Robert Phillipsd21b2a52017-12-12 13:01:25 -050070 REPORTER_ASSERT(reporter, !tex);
71 }
72
Stan Iliev7fa5c312017-04-19 00:23:39 +000073 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -050074 {
Greg Daniel52e16d92018-04-10 09:34:07 -040075 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050076 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -040077 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonc67c31c2018-12-06 10:00:03 -050078 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, kRead_GrIOType, false);
Greg Daniel7ef28f32017-04-20 16:41:55 +000079
Robert Phillipsd21b2a52017-12-12 13:01:25 -050080 REPORTER_ASSERT(reporter, tex);
81 }
jvanverthfd359ca2016-03-18 11:57:24 -070082}
83
84void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050085 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -070086
Greg Daniel8a3f55c2018-03-14 17:32:12 +000087 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -040088 kColorType, true,
Greg Daniel8a3f55c2018-03-14 17:32:12 +000089 GrMipMapped::kNo);
Greg Daniel108bb232018-07-03 16:18:29 -040090
Greg Daniel52e16d92018-04-10 09:34:07 -040091 GrVkImageInfo imageInfo;
92 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -070093
Greg Daniel52e16d92018-04-10 09:34:07 -040094 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
Greg Daniel108bb232018-07-03 16:18:29 -040095 origBackendRT.setPixelConfig(kPixelConfig);
Greg Danielbcf612b2017-05-01 13:50:58 +000096
Robert Phillipsd21b2a52017-12-12 13:01:25 -050097 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
jvanverthfd359ca2016-03-18 11:57:24 -070098 REPORTER_ASSERT(reporter, rt);
jvanverthfd359ca2016-03-18 11:57:24 -070099
100 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500101 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400102 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500103 backendCopy.fImage = VK_NULL_HANDLE;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500104 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400105 backendRT.setPixelConfig(kPixelConfig);
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
110 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500111 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400112 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500113 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500114 // can wrap null alloc
Brian Salomonbdecacf2018-02-02 20:32:49 -0500115 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400116 backendRT.setPixelConfig(kPixelConfig);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500117 rt = gpu->wrapBackendRenderTarget(backendRT);
118 REPORTER_ASSERT(reporter, rt);
119 }
jvanverthfd359ca2016-03-18 11:57:24 -0700120
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000121 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
122 // resource when we're done.
123 gpu->deleteTestingOnlyBackendTexture(origBackendTex);
jvanverthfd359ca2016-03-18 11:57:24 -0700124}
125
126void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500127 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
jvanverthfd359ca2016-03-18 11:57:24 -0700128
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500129 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -0400130 kColorType, true,
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500131 GrMipMapped::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -0400132 GrVkImageInfo imageInfo;
133 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
jvanverthfd359ca2016-03-18 11:57:24 -0700134
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 sk_sp<GrTexture> tex =
136 gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
jvanverthfd359ca2016-03-18 11:57:24 -0700137 REPORTER_ASSERT(reporter, tex);
jvanverthfd359ca2016-03-18 11:57:24 -0700138
139 // image is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500140 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400141 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500142 backendCopy.fImage = VK_NULL_HANDLE;
143 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400144 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500145 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500146 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500147 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500148 REPORTER_ASSERT(reporter, !tex);
149 }
jvanverthfd359ca2016-03-18 11:57:24 -0700150
151 // alloc is null
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500152 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400153 GrVkImageInfo backendCopy = imageInfo;
Greg Daniel8385a8a2018-02-26 13:29:37 -0500154 backendCopy.fAlloc = GrVkAlloc();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500155 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400156 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500157 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500158 REPORTER_ASSERT(reporter, !tex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500159 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500160 REPORTER_ASSERT(reporter, !tex);
161 }
jvanverthfd359ca2016-03-18 11:57:24 -0700162
163 // check adopt creation
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500164 {
Greg Daniel52e16d92018-04-10 09:34:07 -0400165 GrVkImageInfo backendCopy = imageInfo;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500166 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
Greg Daniel108bb232018-07-03 16:18:29 -0400167 backendTex.setPixelConfig(kPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500168 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500169 REPORTER_ASSERT(reporter, tex);
170 }
jvanverthfd359ca2016-03-18 11:57:24 -0700171}
172
bsalomondc0fcd42016-04-11 14:21:33 -0700173DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700174 wrap_tex_test(reporter, ctxInfo.grContext());
175 wrap_rt_test(reporter, ctxInfo.grContext());
176 wrap_trt_test(reporter, ctxInfo.grContext());
jvanverthfd359ca2016-03-18 11:57:24 -0700177}
178
179#endif