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 
22 namespace android {
23 namespace uirenderer {
24 
25 class Caches;
26 class UvMapper;
27 
28 /**
29  * Represents an OpenGL texture.
30  */
31 class Texture {
32 public:
Texture(Caches & caches)33     Texture(Caches& caches) : mCaches(caches) { }
34 
~Texture()35     virtual ~Texture() { }
36 
37     inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
38                 GLenum renderTarget = GL_TEXTURE_2D) {
39         setWrapST(wrap, wrap, bindTexture, force, renderTarget);
40     }
41 
42     virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
43             bool force = false, GLenum renderTarget = GL_TEXTURE_2D);
44 
45     inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
46                 GLenum renderTarget = GL_TEXTURE_2D) {
47         setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
48     }
49 
50     virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
51             bool force = false, GLenum renderTarget = GL_TEXTURE_2D);
52 
53     /**
54      * Convenience method to call glDeleteTextures() on this texture's id.
55      */
56     void deleteTexture() const;
57 
58     /**
59      * Name of the texture.
60      */
61     GLuint id = 0;
62     /**
63      * Generation of the backing bitmap,
64      */
65     uint32_t generation = 0;
66     /**
67      * Indicates whether the texture requires blending.
68      */
69     bool blend = false;
70     /**
71      * Width of the backing bitmap.
72      */
73     uint32_t width = 0;
74     /**
75      * Height of the backing bitmap.
76      */
77     uint32_t height = 0;
78     /**
79      * Indicates whether this texture should be cleaned up after use.
80      */
81     bool cleanup = false;
82     /**
83      * Optional, size of the original bitmap.
84      */
85     uint32_t bitmapSize = 0;
86     /**
87      * Indicates whether this texture will use trilinear filtering.
88      */
89     bool mipMap = false;
90 
91     /**
92      * Optional, pointer to a texture coordinates mapper.
93      */
94     const UvMapper* uvMapper = nullptr;
95 
96     /**
97      * Whether or not the Texture is marked in use and thus not evictable for
98      * the current frame. This is reset at the start of a new frame.
99      */
100     void* isInUse = nullptr;
101 
102 private:
103     /**
104      * Last wrap modes set on this texture.
105      */
106     GLenum mWrapS = GL_CLAMP_TO_EDGE;
107     GLenum mWrapT = GL_CLAMP_TO_EDGE;
108 
109     /**
110      * Last filters set on this texture.
111      */
112     GLenum mMinFilter = GL_NEAREST;
113     GLenum mMagFilter = GL_NEAREST;
114 
115     bool mFirstFilter = true;
116     bool mFirstWrap = true;
117 
118     Caches& mCaches;
119 }; // struct Texture
120 
121 class AutoTexture {
122 public:
AutoTexture(const Texture * texture)123     AutoTexture(const Texture* texture): mTexture(texture) { }
~AutoTexture()124     ~AutoTexture() {
125         if (mTexture && mTexture->cleanup) {
126             mTexture->deleteTexture();
127             delete mTexture;
128         }
129     }
130 
131 private:
132     const Texture* mTexture;
133 }; // class AutoTexture
134 
135 }; // namespace uirenderer
136 }; // namespace android
137 
138 #endif // ANDROID_HWUI_TEXTURE_H
139