Implemented RenderReadPixels for the OpenGL ES 2.0 renderer.
authorSam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 02:55:21 -0400
changeset 6042 ff2ab1e8d516
parent 6041 69d7539ff51f
child 6043 46d78e48ae8b
Implemented RenderReadPixels for the OpenGL ES 2.0 renderer.
src/render/opengles2/SDL_render_gles2.c
--- a/src/render/opengles2/SDL_render_gles2.c	Mon Oct 31 02:44:21 2011 -0400
+++ b/src/render/opengles2/SDL_render_gles2.c	Mon Oct 31 02:55:21 2011 -0400
@@ -786,6 +786,8 @@
 static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count);
 static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
                             const SDL_Rect *dstrect);
+static int GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
+                    Uint32 pixel_format, void * pixels, int pitch);
 static void GLES2_RenderPresent(SDL_Renderer *renderer);
 
 
@@ -1047,6 +1049,57 @@
     return 0;
 }
 
+static int
+GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
+                    Uint32 pixel_format, void * pixels, int pitch)
+{
+    SDL_Window *window = renderer->window;
+    Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
+    void *temp_pixels;
+    int temp_pitch;
+    Uint8 *src, *dst, *tmp;
+    int w, h, length, rows;
+    int status;
+
+    GLES2_ActivateRenderer(renderer);
+
+    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
+    temp_pixels = SDL_malloc(rect->h * temp_pitch);
+    if (!temp_pixels) {
+        SDL_OutOfMemory();
+        return -1;
+    }
+
+    SDL_GetWindowSize(window, &w, &h);
+
+    glPixelStorei(GL_PACK_ALIGNMENT, 1);
+
+    glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
+                       GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
+
+    /* Flip the rows to be top-down */
+    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
+    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
+    dst = (Uint8*)temp_pixels;
+    tmp = SDL_stack_alloc(Uint8, length);
+    rows = rect->h / 2;
+    while (rows--) {
+        SDL_memcpy(tmp, dst, length);
+        SDL_memcpy(dst, src, length);
+        SDL_memcpy(src, tmp, length);
+        dst += temp_pitch;
+        src -= temp_pitch;
+    }
+    SDL_stack_free(tmp);
+
+    status = SDL_ConvertPixels(rect->w, rect->h,
+                               temp_format, temp_pixels, temp_pitch,
+                               pixel_format, pixels, pitch);
+    SDL_free(temp_pixels);
+
+    return status;
+}
+
 static void
 GLES2_RenderPresent(SDL_Renderer *renderer)
 {
@@ -1176,6 +1229,7 @@
     renderer->RenderDrawLines     = &GLES2_RenderDrawLines;
     renderer->RenderFillRects     = &GLES2_RenderFillRects;
     renderer->RenderCopy          = &GLES2_RenderCopy;
+    renderer->RenderReadPixels    = &GLES2_RenderReadPixels;
     renderer->RenderPresent       = &GLES2_RenderPresent;
     renderer->DestroyTexture      = &GLES2_DestroyTexture;
     renderer->DestroyRenderer     = &GLES2_DestroyRenderer;