blob: b72742f45654bccf77768570626839e03e9b7d86 [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"
21
Romain Guyce0537b2010-06-29 21:05:21 -070022#include <GLES2/gl2.h>
John Reck38e0c322015-11-10 12:19:17 -080023#include <SkBitmap.h>
Romain Guyce0537b2010-06-29 21:05:21 -070024
25namespace android {
26namespace uirenderer {
27
Romain Guy8aa195d2013-06-04 18:00:09 -070028class Caches;
Romain Guy3b748a42013-04-17 18:54:38 -070029class UvMapper;
John Reck38e0c322015-11-10 12:19:17 -080030class Layer;
Romain Guy3b748a42013-04-17 18:54:38 -070031
Romain Guyce0537b2010-06-29 21:05:21 -070032/**
33 * Represents an OpenGL texture.
34 */
John Reck38e0c322015-11-10 12:19:17 -080035class Texture : public GpuMemoryTracker {
Romain Guy8aa195d2013-06-04 18:00:09 -070036public:
Chih-Hung Hsieh05160d72016-07-21 18:13:31 -070037 explicit Texture(Caches& caches)
John Reck38e0c322015-11-10 12:19:17 -080038 : GpuMemoryTracker(GpuObjectType::Texture)
39 , mCaches(caches)
40 { }
Romain Guy9ace8f52011-07-07 20:50:11 -070041
Romain Guya404e162013-05-24 16:19:19 -070042 virtual ~Texture() { }
43
Romain Guy8aa195d2013-06-04 18:00:09 -070044 inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false,
Romain Guyd21b6e12011-11-30 20:21:23 -080045 GLenum renderTarget = GL_TEXTURE_2D) {
46 setWrapST(wrap, wrap, bindTexture, force, renderTarget);
47 }
48
Romain Guya404e162013-05-24 16:19:19 -070049 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
Romain Guy8aa195d2013-06-04 18:00:09 -070050 bool force = false, GLenum renderTarget = GL_TEXTURE_2D);
Romain Guye3c26852011-07-25 16:36:01 -070051
Romain Guy8aa195d2013-06-04 18:00:09 -070052 inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false,
Romain Guyd21b6e12011-11-30 20:21:23 -080053 GLenum renderTarget = GL_TEXTURE_2D) {
54 setFilterMinMag(filter, filter, bindTexture, force, renderTarget);
55 }
56
Romain Guya404e162013-05-24 16:19:19 -070057 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
Romain Guy8aa195d2013-06-04 18:00:09 -070058 bool force = false, GLenum renderTarget = GL_TEXTURE_2D);
Romain Guy22158e12010-08-06 11:18:34 -070059
Romain Guyce0537b2010-06-29 21:05:21 -070060 /**
Romain Guybe1b1272013-06-06 14:02:54 -070061 * Convenience method to call glDeleteTextures() on this texture's id.
62 */
John Reck38e0c322015-11-10 12:19:17 -080063 void deleteTexture();
Romain Guybe1b1272013-06-06 14:02:54 -070064
65 /**
John Reck38e0c322015-11-10 12:19:17 -080066 * 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 Guyce0537b2010-06-29 21:05:21 -070071 */
John Reck38e0c322015-11-10 12:19:17 -080072 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 Guyce0537b2010-06-29 21:05:21 -0700112 /**
Romain Guyfe880942010-06-30 16:05:32 -0700113 * Generation of the backing bitmap,
114 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800115 uint32_t generation = 0;
Romain Guyfe880942010-06-30 16:05:32 -0700116 /**
Romain Guyce0537b2010-06-29 21:05:21 -0700117 * Indicates whether the texture requires blending.
118 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800119 bool blend = false;
Romain Guyce0537b2010-06-29 21:05:21 -0700120 /**
Romain Guy22158e12010-08-06 11:18:34 -0700121 * Indicates whether this texture should be cleaned up after use.
122 */
Chris Craike2bb3802015-03-13 15:07:52 -0700123 bool cleanup = false;
Romain Guy9aaa8262010-09-08 15:15:43 -0700124 /**
125 * Optional, size of the original bitmap.
126 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800127 uint32_t bitmapSize = 0;
Romain Guy713e1bb2012-10-16 18:44:09 -0700128 /**
129 * Indicates whether this texture will use trilinear filtering.
130 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800131 bool mipMap = false;
Romain Guy8164c2d2010-10-25 18:03:28 -0700132
Romain Guy3b748a42013-04-17 18:54:38 -0700133 /**
134 * Optional, pointer to a texture coordinates mapper.
135 */
Chris Craik8e93a7c2015-02-23 13:07:57 -0800136 const UvMapper* uvMapper = nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -0700137
John Reck860d1552014-04-11 19:15:05 -0700138 /**
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 Reck00e79c92015-07-21 10:23:59 -0700142 void* isInUse = nullptr;
John Reck860d1552014-04-11 19:15:05 -0700143
Romain Guy713e1bb2012-10-16 18:44:09 -0700144private:
John Reck38e0c322015-11-10 12:19:17 -0800145 // 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);
John Reck48247a22016-01-22 10:55:32 -0800152 void resetCachedParams();
John Reck38e0c322015-11-10 12:19:17 -0800153
154 GLuint mId = 0;
155 uint32_t mWidth = 0;
156 uint32_t mHeight = 0;
157 GLint mFormat = 0;
158
John Reck48247a22016-01-22 10:55:32 -0800159 /* See GLES spec section 3.8.14
160 * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is
161 * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR.
162 * s, t, and r wrap modes are all set to REPEAT."
Romain Guy8164c2d2010-10-25 18:03:28 -0700163 */
John Reck48247a22016-01-22 10:55:32 -0800164 GLenum mWrapS = GL_REPEAT;
165 GLenum mWrapT = GL_REPEAT;
166 GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
167 GLenum mMagFilter = GL_LINEAR;
Romain Guy8aa195d2013-06-04 18:00:09 -0700168
169 Caches& mCaches;
Romain Guyce0537b2010-06-29 21:05:21 -0700170}; // struct Texture
171
Romain Guy22158e12010-08-06 11:18:34 -0700172class AutoTexture {
173public:
Chih-Hung Hsieh05160d72016-07-21 18:13:31 -0700174 explicit AutoTexture(Texture* texture)
Chris Craik386aa032015-12-07 17:08:25 -0800175 : texture(texture) {}
Romain Guy22158e12010-08-06 11:18:34 -0700176 ~AutoTexture() {
Chris Craik386aa032015-12-07 17:08:25 -0800177 if (texture && texture->cleanup) {
178 texture->deleteTexture();
179 delete texture;
Romain Guy22158e12010-08-06 11:18:34 -0700180 }
181 }
182
John Reck38e0c322015-11-10 12:19:17 -0800183 Texture* const texture;
Romain Guy22158e12010-08-06 11:18:34 -0700184}; // class AutoTexture
185
Romain Guyce0537b2010-06-29 21:05:21 -0700186}; // namespace uirenderer
187}; // namespace android
188
Romain Guy5b3b3522010-10-27 18:57:51 -0700189#endif // ANDROID_HWUI_TEXTURE_H