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 // Texture.h: Defines the abstract Texture class and its concrete derived
16 // classes Texture2D and TextureCubeMap. Implements GL texture objects and
17 // related functionality. [OpenGL ES 2.0.24] section 3.7 page 63.
18 
19 #ifndef LIBGLES_CM_TEXTURE_H_
20 #define LIBGLES_CM_TEXTURE_H_
21 
22 #include "Renderbuffer.h"
23 #include "common/Object.hpp"
24 #include "utilities.h"
25 #include "libEGL/Texture.hpp"
26 #include "common/debug.h"
27 
28 #include <GLES/gl.h>
29 
30 #include <vector>
31 
32 namespace egl
33 {
34 class Surface;
35 class Config;
36 }
37 
38 namespace es1
39 {
40 class Framebuffer;
41 
42 enum
43 {
44 	IMPLEMENTATION_MAX_TEXTURE_LEVELS = sw::MIPMAP_LEVELS,
45 	IMPLEMENTATION_MAX_TEXTURE_SIZE = 1 << (IMPLEMENTATION_MAX_TEXTURE_LEVELS - 1),
46 	IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 1 << (IMPLEMENTATION_MAX_TEXTURE_LEVELS - 1),
47 	IMPLEMENTATION_MAX_RENDERBUFFER_SIZE = sw::OUTLINE_RESOLUTION,
48 };
49 
50 class Texture : public egl::Texture
51 {
52 public:
53 	explicit Texture(GLuint name);
54 
55 	sw::Resource *getResource() const;
56 
57 	virtual void addProxyRef(const Renderbuffer *proxy) = 0;
58 	virtual void releaseProxy(const Renderbuffer *proxy) = 0;
59 
60 	virtual GLenum getTarget() const = 0;
61 
62 	bool setMinFilter(GLenum filter);
63 	bool setMagFilter(GLenum filter);
64 	bool setWrapS(GLenum wrap);
65 	bool setWrapT(GLenum wrap);
66 	bool setMaxAnisotropy(GLfloat textureMaxAnisotropy);
67 	void setGenerateMipmap(GLboolean enable);
68 	void setCropRect(GLint u, GLint v, GLint w, GLint h);
69 
70 	GLenum getMinFilter() const;
71 	GLenum getMagFilter() const;
72 	GLenum getWrapS() const;
73 	GLenum getWrapT() const;
74 	GLfloat getMaxAnisotropy() const;
75 	GLboolean getGenerateMipmap() const;
76 	GLint getCropRectU() const;
77 	GLint getCropRectV() const;
78 	GLint getCropRectW() const;
79 	GLint getCropRectH() const;
80 
81 	virtual GLsizei getWidth(GLenum target, GLint level) const = 0;
82 	virtual GLsizei getHeight(GLenum target, GLint level) const = 0;
83 	virtual GLenum getFormat(GLenum target, GLint level) const = 0;
84 	virtual GLenum getType(GLenum target, GLint level) const = 0;
85 	virtual sw::Format getInternalFormat(GLenum target, GLint level) const = 0;
86 	virtual int getLevelCount() const = 0;
87 
88 	virtual bool isSamplerComplete() const = 0;
89 	virtual bool isCompressed(GLenum target, GLint level) const = 0;
90 	virtual bool isDepth(GLenum target, GLint level) const = 0;
91 
92 	virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
93 	virtual egl::Image *getRenderTarget(GLenum target, unsigned int level) = 0;
94 	virtual egl::Image *createSharedImage(GLenum target, unsigned int level);
95 	virtual bool isShared(GLenum target, unsigned int level) const = 0;
96 
97 	virtual void generateMipmaps() = 0;
98 	virtual void autoGenerateMipmaps() = 0;
99 
100 	virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
101 
102 protected:
103 	virtual ~Texture();
104 
105 	void setImage(GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, egl::Image *image);
106 	void subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, egl::Image *image);
107 	void setCompressedImage(GLsizei imageSize, const void *pixels, egl::Image *image);
108 	void subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, egl::Image *image);
109 
110 	bool copy(egl::Image *source, const sw::Rect &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, egl::Image *dest);
111 
112 	bool isMipmapFiltered() const;
113 
114 	GLenum mMinFilter;
115 	GLenum mMagFilter;
116 	GLenum mWrapS;
117 	GLenum mWrapT;
118 	GLfloat mMaxAnisotropy;
119 	GLboolean generateMipmap;
120 	GLint cropRectU;
121 	GLint cropRectV;
122 	GLint cropRectW;
123 	GLint cropRectH;
124 
125 	sw::Resource *resource;
126 };
127 
128 class Texture2D : public Texture
129 {
130 public:
131 	explicit Texture2D(GLuint name);
132 
133 	void addProxyRef(const Renderbuffer *proxy) override;
134 	void releaseProxy(const Renderbuffer *proxy) override;
135 	void sweep() override;
136 
137 	virtual GLenum getTarget() const;
138 
139 	virtual GLsizei getWidth(GLenum target, GLint level) const;
140 	virtual GLsizei getHeight(GLenum target, GLint level) const;
141 	virtual GLenum getFormat(GLenum target, GLint level) const;
142 	virtual GLenum getType(GLenum target, GLint level) const;
143 	virtual sw::Format getInternalFormat(GLenum target, GLint level) const;
144 	virtual int getLevelCount() const;
145 
146 	void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
147 	void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
148 	void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
149 	void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
150 	void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
151 	void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
152 
153 	void setImage(egl::Image *image);
154 
155 	virtual bool isSamplerComplete() const;
156 	virtual bool isCompressed(GLenum target, GLint level) const;
157 	virtual bool isDepth(GLenum target, GLint level) const;
158 	virtual void bindTexImage(egl::Surface *surface);
159 	virtual void releaseTexImage();
160 
161 	virtual void generateMipmaps();
162 	virtual void autoGenerateMipmaps();
163 
164 	virtual Renderbuffer *getRenderbuffer(GLenum target);
165 	virtual egl::Image *getRenderTarget(GLenum target, unsigned int level);
166 	virtual bool isShared(GLenum target, unsigned int level) const;
167 
168 	egl::Image *getImage(unsigned int level);
169 
170 protected:
171 	virtual ~Texture2D();
172 
173 	bool isMipmapComplete() const;
174 
175 	egl::Image *image[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
176 
177 	egl::Surface *mSurface;
178 
179 	// A specific internal reference count is kept for colorbuffer proxy references,
180 	// because, as the renderbuffer acting as proxy will maintain a binding pointer
181 	// back to this texture, there would be a circular reference if we used a binding
182 	// pointer here. This reference count will cause the pointer to be set to null if
183 	// the count drops to zero, but will not cause deletion of the Renderbuffer.
184 	Renderbuffer *mColorbufferProxy;
185 	unsigned int mProxyRefs;
186 };
187 
188 class TextureExternal : public Texture2D
189 {
190 public:
191 	explicit TextureExternal(GLuint name);
192 
193 	virtual GLenum getTarget() const;
194 
195 protected:
196 	virtual ~TextureExternal();
197 };
198 }
199 
200 #endif   // LIBGLES_CM_TEXTURE_H_
201