blob: dd39cae14faba2d34eaa3f4c2d04e1b5eef154d4 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_TEXTURE_H
18#define ANDROID_HWUI_TEXTURE_H
Romain Guyce0537b2010-06-29 21:05:21 -070019
20#include <GLES2/gl2.h>
21
22namespace android {
23namespace uirenderer {
24
Romain Guy3b748a42013-04-17 18:54:38 -070025class UvMapper;
26
Romain Guyce0537b2010-06-29 21:05:21 -070027/**
28 * Represents an OpenGL texture.
29 */
30struct Texture {
Romain Guy22158e12010-08-06 11:18:34 -070031 Texture() {
32 cleanup = false;
Romain Guy9aaa8262010-09-08 15:15:43 -070033 bitmapSize = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -070034
Romain Guy8164c2d2010-10-25 18:03:28 -070035 wrapS = GL_CLAMP_TO_EDGE;
36 wrapT = GL_CLAMP_TO_EDGE;
Romain Guy9ace8f52011-07-07 20:50:11 -070037
38 minFilter = GL_NEAREST;
39 magFilter = GL_NEAREST;
Romain Guye3c26852011-07-25 16:36:01 -070040
Romain Guy713e1bb2012-10-16 18:44:09 -070041 mipMap = false;
42
Romain Guye3c26852011-07-25 16:36:01 -070043 firstFilter = true;
44 firstWrap = true;
Chet Haase98d3a642012-09-26 10:27:40 -070045
46 id = 0;
Romain Guy3b748a42013-04-17 18:54:38 -070047
48 uvMapper = NULL;
Romain Guy9ace8f52011-07-07 20:50:11 -070049 }
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
52 GLenum renderTarget = GL_TEXTURE_2D) {
53 setWrapST(wrap, wrap, bindTexture, force, renderTarget);
54 }
55
56 void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070057 GLenum renderTarget = GL_TEXTURE_2D) {
58
59 if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) {
Romain Guy39d252a2011-12-12 18:14:06 -080060 firstWrap = false;
Romain Guye3c26852011-07-25 16:36:01 -070061
62 this->wrapS = wrapS;
63 this->wrapT = wrapT;
64
65 if (bindTexture) {
66 glBindTexture(renderTarget, id);
67 }
68
69 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
70 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
71 }
Romain Guy9ace8f52011-07-07 20:50:11 -070072 }
73
Romain Guyd21b6e12011-11-30 20:21:23 -080074 void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
75 GLenum renderTarget = GL_TEXTURE_2D) {
76 setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
77 }
78
79 void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070080 GLenum renderTarget = GL_TEXTURE_2D) {
81
82 if (firstFilter || force || min != minFilter || mag != magFilter) {
83 firstFilter = false;
84
85 minFilter = min;
86 magFilter = mag;
87
88 if (bindTexture) {
89 glBindTexture(renderTarget, id);
90 }
91
Romain Guy713e1bb2012-10-16 18:44:09 -070092 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
93
Romain Guye3c26852011-07-25 16:36:01 -070094 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
95 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
96 }
Romain Guy22158e12010-08-06 11:18:34 -070097 }
98
Romain Guyce0537b2010-06-29 21:05:21 -070099 /**
100 * Name of the texture.
101 */
102 GLuint id;
103 /**
Romain Guyfe880942010-06-30 16:05:32 -0700104 * Generation of the backing bitmap,
105 */
106 uint32_t generation;
107 /**
Romain Guyce0537b2010-06-29 21:05:21 -0700108 * Indicates whether the texture requires blending.
109 */
110 bool blend;
111 /**
112 * Width of the backing bitmap.
113 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700114 uint32_t width;
Romain Guyce0537b2010-06-29 21:05:21 -0700115 /**
116 * Height of the backing bitmap.
117 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700118 uint32_t height;
Romain Guy22158e12010-08-06 11:18:34 -0700119 /**
120 * Indicates whether this texture should be cleaned up after use.
121 */
122 bool cleanup;
Romain Guy9aaa8262010-09-08 15:15:43 -0700123 /**
124 * Optional, size of the original bitmap.
125 */
126 uint32_t bitmapSize;
Romain Guy713e1bb2012-10-16 18:44:09 -0700127 /**
128 * Indicates whether this texture will use trilinear filtering.
129 */
130 bool mipMap;
Romain Guy8164c2d2010-10-25 18:03:28 -0700131
Romain Guy3b748a42013-04-17 18:54:38 -0700132 /**
133 * Optional, pointer to a texture coordinates mapper.
134 */
135 const UvMapper* uvMapper;
136
Romain Guy713e1bb2012-10-16 18:44:09 -0700137private:
Romain Guy8164c2d2010-10-25 18:03:28 -0700138 /**
139 * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE.
140 */
141 GLenum wrapS;
142 GLenum wrapT;
Romain Guy9ace8f52011-07-07 20:50:11 -0700143
144 /**
145 * Last filters set on this texture. Defaults to GL_NEAREST.
146 */
147 GLenum minFilter;
148 GLenum magFilter;
Romain Guye3c26852011-07-25 16:36:01 -0700149
Romain Guye3c26852011-07-25 16:36:01 -0700150 bool firstFilter;
151 bool firstWrap;
Romain Guyce0537b2010-06-29 21:05:21 -0700152}; // struct Texture
153
Romain Guy22158e12010-08-06 11:18:34 -0700154class AutoTexture {
155public:
156 AutoTexture(const Texture* texture): mTexture(texture) { }
157 ~AutoTexture() {
158 if (mTexture && mTexture->cleanup) {
159 glDeleteTextures(1, &mTexture->id);
160 delete mTexture;
161 }
162 }
163
164private:
165 const Texture* mTexture;
166}; // class AutoTexture
167
Romain Guyce0537b2010-06-29 21:05:21 -0700168}; // namespace uirenderer
169}; // namespace android
170
Romain Guy5b3b3522010-10-27 18:57:51 -0700171#endif // ANDROID_HWUI_TEXTURE_H