blob: a4aed0706b48c5c56bc7042450fceeeb5b6dd0f8 [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;
Romain Guy9ace8f52011-07-07 20:50:11 -070041 }
42
Romain Guyd21b6e12011-11-30 20:21:23 -080043 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
44 GLenum renderTarget = GL_TEXTURE_2D) {
45 setWrapST(wrap, wrap, bindTexture, force, renderTarget);
46 }
47
48 void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070049 GLenum renderTarget = GL_TEXTURE_2D) {
50
51 if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) {
52 firstWrap = true;
53
54 this->wrapS = wrapS;
55 this->wrapT = wrapT;
56
57 if (bindTexture) {
58 glBindTexture(renderTarget, id);
59 }
60
61 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
62 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
63 }
Romain Guy9ace8f52011-07-07 20:50:11 -070064 }
65
Romain Guyd21b6e12011-11-30 20:21:23 -080066 void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
67 GLenum renderTarget = GL_TEXTURE_2D) {
68 setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
69 }
70
71 void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, bool force = false,
Romain Guye3c26852011-07-25 16:36:01 -070072 GLenum renderTarget = GL_TEXTURE_2D) {
73
74 if (firstFilter || force || min != minFilter || mag != magFilter) {
75 firstFilter = false;
76
77 minFilter = min;
78 magFilter = mag;
79
80 if (bindTexture) {
81 glBindTexture(renderTarget, id);
82 }
83
84 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
85 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
86 }
Romain Guy22158e12010-08-06 11:18:34 -070087 }
88
Romain Guyce0537b2010-06-29 21:05:21 -070089 /**
90 * Name of the texture.
91 */
92 GLuint id;
93 /**
Romain Guyfe880942010-06-30 16:05:32 -070094 * Generation of the backing bitmap,
95 */
96 uint32_t generation;
97 /**
Romain Guyce0537b2010-06-29 21:05:21 -070098 * Indicates whether the texture requires blending.
99 */
100 bool blend;
101 /**
102 * Width of the backing bitmap.
103 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700104 uint32_t width;
Romain Guyce0537b2010-06-29 21:05:21 -0700105 /**
106 * Height of the backing bitmap.
107 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700108 uint32_t height;
Romain Guy22158e12010-08-06 11:18:34 -0700109 /**
110 * Indicates whether this texture should be cleaned up after use.
111 */
112 bool cleanup;
Romain Guy9aaa8262010-09-08 15:15:43 -0700113 /**
114 * Optional, size of the original bitmap.
115 */
116 uint32_t bitmapSize;
Romain Guy8164c2d2010-10-25 18:03:28 -0700117
118 /**
119 * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE.
120 */
121 GLenum wrapS;
122 GLenum wrapT;
Romain Guy9ace8f52011-07-07 20:50:11 -0700123
124 /**
125 * Last filters set on this texture. Defaults to GL_NEAREST.
126 */
127 GLenum minFilter;
128 GLenum magFilter;
Romain Guye3c26852011-07-25 16:36:01 -0700129
130private:
131 bool firstFilter;
132 bool firstWrap;
Romain Guyce0537b2010-06-29 21:05:21 -0700133}; // struct Texture
134
Romain Guy22158e12010-08-06 11:18:34 -0700135class AutoTexture {
136public:
137 AutoTexture(const Texture* texture): mTexture(texture) { }
138 ~AutoTexture() {
139 if (mTexture && mTexture->cleanup) {
140 glDeleteTextures(1, &mTexture->id);
141 delete mTexture;
142 }
143 }
144
145private:
146 const Texture* mTexture;
147}; // class AutoTexture
148
Romain Guyce0537b2010-06-29 21:05:21 -0700149}; // namespace uirenderer
150}; // namespace android
151
Romain Guy5b3b3522010-10-27 18:57:51 -0700152#endif // ANDROID_HWUI_TEXTURE_H