diff -r ac96196b945c -r 15986eb6c500 m3g/m3gcore11/src/m3g_rendercontext.c --- a/m3g/m3gcore11/src/m3g_rendercontext.c Mon Mar 15 12:45:41 2010 +0200 +++ b/m3g/m3gcore11/src/m3g_rendercontext.c Wed Mar 31 23:34:07 2010 +0300 @@ -403,6 +403,69 @@ return ctx->alphaWrite; } +/*! + * \brief Frees all GLES resources allocated by the M3G API + * (EGL surfaces, contexts and texture objects). + * + * \note M3G must not be bound to any target when calling this. + * + */ +M3G_API void m3gFreeGLESResources(M3GRenderContext ctx) +{ +#ifdef M3G_ENABLE_GLES_RESOURCE_HANDLING + + PointerArray image2DObjects; + M3Gint i; + + /* M3G must not be bound to a rendering target at this point. */ + if (ctx->target.type != SURFACE_NONE) { + m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION); + } + + /* EGL might not be initialized yet, so do it here just in case. */ + eglInitialize(eglGetDisplay(EGL_DEFAULT_DISPLAY), NULL, NULL); + eglMakeCurrent(eglGetDisplay(EGL_DEFAULT_DISPLAY), NULL, NULL, NULL); + + /* Delete EGL surfaces */ + for (i = 0; i < M3G_MAX_GL_SURFACES; ++i) { + GLSurfaceRecord *surf = &ctx->glSurface[i]; + if (surf->handle) { + m3gDeleteGLSurface(surf->handle); + } + m3gZero(surf, sizeof(GLSurfaceRecord)); + } + if (ctx->backBuffer.glSurface != NULL) { + m3gDeleteGLSurface(ctx->backBuffer.glSurface); + m3gZero(&ctx->backBuffer, sizeof(BackBuffer)); + } + + /* Delete EGL contexts */ + for (i = 0; i < M3G_MAX_GL_CONTEXTS; ++i) { + GLContextRecord *context = &ctx->glContext[i]; + if (context->handle) { + m3gDeleteGLContext(context->handle); + } + m3gZero(context, sizeof(GLContextRecord)); + } + + /* Delete references to GLES texture objects from all live Image2D objects. + Texture objects themselves have already been destroyed with the last GL context. */ + + m3gInitArray(&image2DObjects); + m3gGetObjectsWithClassID(M3G_INTERFACE(ctx), M3G_CLASS_IMAGE, &image2DObjects); + + i = m3gArraySize(&image2DObjects); + + while (i > 0) { + Image *image = (Image*)m3gGetArrayElement(&image2DObjects, --i); + + m3gInvalidateImage(image); + image->texObject = 0; + } + m3gDestroyArray(&image2DObjects, M3G_INTERFACE(ctx)); +#endif +} + /*! * \internal