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 WEBGL_RENDERBUFFER_H_
7 #define WEBGL_RENDERBUFFER_H_
8 
9 #include "mozilla/LinkedList.h"
10 #include "nsWrapperCache.h"
11 
12 #include "WebGLFramebufferAttachable.h"
13 #include "WebGLObjectModel.h"
14 #include "WebGLStrongTypes.h"
15 
16 namespace mozilla {
17 namespace webgl {
18 struct FormatUsageInfo;
19 }
20 
21 class WebGLRenderbuffer final
22     : public nsWrapperCache
23     , public WebGLRefCountedObject<WebGLRenderbuffer>
24     , public LinkedListElement<WebGLRenderbuffer>
25     , public WebGLRectangleObject
26     , public WebGLFramebufferAttachable
27 {
28     friend class WebGLContext;
29     friend class WebGLFramebuffer;
30     friend class WebGLFBAttachPoint;
31 
32 public:
33     const GLuint mPrimaryRB;
34 protected:
35     const bool mEmulatePackedDepthStencil;
36     GLuint mSecondaryRB;
37     const webgl::FormatUsageInfo* mFormat;
38     GLsizei mSamples;
39 
40     WebGLImageDataStatus mImageDataStatus;
41 
42     bool mHasBeenBound;
43 
44 public:
45     NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLRenderbuffer)
46     NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLRenderbuffer)
47 
48     explicit WebGLRenderbuffer(WebGLContext* webgl);
49 
50     void Delete();
51 
HasUninitializedImageData()52     bool HasUninitializedImageData() const {
53         MOZ_ASSERT(mImageDataStatus != WebGLImageDataStatus::NoImageData);
54         return mImageDataStatus == WebGLImageDataStatus::UninitializedImageData;
55     }
56 
IsDefined()57     bool IsDefined() const {
58         if (!mFormat) {
59             MOZ_ASSERT(!mWidth && !mHeight);
60             return false;
61         }
62         return true;
63     }
64 
Samples()65     GLsizei Samples() const { return mSamples; }
66 
Format()67     const webgl::FormatUsageInfo* Format() const { return mFormat; }
68 
69     int64_t MemoryUsage() const;
70 
GetParentObject()71     WebGLContext* GetParentObject() const {
72         return mContext;
73     }
74 
75     void RenderbufferStorage(const char* funcName, uint32_t samples,
76                              GLenum internalFormat, uint32_t width, uint32_t height);
77     // Only handles a subset of `pname`s.
78     GLint GetRenderbufferParameter(RBTarget target, RBParam pname) const;
79 
80     virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override;
81 
82 protected:
~WebGLRenderbuffer()83     ~WebGLRenderbuffer() {
84         DeleteOnce();
85     }
86 
87     void DoFramebufferRenderbuffer(FBTarget target, GLenum attachment) const;
88     GLenum DoRenderbufferStorage(uint32_t samples, const webgl::FormatUsageInfo* format,
89                                  uint32_t width, uint32_t height);
90 };
91 
92 } // namespace mozilla
93 
94 #endif // WEBGL_RENDERBUFFER_H_
95