|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef WEBGLFRAMEBUFFER_H_ |
|
7 #define WEBGLFRAMEBUFFER_H_ |
|
8 |
|
9 #include "WebGLObjectModel.h" |
|
10 |
|
11 #include "nsWrapperCache.h" |
|
12 |
|
13 #include "mozilla/LinkedList.h" |
|
14 |
|
15 namespace mozilla { |
|
16 |
|
17 class WebGLFramebufferAttachable; |
|
18 class WebGLTexture; |
|
19 class WebGLRenderbuffer; |
|
20 namespace gl { |
|
21 class GLContext; |
|
22 } |
|
23 |
|
24 class WebGLFramebuffer MOZ_FINAL |
|
25 : public nsWrapperCache |
|
26 , public WebGLRefCountedObject<WebGLFramebuffer> |
|
27 , public LinkedListElement<WebGLFramebuffer> |
|
28 , public WebGLContextBoundObject |
|
29 { |
|
30 public: |
|
31 WebGLFramebuffer(WebGLContext* context); |
|
32 |
|
33 ~WebGLFramebuffer() { |
|
34 DeleteOnce(); |
|
35 } |
|
36 |
|
37 struct Attachment |
|
38 { |
|
39 // deleting a texture or renderbuffer immediately detaches it |
|
40 WebGLRefPtr<WebGLTexture> mTexturePtr; |
|
41 WebGLRefPtr<WebGLRenderbuffer> mRenderbufferPtr; |
|
42 GLenum mAttachmentPoint; |
|
43 GLenum mTexImageTarget; |
|
44 GLint mTexImageLevel; |
|
45 mutable bool mNeedsFinalize; |
|
46 |
|
47 Attachment(GLenum aAttachmentPoint = LOCAL_GL_COLOR_ATTACHMENT0) |
|
48 : mAttachmentPoint(aAttachmentPoint) |
|
49 , mNeedsFinalize(false) |
|
50 {} |
|
51 |
|
52 bool IsDefined() const { |
|
53 return Texture() || Renderbuffer(); |
|
54 } |
|
55 |
|
56 bool IsDeleteRequested() const; |
|
57 |
|
58 bool HasAlpha() const; |
|
59 bool IsReadableFloat() const; |
|
60 |
|
61 void SetTexImage(WebGLTexture* tex, GLenum target, GLint level); |
|
62 void SetRenderbuffer(WebGLRenderbuffer* rb); |
|
63 |
|
64 const WebGLTexture* Texture() const { |
|
65 return mTexturePtr; |
|
66 } |
|
67 WebGLTexture* Texture() { |
|
68 return mTexturePtr; |
|
69 } |
|
70 const WebGLRenderbuffer* Renderbuffer() const { |
|
71 return mRenderbufferPtr; |
|
72 } |
|
73 WebGLRenderbuffer* Renderbuffer() { |
|
74 return mRenderbufferPtr; |
|
75 } |
|
76 GLenum TexImageTarget() const { |
|
77 return mTexImageTarget; |
|
78 } |
|
79 GLint TexImageLevel() const { |
|
80 return mTexImageLevel; |
|
81 } |
|
82 |
|
83 bool HasUninitializedImageData() const; |
|
84 void SetImageDataStatus(WebGLImageDataStatus x); |
|
85 |
|
86 void Reset() { |
|
87 mTexturePtr = nullptr; |
|
88 mRenderbufferPtr = nullptr; |
|
89 } |
|
90 |
|
91 const WebGLRectangleObject& RectangleObject() const; |
|
92 |
|
93 bool HasImage() const; |
|
94 bool IsComplete() const; |
|
95 |
|
96 void FinalizeAttachment(gl::GLContext* gl, GLenum attachmentLoc) const; |
|
97 }; |
|
98 |
|
99 void Delete(); |
|
100 |
|
101 bool HasEverBeenBound() { return mHasEverBeenBound; } |
|
102 void SetHasEverBeenBound(bool x) { mHasEverBeenBound = x; } |
|
103 GLuint GLName() { return mGLName; } |
|
104 |
|
105 void FramebufferRenderbuffer(GLenum target, |
|
106 GLenum attachment, |
|
107 GLenum rbtarget, |
|
108 WebGLRenderbuffer* wrb); |
|
109 |
|
110 void FramebufferTexture2D(GLenum target, |
|
111 GLenum attachment, |
|
112 GLenum textarget, |
|
113 WebGLTexture* wtex, |
|
114 GLint level); |
|
115 |
|
116 private: |
|
117 void DetachAttachment(WebGLFramebuffer::Attachment& attachment); |
|
118 void DetachAllAttachments(); |
|
119 const WebGLRectangleObject& GetAnyRectObject() const; |
|
120 Attachment* GetAttachmentOrNull(GLenum attachment); |
|
121 |
|
122 public: |
|
123 bool HasDefinedAttachments() const; |
|
124 bool HasIncompleteAttachments() const; |
|
125 bool AllImageRectsMatch() const; |
|
126 GLenum PrecheckFramebufferStatus() const; |
|
127 GLenum CheckFramebufferStatus() const; |
|
128 |
|
129 bool HasDepthStencilConflict() const { |
|
130 return int(mDepthAttachment.IsDefined()) + |
|
131 int(mStencilAttachment.IsDefined()) + |
|
132 int(mDepthStencilAttachment.IsDefined()) >= 2; |
|
133 } |
|
134 |
|
135 size_t ColorAttachmentCount() const { |
|
136 return mColorAttachments.Length(); |
|
137 } |
|
138 const Attachment& ColorAttachment(size_t colorAttachmentId) const { |
|
139 return mColorAttachments[colorAttachmentId]; |
|
140 } |
|
141 |
|
142 const Attachment& DepthAttachment() const { |
|
143 return mDepthAttachment; |
|
144 } |
|
145 |
|
146 const Attachment& StencilAttachment() const { |
|
147 return mStencilAttachment; |
|
148 } |
|
149 |
|
150 const Attachment& DepthStencilAttachment() const { |
|
151 return mDepthStencilAttachment; |
|
152 } |
|
153 |
|
154 const Attachment& GetAttachment(GLenum attachment) const; |
|
155 |
|
156 void DetachTexture(const WebGLTexture* tex); |
|
157 |
|
158 void DetachRenderbuffer(const WebGLRenderbuffer* rb); |
|
159 |
|
160 const WebGLRectangleObject& RectangleObject() const; |
|
161 |
|
162 WebGLContext* GetParentObject() const { |
|
163 return Context(); |
|
164 } |
|
165 |
|
166 void FinalizeAttachments() const; |
|
167 |
|
168 virtual JSObject* WrapObject(JSContext* cx) MOZ_OVERRIDE; |
|
169 |
|
170 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLFramebuffer) |
|
171 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLFramebuffer) |
|
172 |
|
173 // mask mirrors glClear. |
|
174 bool HasCompletePlanes(GLbitfield mask); |
|
175 |
|
176 bool CheckAndInitializeAttachments(); |
|
177 |
|
178 bool CheckColorAttachmentNumber(GLenum attachment, const char* functionName) const; |
|
179 |
|
180 void EnsureColorAttachments(size_t colorAttachmentId); |
|
181 |
|
182 Attachment* AttachmentFor(GLenum attachment); |
|
183 void NotifyAttachableChanged() const; |
|
184 |
|
185 private: |
|
186 mutable GLenum mStatus; |
|
187 |
|
188 GLuint mGLName; |
|
189 bool mHasEverBeenBound; |
|
190 |
|
191 // we only store pointers to attached renderbuffers, not to attached textures, because |
|
192 // we will only need to initialize renderbuffers. Textures are already initialized. |
|
193 nsTArray<Attachment> mColorAttachments; |
|
194 Attachment mDepthAttachment, |
|
195 mStencilAttachment, |
|
196 mDepthStencilAttachment; |
|
197 }; |
|
198 |
|
199 } // namespace mozilla |
|
200 |
|
201 #endif |