1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Renderbuffer.h: Defines the wrapper class Renderbuffer, as well as the
16 // class hierarchy used to store its contents: RenderbufferStorage, Colorbuffer,
17 // DepthStencilbuffer, Depthbuffer and Stencilbuffer. Implements GL renderbuffer
18 // objects and related functionality. [OpenGL ES 2.0.24] section 4.4.3 page 108.
19 
20 #ifndef LIBGLES_CM_RENDERBUFFER_H_
21 #define LIBGLES_CM_RENDERBUFFER_H_
22 
23 #include "common/Object.hpp"
24 #include "common/Image.hpp"
25 
26 #include <GLES/gl.h>
27 
28 namespace es1
29 {
30 class Texture2D;
31 class Renderbuffer;
32 class Colorbuffer;
33 class DepthStencilbuffer;
34 
35 class RenderbufferInterface
36 {
37 public:
38 	RenderbufferInterface();
39 
~RenderbufferInterface()40 	virtual ~RenderbufferInterface() {};
41 
42 	virtual void addProxyRef(const Renderbuffer *proxy);
43     virtual void releaseProxy(const Renderbuffer *proxy);
44 
45 	virtual egl::Image *getRenderTarget() = 0;
46     virtual egl::Image *createSharedImage() = 0;
47     virtual bool isShared() const = 0;
48 
49 	virtual GLsizei getWidth() const = 0;
50 	virtual GLsizei getHeight() const = 0;
51 	virtual GLenum getFormat() const = 0;
52 	virtual sw::Format getInternalFormat() const = 0;
53 	virtual GLsizei getSamples() const = 0;
54 
55 	GLuint getRedSize() const;
56 	GLuint getGreenSize() const;
57 	GLuint getBlueSize() const;
58 	GLuint getAlphaSize() const;
59 	GLuint getDepthSize() const;
60 	GLuint getStencilSize() const;
61 };
62 
63 class RenderbufferTexture2D : public RenderbufferInterface
64 {
65 public:
66 	RenderbufferTexture2D(Texture2D *texture);
67 
68 	virtual ~RenderbufferTexture2D();
69 
70 	virtual void addProxyRef(const Renderbuffer *proxy);
71     virtual void releaseProxy(const Renderbuffer *proxy);
72 
73 	virtual egl::Image *getRenderTarget();
74     virtual egl::Image *createSharedImage();
75     virtual bool isShared() const;
76 
77 	virtual GLsizei getWidth() const;
78 	virtual GLsizei getHeight() const;
79 	virtual GLenum getFormat() const;
80 	virtual sw::Format getInternalFormat() const;
81 	virtual GLsizei getSamples() const;
82 
83 private:
84 	gl::BindingPointer<Texture2D> mTexture2D;
85 };
86 
87 // A class derived from RenderbufferStorage is created whenever glRenderbufferStorage
88 // is called. The specific concrete type depends on whether the internal format is
89 // colour depth, stencil or packed depth/stencil.
90 class RenderbufferStorage : public RenderbufferInterface
91 {
92 public:
93 	RenderbufferStorage();
94 
95 	virtual ~RenderbufferStorage() = 0;
96 
97 	virtual egl::Image *getRenderTarget() = 0;
98     virtual egl::Image *createSharedImage() = 0;
99     virtual bool isShared() const = 0;
100 
101 	virtual GLsizei getWidth() const;
102 	virtual GLsizei getHeight() const;
103 	virtual GLenum getFormat() const;
104 	virtual sw::Format getInternalFormat() const;
105 	virtual GLsizei getSamples() const;
106 
107 protected:
108 	GLsizei mWidth;
109 	GLsizei mHeight;
110 	GLenum format;
111 	sw::Format internalFormat;
112 	GLsizei mSamples;
113 };
114 
115 // Renderbuffer implements the GL renderbuffer object.
116 // It's only a proxy for a RenderbufferInterface instance; the internal object
117 // can change whenever glRenderbufferStorage is called.
118 class Renderbuffer : public gl::NamedObject
119 {
120 public:
121 	Renderbuffer(GLuint name, RenderbufferInterface *storage);
122 
123 	virtual ~Renderbuffer();
124 
125 	// These functions from Object are overloaded here because
126     // Textures need to maintain their own count of references to them via
127     // Renderbuffers/RenderbufferTextures. These functions invoke those
128     // reference counting functions on the RenderbufferInterface.
129     virtual void addRef();
130     virtual void release();
131 
132 	egl::Image *getRenderTarget();
133     virtual egl::Image *createSharedImage();
134     virtual bool isShared() const;
135 
136 	GLsizei getWidth() const;
137 	GLsizei getHeight() const;
138 	GLenum getFormat() const;
139 	sw::Format getInternalFormat() const;
140 	GLuint getRedSize() const;
141 	GLuint getGreenSize() const;
142 	GLuint getBlueSize() const;
143 	GLuint getAlphaSize() const;
144 	GLuint getDepthSize() const;
145 	GLuint getStencilSize() const;
146 	GLsizei getSamples() const;
147 
148 	void setStorage(RenderbufferStorage *newStorage);
149 
150 private:
151 	RenderbufferInterface *mInstance;
152 };
153 
154 class Colorbuffer : public RenderbufferStorage
155 {
156 public:
157 	explicit Colorbuffer(egl::Image *renderTarget);
158 	Colorbuffer(GLsizei width, GLsizei height, GLenum format, GLsizei samples);
159 
160 	virtual ~Colorbuffer();
161 
162 	virtual egl::Image *getRenderTarget();
163     virtual egl::Image *createSharedImage();
164     virtual bool isShared() const;
165 
166 private:
167 	egl::Image *mRenderTarget;
168 };
169 
170 class DepthStencilbuffer : public RenderbufferStorage
171 {
172 public:
173 	explicit DepthStencilbuffer(egl::Image *depthStencil);
174 	DepthStencilbuffer(GLsizei width, GLsizei height, GLsizei samples);
175 
176 	~DepthStencilbuffer();
177 
178 	virtual egl::Image *getRenderTarget();
179     virtual egl::Image *createSharedImage();
180     virtual bool isShared() const;
181 
182 protected:
183 	egl::Image *mDepthStencil;
184 };
185 
186 class Depthbuffer : public DepthStencilbuffer
187 {
188 public:
189 	explicit Depthbuffer(egl::Image *depthStencil);
190 	Depthbuffer(GLsizei width, GLsizei height, GLsizei samples);
191 
192 	virtual ~Depthbuffer();
193 };
194 
195 class Stencilbuffer : public DepthStencilbuffer
196 {
197 public:
198 	explicit Stencilbuffer(egl::Image *depthStencil);
199 	Stencilbuffer(GLsizei width, GLsizei height, GLsizei samples);
200 
201 	virtual ~Stencilbuffer();
202 };
203 }
204 
205 #endif   // LIBGLES_CM_RENDERBUFFER_H_
206