blob: 8d88bdce3c9ee8bc11b9e5d6a09aa84936892aa8 [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
25/**
26 * Represents an OpenGL texture.
27 */
28struct Texture {
Romain Guy22158e12010-08-06 11:18:34 -070029 Texture() {
30 cleanup = false;
Romain Guy9aaa8262010-09-08 15:15:43 -070031 bitmapSize = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -070032
Romain Guy8164c2d2010-10-25 18:03:28 -070033 wrapS = GL_CLAMP_TO_EDGE;
34 wrapT = GL_CLAMP_TO_EDGE;
Romain Guy9ace8f52011-07-07 20:50:11 -070035
36 minFilter = GL_NEAREST;
37 magFilter = GL_NEAREST;
Romain Guye3c26852011-07-25 16:36:01 -070038
Romain Guy713e1bb2012-10-16 18:44:09 -070039 mipMap = false;
40
Romain Guye3c26852011-07-25 16:36:01 -070041 firstFilter = true;
42 firstWrap = true;
Chet Haase98d3a642012-09-26 10:27:40 -070043
44 id = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -070045 }
46
Romain Guyd21b6e12011-11-30 20:21:23 -080047 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
48 GLenum renderTarget = GL_TEXTURE_2D) {
49 setWrapST(wrap, wrap, bindTexture, force, renderTarget);
50 }
51
52 void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070053 GLenum renderTarget = GL_TEXTURE_2D) {
54
55 if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) {
Romain Guy39d252a2011-12-12 18:14:06 -080056 firstWrap = false;
Romain Guye3c26852011-07-25 16:36:01 -070057
58 this->wrapS = wrapS;
59 this->wrapT = wrapT;
60
61 if (bindTexture) {
62 glBindTexture(renderTarget, id);
63 }
64
65 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
66 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
67 }
Romain Guy9ace8f52011-07-07 20:50:11 -070068 }
69
Romain Guyd21b6e12011-11-30 20:21:23 -080070 void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
71 GLenum renderTarget = GL_TEXTURE_2D) {
72 setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
73 }
74
75 void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070076 GLenum renderTarget = GL_TEXTURE_2D) {
77
78 if (firstFilter || force || min != minFilter || mag != magFilter) {
79 firstFilter = false;
80
81 minFilter = min;
82 magFilter = mag;
83
84 if (bindTexture) {
85 glBindTexture(renderTarget, id);
86 }
87
Romain Guy713e1bb2012-10-16 18:44:09 -070088 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
89
Romain Guye3c26852011-07-25 16:36:01 -070090 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
91 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
92 }
Romain Guy22158e12010-08-06 11:18:34 -070093 }
94
Romain Guyce0537b2010-06-29 21:05:21 -070095 /**
96 * Name of the texture.
97 */
98 GLuint id;
99 /**
Romain Guyfe880942010-06-30 16:05:32 -0700100 * Generation of the backing bitmap,
101 */
102 uint32_t generation;
103 /**
Romain Guyce0537b2010-06-29 21:05:21 -0700104 * Indicates whether the texture requires blending.
105 */
106 bool blend;
107 /**
108 * Width of the backing bitmap.
109 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700110 uint32_t width;
Romain Guyce0537b2010-06-29 21:05:21 -0700111 /**
112 * Height of the backing bitmap.
113 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700114 uint32_t height;
Romain Guy22158e12010-08-06 11:18:34 -0700115 /**
116 * Indicates whether this texture should be cleaned up after use.
117 */
118 bool cleanup;
Romain Guy9aaa8262010-09-08 15:15:43 -0700119 /**
120 * Optional, size of the original bitmap.
121 */
122 uint32_t bitmapSize;
Romain Guy713e1bb2012-10-16 18:44:09 -0700123 /**
124 * Indicates whether this texture will use trilinear filtering.
125 */
126 bool mipMap;
Romain Guy8164c2d2010-10-25 18:03:28 -0700127
Romain Guy713e1bb2012-10-16 18:44:09 -0700128private:
Romain Guy8164c2d2010-10-25 18:03:28 -0700129 /**
130 * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE.
131 */
132 GLenum wrapS;
133 GLenum wrapT;
Romain Guy9ace8f52011-07-07 20:50:11 -0700134
135 /**
136 * Last filters set on this texture. Defaults to GL_NEAREST.
137 */
138 GLenum minFilter;
139 GLenum magFilter;
Romain Guye3c26852011-07-25 16:36:01 -0700140
Romain Guye3c26852011-07-25 16:36:01 -0700141 bool firstFilter;
142 bool firstWrap;
Romain Guyce0537b2010-06-29 21:05:21 -0700143}; // struct Texture
144
Romain Guy22158e12010-08-06 11:18:34 -0700145class AutoTexture {
146public:
147 AutoTexture(const Texture* texture): mTexture(texture) { }
148 ~AutoTexture() {
149 if (mTexture && mTexture->cleanup) {
150 glDeleteTextures(1, &mTexture->id);
151 delete mTexture;
152 }
153 }
154
155private:
156 const Texture* mTexture;
157}; // class AutoTexture
158
Romain Guyce0537b2010-06-29 21:05:21 -0700159}; // namespace uirenderer
160}; // namespace android
161
Romain Guy5b3b3522010-10-27 18:57:51 -0700162#endif // ANDROID_HWUI_TEXTURE_H