blob: 03e2172a7aaf3be9fb88ccbbac0c0d42da9daf87 [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
39 firstFilter = true;
40 firstWrap = true;
Chet Haase98d3a642012-09-26 10:27:40 -070041
42 id = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -070043 }
44
Romain Guyd21b6e12011-11-30 20:21:23 -080045 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
46 GLenum renderTarget = GL_TEXTURE_2D) {
47 setWrapST(wrap, wrap, bindTexture, force, renderTarget);
48 }
49
50 void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070051 GLenum renderTarget = GL_TEXTURE_2D) {
52
53 if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) {
Romain Guy39d252a2011-12-12 18:14:06 -080054 firstWrap = false;
Romain Guye3c26852011-07-25 16:36:01 -070055
56 this->wrapS = wrapS;
57 this->wrapT = wrapT;
58
59 if (bindTexture) {
60 glBindTexture(renderTarget, id);
61 }
62
63 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
64 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
65 }
Romain Guy9ace8f52011-07-07 20:50:11 -070066 }
67
Romain Guyd21b6e12011-11-30 20:21:23 -080068 void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
69 GLenum renderTarget = GL_TEXTURE_2D) {
70 setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
71 }
72
73 void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070074 GLenum renderTarget = GL_TEXTURE_2D) {
75
76 if (firstFilter || force || min != minFilter || mag != magFilter) {
77 firstFilter = false;
78
79 minFilter = min;
80 magFilter = mag;
81
82 if (bindTexture) {
83 glBindTexture(renderTarget, id);
84 }
85
86 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
87 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
88 }
Romain Guy22158e12010-08-06 11:18:34 -070089 }
90
Romain Guyce0537b2010-06-29 21:05:21 -070091 /**
92 * Name of the texture.
93 */
94 GLuint id;
95 /**
Romain Guyfe880942010-06-30 16:05:32 -070096 * Generation of the backing bitmap,
97 */
98 uint32_t generation;
99 /**
Romain Guyce0537b2010-06-29 21:05:21 -0700100 * Indicates whether the texture requires blending.
101 */
102 bool blend;
103 /**
104 * Width of the backing bitmap.
105 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700106 uint32_t width;
Romain Guyce0537b2010-06-29 21:05:21 -0700107 /**
108 * Height of the backing bitmap.
109 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700110 uint32_t height;
Romain Guy22158e12010-08-06 11:18:34 -0700111 /**
112 * Indicates whether this texture should be cleaned up after use.
113 */
114 bool cleanup;
Romain Guy9aaa8262010-09-08 15:15:43 -0700115 /**
116 * Optional, size of the original bitmap.
117 */
118 uint32_t bitmapSize;
Romain Guy8164c2d2010-10-25 18:03:28 -0700119
120 /**
121 * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE.
122 */
123 GLenum wrapS;
124 GLenum wrapT;
Romain Guy9ace8f52011-07-07 20:50:11 -0700125
126 /**
127 * Last filters set on this texture. Defaults to GL_NEAREST.
128 */
129 GLenum minFilter;
130 GLenum magFilter;
Romain Guye3c26852011-07-25 16:36:01 -0700131
132private:
133 bool firstFilter;
134 bool firstWrap;
Romain Guyce0537b2010-06-29 21:05:21 -0700135}; // struct Texture
136
Romain Guy22158e12010-08-06 11:18:34 -0700137class AutoTexture {
138public:
139 AutoTexture(const Texture* texture): mTexture(texture) { }
140 ~AutoTexture() {
141 if (mTexture && mTexture->cleanup) {
142 glDeleteTextures(1, &mTexture->id);
143 delete mTexture;
144 }
145 }
146
147private:
148 const Texture* mTexture;
149}; // class AutoTexture
150
Romain Guyce0537b2010-06-29 21:05:21 -0700151}; // namespace uirenderer
152}; // namespace android
153
Romain Guy5b3b3522010-10-27 18:57:51 -0700154#endif // ANDROID_HWUI_TEXTURE_H