Texture.h revision a404e16e4933857464046d763ed7629cd0c86cbf
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HWUI_TEXTURE_H
18#define ANDROID_HWUI_TEXTURE_H
19
20#include <GLES2/gl2.h>
21
22namespace android {
23namespace uirenderer {
24
25class UvMapper;
26
27/**
28 * Represents an OpenGL texture.
29 */
30struct Texture {
31    Texture() {
32        cleanup = false;
33        bitmapSize = 0;
34
35        wrapS = GL_CLAMP_TO_EDGE;
36        wrapT = GL_CLAMP_TO_EDGE;
37
38        minFilter = GL_NEAREST;
39        magFilter = GL_NEAREST;
40
41        mipMap = false;
42
43        firstFilter = true;
44        firstWrap = true;
45
46        id = 0;
47
48        uvMapper = NULL;
49    }
50
51    virtual ~Texture() { }
52
53    void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
54                GLenum renderTarget = GL_TEXTURE_2D) {
55        setWrapST(wrap, wrap, bindTexture, force, renderTarget);
56    }
57
58    virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
59            bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
60
61        if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) {
62            firstWrap = false;
63
64            this->wrapS = wrapS;
65            this->wrapT = wrapT;
66
67            if (bindTexture) {
68                glBindTexture(renderTarget, id);
69            }
70
71            glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
72            glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
73        }
74    }
75
76    void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
77                GLenum renderTarget = GL_TEXTURE_2D) {
78        setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
79    }
80
81    virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
82            bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
83
84        if (firstFilter || force || min != minFilter || mag != magFilter) {
85            firstFilter = false;
86
87            minFilter = min;
88            magFilter = mag;
89
90            if (bindTexture) {
91                glBindTexture(renderTarget, id);
92            }
93
94            if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
95
96            glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
97            glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
98        }
99    }
100
101    /**
102     * Name of the texture.
103     */
104    GLuint id;
105    /**
106     * Generation of the backing bitmap,
107     */
108    uint32_t generation;
109    /**
110     * Indicates whether the texture requires blending.
111     */
112    bool blend;
113    /**
114     * Width of the backing bitmap.
115     */
116    uint32_t width;
117    /**
118     * Height of the backing bitmap.
119     */
120    uint32_t height;
121    /**
122     * Indicates whether this texture should be cleaned up after use.
123     */
124    bool cleanup;
125    /**
126     * Optional, size of the original bitmap.
127     */
128    uint32_t bitmapSize;
129    /**
130     * Indicates whether this texture will use trilinear filtering.
131     */
132    bool mipMap;
133
134    /**
135     * Optional, pointer to a texture coordinates mapper.
136     */
137    const UvMapper* uvMapper;
138
139private:
140    /**
141     * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE.
142     */
143    GLenum wrapS;
144    GLenum wrapT;
145
146    /**
147     * Last filters set on this texture. Defaults to GL_NEAREST.
148     */
149    GLenum minFilter;
150    GLenum magFilter;
151
152    bool firstFilter;
153    bool firstWrap;
154}; // struct Texture
155
156class AutoTexture {
157public:
158    AutoTexture(const Texture* texture): mTexture(texture) { }
159    ~AutoTexture() {
160        if (mTexture && mTexture->cleanup) {
161            glDeleteTextures(1, &mTexture->id);
162            delete mTexture;
163        }
164    }
165
166private:
167    const Texture* mTexture;
168}; // class AutoTexture
169
170}; // namespace uirenderer
171}; // namespace android
172
173#endif // ANDROID_HWUI_TEXTURE_H
174