GrGLTexture.h revision dd182cbca60a7f0003330c01dfc64f69f56aea90
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef GrGLTexture_DEFINED
11#define GrGLTexture_DEFINED
12
13#include "../GrGpu.h"
14#include "GrGLRenderTarget.h"
15
16/**
17 * A ref counted tex id that deletes the texture in its destructor.
18 */
19class GrGLTexID : public GrRefCnt {
20
21public:
22    GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool ownsID)
23        : fGL(gl)
24        , fTexID(texID)
25        , fOwnsID(ownsID) {
26    }
27
28    virtual ~GrGLTexID() {
29        if (0 != fTexID && fOwnsID) {
30            GR_GL_CALL(fGL, DeleteTextures(1, &fTexID));
31        }
32    }
33
34    void abandon() { fTexID = 0; }
35    GrGLuint id() const { return fTexID; }
36
37private:
38    const GrGLInterface* fGL;
39    GrGLuint             fTexID;
40    bool                 fOwnsID;
41};
42
43////////////////////////////////////////////////////////////////////////////////
44
45
46class GrGLTexture : public GrTexture {
47
48public:
49    enum Orientation {
50        kBottomUp_Orientation,
51        kTopDown_Orientation,
52    };
53
54    struct TexParams {
55        GrGLenum fFilter;
56        GrGLenum fWrapS;
57        GrGLenum fWrapT;
58        GrGLenum fSwizzleRGBA[4];
59        void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
60    };
61
62    struct Desc {
63        int             fWidth;
64        int             fHeight;
65        GrPixelConfig   fConfig;
66        GrGLuint        fTextureID;
67        bool            fOwnsID;
68        Orientation     fOrientation;
69    };
70
71    // creates a texture that is also an RT
72    GrGLTexture(GrGpuGL* gpu,
73                const Desc& textureDesc,
74                const GrGLRenderTarget::Desc& rtDesc);
75
76    // creates a non-RT texture
77    GrGLTexture(GrGpuGL* gpu,
78                const Desc& textureDesc);
79
80
81    virtual ~GrGLTexture() { this->release(); }
82
83    virtual intptr_t getTextureHandle() const;
84
85    // these functions
86    const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
87        *timestamp = fTexParamsTimestamp;
88        return fTexParams;
89    }
90    void setCachedTexParams(const TexParams& texParams,
91                            GrGpu::ResetTimestamp timestamp) {
92        fTexParams = texParams;
93        fTexParamsTimestamp = timestamp;
94    }
95    GrGLuint textureID() const { return fTexIDObj->id(); }
96
97    // Ganesh assumes texture coordinates have their origin
98    // in the top-left corner of the image. OpenGL, however,
99    // has the origin in the lower-left corner. For content that
100    // is loaded by Ganesh we just push the content "upside down"
101    // (by GL's understanding of the world) in glTex*Image and the
102    // addressing just works out. However, content generated by GL
103    // (FBO or externally imported texture) will be updside down
104    // and it is up to the GrGpuGL derivative to handle y-mirroing.
105    Orientation orientation() const { return fOrientation; }
106
107    static const GrGLenum* WrapMode2GLWrap();
108
109protected:
110
111    // overrides of GrTexture
112    virtual void onAbandon();
113    virtual void onRelease();
114
115private:
116    TexParams                       fTexParams;
117    GrGpu::ResetTimestamp           fTexParamsTimestamp;
118    GrGLTexID*                      fTexIDObj;
119    Orientation                     fOrientation;
120
121    void init(GrGpuGL* gpu,
122              const Desc& textureDesc,
123              const GrGLRenderTarget::Desc* rtDesc);
124
125    typedef GrTexture INHERITED;
126};
127
128#endif
129