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" |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 21 | #include "hwui/Bitmap.h" |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 22 | #include "utils/Color.h" |
| 23 | |
| 24 | #include <memory> |
| 25 | |
| 26 | #include <math/mat3.h> |
| 27 | |
| 28 | #include <ui/ColorSpace.h> |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 29 | |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 30 | #include <EGL/egl.h> |
| 31 | #include <EGL/eglext.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 32 | #include <GLES2/gl2.h> |
| 33 | #include <GLES3/gl3.h> |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 34 | #include <SkBitmap.h> |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 35 | |
| 36 | namespace android { |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 37 | |
| 38 | class GraphicBuffer; |
| 39 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 40 | namespace uirenderer { |
| 41 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 42 | class Caches; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 43 | class UvMapper; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 44 | class Layer; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 45 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Represents an OpenGL texture. |
| 48 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 49 | class Texture : public GpuMemoryTracker { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 50 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 51 | static SkBitmap uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending, |
| 52 | sk_sp<SkColorSpace> sRGB); |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 53 | static bool hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 54 | static void colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | bool needSRGB, GLint* outInternalFormat, |
| 56 | GLint* outFormat, GLint* outType); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 57 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 58 | explicit Texture(Caches& caches) : GpuMemoryTracker(GpuObjectType::Texture), mCaches(caches) {} |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 59 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | virtual ~Texture() {} |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 61 | |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 62 | inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { |
| 63 | setWrapST(wrap, wrap, bindTexture, force); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 66 | virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 67 | bool force = false); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 68 | |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 69 | inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { |
| 70 | setFilterMinMag(filter, filter, bindTexture, force); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 73 | virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 74 | bool force = false); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 75 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 76 | /** |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 77 | * Convenience method to call glDeleteTextures() on this texture's id. |
| 78 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 79 | void deleteTexture(); |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 82 | * Sets the width, height, and format of the texture along with allocating |
| 83 | * the texture ID. Does nothing if the width, height, and format are already |
| 84 | * the requested values. |
| 85 | * |
| 86 | * The image data is undefined after calling this. |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 87 | */ |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 88 | void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) { |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 89 | upload(internalFormat, width, height, format, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 90 | internalFormat == GL_RGBA16F ? GL_HALF_FLOAT : GL_UNSIGNED_BYTE, nullptr); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 94 | * Updates this Texture with the contents of the provided Bitmap, |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 95 | * also setting the appropriate width, height, and format. It is not necessary |
| 96 | * to call resize() prior to this. |
| 97 | * |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 98 | * Note this does not set the generation from the Bitmap. |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 99 | */ |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 100 | void upload(Bitmap& source); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Basically glTexImage2D/glTexSubImage2D. |
| 104 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | void upload(GLint internalFormat, uint32_t width, uint32_t height, GLenum format, GLenum type, |
| 106 | const void* pixels); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Wraps an existing texture. |
| 110 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | void wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, GLint format, |
| 112 | GLenum target); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 113 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | GLuint id() const { return mId; } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 115 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | uint32_t width() const { return mWidth; } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 117 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 118 | uint32_t height() const { return mHeight; } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 119 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 120 | GLint format() const { return mFormat; } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 121 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 122 | GLint internalFormat() const { return mInternalFormat; } |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 123 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | GLenum target() const { return mTarget; } |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 125 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 126 | /** |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 127 | * Returns nullptr if this texture does not require color space conversion |
| 128 | * to sRGB, or a valid pointer to a ColorSpaceConnector if a conversion |
| 129 | * is required. |
| 130 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 131 | constexpr const ColorSpaceConnector* getColorSpaceConnector() const { return mConnector.get(); } |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 132 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 133 | constexpr bool hasColorSpaceConversion() const { return mConnector.get() != nullptr; } |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 134 | |
| 135 | TransferFunctionType getTransferFunctionType() const; |
| 136 | |
| 137 | /** |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 138 | * Returns true if this texture uses a linear encoding format. |
| 139 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 140 | constexpr bool isLinear() const { return mIsLinear; } |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 143 | * Generation of the backing bitmap, |
| 144 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 145 | uint32_t generation = 0; |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 146 | /** |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 147 | * Indicates whether the texture requires blending. |
| 148 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 149 | bool blend = false; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 150 | /** |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 151 | * Indicates whether this texture should be cleaned up after use. |
| 152 | */ |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 153 | bool cleanup = false; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Optional, size of the original bitmap. |
| 156 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 157 | uint32_t bitmapSize = 0; |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Indicates whether this texture will use trilinear filtering. |
| 160 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 161 | bool mipMap = false; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 162 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Optional, pointer to a texture coordinates mapper. |
| 165 | */ |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 166 | const UvMapper* uvMapper = nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 167 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Whether or not the Texture is marked in use and thus not evictable for |
| 170 | * the current frame. This is reset at the start of a new frame. |
| 171 | */ |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 172 | void* isInUse = nullptr; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 173 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 174 | private: |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 175 | // TODO: Temporarily grant private access to GlLayer, remove once |
| 176 | // GlLayer can be de-tangled from being a dual-purpose render target |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 177 | // and external texture wrapper |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 178 | friend class GlLayer; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 179 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 180 | // Returns true if the texture layout (size, format, etc.) changed, false if it was the same |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 181 | bool updateLayout(uint32_t width, uint32_t height, GLint internalFormat, GLint format, |
| 182 | GLenum target); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 183 | void uploadHardwareBitmapToTexture(GraphicBuffer* buffer); |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 184 | void resetCachedParams(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 185 | |
| 186 | GLuint mId = 0; |
| 187 | uint32_t mWidth = 0; |
| 188 | uint32_t mHeight = 0; |
| 189 | GLint mFormat = 0; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 190 | GLint mInternalFormat = 0; |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 191 | GLenum mTarget = GL_NONE; |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 192 | EGLImageKHR mEglImageHandle = EGL_NO_IMAGE_KHR; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 193 | |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 194 | /* See GLES spec section 3.8.14 |
| 195 | * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is |
| 196 | * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR. |
| 197 | * s, t, and r wrap modes are all set to REPEAT." |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 198 | */ |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 199 | GLenum mWrapS = GL_REPEAT; |
| 200 | GLenum mWrapT = GL_REPEAT; |
| 201 | GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR; |
| 202 | GLenum mMagFilter = GL_LINEAR; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 203 | |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 204 | // Indicates whether the content of the texture is in linear space |
| 205 | bool mIsLinear = false; |
| 206 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 207 | Caches& mCaches; |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 208 | |
| 209 | std::unique_ptr<ColorSpaceConnector> mConnector; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 210 | }; // struct Texture |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 211 | |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 212 | class AutoTexture { |
| 213 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 214 | explicit AutoTexture(Texture* texture) : texture(texture) {} |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 215 | ~AutoTexture() { |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 216 | if (texture && texture->cleanup) { |
| 217 | texture->deleteTexture(); |
| 218 | delete texture; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 222 | Texture* const texture; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 223 | }; // class AutoTexture |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 224 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 225 | }; // namespace uirenderer |
| 226 | }; // namespace android |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 227 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 228 | #endif // ANDROID_HWUI_TEXTURE_H |