| 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 namespace internal { | |
| 32 | |
| 33 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, | |
| 34 std::vector<unsigned char>* png_representation, | |
| 35 const gfx::Rect& snapshot_bounds) { | |
| 36 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window_handle)); | |
| 37 Display* display = GDK_WINDOW_XDISPLAY(gdk_window); | |
| 38 XID win = GDK_WINDOW_XID(gdk_window); | |
| 39 | |
| 40 gfx::Rect window_bounds; | |
| 41 if (ui::GetWindowRect(win, &window_bounds) == 0) { | |
| 42 LOG(ERROR) << "Couldn't get window bounds"; | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 DCHECK_LE(snapshot_bounds.right(), window_bounds.width()); | |
| 47 DCHECK_LE(snapshot_bounds.bottom(), window_bounds.height()); | |
| 48 | |
| 49 ui::XScopedImage image(XGetImage( | |
| 50 display, win, snapshot_bounds.x(), snapshot_bounds.y(), | |
| 51 snapshot_bounds.width(), snapshot_bounds.height(), AllPlanes, ZPixmap)); | |
| 52 if (!image.get()) { | |
| 53 LOG(ERROR) << "Couldn't get image"; | |
| 54 return false; | |
| 55 } | |
| 56 if (image->depth != 24) { | |
| 57 LOG(ERROR)<< "Unsupported image depth " << image->depth; | |
| 58 return false; | |
| 59 } | |
| 60 cairo_surface_t* surface = | |
| 61 cairo_image_surface_create_for_data( | |
| 62 reinterpret_cast<unsigned char*>(image->data), | |
| 63 CAIRO_FORMAT_RGB24, | |
| 64 image->width, | |
| 65 image->height, | |
| 66 image->bytes_per_line); | |
| 67 | |
| 68 if (!surface) { | |
| 69 LOG(ERROR) << "Unable to create Cairo surface from XImage data"; | |
| 70 return false; | |
| 71 } | |
| 72 cairo_surface_write_to_png_stream( | |
| 73 surface, SnapshotCallback, png_representation); | |
| 74 cairo_surface_destroy(surface); | |
| 75 | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 } // namespace internal | |
| 80 } // namespace chrome | |
| OLD | NEW |