Texture.h revision 98fa4f9e7b33a3004ce9142c9acd4300391b9a0e
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 17#ifndef ANDROID_HWUI_TEXTURE_H 18#define ANDROID_HWUI_TEXTURE_H 19 20#include "GpuMemoryTracker.h" 21#include "hwui/Bitmap.h" 22 23#include <GLES2/gl2.h> 24 25namespace android { 26namespace uirenderer { 27 28class Caches; 29class UvMapper; 30class Layer; 31 32/** 33 * Represents an OpenGL texture. 34 */ 35class Texture : public GpuMemoryTracker { 36public: 37 explicit Texture(Caches& caches) 38 : GpuMemoryTracker(GpuObjectType::Texture) 39 , mCaches(caches) 40 { } 41 42 virtual ~Texture() { } 43 44 inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false, 45 GLenum renderTarget = GL_TEXTURE_2D) { 46 setWrapST(wrap, wrap, bindTexture, force, renderTarget); 47 } 48 49 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, 50 bool force = false, GLenum renderTarget = GL_TEXTURE_2D); 51 52 inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false, 53 GLenum renderTarget = GL_TEXTURE_2D) { 54 setFilterMinMag(filter, filter, bindTexture, force, renderTarget); 55 } 56 57 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, 58 bool force = false, GLenum renderTarget = GL_TEXTURE_2D); 59 60 /** 61 * Convenience method to call glDeleteTextures() on this texture's id. 62 */ 63 void deleteTexture(); 64 65 /** 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. 71 */ 72 void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) { 73 upload(internalFormat, width, height, format, GL_UNSIGNED_BYTE, nullptr); 74 } 75 76 /** 77 * Updates this Texture with the contents of the provided Bitmap, 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 Bitmap. 82 */ 83 void upload(Bitmap& 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 internalFormat, 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 112 GLint internalFormat() const { 113 return mInternalFormat; 114 } 115 116 /** 117 * Generation of the backing bitmap, 118 */ 119 uint32_t generation = 0; 120 /** 121 * Indicates whether the texture requires blending. 122 */ 123 bool blend = false; 124 /** 125 * Indicates whether this texture should be cleaned up after use. 126 */ 127 bool cleanup = false; 128 /** 129 * Optional, size of the original bitmap. 130 */ 131 uint32_t bitmapSize = 0; 132 /** 133 * Indicates whether this texture will use trilinear filtering. 134 */ 135 bool mipMap = false; 136 137 /** 138 * Optional, pointer to a texture coordinates mapper. 139 */ 140 const UvMapper* uvMapper = nullptr; 141 142 /** 143 * Whether or not the Texture is marked in use and thus not evictable for 144 * the current frame. This is reset at the start of a new frame. 145 */ 146 void* isInUse = nullptr; 147 148private: 149 // TODO: Temporarily grant private access to Layer, remove once 150 // Layer can be de-tangled from being a dual-purpose render target 151 // and external texture wrapper 152 friend class Layer; 153 154 // Returns true if the size changed, false if it was the same 155 bool updateSize(uint32_t width, uint32_t height, GLint internalFormat, GLint format); 156 void resetCachedParams(); 157 158 GLuint mId = 0; 159 uint32_t mWidth = 0; 160 uint32_t mHeight = 0; 161 GLint mFormat = 0; 162 GLint mInternalFormat = 0; 163 164 /* See GLES spec section 3.8.14 165 * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is 166 * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR. 167 * s, t, and r wrap modes are all set to REPEAT." 168 */ 169 GLenum mWrapS = GL_REPEAT; 170 GLenum mWrapT = GL_REPEAT; 171 GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR; 172 GLenum mMagFilter = GL_LINEAR; 173 174 Caches& mCaches; 175}; // struct Texture 176 177class AutoTexture { 178public: 179 explicit AutoTexture(Texture* texture) 180 : texture(texture) {} 181 ~AutoTexture() { 182 if (texture && texture->cleanup) { 183 texture->deleteTexture(); 184 delete texture; 185 } 186 } 187 188 Texture* const texture; 189}; // class AutoTexture 190 191}; // namespace uirenderer 192}; // namespace android 193 194#endif // ANDROID_HWUI_TEXTURE_H 195