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