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 | |
| 25 | /** |
| 26 | * Represents an OpenGL texture. |
| 27 | */ |
| 28 | struct Texture { |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 29 | Texture() { |
| 30 | cleanup = false; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 31 | bitmapSize = 0; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 32 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 33 | wrapS = GL_CLAMP_TO_EDGE; |
| 34 | wrapT = GL_CLAMP_TO_EDGE; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 35 | |
| 36 | minFilter = GL_NEAREST; |
| 37 | magFilter = GL_NEAREST; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 38 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 39 | mipMap = false; |
| 40 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 41 | firstFilter = true; |
| 42 | firstWrap = true; |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 43 | |
| 44 | id = 0; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 47 | 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 Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 53 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 54 | |
| 55 | if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) { |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 56 | firstWrap = false; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 57 | |
| 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 Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 70 | 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 Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 76 | 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 Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 88 | if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR; |
| 89 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 90 | glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min); |
| 91 | glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag); |
| 92 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Name of the texture. |
| 97 | */ |
| 98 | GLuint id; |
| 99 | /** |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 100 | * Generation of the backing bitmap, |
| 101 | */ |
| 102 | uint32_t generation; |
| 103 | /** |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 104 | * Indicates whether the texture requires blending. |
| 105 | */ |
| 106 | bool blend; |
| 107 | /** |
| 108 | * Width of the backing bitmap. |
| 109 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 110 | uint32_t width; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Height of the backing bitmap. |
| 113 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 114 | uint32_t height; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Indicates whether this texture should be cleaned up after use. |
| 117 | */ |
| 118 | bool cleanup; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Optional, size of the original bitmap. |
| 121 | */ |
| 122 | uint32_t bitmapSize; |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Indicates whether this texture will use trilinear filtering. |
| 125 | */ |
| 126 | bool mipMap; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 127 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 128 | private: |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE. |
| 131 | */ |
| 132 | GLenum wrapS; |
| 133 | GLenum wrapT; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Last filters set on this texture. Defaults to GL_NEAREST. |
| 137 | */ |
| 138 | GLenum minFilter; |
| 139 | GLenum magFilter; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 140 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 141 | bool firstFilter; |
| 142 | bool firstWrap; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 143 | }; // struct Texture |
| 144 | |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 145 | class AutoTexture { |
| 146 | public: |
| 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 | |
| 155 | private: |
| 156 | const Texture* mTexture; |
| 157 | }; // class AutoTexture |
| 158 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 159 | }; // namespace uirenderer |
| 160 | }; // namespace android |
| 161 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 162 | #endif // ANDROID_HWUI_TEXTURE_H |