xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/vulkan/ImageVk.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // ImageVk.cpp:
7*8975f5c5SAndroid Build Coastguard Worker //    Implements the class methods for ImageVk.
8*8975f5c5SAndroid Build Coastguard Worker //
9*8975f5c5SAndroid Build Coastguard Worker 
10*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/ImageVk.h"
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker #include "common/debug.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Context.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Display.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/ContextVk.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/DisplayVk.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/RenderbufferVk.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/TextureVk.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_utils.h"
20*8975f5c5SAndroid Build Coastguard Worker 
21*8975f5c5SAndroid Build Coastguard Worker namespace rx
22*8975f5c5SAndroid Build Coastguard Worker {
23*8975f5c5SAndroid Build Coastguard Worker 
ImageVk(const egl::ImageState & state,const gl::Context * context)24*8975f5c5SAndroid Build Coastguard Worker ImageVk::ImageVk(const egl::ImageState &state, const gl::Context *context)
25*8975f5c5SAndroid Build Coastguard Worker     : ImageImpl(state), mOwnsImage(false), mImage(nullptr), mContext(context)
26*8975f5c5SAndroid Build Coastguard Worker {}
27*8975f5c5SAndroid Build Coastguard Worker 
~ImageVk()28*8975f5c5SAndroid Build Coastguard Worker ImageVk::~ImageVk() {}
29*8975f5c5SAndroid Build Coastguard Worker 
onDestroy(const egl::Display * display)30*8975f5c5SAndroid Build Coastguard Worker void ImageVk::onDestroy(const egl::Display *display)
31*8975f5c5SAndroid Build Coastguard Worker {
32*8975f5c5SAndroid Build Coastguard Worker     DisplayVk *displayVk   = vk::GetImpl(display);
33*8975f5c5SAndroid Build Coastguard Worker     vk::Renderer *renderer = displayVk->getRenderer();
34*8975f5c5SAndroid Build Coastguard Worker 
35*8975f5c5SAndroid Build Coastguard Worker     if (mImage != nullptr && mOwnsImage)
36*8975f5c5SAndroid Build Coastguard Worker     {
37*8975f5c5SAndroid Build Coastguard Worker         // TODO: We need to handle the case that EGLImage used in two context that aren't shared.
38*8975f5c5SAndroid Build Coastguard Worker         // https://issuetracker.google.com/169868803
39*8975f5c5SAndroid Build Coastguard Worker         mImage->releaseImage(renderer);
40*8975f5c5SAndroid Build Coastguard Worker         mImage->releaseStagedUpdates(renderer);
41*8975f5c5SAndroid Build Coastguard Worker         SafeDelete(mImage);
42*8975f5c5SAndroid Build Coastguard Worker     }
43*8975f5c5SAndroid Build Coastguard Worker     else if (egl::IsExternalImageTarget(mState.target))
44*8975f5c5SAndroid Build Coastguard Worker     {
45*8975f5c5SAndroid Build Coastguard Worker         ASSERT(mState.source != nullptr);
46*8975f5c5SAndroid Build Coastguard Worker         ExternalImageSiblingVk *externalImageSibling =
47*8975f5c5SAndroid Build Coastguard Worker             GetImplAs<ExternalImageSiblingVk>(GetAs<egl::ExternalImageSibling>(mState.source));
48*8975f5c5SAndroid Build Coastguard Worker         externalImageSibling->release(renderer);
49*8975f5c5SAndroid Build Coastguard Worker         mImage = nullptr;
50*8975f5c5SAndroid Build Coastguard Worker 
51*8975f5c5SAndroid Build Coastguard Worker         // This is called as a special case where resources may be allocated by the caller, without
52*8975f5c5SAndroid Build Coastguard Worker         // the caller ever issuing a draw command to free them. Specifically, SurfaceFlinger
53*8975f5c5SAndroid Build Coastguard Worker         // optimistically allocates EGLImages that it may never draw to.
54*8975f5c5SAndroid Build Coastguard Worker         renderer->cleanupGarbage(nullptr);
55*8975f5c5SAndroid Build Coastguard Worker     }
56*8975f5c5SAndroid Build Coastguard Worker }
57*8975f5c5SAndroid Build Coastguard Worker 
initialize(const egl::Display * display)58*8975f5c5SAndroid Build Coastguard Worker egl::Error ImageVk::initialize(const egl::Display *display)
59*8975f5c5SAndroid Build Coastguard Worker {
60*8975f5c5SAndroid Build Coastguard Worker     if (mContext != nullptr)
61*8975f5c5SAndroid Build Coastguard Worker     {
62*8975f5c5SAndroid Build Coastguard Worker         ContextVk *contextVk = vk::GetImpl(mContext);
63*8975f5c5SAndroid Build Coastguard Worker         ANGLE_TRY(ResultToEGL(contextVk->getShareGroup()->lockDefaultContextsPriority(contextVk)));
64*8975f5c5SAndroid Build Coastguard Worker     }
65*8975f5c5SAndroid Build Coastguard Worker 
66*8975f5c5SAndroid Build Coastguard Worker     if (egl::IsTextureTarget(mState.target))
67*8975f5c5SAndroid Build Coastguard Worker     {
68*8975f5c5SAndroid Build Coastguard Worker         ASSERT(mContext != nullptr);
69*8975f5c5SAndroid Build Coastguard Worker         ContextVk *contextVk = vk::GetImpl(mContext);
70*8975f5c5SAndroid Build Coastguard Worker         TextureVk *textureVk = GetImplAs<TextureVk>(GetAs<gl::Texture>(mState.source));
71*8975f5c5SAndroid Build Coastguard Worker 
72*8975f5c5SAndroid Build Coastguard Worker         // Make sure the texture uses renderable format
73*8975f5c5SAndroid Build Coastguard Worker         TextureUpdateResult updateResult = TextureUpdateResult::ImageUnaffected;
74*8975f5c5SAndroid Build Coastguard Worker         ANGLE_TRY(ResultToEGL(textureVk->ensureRenderable(contextVk, &updateResult)));
75*8975f5c5SAndroid Build Coastguard Worker 
76*8975f5c5SAndroid Build Coastguard Worker         // Make sure the texture has created its backing storage
77*8975f5c5SAndroid Build Coastguard Worker         ANGLE_TRY(ResultToEGL(
78*8975f5c5SAndroid Build Coastguard Worker             textureVk->ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels)));
79*8975f5c5SAndroid Build Coastguard Worker 
80*8975f5c5SAndroid Build Coastguard Worker         mImage = &textureVk->getImage();
81*8975f5c5SAndroid Build Coastguard Worker 
82*8975f5c5SAndroid Build Coastguard Worker         // The staging buffer for a texture source should already be initialized
83*8975f5c5SAndroid Build Coastguard Worker 
84*8975f5c5SAndroid Build Coastguard Worker         mOwnsImage = false;
85*8975f5c5SAndroid Build Coastguard Worker     }
86*8975f5c5SAndroid Build Coastguard Worker     else
87*8975f5c5SAndroid Build Coastguard Worker     {
88*8975f5c5SAndroid Build Coastguard Worker         if (egl::IsRenderbufferTarget(mState.target))
89*8975f5c5SAndroid Build Coastguard Worker         {
90*8975f5c5SAndroid Build Coastguard Worker             RenderbufferVk *renderbufferVk =
91*8975f5c5SAndroid Build Coastguard Worker                 GetImplAs<RenderbufferVk>(GetAs<gl::Renderbuffer>(mState.source));
92*8975f5c5SAndroid Build Coastguard Worker             mImage = renderbufferVk->getImage();
93*8975f5c5SAndroid Build Coastguard Worker 
94*8975f5c5SAndroid Build Coastguard Worker             ASSERT(mContext != nullptr);
95*8975f5c5SAndroid Build Coastguard Worker         }
96*8975f5c5SAndroid Build Coastguard Worker         else if (egl::IsExternalImageTarget(mState.target))
97*8975f5c5SAndroid Build Coastguard Worker         {
98*8975f5c5SAndroid Build Coastguard Worker             const ExternalImageSiblingVk *externalImageSibling =
99*8975f5c5SAndroid Build Coastguard Worker                 GetImplAs<ExternalImageSiblingVk>(GetAs<egl::ExternalImageSibling>(mState.source));
100*8975f5c5SAndroid Build Coastguard Worker             mImage = externalImageSibling->getImage();
101*8975f5c5SAndroid Build Coastguard Worker 
102*8975f5c5SAndroid Build Coastguard Worker             ASSERT(mContext == nullptr);
103*8975f5c5SAndroid Build Coastguard Worker         }
104*8975f5c5SAndroid Build Coastguard Worker         else
105*8975f5c5SAndroid Build Coastguard Worker         {
106*8975f5c5SAndroid Build Coastguard Worker             UNREACHABLE();
107*8975f5c5SAndroid Build Coastguard Worker             return egl::EglBadAccess();
108*8975f5c5SAndroid Build Coastguard Worker         }
109*8975f5c5SAndroid Build Coastguard Worker 
110*8975f5c5SAndroid Build Coastguard Worker         mOwnsImage = false;
111*8975f5c5SAndroid Build Coastguard Worker     }
112*8975f5c5SAndroid Build Coastguard Worker 
113*8975f5c5SAndroid Build Coastguard Worker     // mContext is no longer needed, make sure it's not used by accident.
114*8975f5c5SAndroid Build Coastguard Worker     mContext = nullptr;
115*8975f5c5SAndroid Build Coastguard Worker 
116*8975f5c5SAndroid Build Coastguard Worker     return egl::NoError();
117*8975f5c5SAndroid Build Coastguard Worker }
118*8975f5c5SAndroid Build Coastguard Worker 
orphan(const gl::Context * context,egl::ImageSibling * sibling)119*8975f5c5SAndroid Build Coastguard Worker angle::Result ImageVk::orphan(const gl::Context *context, egl::ImageSibling *sibling)
120*8975f5c5SAndroid Build Coastguard Worker {
121*8975f5c5SAndroid Build Coastguard Worker     if (sibling == mState.source)
122*8975f5c5SAndroid Build Coastguard Worker     {
123*8975f5c5SAndroid Build Coastguard Worker         if (egl::IsTextureTarget(mState.target))
124*8975f5c5SAndroid Build Coastguard Worker         {
125*8975f5c5SAndroid Build Coastguard Worker             TextureVk *textureVk = GetImplAs<TextureVk>(GetAs<gl::Texture>(mState.source));
126*8975f5c5SAndroid Build Coastguard Worker             ASSERT(mImage == &textureVk->getImage());
127*8975f5c5SAndroid Build Coastguard Worker             textureVk->releaseOwnershipOfImage(context);
128*8975f5c5SAndroid Build Coastguard Worker             mOwnsImage = true;
129*8975f5c5SAndroid Build Coastguard Worker         }
130*8975f5c5SAndroid Build Coastguard Worker         else if (egl::IsRenderbufferTarget(mState.target))
131*8975f5c5SAndroid Build Coastguard Worker         {
132*8975f5c5SAndroid Build Coastguard Worker             RenderbufferVk *renderbufferVk =
133*8975f5c5SAndroid Build Coastguard Worker                 GetImplAs<RenderbufferVk>(GetAs<gl::Renderbuffer>(mState.source));
134*8975f5c5SAndroid Build Coastguard Worker             ASSERT(mImage == renderbufferVk->getImage());
135*8975f5c5SAndroid Build Coastguard Worker             renderbufferVk->releaseOwnershipOfImage(context);
136*8975f5c5SAndroid Build Coastguard Worker             mOwnsImage = true;
137*8975f5c5SAndroid Build Coastguard Worker         }
138*8975f5c5SAndroid Build Coastguard Worker         else
139*8975f5c5SAndroid Build Coastguard Worker         {
140*8975f5c5SAndroid Build Coastguard Worker             ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
141*8975f5c5SAndroid Build Coastguard Worker             return angle::Result::Stop;
142*8975f5c5SAndroid Build Coastguard Worker         }
143*8975f5c5SAndroid Build Coastguard Worker     }
144*8975f5c5SAndroid Build Coastguard Worker 
145*8975f5c5SAndroid Build Coastguard Worker     return angle::Result::Continue;
146*8975f5c5SAndroid Build Coastguard Worker }
147*8975f5c5SAndroid Build Coastguard Worker 
exportVkImage(void * vkImage,void * vkImageCreateInfo)148*8975f5c5SAndroid Build Coastguard Worker egl::Error ImageVk::exportVkImage(void *vkImage, void *vkImageCreateInfo)
149*8975f5c5SAndroid Build Coastguard Worker {
150*8975f5c5SAndroid Build Coastguard Worker     *reinterpret_cast<VkImage *>(vkImage) = mImage->getImage().getHandle();
151*8975f5c5SAndroid Build Coastguard Worker     auto *info = reinterpret_cast<VkImageCreateInfo *>(vkImageCreateInfo);
152*8975f5c5SAndroid Build Coastguard Worker     *info      = mImage->getVkImageCreateInfo();
153*8975f5c5SAndroid Build Coastguard Worker     return egl::NoError();
154*8975f5c5SAndroid Build Coastguard Worker }
155*8975f5c5SAndroid Build Coastguard Worker 
isFixedRatedCompression(const gl::Context * context)156*8975f5c5SAndroid Build Coastguard Worker bool ImageVk::isFixedRatedCompression(const gl::Context *context)
157*8975f5c5SAndroid Build Coastguard Worker {
158*8975f5c5SAndroid Build Coastguard Worker     ContextVk *contextVk   = vk::GetImpl(context);
159*8975f5c5SAndroid Build Coastguard Worker     vk::Renderer *renderer = contextVk->getRenderer();
160*8975f5c5SAndroid Build Coastguard Worker 
161*8975f5c5SAndroid Build Coastguard Worker     ASSERT(mImage != nullptr && mImage->valid());
162*8975f5c5SAndroid Build Coastguard Worker     ASSERT(renderer->getFeatures().supportsImageCompressionControl.enabled);
163*8975f5c5SAndroid Build Coastguard Worker 
164*8975f5c5SAndroid Build Coastguard Worker     VkImageSubresource2EXT imageSubresource2      = {};
165*8975f5c5SAndroid Build Coastguard Worker     imageSubresource2.sType                       = VK_STRUCTURE_TYPE_IMAGE_SUBRESOURCE_2_EXT;
166*8975f5c5SAndroid Build Coastguard Worker     imageSubresource2.imageSubresource.aspectMask = mImage->getAspectFlags();
167*8975f5c5SAndroid Build Coastguard Worker 
168*8975f5c5SAndroid Build Coastguard Worker     VkImageCompressionPropertiesEXT compressionProperties = {};
169*8975f5c5SAndroid Build Coastguard Worker     compressionProperties.sType               = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
170*8975f5c5SAndroid Build Coastguard Worker     VkSubresourceLayout2EXT subresourceLayout = {};
171*8975f5c5SAndroid Build Coastguard Worker     subresourceLayout.sType                   = VK_STRUCTURE_TYPE_SUBRESOURCE_LAYOUT_2_EXT;
172*8975f5c5SAndroid Build Coastguard Worker     subresourceLayout.pNext                   = &compressionProperties;
173*8975f5c5SAndroid Build Coastguard Worker 
174*8975f5c5SAndroid Build Coastguard Worker     vkGetImageSubresourceLayout2EXT(renderer->getDevice(), mImage->getImage().getHandle(),
175*8975f5c5SAndroid Build Coastguard Worker                                     &imageSubresource2, &subresourceLayout);
176*8975f5c5SAndroid Build Coastguard Worker 
177*8975f5c5SAndroid Build Coastguard Worker     return compressionProperties.imageCompressionFixedRateFlags >
178*8975f5c5SAndroid Build Coastguard Worker                    VK_IMAGE_COMPRESSION_FIXED_RATE_NONE_EXT
179*8975f5c5SAndroid Build Coastguard Worker                ? true
180*8975f5c5SAndroid Build Coastguard Worker                : false;
181*8975f5c5SAndroid Build Coastguard Worker }
182*8975f5c5SAndroid Build Coastguard Worker 
getImageTextureType() const183*8975f5c5SAndroid Build Coastguard Worker gl::TextureType ImageVk::getImageTextureType() const
184*8975f5c5SAndroid Build Coastguard Worker {
185*8975f5c5SAndroid Build Coastguard Worker     return mState.imageIndex.getType();
186*8975f5c5SAndroid Build Coastguard Worker }
187*8975f5c5SAndroid Build Coastguard Worker 
getImageLevel() const188*8975f5c5SAndroid Build Coastguard Worker gl::LevelIndex ImageVk::getImageLevel() const
189*8975f5c5SAndroid Build Coastguard Worker {
190*8975f5c5SAndroid Build Coastguard Worker     return gl::LevelIndex(mState.imageIndex.getLevelIndex());
191*8975f5c5SAndroid Build Coastguard Worker }
192*8975f5c5SAndroid Build Coastguard Worker 
getImageLayer() const193*8975f5c5SAndroid Build Coastguard Worker uint32_t ImageVk::getImageLayer() const
194*8975f5c5SAndroid Build Coastguard Worker {
195*8975f5c5SAndroid Build Coastguard Worker     return mState.imageIndex.hasLayer() ? mState.imageIndex.getLayerIndex() : 0;
196*8975f5c5SAndroid Build Coastguard Worker }
197*8975f5c5SAndroid Build Coastguard Worker 
198*8975f5c5SAndroid Build Coastguard Worker }  // namespace rx
199