blob: 6b442a9725f51e26ce4cf70b35aabe04a7c28d08 [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
12#if SK_SUPPORT_GPU && defined(SK_VULKAN)
13
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
Brian Osmana0ca9092018-05-10 22:04:08 +000087#ifdef SK_SUPPORT_LEGACY_BACKEND_OBJECTS
88 // Verify that modifying the layout via the old textureHandle sitll works in is reflected in the
89 // GrVkTexture and GrBackendTexture.
90 GrVkImageInfo* backendInfo = (GrVkImageInfo*)wrappedImage->getTextureHandle(false);
91 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == backendInfo->fImageLayout);
92
93 backendInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
94 REPORTER_ASSERT(reporter,
95 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == vkTexture->currentLayout());
96 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
97 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == info.fImageLayout);
98#endif
99
Greg Daniel52e16d92018-04-10 09:34:07 -0400100 vkTexture->updateImageLayout(initLayout);
101
102 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
103 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
104
105 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
106 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
107
108 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
109 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
110
111 // Check that we can do things like assigning the backend texture to invalid one, assign an
112 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
113 // our ref counting asserts.
114 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
115
116 GrBackendTexture invalidTexture;
117 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
118 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
119
120 backendTexCopy = invalidTexture;
121 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
122 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
123
124 invalidTexture = backendTex;
125 REPORTER_ASSERT(reporter, invalidTexture.isValid());
126 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
127
Ben Wagnerff134f22018-04-24 16:29:16 -0400128 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
Greg Daniel52e16d92018-04-10 09:34:07 -0400129 REPORTER_ASSERT(reporter, invalidTexture.isValid());
130 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
131
132 gpu->deleteTestingOnlyBackendTexture(backendTex);
133}
134
135#endif