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