| 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 "ui/snapshot/snapshot.h" | |
| 6 | |
| 7 #include <gdk/gdkx.h> | |
| 8 #include <gtk/gtk.h> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "ui/base/x/x11_util.h" | |
| 13 #include "ui/gfx/image/image.h" | |
| 14 #include "ui/gfx/rect.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 cairo_status_t SnapshotCallback(void* closure, | |
| 19 const unsigned char* data, | |
| 20 unsigned int length) { | |
| 21 std::vector<unsigned char>* png_representation = | |
| 22 static_cast<std::vector<unsigned char>*>(closure); | |
| 23 | |
| 24 size_t old_size = png_representation->size(); | |
| 25 png_representation->resize(old_size + length); | |
| 26 memcpy(&(*png_representation)[old_size], data, length); | |
| 27 return CAIRO_STATUS_SUCCESS; | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 namespace ui { | |
| 33 | |
| 34 bool GrabViewSnapshot(gfx::NativeView view_handle, | |
| 35 std::vector<unsigned char>* png_representation, | |
| 36 const gfx::Rect& snapshot_bounds) { | |
| 37 GdkWindow* gdk_window = gtk_widget_get_window(view_handle); | |
| 38 Display* display = GDK_WINDOW_XDISPLAY(gdk_window); | |
| 39 XID win = GDK_WINDOW_XID(gdk_window); | |
| 40 | |
| 41 gfx::Rect window_bounds; | |
| 42 if (ui::GetWindowRect(win, &window_bounds) == 0) { | |
| 43 LOG(ERROR) << "Couldn't get window bounds"; | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 47 DCHECK_LE(snapshot_bounds.right(), window_bounds.width()); | |
| 48 DCHECK_LE(snapshot_bounds.bottom(), window_bounds.height()); | |
| 49 | |
| 50 ui::XScopedImage image(XGetImage( | |
| 51 display, win, snapshot_bounds.x(), snapshot_bounds.y(), | |
| 52 snapshot_bounds.width(), snapshot_bounds.height(), AllPlanes, ZPixmap)); | |
| 53 if (!image.get()) { | |
| 54 LOG(ERROR) << "Couldn't get image"; | |
| 55 return false; | |
| 56 } | |
| 57 if (image->depth != 24) { | |
| 58 LOG(ERROR)<< "Unsupported image depth " << image->depth; | |
| 59 return false; | |
| 60 } | |
| 61 cairo_surface_t* surface = | |
| 62 cairo_image_surface_create_for_data( | |
| 63 reinterpret_cast<unsigned char*>(image->data), | |
| 64 CAIRO_FORMAT_RGB24, | |
| 65 image->width, | |
| 66 image->height, | |
| 67 image->bytes_per_line); | |
| 68 | |
| 69 if (!surface) { | |
| 70 LOG(ERROR) << "Unable to create Cairo surface from XImage data"; | |
| 71 return false; | |
| 72 } | |
| 73 cairo_surface_write_to_png_stream( | |
| 74 surface, SnapshotCallback, png_representation); | |
| 75 cairo_surface_destroy(surface); | |
| 76 | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, | |
| 81 std::vector<unsigned char>* png_representation, | |
| 82 const gfx::Rect& snapshot_bounds) { | |
| 83 return GrabViewSnapshot(GTK_WIDGET(window_handle), png_representation, | |
| 84 snapshot_bounds); | |
| 85 } | |
| 86 | |
| 87 void GrabWindowSnapshotAndScaleAsync( | |
| 88 gfx::NativeWindow window, | |
| 89 const gfx::Rect& snapshot_bounds, | |
| 90 const gfx::Size& target_size, | |
| 91 scoped_refptr<base::TaskRunner> background_task_runner, | |
| 92 GrabWindowSnapshotAsyncCallback callback) { | |
| 93 callback.Run(gfx::Image()); | |
| 94 } | |
| 95 | |
| 96 void GrabViewSnapshotAsync( | |
| 97 gfx::NativeView view, | |
| 98 const gfx::Rect& source_rect, | |
| 99 scoped_refptr<base::TaskRunner> background_task_runner, | |
| 100 const GrabWindowSnapshotAsyncPNGCallback& callback) { | |
| 101 callback.Run(scoped_refptr<base::RefCountedBytes>()); | |
| 102 } | |
| 103 | |
| 104 void GrabWindowSnapshotAsync( | |
| 105 gfx::NativeWindow window, | |
| 106 const gfx::Rect& source_rect, | |
| 107 scoped_refptr<base::TaskRunner> background_task_runner, | |
| 108 const GrabWindowSnapshotAsyncPNGCallback& callback) { | |
| 109 callback.Run(scoped_refptr<base::RefCountedBytes>()); | |
| 110 } | |
| 111 | |
| 112 } // namespace ui | |
| OLD | NEW |