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