blob: 5b7e4e261f3064c50d0ac7abd4f1fb66bc46dd99 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_TEXTURE_H
18#define ANDROID_HWUI_TEXTURE_H
Romain Guyce0537b2010-06-29 21:05:21 -070019
John Reck38e0c322015-11-10 12:19:17 -080020#include "GpuMemoryTracker.h"
sergeyv98fa4f92016-10-24 15:35:21 -070021#include "hwui/Bitmap.h"
Romain Guycaaaa662017-03-27 00:40:21 -070022#include "utils/Color.h"
23
24#include <memory>
25
26#include <math/mat3.h>
27
28#include <ui/ColorSpace.h>
John Reck38e0c322015-11-10 12:19:17 -080029
sergeyv694d4992016-10-27 10:23:13 -070030#include <EGL/egl.h>
31#include <EGL/eglext.h>
John Reck1bcacfd2017-11-03 10:12:19 -070032#include <GLES2/gl2.h>
33#include <GLES3/gl3.h>
sergeyv694d4992016-10-27 10:23:13 -070034#include <SkBitmap.h>
Romain Guyce0537b2010-06-29 21:05:21 -070035
36namespace android {
sergeyv694d4992016-10-27 10:23:13 -070037
38class GraphicBuffer;
39
Romain Guyce0537b2010-06-29 21:05:21 -070040namespace uirenderer {
41
Romain Guy8aa195d2013-06-04 18:00:09 -070042class Caches;
Romain Guy3b748a42013-04-17 18:54:38 -070043class UvMapper;
John Reck38e0c322015-11-10 12:19:17 -080044class Layer;
Romain Guy3b748a42013-04-17 18:54:38 -070045
Romain Guyce0537b2010-06-29 21:05:21 -070046/**
47 * Represents an OpenGL texture.
48 */
John Reck38e0c322015-11-10 12:19:17 -080049class Texture : public GpuMemoryTracker {
Romain Guy8aa195d2013-06-04 18:00:09 -070050public:
John Reck1bcacfd2017-11-03 10:12:19 -070051 static SkBitmap uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending,
52 sk_sp<SkColorSpace> sRGB);
Romain Guycaaaa662017-03-27 00:40:21 -070053 static bool hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending);
sergeyv694d4992016-10-27 10:23:13 -070054 static void colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType,
John Reck1bcacfd2017-11-03 10:12:19 -070055 bool needSRGB, GLint* outInternalFormat,
56 GLint* outFormat, GLint* outType);
sergeyv694d4992016-10-27 10:23:13 -070057
John Reck1bcacfd2017-11-03 10:12:19 -070058 explicit Texture(Caches& caches) : GpuMemoryTracker(GpuObjectType::Texture), mCaches(caches) {}
Romain Guy9ace8f52011-07-07 20:50:11 -070059
John Reck1bcacfd2017-11-03 10:12:19 -070060 virtual ~Texture() {}
Romain Guya404e162013-05-24 16:19:19 -070061
sergeyv2a38c422016-10-25 15:21:50 -070062 inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
63 setWrapST(wrap, wrap, bindTexture, force);
Romain Guyd21b6e12011-11-30 20:21:23 -080064 }
65
Romain Guya404e162013-05-24 16:19:19 -070066 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
John Reck1bcacfd2017-11-03 10:12:19 -070067 bool force = false);
Romain Guye3c26852011-07-25 16:36:01 -070068
sergeyv2a38c422016-10-25 15:21:50 -070069 inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
70 setFilterMinMag(filter, filter, bindTexture, force);
Romain Guyd21b6e12011-11-30 20:21:23 -080071 }
72
Romain Guya404e162013-05-24 16:19:19 -070073 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
John Reck1bcacfd2017-11-03 10:12:19 -070074 bool force = false);
Romain Guy22158e12010-08-06 11:18:34 -070075
Romain Guyce0537b2010-06-29 21:05:21 -070076 /**
Romain Guybe1b1272013-06-06 14:02:54 -070077 * Convenience method to call glDeleteTextures() on this texture's id.
78 */
John Reck38e0c322015-11-10 12:19:17 -080079 void deleteTexture();
Romain Guybe1b1272013-06-06 14:02:54 -070080
81 /**
John Reck38e0c322015-11-10 12:19:17 -080082 * 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 Guyce0537b2010-06-29 21:05:21 -070087 */
Romain Guy253f2c22016-09-28 17:34:42 -070088 void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) {
Romain Guy07ae5052017-06-13 18:25:32 -070089 upload(internalFormat, width, height, format,
John Reck1bcacfd2017-11-03 10:12:19 -070090 internalFormat == GL_RGBA16F ? GL_HALF_FLOAT : GL_UNSIGNED_BYTE, nullptr);
John Reck38e0c322015-11-10 12:19:17 -080091 }
92
93 /**
sergeyv98fa4f92016-10-24 15:35:21 -070094 * Updates this Texture with the contents of the provided Bitmap,
John Reck38e0c322015-11-10 12:19:17 -080095 * also setting the appropriate width, height, and format. It is not necessary
96 * to call resize() prior to this.
97 *
sergeyv98fa4f92016-10-24 15:35:21 -070098 * Note this does not set the generation from the Bitmap.
John Reck38e0c322015-11-10 12:19:17 -080099 */
sergeyv98fa4f92016-10-24 15:35:21 -0700100 void upload(Bitmap& source);
John Reck38e0c322015-11-10 12:19:17 -0800101
102 /**
103 * Basically glTexImage2D/glTexSubImage2D.
104 */
John Reck1bcacfd2017-11-03 10:12:19 -0700105 void upload(GLint internalFormat, uint32_t width, uint32_t height, GLenum format, GLenum type,
106 const void* pixels);
John Reck38e0c322015-11-10 12:19:17 -0800107
108 /**
109 * Wraps an existing texture.
110 */
John Reck1bcacfd2017-11-03 10:12:19 -0700111 void wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, GLint format,
112 GLenum target);
John Reck38e0c322015-11-10 12:19:17 -0800113
John Reck1bcacfd2017-11-03 10:12:19 -0700114 GLuint id() const { return mId; }
John Reck38e0c322015-11-10 12:19:17 -0800115
John Reck1bcacfd2017-11-03 10:12:19 -0700116 uint32_t width() const { return mWidth; }
John Reck38e0c322015-11-10 12:19:17 -0800117
John Reck1bcacfd2017-11-03 10:12:19 -0700118 uint32_t height() const { return mHeight; }
John Reck38e0c322015-11-10 12:19:17 -0800119
John Reck1bcacfd2017-11-03 10:12:19 -0700120 GLint format() const { return mFormat; }
John Reck38e0c322015-11-10 12:19:17 -0800121
John Reck1bcacfd2017-11-03 10:12:19 -0700122 GLint internalFormat() const { return mInternalFormat; }
Romain Guy253f2c22016-09-28 17:34:42 -0700123
John Reck1bcacfd2017-11-03 10:12:19 -0700124 GLenum target() const { return mTarget; }
sergeyv2a38c422016-10-25 15:21:50 -0700125
Romain Guyce0537b2010-06-29 21:05:21 -0700126 /**
Romain Guycaaaa662017-03-27 00:40:21 -0700127 * 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 Reck1bcacfd2017-11-03 10:12:19 -0700131 constexpr const ColorSpaceConnector* getColorSpaceConnector() const { return mConnector.get(); }
Romain Guycaaaa662017-03-27 00:40:21 -0700132
John Reck1bcacfd2017-11-03 10:12:19 -0700133 constexpr bool hasColorSpaceConversion() const { return mConnector.get() != nullptr; }
Romain Guycaaaa662017-03-27 00:40:21 -0700134
135 TransferFunctionType getTransferFunctionType() const;
136
137 /**
Romain Guy636afc12017-02-07 11:21:05 -0800138 * Returns true if this texture uses a linear encoding format.
139 */
John Reck1bcacfd2017-11-03 10:12:19 -0700140 constexpr bool isLinear() const { return mIsLinear; }
Romain Guy636afc12017-02-07 11:21:05 -0800141
142 /**
Romain Guyfe880942010-06-30 16:05:32 -0700143 * Generation of the backing bitmap,
144 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800145 uint32_t generation = 0;
Romain Guyfe880942010-06-30 16:05:32 -0700146 /**
Romain Guyce0537b2010-06-29 21:05:21 -0700147 * Indicates whether the texture requires blending.
148 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800149 bool blend = false;
Romain Guyce0537b2010-06-29 21:05:21 -0700150 /**
Romain Guy22158e12010-08-06 11:18:34 -0700151 * Indicates whether this texture should be cleaned up after use.
152 */
Chris Craike2bb3802015-03-13 15:07:52 -0700153 bool cleanup = false;
Romain Guy9aaa8262010-09-08 15:15:43 -0700154 /**
155 * Optional, size of the original bitmap.
156 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800157 uint32_t bitmapSize = 0;
Romain Guy713e1bb2012-10-16 18:44:09 -0700158 /**
159 * Indicates whether this texture will use trilinear filtering.
160 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800161 bool mipMap = false;
Romain Guy8164c2d2010-10-25 18:03:28 -0700162
Romain Guy3b748a42013-04-17 18:54:38 -0700163 /**
164 * Optional, pointer to a texture coordinates mapper.
165 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800166 const UvMapper* uvMapper = nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -0700167
John Reck860d1552014-04-11 19:15:05 -0700168 /**
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 Reck00e79c92015-07-21 10:23:59 -0700172 void* isInUse = nullptr;
John Reck1bcacfd2017-11-03 10:12:19 -0700173
Romain Guy713e1bb2012-10-16 18:44:09 -0700174private:
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500175 // TODO: Temporarily grant private access to GlLayer, remove once
176 // GlLayer can be de-tangled from being a dual-purpose render target
John Reck38e0c322015-11-10 12:19:17 -0800177 // and external texture wrapper
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500178 friend class GlLayer;
John Reck38e0c322015-11-10 12:19:17 -0800179
Romain Guycaaaa662017-03-27 00:40:21 -0700180 // Returns true if the texture layout (size, format, etc.) changed, false if it was the same
John Reck1bcacfd2017-11-03 10:12:19 -0700181 bool updateLayout(uint32_t width, uint32_t height, GLint internalFormat, GLint format,
182 GLenum target);
sergeyv694d4992016-10-27 10:23:13 -0700183 void uploadHardwareBitmapToTexture(GraphicBuffer* buffer);
John Reck48247a22016-01-22 10:55:32 -0800184 void resetCachedParams();
John Reck38e0c322015-11-10 12:19:17 -0800185
186 GLuint mId = 0;
187 uint32_t mWidth = 0;
188 uint32_t mHeight = 0;
189 GLint mFormat = 0;
Romain Guy253f2c22016-09-28 17:34:42 -0700190 GLint mInternalFormat = 0;
sergeyv2a38c422016-10-25 15:21:50 -0700191 GLenum mTarget = GL_NONE;
sergeyv694d4992016-10-27 10:23:13 -0700192 EGLImageKHR mEglImageHandle = EGL_NO_IMAGE_KHR;
John Reck38e0c322015-11-10 12:19:17 -0800193
John Reck48247a22016-01-22 10:55:32 -0800194 /* 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 Guy8164c2d2010-10-25 18:03:28 -0700198 */
John Reck48247a22016-01-22 10:55:32 -0800199 GLenum mWrapS = GL_REPEAT;
200 GLenum mWrapT = GL_REPEAT;
201 GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
202 GLenum mMagFilter = GL_LINEAR;
Romain Guy8aa195d2013-06-04 18:00:09 -0700203
Romain Guy07ae5052017-06-13 18:25:32 -0700204 // Indicates whether the content of the texture is in linear space
205 bool mIsLinear = false;
206
Romain Guy8aa195d2013-06-04 18:00:09 -0700207 Caches& mCaches;
Romain Guycaaaa662017-03-27 00:40:21 -0700208
209 std::unique_ptr<ColorSpaceConnector> mConnector;
John Reck1bcacfd2017-11-03 10:12:19 -0700210}; // struct Texture
Romain Guyce0537b2010-06-29 21:05:21 -0700211
Romain Guy22158e12010-08-06 11:18:34 -0700212class AutoTexture {
213public:
John Reck1bcacfd2017-11-03 10:12:19 -0700214 explicit AutoTexture(Texture* texture) : texture(texture) {}
Romain Guy22158e12010-08-06 11:18:34 -0700215 ~AutoTexture() {
Chris Craik386aa032015-12-07 17:08:25 -0800216 if (texture && texture->cleanup) {
217 texture->deleteTexture();
218 delete texture;
Romain Guy22158e12010-08-06 11:18:34 -0700219 }
220 }
221
John Reck38e0c322015-11-10 12:19:17 -0800222 Texture* const texture;
John Reck1bcacfd2017-11-03 10:12:19 -0700223}; // class AutoTexture
Romain Guy22158e12010-08-06 11:18:34 -0700224
John Reck1bcacfd2017-11-03 10:12:19 -0700225}; // namespace uirenderer
226}; // namespace android
Romain Guyce0537b2010-06-29 21:05:21 -0700227
John Reck1bcacfd2017-11-03 10:12:19 -0700228#endif // ANDROID_HWUI_TEXTURE_H