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 GLenum getFormat(GLenum target, GLint level) const = 0; 80 virtual GLenum getType(GLenum target, GLint level) const = 0; 81 virtual sw::Format getInternalFormat(GLenum target, GLint level) const = 0; 82 virtual int getLevelCount() const = 0; 83 84 virtual bool isSamplerComplete() const = 0; 85 virtual bool isCompressed(GLenum target, GLint level) const = 0; 86 virtual bool isDepth(GLenum target, GLint level) const = 0; 87 88 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0; 89 virtual egl::Image *getRenderTarget(GLenum target, unsigned int level) = 0; 90 egl::Image *createSharedImage(GLenum target, unsigned int level); 91 virtual bool isShared(GLenum target, unsigned int level) const = 0; 92 93 virtual void generateMipmaps() = 0; 94 virtual void autoGenerateMipmaps() = 0; 95 96 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0; 97 98 protected: 99 ~Texture() override; 100 101 void setImage(egl::Context *context, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, egl::Image *image); 102 void subImage(egl::Context *context, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, egl::Image *image); 103 void setCompressedImage(GLsizei imageSize, const void *pixels, egl::Image *image); 104 void subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, egl::Image *image); 105 106 bool copy(egl::Image *source, const sw::Rect &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, egl::Image *dest); 107 108 bool isMipmapFiltered() const; 109 110 GLenum mMinFilter; 111 GLenum mMagFilter; 112 GLenum mWrapS; 113 GLenum mWrapT; 114 GLfloat mMaxAnisotropy; 115 GLboolean generateMipmap; 116 GLint cropRectU; 117 GLint cropRectV; 118 GLint cropRectW; 119 GLint cropRectH; 120 121 sw::Resource *resource; 122 }; 123 124 class Texture2D : public Texture 125 { 126 public: 127 explicit Texture2D(GLuint name); 128 129 void addProxyRef(const Renderbuffer *proxy) override; 130 void releaseProxy(const Renderbuffer *proxy) override; 131 void sweep() override; 132 133 GLenum getTarget() const override; 134 135 GLsizei getWidth(GLenum target, GLint level) const override; 136 GLsizei getHeight(GLenum target, GLint level) const override; 137 GLenum getFormat(GLenum target, GLint level) const override; 138 GLenum getType(GLenum target, GLint level) const override; 139 sw::Format getInternalFormat(GLenum target, GLint level) const override; 140 int getLevelCount() const override; 141 142 void setImage(egl::Context *context, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels); 143 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels); 144 void subImage(egl::Context *context, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels); 145 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels); 146 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source); 147 void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source); 148 149 void setSharedImage(egl::Image *image); 150 151 bool isSamplerComplete() const override; 152 bool isCompressed(GLenum target, GLint level) const override; 153 bool isDepth(GLenum target, GLint level) const override; 154 void bindTexImage(gl::Surface *surface); 155 void releaseTexImage() override; 156 157 void generateMipmaps() override; 158 void autoGenerateMipmaps() override; 159 160 Renderbuffer *getRenderbuffer(GLenum target) override; 161 egl::Image *getRenderTarget(GLenum target, unsigned int level) override; 162 bool isShared(GLenum target, unsigned int level) const override; 163 164 egl::Image *getImage(unsigned int level); 165 166 protected: 167 ~Texture2D() override; 168 169 bool isMipmapComplete() const; 170 171 egl::Image *image[IMPLEMENTATION_MAX_TEXTURE_LEVELS]; 172 173 gl::Surface *mSurface; 174 175 // A specific internal reference count is kept for colorbuffer proxy references, 176 // because, as the renderbuffer acting as proxy will maintain a binding pointer 177 // back to this texture, there would be a circular reference if we used a binding 178 // pointer here. This reference count will cause the pointer to be set to null if 179 // the count drops to zero, but will not cause deletion of the Renderbuffer. 180 Renderbuffer *mColorbufferProxy; 181 unsigned int mProxyRefs; 182 }; 183 184 class TextureExternal : public Texture2D 185 { 186 public: 187 explicit TextureExternal(GLuint name); 188 189 GLenum getTarget() const override; 190 191 protected: 192 ~TextureExternal() override; 193 }; 194 } 195 196 #endif // LIBGLES_CM_TEXTURE_H_ 197