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 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 20 | #include "GpuMemoryTracker.h" |
| 21 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 22 | #include <GLES2/gl2.h> |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 23 | #include <SkBitmap.h> |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 28 | class Caches; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 29 | class UvMapper; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 30 | class Layer; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 31 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Represents an OpenGL texture. |
| 34 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 35 | class Texture : public GpuMemoryTracker { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 36 | public: |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 37 | Texture(Caches& caches) |
| 38 | : GpuMemoryTracker(GpuObjectType::Texture) |
| 39 | , mCaches(caches) |
| 40 | { } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 41 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 42 | virtual ~Texture() { } |
| 43 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 44 | inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false, |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 45 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 46 | setWrapST(wrap, wrap, bindTexture, force, renderTarget); |
| 47 | } |
| 48 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 49 | virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 50 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 51 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 52 | inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false, |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 53 | GLenum renderTarget = GL_TEXTURE_2D) { |
| 54 | setFilterMinMag(filter, filter, bindTexture, force, renderTarget); |
| 55 | } |
| 56 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 57 | virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 58 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 59 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 60 | /** |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 61 | * Convenience method to call glDeleteTextures() on this texture's id. |
| 62 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 63 | void deleteTexture(); |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 66 | * Sets the width, height, and format of the texture along with allocating |
| 67 | * the texture ID. Does nothing if the width, height, and format are already |
| 68 | * the requested values. |
| 69 | * |
| 70 | * The image data is undefined after calling this. |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 71 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 72 | void resize(uint32_t width, uint32_t height, GLint format) { |
| 73 | upload(format, width, height, format, GL_UNSIGNED_BYTE, nullptr); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Updates this Texture with the contents of the provided SkBitmap, |
| 78 | * also setting the appropriate width, height, and format. It is not necessary |
| 79 | * to call resize() prior to this. |
| 80 | * |
| 81 | * Note this does not set the generation from the SkBitmap. |
| 82 | */ |
| 83 | void upload(const SkBitmap& source); |
| 84 | |
| 85 | /** |
| 86 | * Basically glTexImage2D/glTexSubImage2D. |
| 87 | */ |
| 88 | void upload(GLint internalformat, uint32_t width, uint32_t height, |
| 89 | GLenum format, GLenum type, const void* pixels); |
| 90 | |
| 91 | /** |
| 92 | * Wraps an existing texture. |
| 93 | */ |
| 94 | void wrap(GLuint id, uint32_t width, uint32_t height, GLint format); |
| 95 | |
| 96 | GLuint id() const { |
| 97 | return mId; |
| 98 | } |
| 99 | |
| 100 | uint32_t width() const { |
| 101 | return mWidth; |
| 102 | } |
| 103 | |
| 104 | uint32_t height() const { |
| 105 | return mHeight; |
| 106 | } |
| 107 | |
| 108 | GLint format() const { |
| 109 | return mFormat; |
| 110 | } |
| 111 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 112 | /** |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 113 | * Generation of the backing bitmap, |
| 114 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 115 | uint32_t generation = 0; |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 116 | /** |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 117 | * Indicates whether the texture requires blending. |
| 118 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 119 | bool blend = false; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 120 | /** |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 121 | * Indicates whether this texture should be cleaned up after use. |
| 122 | */ |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 123 | bool cleanup = false; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Optional, size of the original bitmap. |
| 126 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 127 | uint32_t bitmapSize = 0; |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Indicates whether this texture will use trilinear filtering. |
| 130 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 131 | bool mipMap = false; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 132 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Optional, pointer to a texture coordinates mapper. |
| 135 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 136 | const UvMapper* uvMapper = nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 137 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Whether or not the Texture is marked in use and thus not evictable for |
| 140 | * the current frame. This is reset at the start of a new frame. |
| 141 | */ |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 142 | void* isInUse = nullptr; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 143 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 144 | private: |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 145 | // TODO: Temporarily grant private access to Layer, remove once |
| 146 | // Layer can be de-tangled from being a dual-purpose render target |
| 147 | // and external texture wrapper |
| 148 | friend class Layer; |
| 149 | |
| 150 | // Returns true if the size changed, false if it was the same |
| 151 | bool updateSize(uint32_t width, uint32_t height, GLint format); |
| 152 | |
| 153 | GLuint mId = 0; |
| 154 | uint32_t mWidth = 0; |
| 155 | uint32_t mHeight = 0; |
| 156 | GLint mFormat = 0; |
| 157 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 158 | /** |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 159 | * Last wrap modes set on this texture. |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 160 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 161 | GLenum mWrapS = GL_CLAMP_TO_EDGE; |
| 162 | GLenum mWrapT = GL_CLAMP_TO_EDGE; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 163 | |
| 164 | /** |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 165 | * Last filters set on this texture. |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 166 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 167 | GLenum mMinFilter = GL_NEAREST; |
| 168 | GLenum mMagFilter = GL_NEAREST; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 169 | |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 170 | bool mFirstFilter = true; |
| 171 | bool mFirstWrap = true; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 172 | |
| 173 | Caches& mCaches; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 174 | }; // struct Texture |
| 175 | |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 176 | class AutoTexture { |
| 177 | public: |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 178 | AutoTexture(Texture* texture) |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 179 | : texture(texture) {} |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 180 | ~AutoTexture() { |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 181 | if (texture && texture->cleanup) { |
| 182 | texture->deleteTexture(); |
| 183 | delete texture; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 187 | Texture* const texture; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 188 | }; // class AutoTexture |
| 189 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 190 | }; // namespace uirenderer |
| 191 | }; // namespace android |
| 192 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 193 | #endif // ANDROID_HWUI_TEXTURE_H |