1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2018 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/core/SkImage.h"
15cb93a386Sopenharmony_ci#include "include/gpu/GrBackendSurface.h"
16cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
17cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkTypes.h"
18cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkVulkan.h"
19cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
20cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
21cb93a386Sopenharmony_ci#include "src/gpu/GrTextureProxy.h"
22cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkGpu.h"
23cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkImageLayout.h"
24cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkTexture.h"
25cb93a386Sopenharmony_ci#include "src/image/SkImage_Base.h"
26cb93a386Sopenharmony_ci#include "src/image/SkImage_Gpu.h"
27cb93a386Sopenharmony_ci#include "src/image/SkImage_GpuBase.h"
28cb93a386Sopenharmony_ci#include "src/image/SkSurface_Gpu.h"
29cb93a386Sopenharmony_ci#include "tests/Test.h"
30cb93a386Sopenharmony_ci#include "tools/gpu/ManagedBackendTexture.h"
31cb93a386Sopenharmony_ci#include "tools/gpu/ProxyUtils.h"
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDRMModifierTest, reporter, ctxInfo) {
34cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    const GrVkCaps* vkCaps = static_cast<const GrVkCaps*>(dContext->priv().caps());
37cb93a386Sopenharmony_ci    if (!vkCaps->supportsDRMFormatModifiers()) {
38cb93a386Sopenharmony_ci        return;
39cb93a386Sopenharmony_ci    }
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    // First make a normal backend texture with DRM
42cb93a386Sopenharmony_ci    auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
43cb93a386Sopenharmony_ci            dContext, 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
44cb93a386Sopenharmony_ci    if (!mbet) {
45cb93a386Sopenharmony_ci        ERRORF(reporter, "Could not create backend texture.");
46cb93a386Sopenharmony_ci        return;
47cb93a386Sopenharmony_ci    }
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci    GrVkImageInfo info;
50cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, mbet->texture().getVkImageInfo(&info));
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    // Next we will use the same VkImageInfo but lie to say tiling is a DRM modifier. This should
53cb93a386Sopenharmony_ci    // cause us to think the resource is eternal/read only internally. Though since we don't
54cb93a386Sopenharmony_ci    // explicitly pass in the tiling to anything, this shouldn't cause us to do anything illegal.
55cb93a386Sopenharmony_ci    info.fImageTiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci    GrBackendTexture drmBETex = GrBackendTexture(1, 1, info);
58cb93a386Sopenharmony_ci    GrBackendFormat drmFormat = GrBackendFormat::MakeVk(info.fFormat, true);
59cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, drmFormat == drmBETex.getBackendFormat());
60cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, drmBETex.textureType() == GrTextureType::kExternal);
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    // Now wrap the texture in an SkImage and make sure we have the required read only properties
63cb93a386Sopenharmony_ci    sk_sp<SkImage> drmImage = SkImage::MakeFromTexture(dContext,
64cb93a386Sopenharmony_ci                                                       drmBETex,
65cb93a386Sopenharmony_ci                                                       kTopLeft_GrSurfaceOrigin,
66cb93a386Sopenharmony_ci                                                       kRGBA_8888_SkColorType,
67cb93a386Sopenharmony_ci                                                       kPremul_SkAlphaType,
68cb93a386Sopenharmony_ci                                                       nullptr);
69cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, drmImage);
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter,
72cb93a386Sopenharmony_ci            GrBackendTexture::TestingOnly_Equals(drmImage->getBackendTexture(false), drmBETex));
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ci    auto[view, _] = as_IB(drmImage.get()) -> asView(dContext, GrMipmapped::kNo);
75cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, view);
76cb93a386Sopenharmony_ci    const GrSurfaceProxy* proxy = view.proxy();
77cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, proxy);
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, proxy->readOnly());
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci    const GrSurface* surf = proxy->peekSurface();
82cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, surf);
83cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, surf->readOnly());
84cb93a386Sopenharmony_ci
85cb93a386Sopenharmony_ci    drmImage.reset();
86cb93a386Sopenharmony_ci}
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) {
89cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci    auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
92cb93a386Sopenharmony_ci            dContext, 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
93cb93a386Sopenharmony_ci    if (!mbet) {
94cb93a386Sopenharmony_ci        ERRORF(reporter, "Could not create backend texture.");
95cb93a386Sopenharmony_ci        return;
96cb93a386Sopenharmony_ci    }
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_ci    GrVkImageInfo info;
99cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, mbet->texture().getVkImageInfo(&info));
100cb93a386Sopenharmony_ci    VkImageLayout initLayout = info.fImageLayout;
101cb93a386Sopenharmony_ci
102cb93a386Sopenharmony_ci    // Verify that setting that layout via a copy of a backendTexture is reflected in all the
103cb93a386Sopenharmony_ci    // backendTextures.
104cb93a386Sopenharmony_ci    GrBackendTexture backendTex1 = mbet->texture();
105cb93a386Sopenharmony_ci    GrBackendTexture backendTex2 = backendTex1;
106cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
107cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
108cb93a386Sopenharmony_ci
109cb93a386Sopenharmony_ci    backendTex2.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
112cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
115cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
116cb93a386Sopenharmony_ci
117cb93a386Sopenharmony_ci    // Setting back the layout since we didn't actually change it
118cb93a386Sopenharmony_ci    backendTex1.setVkImageLayout(initLayout);
119cb93a386Sopenharmony_ci
120cb93a386Sopenharmony_ci    sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(
121cb93a386Sopenharmony_ci            dContext,
122cb93a386Sopenharmony_ci            backendTex1,
123cb93a386Sopenharmony_ci            kTopLeft_GrSurfaceOrigin,
124cb93a386Sopenharmony_ci            kRGBA_8888_SkColorType,
125cb93a386Sopenharmony_ci            kPremul_SkAlphaType,
126cb93a386Sopenharmony_ci            /*color space*/ nullptr,
127cb93a386Sopenharmony_ci            sk_gpu_test::ManagedBackendTexture::ReleaseProc,
128cb93a386Sopenharmony_ci            mbet->releaseContext());
129cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, wrappedImage.get());
130cb93a386Sopenharmony_ci
131cb93a386Sopenharmony_ci    GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
132cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, proxy);
133cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, proxy->isInstantiated());
134cb93a386Sopenharmony_ci    GrTexture* texture = proxy->peekTexture();
135cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, texture);
136cb93a386Sopenharmony_ci
137cb93a386Sopenharmony_ci    // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
138cb93a386Sopenharmony_ci    GrVkImage* vkTexture = static_cast<GrVkTexture*>(texture)->textureImage();
139cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
140cb93a386Sopenharmony_ci    vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
143cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ci    GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
146cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
147cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
148cb93a386Sopenharmony_ci
149cb93a386Sopenharmony_ci    // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
150cb93a386Sopenharmony_ci    backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
151cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
152cb93a386Sopenharmony_ci
153cb93a386Sopenharmony_ci    vkTexture->updateImageLayout(initLayout);
154cb93a386Sopenharmony_ci
155cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
156cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
157cb93a386Sopenharmony_ci
158cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
159cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
160cb93a386Sopenharmony_ci
161cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
162cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_ci    // Check that we can do things like assigning the backend texture to invalid one, assign an
165cb93a386Sopenharmony_ci    // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
166cb93a386Sopenharmony_ci    // our ref counting asserts.
167cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex1, backendTex2));
168cb93a386Sopenharmony_ci
169cb93a386Sopenharmony_ci    GrBackendTexture invalidTexture;
170cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !invalidTexture.isValid());
171cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2));
172cb93a386Sopenharmony_ci
173cb93a386Sopenharmony_ci    backendTex2 = invalidTexture;
174cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !backendTex2.isValid());
175cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2));
176cb93a386Sopenharmony_ci
177cb93a386Sopenharmony_ci    invalidTexture = backendTex1;
178cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, invalidTexture.isValid());
179cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex1));
180cb93a386Sopenharmony_ci
181cb93a386Sopenharmony_ci    invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
182cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, invalidTexture.isValid());
183cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
184cb93a386Sopenharmony_ci}
185cb93a386Sopenharmony_ci
186cb93a386Sopenharmony_ci// This test is disabled because it executes illegal vulkan calls which cause the validations layers
187cb93a386Sopenharmony_ci// to fail and makes us assert. Once fixed to use a valid vulkan call sequence it should be
188cb93a386Sopenharmony_ci// renenabled, see skbug.com/8936.
189cb93a386Sopenharmony_ci#if 0
190cb93a386Sopenharmony_ci// Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed.
191cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) {
192cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
193cb93a386Sopenharmony_ci    GrGpu* gpu = dContext->priv().getGpu();
194cb93a386Sopenharmony_ci    GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
195cb93a386Sopenharmony_ci    if (!vkGpu->vkCaps().supportsExternalMemory()) {
196cb93a386Sopenharmony_ci        return;
197cb93a386Sopenharmony_ci    }
198cb93a386Sopenharmony_ci
199cb93a386Sopenharmony_ci    GrBackendTexture backendTex = dContext->createBackendTexture(
200cb93a386Sopenharmony_ci            1, 1, kRGBA_8888_SkColorType,
201cb93a386Sopenharmony_ci            SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo);
202cb93a386Sopenharmony_ci    sk_sp<SkImage> image;
203cb93a386Sopenharmony_ci    // Make a backend texture with an external queue family and general layout.
204cb93a386Sopenharmony_ci    GrVkImageInfo vkInfo;
205cb93a386Sopenharmony_ci    if (!backendTex.getVkImageInfo(&vkInfo)) {
206cb93a386Sopenharmony_ci        return;
207cb93a386Sopenharmony_ci    }
208cb93a386Sopenharmony_ci    vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
209cb93a386Sopenharmony_ci    // Use a read-only layout as these are the ones where we can otherwise skip a transition.
210cb93a386Sopenharmony_ci    vkInfo.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
211cb93a386Sopenharmony_ci
212cb93a386Sopenharmony_ci    GrBackendTexture vkExtTex(1, 1, vkInfo);
213cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, vkExtTex.isValid());
214cb93a386Sopenharmony_ci    image = SkImage::MakeFromTexture(dContext, vkExtTex, kTopLeft_GrSurfaceOrigin,
215cb93a386Sopenharmony_ci                                     kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr,
216cb93a386Sopenharmony_ci                                     nullptr);
217cb93a386Sopenharmony_ci
218cb93a386Sopenharmony_ci    if (!image) {
219cb93a386Sopenharmony_ci        return;
220cb93a386Sopenharmony_ci    }
221cb93a386Sopenharmony_ci
222cb93a386Sopenharmony_ci    GrTexture* texture = image->getTexture();
223cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, texture);
224cb93a386Sopenharmony_ci    GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
225cb93a386Sopenharmony_ci
226cb93a386Sopenharmony_ci    // Change our backend texture to the internal queue, with the same layout. This should force a
227cb93a386Sopenharmony_ci    // queue transition even though the layouts match.
228cb93a386Sopenharmony_ci    vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0,
229cb93a386Sopenharmony_ci                          VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, false, false);
230cb93a386Sopenharmony_ci
231cb93a386Sopenharmony_ci    // Get our image info again and make sure we transitioned queues.
232cb93a386Sopenharmony_ci    GrBackendTexture newBackendTexture = image->getBackendTexture(true);
233cb93a386Sopenharmony_ci    GrVkImageInfo newVkInfo;
234cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
235cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
236cb93a386Sopenharmony_ci
237cb93a386Sopenharmony_ci    image.reset();
238cb93a386Sopenharmony_ci    dContext->submit(true);
239cb93a386Sopenharmony_ci    dContext->deleteBackendTexture(backendTex);
240cb93a386Sopenharmony_ci}
241cb93a386Sopenharmony_ci#endif
242cb93a386Sopenharmony_ci
243cb93a386Sopenharmony_ci#endif
244