Home | History | Annotate | Download | only in libGLESv2
      1 //
      2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // Texture.h: Defines the abstract gl::Texture class and its concrete derived
      8 // classes Texture2D and TextureCubeMap. Implements GL texture objects and
      9 // related functionality. [OpenGL ES 2.0.24] section 3.7 page 63.
     10 
     11 #ifndef LIBGLESV2_TEXTURE_H_
     12 #define LIBGLESV2_TEXTURE_H_
     13 
     14 #include <vector>
     15 
     16 #define GL_APICALL
     17 #include <GLES2/gl2.h>
     18 
     19 #include "common/debug.h"
     20 #include "common/RefCountObject.h"
     21 #include "libGLESv2/angletypes.h"
     22 
     23 namespace egl
     24 {
     25 class Surface;
     26 }
     27 
     28 namespace rx
     29 {
     30 class Renderer;
     31 class TextureStorageInterface;
     32 class TextureStorageInterface2D;
     33 class TextureStorageInterfaceCube;
     34 class RenderTarget;
     35 class Image;
     36 }
     37 
     38 namespace gl
     39 {
     40 class Framebuffer;
     41 class Renderbuffer;
     42 
     43 enum
     44 {
     45     // These are the maximums the implementation can support
     46     // The actual GL caps are limited by the device caps
     47     // and should be queried from the Context
     48     IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
     49     IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
     50 
     51     IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15   // 1+log2 of MAX_TEXTURE_SIZE
     52 };
     53 
     54 class Texture : public RefCountObject
     55 {
     56   public:
     57     Texture(rx::Renderer *renderer, GLuint id);
     58 
     59     virtual ~Texture();
     60 
     61     virtual void addProxyRef(const Renderbuffer *proxy) = 0;
     62     virtual void releaseProxy(const Renderbuffer *proxy) = 0;
     63 
     64     virtual GLenum getTarget() const = 0;
     65 
     66     bool setMinFilter(GLenum filter);
     67     bool setMagFilter(GLenum filter);
     68     bool setWrapS(GLenum wrap);
     69     bool setWrapT(GLenum wrap);
     70     bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
     71     bool setUsage(GLenum usage);
     72 
     73     GLenum getMinFilter() const;
     74     GLenum getMagFilter() const;
     75     GLenum getWrapS() const;
     76     GLenum getWrapT() const;
     77     float getMaxAnisotropy() const;
     78     int getLodOffset();
     79     void getSamplerState(SamplerState *sampler);
     80     GLenum getUsage() const;
     81     bool isMipmapFiltered() const;
     82 
     83     virtual bool isSamplerComplete() const = 0;
     84 
     85     rx::TextureStorageInterface *getNativeTexture();
     86     virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
     87 
     88     virtual void generateMipmaps() = 0;
     89     virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
     90 
     91     bool hasDirtyParameters() const;
     92     bool hasDirtyImages() const;
     93     void resetDirty();
     94     unsigned int getTextureSerial();
     95     unsigned int getRenderTargetSerial(GLenum target);
     96 
     97     bool isImmutable() const;
     98 
     99     static const GLuint INCOMPLETE_TEXTURE_ID = static_cast<GLuint>(-1);   // Every texture takes an id at creation time. The value is arbitrary because it is never registered with the resource manager.
    100 
    101   protected:
    102     void setImage(GLint unpackAlignment, const void *pixels, rx::Image *image);
    103     bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image);
    104     void setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image);
    105     bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, rx::Image *image);
    106 
    107     GLint creationLevels(GLsizei width, GLsizei height) const;
    108     GLint creationLevels(GLsizei size) const;
    109 
    110     virtual void createTexture() = 0;
    111     virtual void updateTexture() = 0;
    112     virtual void convertToRenderTarget() = 0;
    113     virtual rx::RenderTarget *getRenderTarget(GLenum target) = 0;
    114 
    115     virtual int levelCount() = 0;
    116 
    117     rx::Renderer *mRenderer;
    118 
    119     SamplerState mSamplerState;
    120     GLenum mUsage;
    121 
    122     bool mDirtyImages;
    123 
    124     bool mImmutable;
    125 
    126   private:
    127     DISALLOW_COPY_AND_ASSIGN(Texture);
    128 
    129     virtual rx::TextureStorageInterface *getStorage(bool renderTarget) = 0;
    130 };
    131 
    132 class Texture2D : public Texture
    133 {
    134   public:
    135     Texture2D(rx::Renderer *renderer, GLuint id);
    136 
    137     ~Texture2D();
    138 
    139     void addProxyRef(const Renderbuffer *proxy);
    140     void releaseProxy(const Renderbuffer *proxy);
    141 
    142     virtual GLenum getTarget() const;
    143 
    144     GLsizei getWidth(GLint level) const;
    145     GLsizei getHeight(GLint level) const;
    146     GLenum getInternalFormat(GLint level) const;
    147     GLenum getActualFormat(GLint level) const;
    148     bool isCompressed(GLint level) const;
    149     bool isDepth(GLint level) const;
    150 
    151     void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    152     void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
    153     void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    154     void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
    155     void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
    156     virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
    157     void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
    158 
    159     virtual bool isSamplerComplete() const;
    160     virtual void bindTexImage(egl::Surface *surface);
    161     virtual void releaseTexImage();
    162 
    163     virtual void generateMipmaps();
    164 
    165     virtual Renderbuffer *getRenderbuffer(GLenum target);
    166 
    167   protected:
    168     friend class RenderbufferTexture2D;
    169     virtual rx::RenderTarget *getRenderTarget(GLenum target);
    170     virtual rx::RenderTarget *getDepthStencil(GLenum target);
    171     virtual int levelCount();
    172 
    173   private:
    174     DISALLOW_COPY_AND_ASSIGN(Texture2D);
    175 
    176     virtual void createTexture();
    177     virtual void updateTexture();
    178     virtual void convertToRenderTarget();
    179     virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
    180 
    181     bool isMipmapComplete() const;
    182 
    183     void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
    184     void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
    185 
    186     rx::Image *mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
    187 
    188     rx::TextureStorageInterface2D *mTexStorage;
    189     egl::Surface *mSurface;
    190 
    191     // A specific internal reference count is kept for colorbuffer proxy references,
    192     // because, as the renderbuffer acting as proxy will maintain a binding pointer
    193     // back to this texture, there would be a circular reference if we used a binding
    194     // pointer here. This reference count will cause the pointer to be set to NULL if
    195     // the count drops to zero, but will not cause deletion of the Renderbuffer.
    196     Renderbuffer *mColorbufferProxy;
    197     unsigned int mProxyRefs;
    198 };
    199 
    200 class TextureCubeMap : public Texture
    201 {
    202   public:
    203     TextureCubeMap(rx::Renderer *renderer, GLuint id);
    204 
    205     ~TextureCubeMap();
    206 
    207     void addProxyRef(const Renderbuffer *proxy);
    208     void releaseProxy(const Renderbuffer *proxy);
    209 
    210     virtual GLenum getTarget() const;
    211 
    212     GLsizei getWidth(GLenum target, GLint level) const;
    213     GLsizei getHeight(GLenum target, GLint level) const;
    214     GLenum getInternalFormat(GLenum target, GLint level) const;
    215     GLenum getActualFormat(GLenum target, GLint level) const;
    216     bool isCompressed(GLenum target, GLint level) const;
    217 
    218     void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    219     void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    220     void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    221     void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    222     void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    223     void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    224 
    225     void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
    226 
    227     void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    228     void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
    229     void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
    230     virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
    231     void storage(GLsizei levels, GLenum internalformat, GLsizei size);
    232 
    233     virtual bool isSamplerComplete() const;
    234 
    235     virtual void generateMipmaps();
    236 
    237     virtual Renderbuffer *getRenderbuffer(GLenum target);
    238 
    239     static unsigned int faceIndex(GLenum face);
    240 
    241   protected:
    242     friend class RenderbufferTextureCubeMap;
    243     virtual rx::RenderTarget *getRenderTarget(GLenum target);
    244     virtual int levelCount();
    245 
    246   private:
    247     DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
    248 
    249     virtual void createTexture();
    250     virtual void updateTexture();
    251     virtual void convertToRenderTarget();
    252     virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
    253 
    254     bool isCubeComplete() const;
    255     bool isMipmapCubeComplete() const;
    256 
    257     void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
    258     void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
    259     void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
    260 
    261     rx::Image *mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
    262 
    263     rx::TextureStorageInterfaceCube *mTexStorage;
    264 
    265     // A specific internal reference count is kept for colorbuffer proxy references,
    266     // because, as the renderbuffer acting as proxy will maintain a binding pointer
    267     // back to this texture, there would be a circular reference if we used a binding
    268     // pointer here. This reference count will cause the pointer to be set to NULL if
    269     // the count drops to zero, but will not cause deletion of the Renderbuffer.
    270     Renderbuffer *mFaceProxies[6];
    271     unsigned int *mFaceProxyRefs[6];
    272 };
    273 }
    274 
    275 #endif   // LIBGLESV2_TEXTURE_H_
    276