1/* 2 * Copyright (C) 2012 Intel Corporation. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24*/ 25 26#include "config.h" 27#include "SnapshotImageGL.h" 28 29#include <cairo.h> 30 31#if USE(OPENGL_ES_2) 32#include <GLES2/gl2.h> 33#include <GLES2/gl2ext.h> 34#else 35#include "OpenGLShims.h" 36#endif 37 38PassRefPtr<cairo_surface_t> getImageSurfaceFromFrameBuffer(int x, int y, int width, int height) 39{ 40 RefPtr<cairo_surface_t> newSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height)); 41 unsigned char* data = cairo_image_surface_get_data(newSurface.get()); 42 43#if USE(OPENGL_ES_2) 44 GLenum format = GL_RGBA; 45#else 46 GLenum format = GL_BGRA; 47#endif 48 49 glReadPixels(x, y, width, height, format, GL_UNSIGNED_BYTE, data); 50 51#if USE(OPENGL_ES_2) 52 // Convert to BGRA. 53 int totalBytes = width * height * 4; 54 55 for (int i = 0; i < totalBytes; i += 4) 56 std::swap(data[i], data[i + 2]); 57#endif 58 59 // Textures are flipped on the Y axis, so we need to flip the image back. 60 unsigned* buf = reinterpret_cast<unsigned*>(data); 61 62 for (int i = 0; i < height / 2; ++i) { 63 for (int j = 0; j < width; ++j) { 64 unsigned tmp = buf[i * width + j]; 65 buf[i * width + j] = buf[(height - i - 1) * width + j]; 66 buf[(height - i - 1) * width + j] = tmp; 67 } 68 } 69 70 cairo_surface_mark_dirty(newSurface.get()); 71 return newSurface; 72} 73