blob: c5d707b662c20d3f33a9a786ce5fe9ee5e85f290 [file] [log] [blame]
Greg Daniel52e16d92018-04-10 09:34:07 -04001/*
2 * Copyright 2018 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)
Greg Daniel52e16d92018-04-10 09:34:07 -040013
14#include "GrTest.h"
15#include "Test.h"
16
17#include "GrBackendSurface.h"
18#include "GrContextPriv.h"
19#include "GrTextureProxy.h"
20#include "GrTexture.h"
21#include "SkImage.h"
22#include "SkImage_Base.h"
23#include "vk/GrVkGpu.h"
24#include "vk/GrVkImageLayout.h"
25#include "vk/GrVkTexture.h"
26#include "vk/GrVkTypes.h"
27
28DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) {
29 GrContext* context = ctxInfo.grContext();
30 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
31
32 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(nullptr, 1, 1,
33 kRGBA_8888_GrPixelConfig,
34 false,
35 GrMipMapped::kNo);
36 REPORTER_ASSERT(reporter, backendTex.isValid());
37
38 GrVkImageInfo info;
39 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
40 VkImageLayout initLayout = info.fImageLayout;
41
42 // Verify that setting that layout via a copy of a backendTexture is reflected in all the
43 // backendTextures.
44 GrBackendTexture backendTexCopy = backendTex;
45 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
46 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
47
48 backendTexCopy.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
49
50 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
51 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
52
53 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
54 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
55
56 // Setting back the layout since we didn't actually change it
57 backendTex.setVkImageLayout(initLayout);
58
59 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(context, backendTex,
60 kTopLeft_GrSurfaceOrigin,
61 kRGBA_8888_SkColorType,
62 kPremul_SkAlphaType, nullptr);
63 REPORTER_ASSERT(reporter, wrappedImage.get());
64
65 sk_sp<GrTextureProxy> texProxy = as_IB(wrappedImage)->asTextureProxyRef();
66 REPORTER_ASSERT(reporter, texProxy.get());
67 REPORTER_ASSERT(reporter, texProxy->priv().isInstantiated());
68 GrTexture* texture = texProxy->priv().peekTexture();
69 REPORTER_ASSERT(reporter, texture);
70
71 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
72 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
73 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
74 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
75
76 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
77 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
78
79 GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
80 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
81 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
82
83 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
84 backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
85 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
86
Greg Daniel52e16d92018-04-10 09:34:07 -040087 vkTexture->updateImageLayout(initLayout);
88
89 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
90 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
91
92 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
93 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
94
95 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
96 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
97
98 // Check that we can do things like assigning the backend texture to invalid one, assign an
99 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
100 // our ref counting asserts.
101 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
102
103 GrBackendTexture invalidTexture;
104 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
105 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
106
107 backendTexCopy = invalidTexture;
108 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
109 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
110
111 invalidTexture = backendTex;
112 REPORTER_ASSERT(reporter, invalidTexture.isValid());
113 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
114
Ben Wagnerff134f22018-04-24 16:29:16 -0400115 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
Greg Daniel52e16d92018-04-10 09:34:07 -0400116 REPORTER_ASSERT(reporter, invalidTexture.isValid());
117 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
118
119 gpu->deleteTestingOnlyBackendTexture(backendTex);
120}
121
122#endif