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 | |
| 39 | firstFilter = true; |
| 40 | firstWrap = true; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 43 | void setWrap(GLenum wrapS, GLenum wrapT, bool bindTexture = false, bool force = false, |
| 44 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 45 | |
| 46 | if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) { |
| 47 | firstWrap = true; |
| 48 | |
| 49 | this->wrapS = wrapS; |
| 50 | this->wrapT = wrapT; |
| 51 | |
| 52 | if (bindTexture) { |
| 53 | glBindTexture(renderTarget, id); |
| 54 | } |
| 55 | |
| 56 | glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS); |
| 57 | glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT); |
| 58 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 61 | void setFilter(GLenum min, GLenum mag, bool bindTexture = false, bool force = false, |
| 62 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 63 | |
| 64 | if (firstFilter || force || min != minFilter || mag != magFilter) { |
| 65 | firstFilter = false; |
| 66 | |
| 67 | minFilter = min; |
| 68 | magFilter = mag; |
| 69 | |
| 70 | if (bindTexture) { |
| 71 | glBindTexture(renderTarget, id); |
| 72 | } |
| 73 | |
| 74 | glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min); |
| 75 | glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag); |
| 76 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Name of the texture. |
| 81 | */ |
| 82 | GLuint id; |
| 83 | /** |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 84 | * Generation of the backing bitmap, |
| 85 | */ |
| 86 | uint32_t generation; |
| 87 | /** |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 88 | * Indicates whether the texture requires blending. |
| 89 | */ |
| 90 | bool blend; |
| 91 | /** |
| 92 | * Width of the backing bitmap. |
| 93 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 94 | uint32_t width; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Height of the backing bitmap. |
| 97 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 98 | uint32_t height; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Indicates whether this texture should be cleaned up after use. |
| 101 | */ |
| 102 | bool cleanup; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Optional, size of the original bitmap. |
| 105 | */ |
| 106 | uint32_t bitmapSize; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Last wrap modes set on this texture. Defaults to GL_CLAMP_TO_EDGE. |
| 110 | */ |
| 111 | GLenum wrapS; |
| 112 | GLenum wrapT; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Last filters set on this texture. Defaults to GL_NEAREST. |
| 116 | */ |
| 117 | GLenum minFilter; |
| 118 | GLenum magFilter; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 119 | |
| 120 | private: |
| 121 | bool firstFilter; |
| 122 | bool firstWrap; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 123 | }; // struct Texture |
| 124 | |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 125 | class AutoTexture { |
| 126 | public: |
| 127 | AutoTexture(const Texture* texture): mTexture(texture) { } |
| 128 | ~AutoTexture() { |
| 129 | if (mTexture && mTexture->cleanup) { |
| 130 | glDeleteTextures(1, &mTexture->id); |
| 131 | delete mTexture; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | const Texture* mTexture; |
| 137 | }; // class AutoTexture |
| 138 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 139 | }; // namespace uirenderer |
| 140 | }; // namespace android |
| 141 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 142 | #endif // ANDROID_HWUI_TEXTURE_H |