diff options
Diffstat (limited to 'opengl/libagl/texture.cpp')
-rw-r--r-- | opengl/libagl/texture.cpp | 262 |
1 files changed, 200 insertions, 62 deletions
diff --git a/opengl/libagl/texture.cpp b/opengl/libagl/texture.cpp index b6f534b..d675107 100644 --- a/opengl/libagl/texture.cpp +++ b/opengl/libagl/texture.cpp @@ -2,16 +2,16 @@ ** ** Copyright 2006, The Android Open Source Project ** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at ** -** http://www.apache.org/licenses/LICENSE-2.0 +** http://www.apache.org/licenses/LICENSE-2.0 ** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and ** limitations under the License. */ @@ -23,6 +23,13 @@ #include "texture.h" #include "TextureObjectManager.h" +#include <private/ui/android_natives_priv.h> + +#ifdef LIBAGL_USE_GRALLOC_COPYBITS +#include "copybit.h" +#include "gralloc_priv.h" +#endif // LIBAGL_USE_GRALLOC_COPYBITS + namespace android { // ---------------------------------------------------------------------------- @@ -48,7 +55,7 @@ void ogles_init_texture(ogles_context_t* c) // each context has a default named (0) texture (not shared) c->textures.defaultTexture = new EGLTextureObject(); c->textures.defaultTexture->incStrong(c); - + // bind the default texture to each texture unit for (int i=0; i<GGL_TEXTURE_UNIT_COUNT ; i++) { bindTextureTmu(c, i, 0, c->textures.defaultTexture); @@ -96,7 +103,7 @@ void validate_tmu(ogles_context_t* c, int i) } } -void ogles_validate_texture_impl(ogles_context_t* c) +void ogles_validate_texture(ogles_context_t* c) { for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; i++) { if (c->rasterizer.state.texture[i].enable) @@ -110,6 +117,66 @@ void invalidate_texture(ogles_context_t* c, int tmu, uint8_t flags = 0xFF) { c->textures.tmu[tmu].dirty = flags; } +/* + * If the active textures are EGLImage, they need to be locked before + * they can be used. + * + * FIXME: code below is far from being optimal + * + */ + +void ogles_lock_textures(ogles_context_t* c) +{ + for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; i++) { + if (c->rasterizer.state.texture[i].enable) { + texture_unit_t& u(c->textures.tmu[i]); + android_native_buffer_t* native_buffer = u.texture->buffer; + if (native_buffer) { + c->rasterizer.procs.activeTexture(c, i); + hw_module_t const* pModule; + if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule)) + continue; + + gralloc_module_t const* module = + reinterpret_cast<gralloc_module_t const*>(pModule); + + void* vaddr; + int err = module->lock(module, native_buffer->handle, + GRALLOC_USAGE_SW_READ_OFTEN, + 0, 0, native_buffer->width, native_buffer->height, + &vaddr); + + u.texture->setImageBits(vaddr); + c->rasterizer.procs.bindTexture(c, &(u.texture->surface)); + } + } + } +} + +void ogles_unlock_textures(ogles_context_t* c) +{ + for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; i++) { + if (c->rasterizer.state.texture[i].enable) { + texture_unit_t& u(c->textures.tmu[i]); + android_native_buffer_t* native_buffer = u.texture->buffer; + if (native_buffer) { + c->rasterizer.procs.activeTexture(c, i); + hw_module_t const* pModule; + if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule)) + continue; + + gralloc_module_t const* module = + reinterpret_cast<gralloc_module_t const*>(pModule); + + module->unlock(module, native_buffer->handle); + u.texture->setImageBits(NULL); + c->rasterizer.procs.bindTexture(c, &(u.texture->surface)); + } + } + } + c->rasterizer.procs.activeTexture(c, c->textures.active); +} + // ---------------------------------------------------------------------------- #if 0 #pragma mark - @@ -255,7 +322,7 @@ sp<EGLTextureObject> getAndBindActiveTextureObject(ogles_context_t* c) u.texture->decStrong(c); if (name == 0) { - // 0 is our local texture object, not shared with anyone. + // 0 is our local texture object, not shared with anyone. // But it affects all bound TMUs immediately. // (we need to invalidate all units bound to this texture object) tex = c->textures.defaultTexture; @@ -273,7 +340,7 @@ sp<EGLTextureObject> getAndBindActiveTextureObject(ogles_context_t* c) u.texture = tex.get(); u.texture->incStrong(c); u.name = name; - invalidate_texture(c, active); + invalidate_texture(c, active); return tex; } @@ -282,7 +349,7 @@ void bindTextureTmu( { if (tex.get() == c->textures.tmu[tmu].texture) return; - + // free the reference to the previously bound object texture_unit_t& u(c->textures.tmu[tmu]); if (u.texture) @@ -310,7 +377,7 @@ int createTextureSurface(ogles_context_t* c, if (formatIdx == 0) { // we don't know what to do with this return GL_INVALID_OPERATION; } - + // figure out the size we need as well as the stride const GGLFormat& pixelFormat(c->rasterizer.formats[formatIdx]); const int32_t align = c->textures.unpackAlignment-1; @@ -530,8 +597,8 @@ static void texParameterx( ogles_error(c, GL_INVALID_ENUM); return; } - - EGLTextureObject* textureObject = c->textures.tmu[c->textures.active].texture; + + EGLTextureObject* textureObject = c->textures.tmu[c->textures.active].texture; switch (pname) { case GL_TEXTURE_WRAP_S: if ((param == GL_REPEAT) || @@ -581,13 +648,12 @@ invalid_enum: } -static void drawTexxOES(GLfixed x, GLfixed y, GLfixed z, GLfixed w, GLfixed h, + +static void drawTexxOESImp(GLfixed x, GLfixed y, GLfixed z, GLfixed w, GLfixed h, ogles_context_t* c) { - // quickly reject empty rects - if ((w|h) <= 0) - return; - + ogles_lock_textures(c); + const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s; y = gglIntToFixed(cbSurface.height) - (y + h); w >>= FIXED_BITS; @@ -610,7 +676,7 @@ static void drawTexxOES(GLfixed x, GLfixed y, GLfixed z, GLfixed w, GLfixed h, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP); u.dirty = 0xFF; // XXX: should be more subtle - EGLTextureObject* textureObject = u.texture; + EGLTextureObject* textureObject = u.texture; const GLint Ucr = textureObject->crop_rect[0] << 16; const GLint Vcr = textureObject->crop_rect[1] << 16; const GLint Wcr = textureObject->crop_rect[2] << 16; @@ -641,11 +707,30 @@ static void drawTexxOES(GLfixed x, GLfixed y, GLfixed z, GLfixed w, GLfixed h, c->rasterizer.procs.disable(c, GGL_W_LERP); c->rasterizer.procs.disable(c, GGL_AA); c->rasterizer.procs.shadeModel(c, GL_FLAT); - c->rasterizer.procs.recti(c, + c->rasterizer.procs.recti(c, gglFixedToIntRound(x), gglFixedToIntRound(y), gglFixedToIntRound(x)+w, gglFixedToIntRound(y)+h); + + ogles_unlock_textures(c); +} + +static void drawTexxOES(GLfixed x, GLfixed y, GLfixed z, GLfixed w, GLfixed h, + ogles_context_t* c) +{ +#ifdef LIBAGL_USE_GRALLOC_COPYBITS + if (drawTexiOESWithCopybit(gglFixedToIntRound(x), + gglFixedToIntRound(y), gglFixedToIntRound(z), + gglFixedToIntRound(w), gglFixedToIntRound(h), c)) { + return; + } +#else + // quickly reject empty rects + if ((w|h) <= 0) + return; +#endif + drawTexxOESImp(x, y, z, w, h, c); } static void drawTexiOES(GLint x, GLint y, GLint z, GLint w, GLint h, ogles_context_t* c) @@ -656,14 +741,21 @@ static void drawTexiOES(GLint x, GLint y, GLint z, GLint w, GLint h, ogles_conte // which is a lot faster. if (ggl_likely(c->rasterizer.state.enabled_tmu == 1)) { +#ifdef LIBAGL_USE_GRALLOC_COPYBITS + if (drawTexiOESWithCopybit(x, y, z, w, h, c)) { + return; + } +#endif const int tmu = 0; texture_unit_t& u(c->textures.tmu[tmu]); - EGLTextureObject* textureObject = u.texture; + EGLTextureObject* textureObject = u.texture; const GLint Wcr = textureObject->crop_rect[2]; const GLint Hcr = textureObject->crop_rect[3]; if ((w == Wcr) && (h == -Hcr)) { +#ifndef LIBAGL_USE_GRALLOC_COPYBITS if ((w|h) <= 0) return; // quickly reject empty rects +#endif if (u.dirty) { c->rasterizer.procs.activeTexture(c, tmu); @@ -679,14 +771,14 @@ static void drawTexiOES(GLint x, GLint y, GLint z, GLint w, GLint h, ogles_conte GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); u.dirty = 0xFF; // XXX: should be more subtle c->rasterizer.procs.activeTexture(c, c->textures.active); - + const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s; y = cbSurface.height - (y + h); const GLint Ucr = textureObject->crop_rect[0]; const GLint Vcr = textureObject->crop_rect[1]; const GLint s0 = Ucr - x; const GLint t0 = (Vcr + Hcr) - y; - + const GLuint tw = textureObject->surface.width; const GLuint th = textureObject->surface.height; if ((uint32_t(s0+x+w) > tw) || (uint32_t(t0+y+h) > th)) { @@ -694,7 +786,9 @@ static void drawTexiOES(GLint x, GLint y, GLint z, GLint w, GLint h, ogles_conte // in this case, so we just use the slow case, which // at least won't crash goto slow_case; - } + } + + ogles_lock_textures(c); c->rasterizer.procs.texCoord2i(c, s0, t0); const uint32_t enables = c->rasterizer.state.enables; @@ -706,12 +800,15 @@ static void drawTexiOES(GLint x, GLint y, GLint z, GLint w, GLint h, ogles_conte c->rasterizer.procs.disable(c, GGL_AA); c->rasterizer.procs.shadeModel(c, GL_FLAT); c->rasterizer.procs.recti(c, x, y, x+w, y+h); + + ogles_unlock_textures(c); + return; } } slow_case: - drawTexxOES( + drawTexxOESImp( gglIntToFixed(x), gglIntToFixed(y), gglIntToFixed(z), gglIntToFixed(w), gglIntToFixed(h), c); @@ -749,7 +846,7 @@ void glBindTexture(GLenum target, GLuint texture) } // Bind or create a texture - sp<EGLTextureObject> tex; + sp<EGLTextureObject> tex; if (texture == 0) { // 0 is our local texture object tex = c->textures.defaultTexture; @@ -837,7 +934,7 @@ void glPixelStorei(GLenum pname, GLint param) if ((pname != GL_PACK_ALIGNMENT) && (pname != GL_UNPACK_ALIGNMENT)) { ogles_error(c, GL_INVALID_ENUM); return; - } + } if ((param<=0 || param>8) || (param & (param-1))) { ogles_error(c, GL_INVALID_VALUE); return; @@ -945,7 +1042,7 @@ void glCompressedTexImage2D( } // "uncompress" the texture since pixelflinger doesn't support - // any compressed texture format natively. + // any compressed texture format natively. GLenum format; GLenum type; switch (internalformat) { @@ -1009,7 +1106,7 @@ void glTexImage2D( GLenum format, GLenum type, const GLvoid *pixels) { ogles_context_t* c = ogles_context_t::get(); - if (target != GL_TEXTURE_2D && target != GL_DIRECT_TEXTURE_2D_QUALCOMM) { + if (target != GL_TEXTURE_2D) { ogles_error(c, GL_INVALID_ENUM); return; } @@ -1017,7 +1114,7 @@ void glTexImage2D( ogles_error(c, GL_INVALID_VALUE); return; } - if (format != internalformat) { + if (format != (GLenum)internalformat) { ogles_error(c, GL_INVALID_OPERATION); return; } @@ -1027,16 +1124,10 @@ void glTexImage2D( int32_t size = 0; GGLSurface* surface = 0; - if (target != GL_DIRECT_TEXTURE_2D_QUALCOMM) { - int error = createTextureSurface(c, &surface, &size, - level, format, type, width, height); - if (error) { - ogles_error(c, error); - return; - } - } else if (pixels == 0 || level != 0) { - // pixel can't be null for direct texture - ogles_error(c, GL_INVALID_OPERATION); + int error = createTextureSurface(c, &surface, &size, + level, format, type, width, height); + if (error) { + ogles_error(c, error); return; } @@ -1057,18 +1148,12 @@ void glTexImage2D( userSurface.compressedFormat = 0; userSurface.data = (GLubyte*)pixels; - if (target != GL_DIRECT_TEXTURE_2D_QUALCOMM) { - int err = copyPixels(c, *surface, 0, 0, userSurface, 0, 0, width, height); - if (err) { - ogles_error(c, err); - return; - } - generateMipmap(c, level); - } else { - // bind it to the texture unit - sp<EGLTextureObject> tex = getAndBindActiveTextureObject(c); - tex->setSurface(&userSurface); + int err = copyPixels(c, *surface, 0, 0, userSurface, 0, 0, width, height); + if (err) { + ogles_error(c, err); + return; } + generateMipmap(c, level); } } @@ -1143,7 +1228,7 @@ void glTexSubImage2D( int err = copyPixels(c, surface, xoffset, yoffset, - userSurface, 0, 0, width, height); + userSurface, 0, 0, width, height); if (err) { ogles_error(c, err); return; @@ -1196,7 +1281,7 @@ void glCopyTexImage2D( case GL_LUMINANCE_ALPHA: case GL_LUMINANCE: type = GL_UNSIGNED_BYTE; - break; + break; } // figure out the format to use for the new texture @@ -1206,7 +1291,7 @@ void glCopyTexImage2D( case GGL_PIXEL_FORMAT_RGBA_5551: case GGL_PIXEL_FORMAT_RGBA_4444: format = internalformat; - break; + break; case GGL_PIXEL_FORMAT_RGBX_8888: case GGL_PIXEL_FORMAT_RGB_888: case GGL_PIXEL_FORMAT_RGB_565: @@ -1215,7 +1300,7 @@ void glCopyTexImage2D( case GL_LUMINANCE: case GL_RGB: format = internalformat; - break; + break; } break; } @@ -1235,7 +1320,7 @@ void glCopyTexImage2D( ogles_error(c, error); return; } - + // The bottom row is stored first in textures GGLSurface txSurface(*surface); txSurface.stride = -txSurface.stride; @@ -1245,7 +1330,7 @@ void glCopyTexImage2D( int err = copyPixels(c, txSurface, 0, 0, - cbSurface, x, y, cbSurface.width, cbSurface.height); + cbSurface, x, y, cbSurface.width, cbSurface.height); if (err) { ogles_error(c, err); } @@ -1295,7 +1380,7 @@ void glCopyTexSubImage2D( int err = copyPixels(c, surface, xoffset, yoffset, - cbSurface, x, y, width, height); + cbSurface, x, y, width, height); if (err) { ogles_error(c, err); return; @@ -1365,7 +1450,7 @@ void glReadPixels( return; } - ggl->colorBuffer(ggl, &userSurface); // destination is user buffer + ggl->colorBuffer(ggl, &userSurface); // destination is user buffer ggl->bindTexture(ggl, &readSurface); // source is read-buffer ggl->texCoord2i(ggl, x, readSurface.height - (y + height)); ggl->recti(ggl, 0, 0, width, height); @@ -1419,3 +1504,56 @@ void glDrawTexxOES(GLfixed x, GLfixed y, GLfixed z, GLfixed w, GLfixed h) { ogles_context_t* c = ogles_context_t::get(); drawTexxOES(x, y, z, w, h, c); } + +// ---------------------------------------------------------------------------- +#if 0 +#pragma mark - +#pragma mark EGL Image Extension +#endif + +void glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image) +{ + ogles_context_t* c = ogles_context_t::get(); + if (target != GL_TEXTURE_2D) { + ogles_error(c, GL_INVALID_ENUM); + return; + } + + android_native_buffer_t* native_buffer = (android_native_buffer_t*)image; + if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) { + ogles_error(c, GL_INVALID_VALUE); + return; + } + if (native_buffer->common.version != sizeof(android_native_buffer_t)) { + ogles_error(c, GL_INVALID_VALUE); + return; + } + + // bind it to the texture unit + sp<EGLTextureObject> tex = getAndBindActiveTextureObject(c); + tex->setImage(native_buffer); + + /* + * Here an implementation can retrieve the buffer_handle_t of this buffer + * which gives it access to an arbitrary-defined kernel resource + * (or anything else for that matter). + * There needs to be an intimate knowledge between GLES and buffer_handle_t, + * so make sure to validate the handle before using it. + * Typically, buffer_handle_t comes from the gralloc HAL which is provided + * by the implementor of GLES. + * + */ +#ifdef LIBAGL_USE_GRALLOC_COPYBITS + tex->copybits_fd = -1; + private_handle_t* hand; + if ((hand = private_handle_t::dynamicCast(native_buffer->handle)) != NULL) { + if (hand->usesPhysicallyContiguousMemory()) { + tex->copybits_fd = hand->fd; + } + } +#endif // LIBAGL_USE_GRALLOC_COPYBITS +} + +void glEGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image) +{ +} |