Texture.h revision 2a38c42e921451abebb4ee5f5ecd738f1b6b04ed
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 setWrapST(wrap, wrap, bindTexture, force); 46 } 47 48 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, 49 bool force = false); 50 51 inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { 52 setFilterMinMag(filter, filter, bindTexture, force); 53 } 54 55 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, 56 bool force = false); 57 58 /** 59 * Convenience method to call glDeleteTextures() on this texture's id. 60 */ 61 void deleteTexture(); 62 63 /** 64 * Sets the width, height, and format of the texture along with allocating 65 * the texture ID. Does nothing if the width, height, and format are already 66 * the requested values. 67 * 68 * The image data is undefined after calling this. 69 */ 70 void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) { 71 upload(internalFormat, width, height, format, GL_UNSIGNED_BYTE, nullptr); 72 } 73 74 /** 75 * Updates this Texture with the contents of the provided Bitmap, 76 * also setting the appropriate width, height, and format. It is not necessary 77 * to call resize() prior to this. 78 * 79 * Note this does not set the generation from the Bitmap. 80 */ 81 void upload(Bitmap& source); 82 83 /** 84 * Basically glTexImage2D/glTexSubImage2D. 85 */ 86 void upload(GLint internalFormat, uint32_t width, uint32_t height, 87 GLenum format, GLenum type, const void* pixels); 88 89 /** 90 * Wraps an existing texture. 91 */ 92 void wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, 93 GLint format, GLenum target); 94 95 GLuint id() const { 96 return mId; 97 } 98 99 uint32_t width() const { 100 return mWidth; 101 } 102 103 uint32_t height() const { 104 return mHeight; 105 } 106 107 GLint format() const { 108 return mFormat; 109 } 110 111 GLint internalFormat() const { 112 return mInternalFormat; 113 } 114 115 GLenum target() const { 116 return mTarget; 117 } 118 119 /** 120 * Generation of the backing bitmap, 121 */ 122 uint32_t generation = 0; 123 /** 124 * Indicates whether the texture requires blending. 125 */ 126 bool blend = false; 127 /** 128 * Indicates whether this texture should be cleaned up after use. 129 */ 130 bool cleanup = false; 131 /** 132 * Optional, size of the original bitmap. 133 */ 134 uint32_t bitmapSize = 0; 135 /** 136 * Indicates whether this texture will use trilinear filtering. 137 */ 138 bool mipMap = false; 139 140 /** 141 * Optional, pointer to a texture coordinates mapper. 142 */ 143 const UvMapper* uvMapper = nullptr; 144 145 /** 146 * Whether or not the Texture is marked in use and thus not evictable for 147 * the current frame. This is reset at the start of a new frame. 148 */ 149 void* isInUse = nullptr; 150 151private: 152 // TODO: Temporarily grant private access to Layer, remove once 153 // Layer can be de-tangled from being a dual-purpose render target 154 // and external texture wrapper 155 friend class Layer; 156 157 // Returns true if the size changed, false if it was the same 158 bool updateSize(uint32_t width, uint32_t height, GLint internalFormat, 159 GLint format, GLenum target); 160 void resetCachedParams(); 161 162 GLuint mId = 0; 163 uint32_t mWidth = 0; 164 uint32_t mHeight = 0; 165 GLint mFormat = 0; 166 GLint mInternalFormat = 0; 167 GLenum mTarget = GL_NONE; 168 169 /* See GLES spec section 3.8.14 170 * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is 171 * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR. 172 * s, t, and r wrap modes are all set to REPEAT." 173 */ 174 GLenum mWrapS = GL_REPEAT; 175 GLenum mWrapT = GL_REPEAT; 176 GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR; 177 GLenum mMagFilter = GL_LINEAR; 178 179 Caches& mCaches; 180}; // struct Texture 181 182class AutoTexture { 183public: 184 explicit AutoTexture(Texture* texture) 185 : texture(texture) {} 186 ~AutoTexture() { 187 if (texture && texture->cleanup) { 188 texture->deleteTexture(); 189 delete texture; 190 } 191 } 192 193 Texture* const texture; 194}; // class AutoTexture 195 196}; // namespace uirenderer 197}; // namespace android 198 199#endif // ANDROID_HWUI_TEXTURE_H 200