Texture.h revision 694d499662838123f474f41b31dea84ec5d563f0
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#include <EGL/egl.h> 25#include <EGL/eglext.h> 26#include <SkBitmap.h> 27 28namespace android { 29 30class GraphicBuffer; 31 32namespace uirenderer { 33 34class Caches; 35class UvMapper; 36class Layer; 37 38/** 39 * Represents an OpenGL texture. 40 */ 41class Texture : public GpuMemoryTracker { 42public: 43 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 48 explicit Texture(Caches& caches) 49 : GpuMemoryTracker(GpuObjectType::Texture) 50 , mCaches(caches) 51 { } 52 53 virtual ~Texture() { } 54 55 inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { 56 setWrapST(wrap, wrap, bindTexture, force); 57 } 58 59 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, 60 bool force = false); 61 62 inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { 63 setFilterMinMag(filter, filter, bindTexture, force); 64 } 65 66 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, 67 bool force = false); 68 69 /** 70 * Convenience method to call glDeleteTextures() on this texture's id. 71 */ 72 void deleteTexture(); 73 74 /** 75 * 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. 80 */ 81 void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) { 82 upload(internalFormat, width, height, format, GL_UNSIGNED_BYTE, nullptr); 83 } 84 85 /** 86 * Updates this Texture with the contents of the provided Bitmap, 87 * also setting the appropriate width, height, and format. It is not necessary 88 * to call resize() prior to this. 89 * 90 * Note this does not set the generation from the Bitmap. 91 */ 92 void upload(Bitmap& source); 93 94 /** 95 * Basically glTexImage2D/glTexSubImage2D. 96 */ 97 void upload(GLint internalFormat, uint32_t width, uint32_t height, 98 GLenum format, GLenum type, const void* pixels); 99 100 /** 101 * Wraps an existing texture. 102 */ 103 void wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, 104 GLint format, GLenum target); 105 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 122 GLint internalFormat() const { 123 return mInternalFormat; 124 } 125 126 GLenum target() const { 127 return mTarget; 128 } 129 130 /** 131 * Generation of the backing bitmap, 132 */ 133 uint32_t generation = 0; 134 /** 135 * Indicates whether the texture requires blending. 136 */ 137 bool blend = false; 138 /** 139 * Indicates whether this texture should be cleaned up after use. 140 */ 141 bool cleanup = false; 142 /** 143 * Optional, size of the original bitmap. 144 */ 145 uint32_t bitmapSize = 0; 146 /** 147 * Indicates whether this texture will use trilinear filtering. 148 */ 149 bool mipMap = false; 150 151 /** 152 * Optional, pointer to a texture coordinates mapper. 153 */ 154 const UvMapper* uvMapper = nullptr; 155 156 /** 157 * Whether or not the Texture is marked in use and thus not evictable for 158 * the current frame. This is reset at the start of a new frame. 159 */ 160 void* isInUse = nullptr; 161private: 162 // TODO: Temporarily grant private access to Layer, remove once 163 // Layer can be de-tangled from being a dual-purpose render target 164 // and external texture wrapper 165 friend class Layer; 166 167 // Returns true if the size changed, false if it was the same 168 bool updateSize(uint32_t width, uint32_t height, GLint internalFormat, 169 GLint format, GLenum target); 170 void uploadHardwareBitmapToTexture(GraphicBuffer* buffer); 171 void resetCachedParams(); 172 173 GLuint mId = 0; 174 uint32_t mWidth = 0; 175 uint32_t mHeight = 0; 176 GLint mFormat = 0; 177 GLint mInternalFormat = 0; 178 GLenum mTarget = GL_NONE; 179 EGLImageKHR mEglImageHandle = EGL_NO_IMAGE_KHR; 180 181 /* See GLES spec section 3.8.14 182 * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is 183 * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR. 184 * s, t, and r wrap modes are all set to REPEAT." 185 */ 186 GLenum mWrapS = GL_REPEAT; 187 GLenum mWrapT = GL_REPEAT; 188 GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR; 189 GLenum mMagFilter = GL_LINEAR; 190 191 Caches& mCaches; 192}; // struct Texture 193 194class AutoTexture { 195public: 196 explicit AutoTexture(Texture* texture) 197 : texture(texture) {} 198 ~AutoTexture() { 199 if (texture && texture->cleanup) { 200 texture->deleteTexture(); 201 delete texture; 202 } 203 } 204 205 Texture* const texture; 206}; // class AutoTexture 207 208}; // namespace uirenderer 209}; // namespace android 210 211#endif // ANDROID_HWUI_TEXTURE_H 212