xref: /aosp_15_r20/external/angle/src/libANGLE/Image.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2015 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 
7*8975f5c5SAndroid Build Coastguard Worker // Image.h: Defines the egl::Image class representing the EGLimage object.
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #ifndef LIBANGLE_IMAGE_H_
10*8975f5c5SAndroid Build Coastguard Worker #define LIBANGLE_IMAGE_H_
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker #include "common/FastVector.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "common/SimpleMutex.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "common/angleutils.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/AttributeMap.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Debug.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Error.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/FramebufferAttachment.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/RefCountObject.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/formatutils.h"
21*8975f5c5SAndroid Build Coastguard Worker 
22*8975f5c5SAndroid Build Coastguard Worker namespace rx
23*8975f5c5SAndroid Build Coastguard Worker {
24*8975f5c5SAndroid Build Coastguard Worker class EGLImplFactory;
25*8975f5c5SAndroid Build Coastguard Worker class ImageImpl;
26*8975f5c5SAndroid Build Coastguard Worker class ExternalImageSiblingImpl;
27*8975f5c5SAndroid Build Coastguard Worker 
28*8975f5c5SAndroid Build Coastguard Worker // Used for distinguishing dirty bit messages from gl::Texture/rx::TexureImpl/gl::Image.
29*8975f5c5SAndroid Build Coastguard Worker constexpr size_t kTextureImageImplObserverMessageIndex = 0;
30*8975f5c5SAndroid Build Coastguard Worker constexpr size_t kTextureImageSiblingMessageIndex      = 1;
31*8975f5c5SAndroid Build Coastguard Worker }  // namespace rx
32*8975f5c5SAndroid Build Coastguard Worker 
33*8975f5c5SAndroid Build Coastguard Worker namespace egl
34*8975f5c5SAndroid Build Coastguard Worker {
35*8975f5c5SAndroid Build Coastguard Worker class Image;
36*8975f5c5SAndroid Build Coastguard Worker class Display;
37*8975f5c5SAndroid Build Coastguard Worker class ContextMutex;
38*8975f5c5SAndroid Build Coastguard Worker 
39*8975f5c5SAndroid Build Coastguard Worker // Only currently Renderbuffers and Textures can be bound with images. This makes the relationship
40*8975f5c5SAndroid Build Coastguard Worker // explicit, and also ensures that an image sibling can determine if it's been initialized or not,
41*8975f5c5SAndroid Build Coastguard Worker // which is important for the robust resource init extension with Textures and EGLImages.
42*8975f5c5SAndroid Build Coastguard Worker class ImageSibling : public gl::FramebufferAttachmentObject
43*8975f5c5SAndroid Build Coastguard Worker {
44*8975f5c5SAndroid Build Coastguard Worker   public:
45*8975f5c5SAndroid Build Coastguard Worker     ImageSibling();
46*8975f5c5SAndroid Build Coastguard Worker     ~ImageSibling() override;
47*8975f5c5SAndroid Build Coastguard Worker 
48*8975f5c5SAndroid Build Coastguard Worker     bool isEGLImageTarget() const;
49*8975f5c5SAndroid Build Coastguard Worker     gl::InitState sourceEGLImageInitState() const;
50*8975f5c5SAndroid Build Coastguard Worker     void setSourceEGLImageInitState(gl::InitState initState) const;
51*8975f5c5SAndroid Build Coastguard Worker 
52*8975f5c5SAndroid Build Coastguard Worker     bool isRenderable(const gl::Context *context,
53*8975f5c5SAndroid Build Coastguard Worker                       GLenum binding,
54*8975f5c5SAndroid Build Coastguard Worker                       const gl::ImageIndex &imageIndex) const override;
55*8975f5c5SAndroid Build Coastguard Worker     bool isYUV() const override;
56*8975f5c5SAndroid Build Coastguard Worker     bool isExternalImageWithoutIndividualSync() const override;
57*8975f5c5SAndroid Build Coastguard Worker     bool hasFrontBufferUsage() const override;
58*8975f5c5SAndroid Build Coastguard Worker     bool hasProtectedContent() const override;
hasFoveatedRendering()59*8975f5c5SAndroid Build Coastguard Worker     bool hasFoveatedRendering() const override { return false; }
getFoveationState()60*8975f5c5SAndroid Build Coastguard Worker     const gl::FoveationState *getFoveationState() const override { return nullptr; }
61*8975f5c5SAndroid Build Coastguard Worker 
62*8975f5c5SAndroid Build Coastguard Worker   protected:
63*8975f5c5SAndroid Build Coastguard Worker     // Set the image target of this sibling
64*8975f5c5SAndroid Build Coastguard Worker     void setTargetImage(const gl::Context *context, egl::Image *imageTarget);
65*8975f5c5SAndroid Build Coastguard Worker 
66*8975f5c5SAndroid Build Coastguard Worker     // Orphan all EGL image sources and targets
67*8975f5c5SAndroid Build Coastguard Worker     angle::Result orphanImages(const gl::Context *context,
68*8975f5c5SAndroid Build Coastguard Worker                                RefCountObjectReleaser<Image> *outReleaseImage);
69*8975f5c5SAndroid Build Coastguard Worker 
70*8975f5c5SAndroid Build Coastguard Worker     void notifySiblings(angle::SubjectMessage message);
71*8975f5c5SAndroid Build Coastguard Worker 
72*8975f5c5SAndroid Build Coastguard Worker   private:
73*8975f5c5SAndroid Build Coastguard Worker     friend class Image;
74*8975f5c5SAndroid Build Coastguard Worker 
75*8975f5c5SAndroid Build Coastguard Worker     // Called from Image only to add a new source image
76*8975f5c5SAndroid Build Coastguard Worker     void addImageSource(egl::Image *imageSource);
77*8975f5c5SAndroid Build Coastguard Worker 
78*8975f5c5SAndroid Build Coastguard Worker     // Called from Image only to remove a source image when the Image is being deleted
79*8975f5c5SAndroid Build Coastguard Worker     void removeImageSource(egl::Image *imageSource);
80*8975f5c5SAndroid Build Coastguard Worker 
81*8975f5c5SAndroid Build Coastguard Worker     static constexpr size_t kSourcesOfSetSize = 2;
82*8975f5c5SAndroid Build Coastguard Worker     angle::FlatUnorderedSet<Image *, kSourcesOfSetSize> mSourcesOf;
83*8975f5c5SAndroid Build Coastguard Worker     BindingPointer<Image> mTargetOf;
84*8975f5c5SAndroid Build Coastguard Worker };
85*8975f5c5SAndroid Build Coastguard Worker 
86*8975f5c5SAndroid Build Coastguard Worker // Wrapper for EGLImage sources that are not owned by ANGLE, these often have to do
87*8975f5c5SAndroid Build Coastguard Worker // platform-specific queries for format and size information.
88*8975f5c5SAndroid Build Coastguard Worker class ExternalImageSibling : public ImageSibling
89*8975f5c5SAndroid Build Coastguard Worker {
90*8975f5c5SAndroid Build Coastguard Worker   public:
91*8975f5c5SAndroid Build Coastguard Worker     ExternalImageSibling(rx::EGLImplFactory *factory,
92*8975f5c5SAndroid Build Coastguard Worker                          const gl::Context *context,
93*8975f5c5SAndroid Build Coastguard Worker                          EGLenum target,
94*8975f5c5SAndroid Build Coastguard Worker                          EGLClientBuffer buffer,
95*8975f5c5SAndroid Build Coastguard Worker                          const AttributeMap &attribs);
96*8975f5c5SAndroid Build Coastguard Worker     ~ExternalImageSibling() override;
97*8975f5c5SAndroid Build Coastguard Worker 
98*8975f5c5SAndroid Build Coastguard Worker     void onDestroy(const egl::Display *display);
99*8975f5c5SAndroid Build Coastguard Worker 
100*8975f5c5SAndroid Build Coastguard Worker     Error initialize(const Display *display, const gl::Context *context);
101*8975f5c5SAndroid Build Coastguard Worker 
102*8975f5c5SAndroid Build Coastguard Worker     gl::Extents getAttachmentSize(const gl::ImageIndex &imageIndex) const override;
103*8975f5c5SAndroid Build Coastguard Worker     gl::Format getAttachmentFormat(GLenum binding, const gl::ImageIndex &imageIndex) const override;
104*8975f5c5SAndroid Build Coastguard Worker     GLsizei getAttachmentSamples(const gl::ImageIndex &imageIndex) const override;
105*8975f5c5SAndroid Build Coastguard Worker     GLuint getLevelCount() const;
106*8975f5c5SAndroid Build Coastguard Worker     bool isRenderable(const gl::Context *context,
107*8975f5c5SAndroid Build Coastguard Worker                       GLenum binding,
108*8975f5c5SAndroid Build Coastguard Worker                       const gl::ImageIndex &imageIndex) const override;
109*8975f5c5SAndroid Build Coastguard Worker     bool isTextureable(const gl::Context *context) const;
110*8975f5c5SAndroid Build Coastguard Worker     bool isYUV() const override;
111*8975f5c5SAndroid Build Coastguard Worker     bool hasFrontBufferUsage() const override;
112*8975f5c5SAndroid Build Coastguard Worker     bool isCubeMap() const;
113*8975f5c5SAndroid Build Coastguard Worker     bool hasProtectedContent() const override;
114*8975f5c5SAndroid Build Coastguard Worker 
115*8975f5c5SAndroid Build Coastguard Worker     void onAttach(const gl::Context *context, rx::UniqueSerial framebufferSerial) override;
116*8975f5c5SAndroid Build Coastguard Worker     void onDetach(const gl::Context *context, rx::UniqueSerial framebufferSerial) override;
117*8975f5c5SAndroid Build Coastguard Worker     GLuint getId() const override;
118*8975f5c5SAndroid Build Coastguard Worker 
119*8975f5c5SAndroid Build Coastguard Worker     gl::InitState initState(GLenum binding, const gl::ImageIndex &imageIndex) const override;
120*8975f5c5SAndroid Build Coastguard Worker     void setInitState(GLenum binding,
121*8975f5c5SAndroid Build Coastguard Worker                       const gl::ImageIndex &imageIndex,
122*8975f5c5SAndroid Build Coastguard Worker                       gl::InitState initState) override;
123*8975f5c5SAndroid Build Coastguard Worker 
124*8975f5c5SAndroid Build Coastguard Worker     rx::ExternalImageSiblingImpl *getImplementation() const;
125*8975f5c5SAndroid Build Coastguard Worker 
126*8975f5c5SAndroid Build Coastguard Worker   protected:
127*8975f5c5SAndroid Build Coastguard Worker     rx::FramebufferAttachmentObjectImpl *getAttachmentImpl() const override;
128*8975f5c5SAndroid Build Coastguard Worker 
129*8975f5c5SAndroid Build Coastguard Worker   private:
130*8975f5c5SAndroid Build Coastguard Worker     // ObserverInterface implementation.
131*8975f5c5SAndroid Build Coastguard Worker     void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override;
132*8975f5c5SAndroid Build Coastguard Worker 
133*8975f5c5SAndroid Build Coastguard Worker     std::unique_ptr<rx::ExternalImageSiblingImpl> mImplementation;
134*8975f5c5SAndroid Build Coastguard Worker     angle::ObserverBinding mImplObserverBinding;
135*8975f5c5SAndroid Build Coastguard Worker };
136*8975f5c5SAndroid Build Coastguard Worker 
137*8975f5c5SAndroid Build Coastguard Worker struct ImageState : private angle::NonCopyable
138*8975f5c5SAndroid Build Coastguard Worker {
139*8975f5c5SAndroid Build Coastguard Worker     ImageState(ImageID id, EGLenum target, ImageSibling *buffer, const AttributeMap &attribs);
140*8975f5c5SAndroid Build Coastguard Worker     ~ImageState();
141*8975f5c5SAndroid Build Coastguard Worker 
142*8975f5c5SAndroid Build Coastguard Worker     ImageID id;
143*8975f5c5SAndroid Build Coastguard Worker 
144*8975f5c5SAndroid Build Coastguard Worker     EGLLabelKHR label;
145*8975f5c5SAndroid Build Coastguard Worker     EGLenum target;
146*8975f5c5SAndroid Build Coastguard Worker     gl::ImageIndex imageIndex;
147*8975f5c5SAndroid Build Coastguard Worker     ImageSibling *source;
148*8975f5c5SAndroid Build Coastguard Worker 
149*8975f5c5SAndroid Build Coastguard Worker     gl::Format format;
150*8975f5c5SAndroid Build Coastguard Worker     bool yuv;
151*8975f5c5SAndroid Build Coastguard Worker     bool cubeMap;
152*8975f5c5SAndroid Build Coastguard Worker     gl::Extents size;
153*8975f5c5SAndroid Build Coastguard Worker     size_t samples;
154*8975f5c5SAndroid Build Coastguard Worker     GLuint levelCount;
155*8975f5c5SAndroid Build Coastguard Worker     EGLenum colorspace;
156*8975f5c5SAndroid Build Coastguard Worker     bool hasProtectedContent;
157*8975f5c5SAndroid Build Coastguard Worker 
158*8975f5c5SAndroid Build Coastguard Worker     mutable angle::SimpleMutex targetsLock;
159*8975f5c5SAndroid Build Coastguard Worker 
160*8975f5c5SAndroid Build Coastguard Worker     static constexpr size_t kTargetsSetSize = 2;
161*8975f5c5SAndroid Build Coastguard Worker     angle::FlatUnorderedSet<ImageSibling *, kTargetsSetSize> targets;
162*8975f5c5SAndroid Build Coastguard Worker };
163*8975f5c5SAndroid Build Coastguard Worker 
164*8975f5c5SAndroid Build Coastguard Worker class Image final : public ThreadSafeRefCountObject, public LabeledObject
165*8975f5c5SAndroid Build Coastguard Worker {
166*8975f5c5SAndroid Build Coastguard Worker   public:
167*8975f5c5SAndroid Build Coastguard Worker     Image(rx::EGLImplFactory *factory,
168*8975f5c5SAndroid Build Coastguard Worker           ImageID id,
169*8975f5c5SAndroid Build Coastguard Worker           const gl::Context *context,
170*8975f5c5SAndroid Build Coastguard Worker           EGLenum target,
171*8975f5c5SAndroid Build Coastguard Worker           ImageSibling *buffer,
172*8975f5c5SAndroid Build Coastguard Worker           const AttributeMap &attribs);
173*8975f5c5SAndroid Build Coastguard Worker 
174*8975f5c5SAndroid Build Coastguard Worker     void onDestroy(const Display *display) override;
175*8975f5c5SAndroid Build Coastguard Worker     ~Image() override;
176*8975f5c5SAndroid Build Coastguard Worker 
id()177*8975f5c5SAndroid Build Coastguard Worker     ImageID id() const { return mState.id; }
178*8975f5c5SAndroid Build Coastguard Worker 
179*8975f5c5SAndroid Build Coastguard Worker     void setLabel(EGLLabelKHR label) override;
180*8975f5c5SAndroid Build Coastguard Worker     EGLLabelKHR getLabel() const override;
181*8975f5c5SAndroid Build Coastguard Worker 
182*8975f5c5SAndroid Build Coastguard Worker     const gl::Format &getFormat() const;
183*8975f5c5SAndroid Build Coastguard Worker     bool isRenderable(const gl::Context *context) const;
184*8975f5c5SAndroid Build Coastguard Worker     bool isTexturable(const gl::Context *context) const;
185*8975f5c5SAndroid Build Coastguard Worker     bool isYUV() const;
186*8975f5c5SAndroid Build Coastguard Worker     bool isExternalImageWithoutIndividualSync() const;
187*8975f5c5SAndroid Build Coastguard Worker     bool hasFrontBufferUsage() const;
188*8975f5c5SAndroid Build Coastguard Worker     // Returns true only if the eglImage contains a complete cubemap
189*8975f5c5SAndroid Build Coastguard Worker     bool isCubeMap() const;
190*8975f5c5SAndroid Build Coastguard Worker     size_t getWidth() const;
191*8975f5c5SAndroid Build Coastguard Worker     size_t getHeight() const;
192*8975f5c5SAndroid Build Coastguard Worker     const gl::Extents &getExtents() const;
193*8975f5c5SAndroid Build Coastguard Worker     bool isLayered() const;
194*8975f5c5SAndroid Build Coastguard Worker     size_t getSamples() const;
195*8975f5c5SAndroid Build Coastguard Worker     GLuint getLevelCount() const;
196*8975f5c5SAndroid Build Coastguard Worker     bool hasProtectedContent() const;
197*8975f5c5SAndroid Build Coastguard Worker     bool isFixedRatedCompression(const gl::Context *context) const;
getColorspaceAttribute()198*8975f5c5SAndroid Build Coastguard Worker     EGLenum getColorspaceAttribute() const { return mState.colorspace; }
199*8975f5c5SAndroid Build Coastguard Worker 
200*8975f5c5SAndroid Build Coastguard Worker     Error initialize(const Display *display, const gl::Context *context);
201*8975f5c5SAndroid Build Coastguard Worker 
202*8975f5c5SAndroid Build Coastguard Worker     rx::ImageImpl *getImplementation() const;
203*8975f5c5SAndroid Build Coastguard Worker 
204*8975f5c5SAndroid Build Coastguard Worker     bool orphaned() const;
205*8975f5c5SAndroid Build Coastguard Worker     gl::InitState sourceInitState() const;
206*8975f5c5SAndroid Build Coastguard Worker     void setInitState(gl::InitState initState);
207*8975f5c5SAndroid Build Coastguard Worker 
208*8975f5c5SAndroid Build Coastguard Worker     Error exportVkImage(void *vkImage, void *vkImageCreateInfo);
209*8975f5c5SAndroid Build Coastguard Worker 
getContextMutex()210*8975f5c5SAndroid Build Coastguard Worker     ContextMutex *getContextMutex() const { return mContextMutex; }
211*8975f5c5SAndroid Build Coastguard Worker 
212*8975f5c5SAndroid Build Coastguard Worker   private:
213*8975f5c5SAndroid Build Coastguard Worker     friend class ImageSibling;
214*8975f5c5SAndroid Build Coastguard Worker 
215*8975f5c5SAndroid Build Coastguard Worker     // Called from ImageSibling only notify the image that a new target sibling exists for state
216*8975f5c5SAndroid Build Coastguard Worker     // tracking.
217*8975f5c5SAndroid Build Coastguard Worker     void addTargetSibling(ImageSibling *sibling);
218*8975f5c5SAndroid Build Coastguard Worker 
219*8975f5c5SAndroid Build Coastguard Worker     // Called from ImageSibling only to notify the image that a sibling (source or target) has
220*8975f5c5SAndroid Build Coastguard Worker     // been respecified and state tracking should be updated.
221*8975f5c5SAndroid Build Coastguard Worker     angle::Result orphanSibling(const gl::Context *context, ImageSibling *sibling);
222*8975f5c5SAndroid Build Coastguard Worker 
223*8975f5c5SAndroid Build Coastguard Worker     void notifySiblings(const ImageSibling *notifier, angle::SubjectMessage message);
224*8975f5c5SAndroid Build Coastguard Worker 
225*8975f5c5SAndroid Build Coastguard Worker     ImageState mState;
226*8975f5c5SAndroid Build Coastguard Worker     rx::ImageImpl *mImplementation;
227*8975f5c5SAndroid Build Coastguard Worker     bool mOrphanedAndNeedsInit;
228*8975f5c5SAndroid Build Coastguard Worker     bool mIsTexturable = false;
229*8975f5c5SAndroid Build Coastguard Worker     bool mIsRenderable = false;
230*8975f5c5SAndroid Build Coastguard Worker 
231*8975f5c5SAndroid Build Coastguard Worker     ContextMutex *mContextMutex;  // Reference counted
232*8975f5c5SAndroid Build Coastguard Worker };
233*8975f5c5SAndroid Build Coastguard Worker }  // namespace egl
234*8975f5c5SAndroid Build Coastguard Worker 
235*8975f5c5SAndroid Build Coastguard Worker #endif  // LIBANGLE_IMAGE_H_
236