Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 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 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <utils/Errors.h> |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <ui/GraphicBuffer.h> |
| 25 | |
| 26 | #include <GLES/gl.h> |
| 27 | #include <GLES/glext.h> |
| 28 | |
| 29 | #include <hardware/hardware.h> |
| 30 | |
| 31 | #include "clz.h" |
| 32 | #include "DisplayHardware/DisplayHardware.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 33 | #include "GLExtensions.h" |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 34 | #include "TextureManager.h" |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | // --------------------------------------------------------------------------- |
| 39 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 40 | TextureManager::TextureManager() |
| 41 | : mGLExtensions(GLExtensions::getInstance()) |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 45 | GLenum TextureManager::getTextureTarget(const Image* image) { |
| 46 | #if defined(GL_OES_texture_external) |
| 47 | switch (image->target) { |
| 48 | case Texture::TEXTURE_EXTERNAL: |
| 49 | return GL_TEXTURE_EXTERNAL_OES; |
| 50 | } |
| 51 | #endif |
| 52 | return GL_TEXTURE_2D; |
| 53 | } |
| 54 | |
| 55 | status_t TextureManager::initTexture(Texture* texture) |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 56 | { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 57 | if (texture->name != -1UL) |
| 58 | return INVALID_OPERATION; |
| 59 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 60 | GLuint textureName = -1; |
| 61 | glGenTextures(1, &textureName); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 62 | texture->name = textureName; |
| 63 | texture->width = 0; |
| 64 | texture->height = 0; |
| 65 | |
| 66 | const GLenum target = GL_TEXTURE_2D; |
| 67 | glBindTexture(target, textureName); |
| 68 | glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 69 | glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 70 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 71 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 72 | |
| 73 | return NO_ERROR; |
| 74 | } |
| 75 | |
| 76 | status_t TextureManager::initTexture(Image* pImage, int32_t format) |
| 77 | { |
| 78 | if (pImage->name != -1UL) |
| 79 | return INVALID_OPERATION; |
| 80 | |
| 81 | GLuint textureName = -1; |
| 82 | glGenTextures(1, &textureName); |
| 83 | pImage->name = textureName; |
| 84 | pImage->width = 0; |
| 85 | pImage->height = 0; |
| 86 | |
| 87 | GLenum target = GL_TEXTURE_2D; |
| 88 | #if defined(GL_OES_texture_external) |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 89 | if (GLExtensions::getInstance().haveTextureExternal()) { |
| 90 | if (format && isSupportedYuvFormat(format)) { |
| 91 | target = GL_TEXTURE_EXTERNAL_OES; |
| 92 | pImage->target = Texture::TEXTURE_EXTERNAL; |
| 93 | } |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 94 | } |
| 95 | #endif |
| 96 | |
| 97 | glBindTexture(target, textureName); |
| 98 | glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 99 | glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 100 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 101 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 102 | |
| 103 | return NO_ERROR; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool TextureManager::isSupportedYuvFormat(int format) |
| 107 | { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 108 | return isYuvFormat(format); |
| 109 | } |
| 110 | |
| 111 | bool TextureManager::isYuvFormat(int format) |
| 112 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 113 | switch (format) { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 114 | case HAL_PIXEL_FORMAT_NV16: |
| 115 | case HAL_PIXEL_FORMAT_NV21: |
| 116 | case HAL_PIXEL_FORMAT_IYUV: |
| 117 | case HAL_PIXEL_FORMAT_YUV9: |
| 118 | case HAL_PIXEL_FORMAT_YUY2: |
| 119 | case HAL_PIXEL_FORMAT_UYVY: |
| 120 | case HAL_PIXEL_FORMAT_NV12: |
| 121 | case HAL_PIXEL_FORMAT_NV61: |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 122 | case HAL_PIXEL_FORMAT_NV12_ADRENO_TILED: |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 123 | return true; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 128 | status_t TextureManager::initEglImage(Image* pImage, |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 129 | EGLDisplay dpy, const sp<GraphicBuffer>& buffer) |
| 130 | { |
| 131 | status_t err = NO_ERROR; |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 132 | if (!pImage->dirty) return err; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 133 | |
| 134 | // free the previous image |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 135 | if (pImage->image != EGL_NO_IMAGE_KHR) { |
| 136 | eglDestroyImageKHR(dpy, pImage->image); |
| 137 | pImage->image = EGL_NO_IMAGE_KHR; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // construct an EGL_NATIVE_BUFFER_ANDROID |
| 141 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 142 | |
| 143 | // create the new EGLImageKHR |
| 144 | const EGLint attrs[] = { |
| 145 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 146 | EGL_NONE, EGL_NONE |
| 147 | }; |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 148 | pImage->image = eglCreateImageKHR( |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 149 | dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 150 | (EGLClientBuffer)clientBuf, attrs); |
| 151 | |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 152 | if (pImage->image != EGL_NO_IMAGE_KHR) { |
| 153 | if (pImage->name == -1UL) { |
| 154 | initTexture(pImage, buffer->format); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 155 | } |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 156 | const GLenum target = getTextureTarget(pImage); |
| 157 | glBindTexture(target, pImage->name); |
| 158 | glEGLImageTargetTexture2DOES(target, (GLeglImageOES)pImage->image); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 159 | GLint error = glGetError(); |
| 160 | if (error != GL_NO_ERROR) { |
| 161 | LOGE("glEGLImageTargetTexture2DOES(%p) failed err=0x%04x", |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 162 | pImage->image, error); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 163 | err = INVALID_OPERATION; |
| 164 | } else { |
| 165 | // Everything went okay! |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 166 | pImage->dirty = false; |
| 167 | pImage->width = clientBuf->width; |
| 168 | pImage->height = clientBuf->height; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 169 | } |
| 170 | } else { |
| 171 | LOGE("eglCreateImageKHR() failed. err=0x%4x", eglGetError()); |
| 172 | err = INVALID_OPERATION; |
| 173 | } |
| 174 | return err; |
| 175 | } |
| 176 | |
| 177 | status_t TextureManager::loadTexture(Texture* texture, |
| 178 | const Region& dirty, const GGLSurface& t) |
| 179 | { |
| 180 | if (texture->name == -1UL) { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 181 | status_t err = initTexture(texture); |
| 182 | LOGE_IF(err, "loadTexture failed in initTexture (%s)", strerror(err)); |
| 183 | return err; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 186 | if (texture->target != GL_TEXTURE_2D) |
| 187 | return INVALID_OPERATION; |
| 188 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 189 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 190 | |
| 191 | /* |
| 192 | * In OpenGL ES we can't specify a stride with glTexImage2D (however, |
| 193 | * GL_UNPACK_ALIGNMENT is a limited form of stride). |
| 194 | * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we |
| 195 | * need to do something reasonable (here creating a bigger texture). |
| 196 | * |
| 197 | * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT); |
| 198 | * |
| 199 | * This situation doesn't happen often, but some h/w have a limitation |
| 200 | * for their framebuffer (eg: must be multiple of 8 pixels), and |
| 201 | * we need to take that into account when using these buffers as |
| 202 | * textures. |
| 203 | * |
| 204 | * This should never be a problem with POT textures |
| 205 | */ |
| 206 | |
| 207 | int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format)); |
| 208 | unpack = 1 << ((unpack > 3) ? 3 : unpack); |
| 209 | glPixelStorei(GL_UNPACK_ALIGNMENT, unpack); |
| 210 | |
| 211 | /* |
| 212 | * round to POT if needed |
| 213 | */ |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 214 | if (!mGLExtensions.haveNpot()) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 215 | texture->NPOTAdjust = true; |
| 216 | } |
| 217 | |
| 218 | if (texture->NPOTAdjust) { |
| 219 | // find the smallest power-of-two that will accommodate our surface |
| 220 | texture->potWidth = 1 << (31 - clz(t.width)); |
| 221 | texture->potHeight = 1 << (31 - clz(t.height)); |
| 222 | if (texture->potWidth < t.width) texture->potWidth <<= 1; |
| 223 | if (texture->potHeight < t.height) texture->potHeight <<= 1; |
| 224 | texture->wScale = float(t.width) / texture->potWidth; |
| 225 | texture->hScale = float(t.height) / texture->potHeight; |
| 226 | } else { |
| 227 | texture->potWidth = t.width; |
| 228 | texture->potHeight = t.height; |
| 229 | } |
| 230 | |
| 231 | Rect bounds(dirty.bounds()); |
| 232 | GLvoid* data = 0; |
| 233 | if (texture->width != t.width || texture->height != t.height) { |
| 234 | texture->width = t.width; |
| 235 | texture->height = t.height; |
| 236 | |
| 237 | // texture size changed, we need to create a new one |
| 238 | bounds.set(Rect(t.width, t.height)); |
| 239 | if (t.width == texture->potWidth && |
| 240 | t.height == texture->potHeight) { |
| 241 | // we can do it one pass |
| 242 | data = t.data; |
| 243 | } |
| 244 | |
| 245 | if (t.format == HAL_PIXEL_FORMAT_RGB_565) { |
| 246 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 247 | GL_RGB, texture->potWidth, texture->potHeight, 0, |
| 248 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data); |
| 249 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) { |
| 250 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 251 | GL_RGBA, texture->potWidth, texture->potHeight, 0, |
| 252 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data); |
| 253 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 || |
| 254 | t.format == HAL_PIXEL_FORMAT_RGBX_8888) { |
| 255 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 256 | GL_RGBA, texture->potWidth, texture->potHeight, 0, |
| 257 | GL_RGBA, GL_UNSIGNED_BYTE, data); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 258 | } else if (isYuvFormat(t.format)) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 259 | // just show the Y plane of YUV buffers |
| 260 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 261 | GL_LUMINANCE, texture->potWidth, texture->potHeight, 0, |
| 262 | GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 263 | } else { |
| 264 | // oops, we don't handle this format! |
| 265 | LOGE("texture=%d, using format %d, which is not " |
| 266 | "supported by the GL", texture->name, t.format); |
| 267 | } |
| 268 | } |
| 269 | if (!data) { |
| 270 | if (t.format == HAL_PIXEL_FORMAT_RGB_565) { |
| 271 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 272 | 0, bounds.top, t.width, bounds.height(), |
| 273 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, |
| 274 | t.data + bounds.top*t.stride*2); |
| 275 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) { |
| 276 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 277 | 0, bounds.top, t.width, bounds.height(), |
| 278 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, |
| 279 | t.data + bounds.top*t.stride*2); |
| 280 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 || |
| 281 | t.format == HAL_PIXEL_FORMAT_RGBX_8888) { |
| 282 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 283 | 0, bounds.top, t.width, bounds.height(), |
| 284 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 285 | t.data + bounds.top*t.stride*4); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 286 | } else if (isYuvFormat(t.format)) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 287 | // just show the Y plane of YUV buffers |
| 288 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 289 | 0, bounds.top, t.width, bounds.height(), |
| 290 | GL_LUMINANCE, GL_UNSIGNED_BYTE, |
| 291 | t.data + bounds.top*t.stride); |
| 292 | } |
| 293 | } |
| 294 | return NO_ERROR; |
| 295 | } |
| 296 | |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 297 | void TextureManager::activateTexture(const Texture& texture, bool filter) |
| 298 | { |
| 299 | const GLenum target = getTextureTarget(&texture); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 300 | if (target == Texture::TEXTURE_2D) { |
| 301 | glBindTexture(GL_TEXTURE_2D, texture.name); |
| 302 | glEnable(GL_TEXTURE_2D); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 303 | #if defined(GL_OES_texture_external) |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 304 | if (GLExtensions::getInstance().haveTextureExternal()) { |
| 305 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 306 | } |
| 307 | #endif |
| 308 | } |
| 309 | #if defined(GL_OES_texture_external) |
| 310 | else { |
| 311 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture.name); |
| 312 | glEnable(GL_TEXTURE_EXTERNAL_OES); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 313 | glDisable(GL_TEXTURE_2D); |
| 314 | } |
| 315 | #endif |
| 316 | |
| 317 | if (filter) { |
| 318 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 319 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 320 | } else { |
| 321 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 322 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void TextureManager::deactivateTextures() |
| 327 | { |
| 328 | glDisable(GL_TEXTURE_2D); |
| 329 | #if defined(GL_OES_texture_external) |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 330 | if (GLExtensions::getInstance().haveTextureExternal()) { |
| 331 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 332 | } |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 333 | #endif |
| 334 | } |
| 335 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 336 | // --------------------------------------------------------------------------- |
| 337 | |
| 338 | }; // namespace android |