Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_TEXTURE_H |
| 18 | #define ANDROID_HWUI_TEXTURE_H |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 25 | class UvMapper; |
| 26 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Represents an OpenGL texture. |
| 29 | */ |
| 30 | struct Texture { |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 31 | Texture() { |
| 32 | cleanup = false; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 33 | bitmapSize = 0; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 34 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 35 | wrapS = GL_CLAMP_TO_EDGE; |
| 36 | wrapT = GL_CLAMP_TO_EDGE; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 37 | |
| 38 | minFilter = GL_NEAREST; |
| 39 | magFilter = GL_NEAREST; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 40 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 41 | mipMap = false; |
| 42 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 43 | firstFilter = true; |
| 44 | firstWrap = true; |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 45 | |
| 46 | id = 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 47 | |
| 48 | uvMapper = NULL; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 51 | virtual ~Texture() { } |
| 52 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 53 | void setWrap(GLenum wrap, bool bindTexture = false, bool force = false, |
| 54 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 55 | setWrapST(wrap, wrap, bindTexture, force, renderTarget); |
| 56 | } |
| 57 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 58 | virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, |
| 59 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) { |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 60 | |
| 61 | if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) { |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 62 | firstWrap = false; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 63 | |
| 64 | this->wrapS = wrapS; |
| 65 | this->wrapT = wrapT; |
| 66 | |
| 67 | if (bindTexture) { |
| 68 | glBindTexture(renderTarget, id); |
| 69 | } |
| 70 | |
| 71 | glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS); |
| 72 | glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT); |
| 73 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 76 | void setFilter(GLenum filter, bool bindTexture = false, bool force = false, |
| 77 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 78 | setFilterMinMag(filter, filter, bindTexture, force, renderTarget); |
| 79 | } |
| 80 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 81 | virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, |
| 82 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) { |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 83 | |
| 84 | if (firstFilter || force || min != minFilter || mag != magFilter) { |
| 85 | firstFilter = false; |
| 86 | |
| 87 | minFilter = min; |
| 88 | magFilter = mag; |
| 89 | |
| 90 | if (bindTexture) { |
| 91 | glBindTexture(renderTarget, id); |
| 92 | } |
| 93 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 94 | if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR; |
| 95 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 96 | glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min); |
| 97 | glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag); |
| 98 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Name of the texture. |
| 103 | */ |
| 104 | GLuint id; |
| 105 | /** |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 106 | * Generation of the backing bitmap, |
| 107 | */ |
| 108 | uint32_t generation; |
| 109 | /** |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 110 | * Indicates whether the texture requires blending. |
| 111 | */ |
| 112 | bool blend; |
| 113 | /** |
| 114 | * Width of the backing bitmap. |
| 115 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 116 | uint32_t width; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Height of the backing bitmap. |
| 119 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 120 | uint32_t height; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Indicates whether this texture should be cleaned up after use. |
| 123 | */ |
| 124 | bool cleanup; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Optional, size of the original bitmap. |
| 127 | */ |
| 128 | uint32_t bitmapSize; |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Indicates whether this texture will use trilinear filtering. |
| 131 | */ |
| 132 | bool mipMap; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 133 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Optional, pointer to a texture coordinates mapper. |
| 136 | */ |
| 137 | const UvMapper* uvMapper; |
| 138 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 139 | private: |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE. |
| 142 | */ |
| 143 | GLenum wrapS; |
| 144 | GLenum wrapT; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Last filters set on this texture. Defaults to GL_NEAREST. |
| 148 | */ |
| 149 | GLenum minFilter; |
| 150 | GLenum magFilter; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 151 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 152 | bool firstFilter; |
| 153 | bool firstWrap; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 154 | }; // struct Texture |
| 155 | |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 156 | class AutoTexture { |
| 157 | public: |
| 158 | AutoTexture(const Texture* texture): mTexture(texture) { } |
| 159 | ~AutoTexture() { |
| 160 | if (mTexture && mTexture->cleanup) { |
| 161 | glDeleteTextures(1, &mTexture->id); |
| 162 | delete mTexture; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | const Texture* mTexture; |
| 168 | }; // class AutoTexture |
| 169 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 170 | }; // namespace uirenderer |
| 171 | }; // namespace android |
| 172 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 173 | #endif // ANDROID_HWUI_TEXTURE_H |