| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | |
| 6 | |
| 7 #include <gdk/gdkx.h> | |
| 8 #include <gtk/gtk.h> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "ui/base/x/x11_util.h" | |
| 12 #include "ui/gfx/rect.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 cairo_status_t SnapshotCallback(void* closure, | |
| 17 const unsigned char* data, | |
| 18 unsigned int length) { | |
| 19 std::vector<unsigned char>* png_representation = | |
| 20 static_cast<std::vector<unsigned char>*>(closure); | |
| 21 | |
| 22 size_t old_size = png_representation->size(); | |
| 23 png_representation->resize(old_size + length); | |
| 24 memcpy(&(*png_representation)[old_size], data, length); | |
| 25 return CAIRO_STATUS_SUCCESS; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace chrome { | |
| 31 | |
| 32 bool GrabWindowSnapshotImpl(gfx::NativeWindow window_handle, | |
| 33 std::vector<unsigned char>* png_representation, | |
| 34 const gfx::Rect& snapshot_bounds) { | |
| 35 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window_handle)); | |
| 36 Display* display = GDK_WINDOW_XDISPLAY(gdk_window); | |
| 37 XID win = GDK_WINDOW_XID(gdk_window); | |
| 38 | |
| 39 gfx::Rect window_bounds; | |
| 40 if (ui::GetWindowRect(win, &window_bounds) == 0) { | |
| 41 LOG(ERROR) << "Couldn't get window bounds"; | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 DCHECK_LE(snapshot_bounds.right(), window_bounds.width()); | |
| 46 DCHECK_LE(snapshot_bounds.bottom(), window_bounds.height()); | |
| 47 | |
| 48 XImage* image = XGetImage( | |
| 49 display, win, snapshot_bounds.x(), snapshot_bounds.y(), | |
| 50 snapshot_bounds.width(), snapshot_bounds.height(), AllPlanes, ZPixmap); | |
| 51 if (!image) { | |
| 52 LOG(ERROR) << "Couldn't get image"; | |
| 53 return false; | |
| 54 } | |
| 55 if (image->depth != 24) { | |
| 56 LOG(ERROR)<< "Unsupported image depth " << image->depth; | |
| 57 return false; | |
| 58 } | |
| 59 cairo_surface_t* surface = | |
| 60 cairo_image_surface_create_for_data( | |
| 61 reinterpret_cast<unsigned char*>(image->data), | |
| 62 CAIRO_FORMAT_RGB24, | |
| 63 image->width, | |
| 64 image->height, | |
| 65 image->bytes_per_line); | |
| 66 | |
| 67 if (!surface) { | |
| 68 LOG(ERROR) << "Unable to create Cairo surface from XImage data"; | |
| 69 return false; | |
| 70 } | |
| 71 cairo_surface_write_to_png_stream( | |
| 72 surface, SnapshotCallback, png_representation); | |
| 73 cairo_surface_destroy(surface); | |
| 74 | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 } // namespace chrome | |
| OLD | NEW |