blob: ce9d4dc234a892b5c0c74919d7abfccc78885472 [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"
John Reck38e0c322015-11-10 12:19:17 -080022
Romain Guyce0537b2010-06-29 21:05:21 -070023#include <GLES2/gl2.h>
sergeyv694d4992016-10-27 10:23:13 -070024#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <SkBitmap.h>
Romain Guyce0537b2010-06-29 21:05:21 -070027
28namespace android {
sergeyv694d4992016-10-27 10:23:13 -070029
30class GraphicBuffer;
31
Romain Guyce0537b2010-06-29 21:05:21 -070032namespace uirenderer {
33
Romain Guy8aa195d2013-06-04 18:00:09 -070034class Caches;
Romain Guy3b748a42013-04-17 18:54:38 -070035class UvMapper;
John Reck38e0c322015-11-10 12:19:17 -080036class Layer;
Romain Guy3b748a42013-04-17 18:54:38 -070037
Romain Guyce0537b2010-06-29 21:05:21 -070038/**
39 * Represents an OpenGL texture.
40 */
John Reck38e0c322015-11-10 12:19:17 -080041class Texture : public GpuMemoryTracker {
Romain Guy8aa195d2013-06-04 18:00:09 -070042public:
sergeyv694d4992016-10-27 10:23:13 -070043 static SkBitmap uploadToN32(const SkBitmap& bitmap, bool hasSRGB, sk_sp<SkColorSpace> sRGB);
44 static bool hasUnsupportedColorType(const SkImageInfo& info, bool hasSRGB, SkColorSpace* sRGB);
45 static void colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType,
46 bool needSRGB, GLint* outInternalFormat, GLint* outFormat, GLint* outType);
47
Chih-Hung Hsieh05160d72016-07-21 18:13:31 -070048 explicit Texture(Caches& caches)
John Reck38e0c322015-11-10 12:19:17 -080049 : GpuMemoryTracker(GpuObjectType::Texture)
50 , mCaches(caches)
51 { }
Romain Guy9ace8f52011-07-07 20:50:11 -070052
Romain Guya404e162013-05-24 16:19:19 -070053 virtual ~Texture() { }
54
sergeyv2a38c422016-10-25 15:21:50 -070055 inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
56 setWrapST(wrap, wrap, bindTexture, force);
Romain Guyd21b6e12011-11-30 20:21:23 -080057 }
58
Romain Guya404e162013-05-24 16:19:19 -070059 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
sergeyv2a38c422016-10-25 15:21:50 -070060 bool force = false);
Romain Guye3c26852011-07-25 16:36:01 -070061
sergeyv2a38c422016-10-25 15:21:50 -070062 inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
63 setFilterMinMag(filter, filter, bindTexture, force);
Romain Guyd21b6e12011-11-30 20:21:23 -080064 }
65
Romain Guya404e162013-05-24 16:19:19 -070066 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
sergeyv2a38c422016-10-25 15:21:50 -070067 bool force = false);
Romain Guy22158e12010-08-06 11:18:34 -070068
Romain Guyce0537b2010-06-29 21:05:21 -070069 /**
Romain Guybe1b1272013-06-06 14:02:54 -070070 * Convenience method to call glDeleteTextures() on this texture's id.
71 */
John Reck38e0c322015-11-10 12:19:17 -080072 void deleteTexture();
Romain Guybe1b1272013-06-06 14:02:54 -070073
74 /**
John Reck38e0c322015-11-10 12:19:17 -080075 * Sets the width, height, and format of the texture along with allocating
76 * the texture ID. Does nothing if the width, height, and format are already
77 * the requested values.
78 *
79 * The image data is undefined after calling this.
Romain Guyce0537b2010-06-29 21:05:21 -070080 */
Romain Guy253f2c22016-09-28 17:34:42 -070081 void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) {
82 upload(internalFormat, width, height, format, GL_UNSIGNED_BYTE, nullptr);
John Reck38e0c322015-11-10 12:19:17 -080083 }
84
85 /**
sergeyv98fa4f92016-10-24 15:35:21 -070086 * Updates this Texture with the contents of the provided Bitmap,
John Reck38e0c322015-11-10 12:19:17 -080087 * also setting the appropriate width, height, and format. It is not necessary
88 * to call resize() prior to this.
89 *
sergeyv98fa4f92016-10-24 15:35:21 -070090 * Note this does not set the generation from the Bitmap.
John Reck38e0c322015-11-10 12:19:17 -080091 */
sergeyv98fa4f92016-10-24 15:35:21 -070092 void upload(Bitmap& source);
John Reck38e0c322015-11-10 12:19:17 -080093
94 /**
95 * Basically glTexImage2D/glTexSubImage2D.
96 */
Romain Guy253f2c22016-09-28 17:34:42 -070097 void upload(GLint internalFormat, uint32_t width, uint32_t height,
John Reck38e0c322015-11-10 12:19:17 -080098 GLenum format, GLenum type, const void* pixels);
99
100 /**
101 * Wraps an existing texture.
102 */
sergeyv2a38c422016-10-25 15:21:50 -0700103 void wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat,
104 GLint format, GLenum target);
John Reck38e0c322015-11-10 12:19:17 -0800105
106 GLuint id() const {
107 return mId;
108 }
109
110 uint32_t width() const {
111 return mWidth;
112 }
113
114 uint32_t height() const {
115 return mHeight;
116 }
117
118 GLint format() const {
119 return mFormat;
120 }
121
Romain Guy253f2c22016-09-28 17:34:42 -0700122 GLint internalFormat() const {
123 return mInternalFormat;
124 }
125
sergeyv2a38c422016-10-25 15:21:50 -0700126 GLenum target() const {
127 return mTarget;
128 }
129
Romain Guyce0537b2010-06-29 21:05:21 -0700130 /**
Romain Guy636afc12017-02-07 11:21:05 -0800131 * Returns true if this texture uses a linear encoding format.
132 */
133 bool isLinear() const;
134
135 /**
Romain Guyfe880942010-06-30 16:05:32 -0700136 * Generation of the backing bitmap,
137 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800138 uint32_t generation = 0;
Romain Guyfe880942010-06-30 16:05:32 -0700139 /**
Romain Guyce0537b2010-06-29 21:05:21 -0700140 * Indicates whether the texture requires blending.
141 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800142 bool blend = false;
Romain Guyce0537b2010-06-29 21:05:21 -0700143 /**
Romain Guy22158e12010-08-06 11:18:34 -0700144 * Indicates whether this texture should be cleaned up after use.
145 */
Chris Craike2bb3802015-03-13 15:07:52 -0700146 bool cleanup = false;
Romain Guy9aaa8262010-09-08 15:15:43 -0700147 /**
148 * Optional, size of the original bitmap.
149 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800150 uint32_t bitmapSize = 0;
Romain Guy713e1bb2012-10-16 18:44:09 -0700151 /**
152 * Indicates whether this texture will use trilinear filtering.
153 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800154 bool mipMap = false;
Romain Guy8164c2d2010-10-25 18:03:28 -0700155
Romain Guy3b748a42013-04-17 18:54:38 -0700156 /**
157 * Optional, pointer to a texture coordinates mapper.
158 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800159 const UvMapper* uvMapper = nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -0700160
John Reck860d1552014-04-11 19:15:05 -0700161 /**
162 * Whether or not the Texture is marked in use and thus not evictable for
163 * the current frame. This is reset at the start of a new frame.
164 */
John Reck00e79c92015-07-21 10:23:59 -0700165 void* isInUse = nullptr;
Romain Guy713e1bb2012-10-16 18:44:09 -0700166private:
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500167 // TODO: Temporarily grant private access to GlLayer, remove once
168 // GlLayer can be de-tangled from being a dual-purpose render target
John Reck38e0c322015-11-10 12:19:17 -0800169 // and external texture wrapper
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500170 friend class GlLayer;
John Reck38e0c322015-11-10 12:19:17 -0800171
172 // Returns true if the size changed, false if it was the same
sergeyv2a38c422016-10-25 15:21:50 -0700173 bool updateSize(uint32_t width, uint32_t height, GLint internalFormat,
174 GLint format, GLenum target);
sergeyv694d4992016-10-27 10:23:13 -0700175 void uploadHardwareBitmapToTexture(GraphicBuffer* buffer);
John Reck48247a22016-01-22 10:55:32 -0800176 void resetCachedParams();
John Reck38e0c322015-11-10 12:19:17 -0800177
178 GLuint mId = 0;
179 uint32_t mWidth = 0;
180 uint32_t mHeight = 0;
181 GLint mFormat = 0;
Romain Guy253f2c22016-09-28 17:34:42 -0700182 GLint mInternalFormat = 0;
sergeyv2a38c422016-10-25 15:21:50 -0700183 GLenum mTarget = GL_NONE;
sergeyv694d4992016-10-27 10:23:13 -0700184 EGLImageKHR mEglImageHandle = EGL_NO_IMAGE_KHR;
John Reck38e0c322015-11-10 12:19:17 -0800185
John Reck48247a22016-01-22 10:55:32 -0800186 /* See GLES spec section 3.8.14
187 * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is
188 * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR.
189 * s, t, and r wrap modes are all set to REPEAT."
Romain Guy8164c2d2010-10-25 18:03:28 -0700190 */
John Reck48247a22016-01-22 10:55:32 -0800191 GLenum mWrapS = GL_REPEAT;
192 GLenum mWrapT = GL_REPEAT;
193 GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
194 GLenum mMagFilter = GL_LINEAR;
Romain Guy8aa195d2013-06-04 18:00:09 -0700195
196 Caches& mCaches;
Romain Guyce0537b2010-06-29 21:05:21 -0700197}; // struct Texture
198
Romain Guy22158e12010-08-06 11:18:34 -0700199class AutoTexture {
200public:
Chih-Hung Hsieh05160d72016-07-21 18:13:31 -0700201 explicit AutoTexture(Texture* texture)
Chris Craik386aa032015-12-07 17:08:25 -0800202 : texture(texture) {}
Romain Guy22158e12010-08-06 11:18:34 -0700203 ~AutoTexture() {
Chris Craik386aa032015-12-07 17:08:25 -0800204 if (texture && texture->cleanup) {
205 texture->deleteTexture();
206 delete texture;
Romain Guy22158e12010-08-06 11:18:34 -0700207 }
208 }
209
John Reck38e0c322015-11-10 12:19:17 -0800210 Texture* const texture;
Romain Guy22158e12010-08-06 11:18:34 -0700211}; // class AutoTexture
212
Romain Guyce0537b2010-06-29 21:05:21 -0700213}; // namespace uirenderer
214}; // namespace android
215
Romain Guy5b3b3522010-10-27 18:57:51 -0700216#endif // ANDROID_HWUI_TEXTURE_H