1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci// This is a GPU-backend specific test. It relies on static intializers to work
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#if defined(SK_VULKAN)
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include "include/gpu/GrBackendSurface.h"
15cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
16cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkTypes.h"
17cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkVulkan.h"
18cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
19cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTarget.h"
20cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
21cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkCaps.h"
22cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkGpu.h"
23cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkMemory.h"
24cb93a386Sopenharmony_ci#include "tests/Test.h"
25cb93a386Sopenharmony_ci#include "tools/gpu/GrContextFactory.h"
26cb93a386Sopenharmony_ci#include "tools/gpu/ManagedBackendTexture.h"
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ciusing sk_gpu_test::GrContextFactory;
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ciconst int kW = 1024;
31cb93a386Sopenharmony_ciconst int kH = 1024;
32cb93a386Sopenharmony_ciconst SkColorType kColorType = SkColorType::kRGBA_8888_SkColorType;
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_civoid wrap_tex_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
35cb93a386Sopenharmony_ci    GrGpu* gpu = dContext->priv().getGpu();
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
38cb93a386Sopenharmony_ci            dContext, kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
39cb93a386Sopenharmony_ci    if (!mbet) {
40cb93a386Sopenharmony_ci        ERRORF(reporter, "Could not create backend texture.");
41cb93a386Sopenharmony_ci        return;
42cb93a386Sopenharmony_ci    }
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    GrBackendTexture origBackendTex = mbet->texture();
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    GrVkImageInfo imageInfo;
47cb93a386Sopenharmony_ci    SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci    {
50cb93a386Sopenharmony_ci        sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership,
51cb93a386Sopenharmony_ci                                                       GrWrapCacheable::kNo, kRead_GrIOType);
52cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tex);
53cb93a386Sopenharmony_ci    }
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci    // image is null
56cb93a386Sopenharmony_ci    {
57cb93a386Sopenharmony_ci        GrVkImageInfo backendCopy = imageInfo;
58cb93a386Sopenharmony_ci        backendCopy.fImage = VK_NULL_HANDLE;
59cb93a386Sopenharmony_ci        GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
60cb93a386Sopenharmony_ci        sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
61cb93a386Sopenharmony_ci                backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
62cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tex);
63cb93a386Sopenharmony_ci        tex = gpu->wrapBackendTexture(
64cb93a386Sopenharmony_ci                backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
65cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tex);
66cb93a386Sopenharmony_ci    }
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    // alloc is null
69cb93a386Sopenharmony_ci    {
70cb93a386Sopenharmony_ci        GrVkImageInfo backendCopy = imageInfo;
71cb93a386Sopenharmony_ci        backendCopy.fAlloc = GrVkAlloc();
72cb93a386Sopenharmony_ci        GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
73cb93a386Sopenharmony_ci        sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
74cb93a386Sopenharmony_ci                backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
75cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tex);
76cb93a386Sopenharmony_ci        tex = gpu->wrapBackendTexture(
77cb93a386Sopenharmony_ci                backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
78cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tex);
79cb93a386Sopenharmony_ci    }
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci    // check adopt creation
82cb93a386Sopenharmony_ci    {
83cb93a386Sopenharmony_ci        GrVkImageInfo backendCopy = imageInfo;
84cb93a386Sopenharmony_ci        GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
85cb93a386Sopenharmony_ci        sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
86cb93a386Sopenharmony_ci                backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tex);
89cb93a386Sopenharmony_ci        if (tex) {
90cb93a386Sopenharmony_ci            mbet->wasAdopted();
91cb93a386Sopenharmony_ci        }
92cb93a386Sopenharmony_ci    }
93cb93a386Sopenharmony_ci}
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_civoid wrap_rt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
96cb93a386Sopenharmony_ci    GrGpu* gpu = dContext->priv().getGpu();
97cb93a386Sopenharmony_ci    GrColorType ct = SkColorTypeToGrColorType(kColorType);
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci    for (int sampleCnt : {1, 4}) {
100cb93a386Sopenharmony_ci        GrBackendFormat format = gpu->caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
101cb93a386Sopenharmony_ci        if (sampleCnt > gpu->caps()->maxRenderTargetSampleCount(format)) {
102cb93a386Sopenharmony_ci            continue;
103cb93a386Sopenharmony_ci        }
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci        GrBackendRenderTarget origBackendRT =
106cb93a386Sopenharmony_ci                gpu->createTestingOnlyBackendRenderTarget({kW, kH}, ct, sampleCnt);
107cb93a386Sopenharmony_ci        if (!origBackendRT.isValid()) {
108cb93a386Sopenharmony_ci            ERRORF(reporter, "Could not create backend render target.");
109cb93a386Sopenharmony_ci        }
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci        GrVkImageInfo imageInfo;
112cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, origBackendRT.getVkImageInfo(&imageInfo));
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ci        sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
115cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, rt);
116cb93a386Sopenharmony_ci
117cb93a386Sopenharmony_ci        // image is null
118cb93a386Sopenharmony_ci        {
119cb93a386Sopenharmony_ci            GrVkImageInfo backendCopy = imageInfo;
120cb93a386Sopenharmony_ci            backendCopy.fImage = VK_NULL_HANDLE;
121cb93a386Sopenharmony_ci            GrBackendRenderTarget backendRT(kW, kH, 1, backendCopy);
122cb93a386Sopenharmony_ci            rt = gpu->wrapBackendRenderTarget(backendRT);
123cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, !rt);
124cb93a386Sopenharmony_ci        }
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci        // alloc is null
127cb93a386Sopenharmony_ci        {
128cb93a386Sopenharmony_ci            GrVkImageInfo backendCopy = imageInfo;
129cb93a386Sopenharmony_ci            backendCopy.fAlloc = GrVkAlloc();
130cb93a386Sopenharmony_ci            // can wrap null alloc
131cb93a386Sopenharmony_ci            GrBackendRenderTarget backendRT(kW, kH, 1, backendCopy);
132cb93a386Sopenharmony_ci            rt = gpu->wrapBackendRenderTarget(backendRT);
133cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, rt);
134cb93a386Sopenharmony_ci        }
135cb93a386Sopenharmony_ci
136cb93a386Sopenharmony_ci        gpu->deleteTestingOnlyBackendRenderTarget(origBackendRT);
137cb93a386Sopenharmony_ci    }
138cb93a386Sopenharmony_ci}
139cb93a386Sopenharmony_ci
140cb93a386Sopenharmony_civoid wrap_trt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
141cb93a386Sopenharmony_ci    GrGpu* gpu = dContext->priv().getGpu();
142cb93a386Sopenharmony_ci
143cb93a386Sopenharmony_ci    auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
144cb93a386Sopenharmony_ci            dContext, kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kYes);
145cb93a386Sopenharmony_ci    if (!mbet) {
146cb93a386Sopenharmony_ci        ERRORF(reporter, "Could not create renderable backend texture.");
147cb93a386Sopenharmony_ci        return;
148cb93a386Sopenharmony_ci    }
149cb93a386Sopenharmony_ci    GrBackendTexture origBackendTex = mbet->texture();
150cb93a386Sopenharmony_ci
151cb93a386Sopenharmony_ci    GrVkImageInfo imageInfo;
152cb93a386Sopenharmony_ci    SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
153cb93a386Sopenharmony_ci
154cb93a386Sopenharmony_ci    sk_sp<GrTexture> tex = gpu->wrapRenderableBackendTexture(
155cb93a386Sopenharmony_ci            origBackendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
156cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, tex);
157cb93a386Sopenharmony_ci
158cb93a386Sopenharmony_ci    // image is null
159cb93a386Sopenharmony_ci    {
160cb93a386Sopenharmony_ci        GrVkImageInfo backendCopy = imageInfo;
161cb93a386Sopenharmony_ci        backendCopy.fImage = VK_NULL_HANDLE;
162cb93a386Sopenharmony_ci        GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
163cb93a386Sopenharmony_ci        tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
164cb93a386Sopenharmony_ci                                                GrWrapCacheable::kNo);
165cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tex);
166cb93a386Sopenharmony_ci        tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
167cb93a386Sopenharmony_ci                                                GrWrapCacheable::kNo);
168cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tex);
169cb93a386Sopenharmony_ci    }
170cb93a386Sopenharmony_ci
171cb93a386Sopenharmony_ci    // alloc is null
172cb93a386Sopenharmony_ci    {
173cb93a386Sopenharmony_ci        GrVkImageInfo backendCopy = imageInfo;
174cb93a386Sopenharmony_ci        backendCopy.fAlloc = GrVkAlloc();
175cb93a386Sopenharmony_ci        GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
176cb93a386Sopenharmony_ci        tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
177cb93a386Sopenharmony_ci                                                GrWrapCacheable::kNo);
178cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tex);
179cb93a386Sopenharmony_ci        tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
180cb93a386Sopenharmony_ci                                                GrWrapCacheable::kNo);
181cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !tex);
182cb93a386Sopenharmony_ci    }
183cb93a386Sopenharmony_ci
184cb93a386Sopenharmony_ci    // check rendering with MSAA
185cb93a386Sopenharmony_ci    {
186cb93a386Sopenharmony_ci        int maxSamples = dContext->priv().caps()->maxRenderTargetSampleCount(
187cb93a386Sopenharmony_ci                origBackendTex.getBackendFormat());
188cb93a386Sopenharmony_ci        bool shouldSucceed = maxSamples > 1;
189cb93a386Sopenharmony_ci        tex = gpu->wrapRenderableBackendTexture(origBackendTex, 2, kBorrow_GrWrapOwnership,
190cb93a386Sopenharmony_ci                                                GrWrapCacheable::kNo);
191cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, SkToBool(tex) == shouldSucceed);
192cb93a386Sopenharmony_ci    }
193cb93a386Sopenharmony_ci
194cb93a386Sopenharmony_ci    // check adopt creation
195cb93a386Sopenharmony_ci    {
196cb93a386Sopenharmony_ci        GrVkImageInfo backendCopy = imageInfo;
197cb93a386Sopenharmony_ci        GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
198cb93a386Sopenharmony_ci        tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
199cb93a386Sopenharmony_ci                                                GrWrapCacheable::kNo);
200cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, tex);
201cb93a386Sopenharmony_ci        if (tex) {
202cb93a386Sopenharmony_ci            mbet->wasAdopted();
203cb93a386Sopenharmony_ci        }
204cb93a386Sopenharmony_ci    }
205cb93a386Sopenharmony_ci}
206cb93a386Sopenharmony_ci
207cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
208cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
209cb93a386Sopenharmony_ci
210cb93a386Sopenharmony_ci    wrap_tex_test(reporter, dContext);
211cb93a386Sopenharmony_ci    wrap_rt_test(reporter, dContext);
212cb93a386Sopenharmony_ci    wrap_trt_test(reporter, dContext);
213cb93a386Sopenharmony_ci}
214cb93a386Sopenharmony_ci
215cb93a386Sopenharmony_ci#endif
216