1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // FramebufferAttachment.h: Defines the wrapper class gl::FramebufferAttachment, as well as the
8 // objects and related functionality. [OpenGL ES 2.0.24] section 4.4.3 page 108.
9 
10 #ifndef LIBANGLE_FRAMEBUFFERATTACHMENT_H_
11 #define LIBANGLE_FRAMEBUFFERATTACHMENT_H_
12 
13 #include "angle_gl.h"
14 #include "common/angleutils.h"
15 #include "libANGLE/Error.h"
16 #include "libANGLE/ImageIndex.h"
17 #include "libANGLE/Observer.h"
18 #include "libANGLE/angletypes.h"
19 #include "libANGLE/formatutils.h"
20 #include "libANGLE/renderer/FramebufferAttachmentObjectImpl.h"
21 
22 namespace egl
23 {
24 class Surface;
25 }
26 
27 namespace rx
28 {
29 // An implementation-specific object associated with an attachment.
30 
31 class FramebufferAttachmentRenderTarget : angle::NonCopyable
32 {
33   public:
FramebufferAttachmentRenderTarget()34     FramebufferAttachmentRenderTarget() {}
~FramebufferAttachmentRenderTarget()35     virtual ~FramebufferAttachmentRenderTarget() {}
36 };
37 
38 class FramebufferAttachmentObjectImpl;
39 }  // namespace rx
40 
41 namespace gl
42 {
43 class FramebufferAttachmentObject;
44 class Renderbuffer;
45 class Texture;
46 
47 // FramebufferAttachment implements a GL framebuffer attachment.
48 // Attachments are "light" containers, which store pointers to ref-counted GL objects.
49 // We support GL texture (2D/3D/Cube/2D array) and renderbuffer object attachments.
50 // Note: Our old naming scheme used the term "Renderbuffer" for both GL renderbuffers and for
51 // framebuffer attachments, which confused their usage.
52 
53 class FramebufferAttachment final
54 {
55   public:
56     FramebufferAttachment();
57 
58     FramebufferAttachment(const Context *context,
59                           GLenum type,
60                           GLenum binding,
61                           const ImageIndex &textureIndex,
62                           FramebufferAttachmentObject *resource,
63                           rx::Serial framebufferSerial);
64 
65     FramebufferAttachment(FramebufferAttachment &&other);
66     FramebufferAttachment &operator=(FramebufferAttachment &&other);
67 
68     ~FramebufferAttachment();
69 
70     void detach(const Context *context, rx::Serial framebufferSerial);
71     void attach(const Context *context,
72                 GLenum type,
73                 GLenum binding,
74                 const ImageIndex &textureIndex,
75                 FramebufferAttachmentObject *resource,
76                 GLsizei numViews,
77                 GLuint baseViewIndex,
78                 bool isMultiview,
79                 GLsizei samples,
80                 rx::Serial framebufferSerial);
81 
82     // Helper methods
83     GLuint getRedSize() const;
84     GLuint getGreenSize() const;
85     GLuint getBlueSize() const;
86     GLuint getAlphaSize() const;
87     GLuint getDepthSize() const;
88     GLuint getStencilSize() const;
89     GLenum getComponentType() const;
90     GLenum getColorEncoding() const;
91 
isTextureWithId(TextureID textureId)92     bool isTextureWithId(TextureID textureId) const
93     {
94         return mType == GL_TEXTURE && id() == textureId.value;
95     }
isExternalTexture()96     bool isExternalTexture() const
97     {
98         return mType == GL_TEXTURE && getTextureImageIndex().getType() == gl::TextureType::External;
99     }
isRenderbufferWithId(GLuint renderbufferId)100     bool isRenderbufferWithId(GLuint renderbufferId) const
101     {
102         return mType == GL_RENDERBUFFER && id() == renderbufferId;
103     }
104 
getBinding()105     GLenum getBinding() const { return mTarget.binding(); }
106     GLuint id() const;
107 
108     // These methods are only legal to call on Texture attachments
109     const ImageIndex &getTextureImageIndex() const;
110     TextureTarget cubeMapFace() const;
111     GLint mipLevel() const;
112     GLint layer() const;
113     bool isLayered() const;
114 
getNumViews()115     GLsizei getNumViews() const { return mNumViews; }
116 
117     bool isMultiview() const;
118     GLint getBaseViewIndex() const;
119 
isRenderToTexture()120     bool isRenderToTexture() const
121     {
122         return mRenderToTextureSamples != kDefaultRenderToTextureSamples;
123     }
getRenderToTextureSamples()124     GLsizei getRenderToTextureSamples() const { return mRenderToTextureSamples; }
125 
126     // The size of the underlying resource the attachment points to. The 'depth' value will
127     // correspond to a 3D texture depth or the layer count of a 2D array texture. For Surfaces and
128     // Renderbuffers, it will always be 1.
129     Extents getSize() const;
130     Format getFormat() const;
131     GLsizei getSamples() const;
132     // This will always return the actual sample count of the attachment even if
133     // render_to_texture extension is active on this FBattachment object.
134     GLsizei getResourceSamples() const;
type()135     GLenum type() const { return mType; }
isAttached()136     bool isAttached() const { return mType != GL_NONE; }
137     bool isRenderable(const Context *context) const;
138     bool isYUV() const;
139 
140     Renderbuffer *getRenderbuffer() const;
141     Texture *getTexture() const;
142     const egl::Surface *getSurface() const;
143     FramebufferAttachmentObject *getResource() const;
144     InitState initState() const;
145     angle::Result initializeContents(const Context *context);
146     void setInitState(InitState initState) const;
147 
148     // "T" must be static_castable from FramebufferAttachmentRenderTarget
149     template <typename T>
getRenderTarget(const Context * context,GLsizei samples,T ** rtOut)150     angle::Result getRenderTarget(const Context *context, GLsizei samples, T **rtOut) const
151     {
152         static_assert(std::is_base_of<rx::FramebufferAttachmentRenderTarget, T>(),
153                       "Invalid RenderTarget class.");
154         return getRenderTargetImpl(
155             context, samples, reinterpret_cast<rx::FramebufferAttachmentRenderTarget **>(rtOut));
156     }
157 
158     bool operator==(const FramebufferAttachment &other) const;
159     bool operator!=(const FramebufferAttachment &other) const;
160 
161     static const GLsizei kDefaultNumViews;
162     static const GLint kDefaultBaseViewIndex;
163     static const GLint kDefaultRenderToTextureSamples;
164 
165   private:
166     angle::Result getRenderTargetImpl(const Context *context,
167                                       GLsizei samples,
168                                       rx::FramebufferAttachmentRenderTarget **rtOut) const;
169 
170     // A framebuffer attachment points to one of three types of resources: Renderbuffers,
171     // Textures and egl::Surface. The "Target" struct indicates which part of the
172     // object an attachment references. For the three types:
173     //   - a Renderbuffer has a unique renderable target, and needs no target index
174     //   - a Texture has targets for every image and uses an ImageIndex
175     //   - a Surface has targets for Color and Depth/Stencil, and uses the attachment binding
176     class Target
177     {
178       public:
179         Target();
180         Target(GLenum binding, const ImageIndex &imageIndex);
181         Target(const Target &other);
182         Target &operator=(const Target &other);
183 
binding()184         GLenum binding() const { return mBinding; }
textureIndex()185         const ImageIndex &textureIndex() const { return mTextureIndex; }
186 
187       private:
188         GLenum mBinding;
189         ImageIndex mTextureIndex;
190     };
191 
192     GLenum mType;
193     Target mTarget;
194     FramebufferAttachmentObject *mResource;
195     GLsizei mNumViews;
196     bool mIsMultiview;
197     GLint mBaseViewIndex;
198     GLsizei mRenderToTextureSamples;
199 };
200 
201 // A base class for objects that FBO Attachments may point to.
202 class FramebufferAttachmentObject : public angle::Subject, public angle::ObserverInterface
203 {
204   public:
205     FramebufferAttachmentObject();
206     ~FramebufferAttachmentObject() override;
207 
208     virtual Extents getAttachmentSize(const ImageIndex &imageIndex) const                  = 0;
209     virtual Format getAttachmentFormat(GLenum binding, const ImageIndex &imageIndex) const = 0;
210     virtual GLsizei getAttachmentSamples(const ImageIndex &imageIndex) const               = 0;
211     virtual bool isRenderable(const Context *context,
212                               GLenum binding,
213                               const ImageIndex &imageIndex) const                          = 0;
214     virtual bool isYUV() const                                                             = 0;
215 
216     virtual void onAttach(const Context *context, rx::Serial framebufferSerial) = 0;
217     virtual void onDetach(const Context *context, rx::Serial framebufferSerial) = 0;
218     virtual GLuint getId() const                                                = 0;
219 
220     // These are used for robust resource initialization.
221     virtual InitState initState(const ImageIndex &imageIndex) const              = 0;
222     virtual void setInitState(const ImageIndex &imageIndex, InitState initState) = 0;
223 
224     angle::Result getAttachmentRenderTarget(const Context *context,
225                                             GLenum binding,
226                                             const ImageIndex &imageIndex,
227                                             GLsizei samples,
228                                             rx::FramebufferAttachmentRenderTarget **rtOut) const;
229 
230     angle::Result initializeContents(const Context *context, const ImageIndex &imageIndex);
231 
232   protected:
233     virtual rx::FramebufferAttachmentObjectImpl *getAttachmentImpl() const = 0;
234 };
235 
getTextureImageIndex()236 inline const ImageIndex &FramebufferAttachment::getTextureImageIndex() const
237 {
238     ASSERT(type() == GL_TEXTURE);
239     return mTarget.textureIndex();
240 }
241 
getSize()242 inline Extents FramebufferAttachment::getSize() const
243 {
244     ASSERT(mResource);
245     return mResource->getAttachmentSize(mTarget.textureIndex());
246 }
247 
getFormat()248 inline Format FramebufferAttachment::getFormat() const
249 {
250     ASSERT(mResource);
251     return mResource->getAttachmentFormat(mTarget.binding(), mTarget.textureIndex());
252 }
253 
getSamples()254 inline GLsizei FramebufferAttachment::getSamples() const
255 {
256     return (mRenderToTextureSamples != kDefaultRenderToTextureSamples) ? getRenderToTextureSamples()
257                                                                        : getResourceSamples();
258 }
259 
getResourceSamples()260 inline GLsizei FramebufferAttachment::getResourceSamples() const
261 {
262     ASSERT(mResource);
263     return mResource->getAttachmentSamples(mTarget.textureIndex());
264 }
265 
getRenderTargetImpl(const Context * context,GLsizei samples,rx::FramebufferAttachmentRenderTarget ** rtOut)266 inline angle::Result FramebufferAttachment::getRenderTargetImpl(
267     const Context *context,
268     GLsizei samples,
269     rx::FramebufferAttachmentRenderTarget **rtOut) const
270 {
271     ASSERT(mResource);
272     return mResource->getAttachmentRenderTarget(context, mTarget.binding(), mTarget.textureIndex(),
273                                                 samples, rtOut);
274 }
275 
isRenderable(const Context * context)276 inline bool FramebufferAttachment::isRenderable(const Context *context) const
277 {
278     ASSERT(mResource);
279     return mResource->isRenderable(context, mTarget.binding(), mTarget.textureIndex());
280 }
281 
isYUV()282 inline bool FramebufferAttachment::isYUV() const
283 {
284     ASSERT(mResource);
285     return mResource->isYUV();
286 }
287 
288 }  // namespace gl
289 
290 #endif  // LIBANGLE_FRAMEBUFFERATTACHMENT_H_
291