OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
6 | 6 |
7 #include <gdk/gdkx.h> | 7 #include <gdk/gdkx.h> |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "ui/base/x/x11_util.h" | 11 #include "ui/base/x/x11_util.h" |
12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
13 | 13 |
14 namespace browser { | 14 namespace browser { |
15 | 15 |
16 namespace { | |
17 gfx::Rect GetWindowBounds(gfx::NativeWindow window_handle) { | |
jonathan.backer
2011/11/14 13:45:02
There are two functions in ui/base/x11_util.h that
sky
2011/11/14 16:23:09
nit: newline between 16 and 17.
| |
18 GdkWindow* gdk_window = GTK_WIDGET(window_handle)->window; | |
19 Display* display = GDK_WINDOW_XDISPLAY(gdk_window); | |
20 XID win = GDK_WINDOW_XID(gdk_window); | |
21 XWindowAttributes attr; | |
22 if (XGetWindowAttributes(display, win, &attr) == 0) { | |
23 return gfx::Rect(); | |
24 } | |
25 return gfx::Rect(0, 0, attr.width, attr.height); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
16 static cairo_status_t SnapshotCallback( | 30 static cairo_status_t SnapshotCallback( |
17 void *closure, const unsigned char *data, unsigned int length) { | 31 void *closure, const unsigned char *data, unsigned int length) { |
18 std::vector<unsigned char>* png_representation = | 32 std::vector<unsigned char>* png_representation = |
19 static_cast<std::vector<unsigned char>*>(closure); | 33 static_cast<std::vector<unsigned char>*>(closure); |
20 | 34 |
21 size_t old_size = png_representation->size(); | 35 size_t old_size = png_representation->size(); |
22 png_representation->resize(old_size + length); | 36 png_representation->resize(old_size + length); |
23 memcpy(&(*png_representation)[old_size], data, length); | 37 memcpy(&(*png_representation)[old_size], data, length); |
24 return CAIRO_STATUS_SUCCESS; | 38 return CAIRO_STATUS_SUCCESS; |
25 } | 39 } |
26 | 40 |
27 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow gtk_window, | 41 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window_handle, |
28 std::vector<unsigned char>* png_representation) { | 42 std::vector<unsigned char>* png_representation) { |
29 GdkWindow* gdk_window = GTK_WIDGET(gtk_window)->window; | 43 return GrabWindowSnapshot(window_handle, png_representation, |
30 Display* display = GDK_WINDOW_XDISPLAY(gdk_window); | 44 GetWindowBounds(window_handle)); |
31 XID win = GDK_WINDOW_XID(gdk_window); | 45 } |
32 XWindowAttributes attr; | 46 |
33 if (XGetWindowAttributes(display, win, &attr) == 0) { | 47 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window_handle, |
48 std::vector<unsigned char>* png_representation, | |
49 const gfx::Rect snapshot_bounds) { | |
50 gfx::Rect window_bounds = GetWindowBounds(window_handle); | |
51 if (window_bounds.IsEmpty()) { | |
34 LOG(ERROR) << "Couldn't get window attributes"; | 52 LOG(ERROR) << "Couldn't get window attributes"; |
35 return gfx::Rect(); | 53 return gfx::Rect(); |
36 } | 54 } |
55 | |
56 DCHECK(snapshot_bounds.right() <= window_bounds.width()); | |
57 DCHECK(snapshot_bounds.bottom() <= window_bounds.height()); | |
58 | |
59 GdkWindow* gdk_window = GTK_WIDGET(window_handle)->window; | |
jonathan.backer
2011/11/14 13:45:02
GetX11WindowFromGtkWidget
| |
60 Display* display = GDK_WINDOW_XDISPLAY(gdk_window); | |
jonathan.backer
2011/11/14 13:45:02
GetXDisplay?
pkotwicz
2011/11/15 15:56:12
It seems as if we are trying to deprecate this met
Elliot Glaysher
2011/11/15 18:06:34
Please don't use GetXDisplay(). We aren't guarante
| |
61 XID win = GDK_WINDOW_XID(gdk_window); | |
62 | |
37 XImage* image = XGetImage( | 63 XImage* image = XGetImage( |
38 display, win, 0, 0, attr.width, attr.height, AllPlanes, ZPixmap); | 64 display, win, snapshot_bounds.x(), snapshot_bounds.y(), |
65 snapshot_bounds.width(), snapshot_bounds.height(), AllPlanes, ZPixmap); | |
39 if (!image) { | 66 if (!image) { |
40 LOG(ERROR) << "Couldn't get image"; | 67 LOG(ERROR) << "Couldn't get image"; |
41 return gfx::Rect(); | 68 return gfx::Rect(); |
42 } | 69 } |
43 if (image->depth != 24) { | 70 if (image->depth != 24) { |
44 LOG(ERROR)<< "Unsupported image depth " << image->depth; | 71 LOG(ERROR)<< "Unsupported image depth " << image->depth; |
45 return gfx::Rect(); | 72 return gfx::Rect(); |
46 } | 73 } |
47 cairo_surface_t* surface = | 74 cairo_surface_t* surface = |
48 cairo_image_surface_create_for_data( | 75 cairo_image_surface_create_for_data( |
49 reinterpret_cast<unsigned char*>(image->data), | 76 reinterpret_cast<unsigned char*>(image->data), |
50 CAIRO_FORMAT_RGB24, | 77 CAIRO_FORMAT_RGB24, |
51 image->width, | 78 image->width, |
52 image->height, | 79 image->height, |
53 image->bytes_per_line); | 80 image->bytes_per_line); |
54 | 81 |
55 if (!surface) { | 82 if (!surface) { |
56 LOG(ERROR) << "Unable to create Cairo surface from XImage data"; | 83 LOG(ERROR) << "Unable to create Cairo surface from XImage data"; |
57 return gfx::Rect(); | 84 return gfx::Rect(); |
58 } | 85 } |
59 cairo_surface_write_to_png_stream( | 86 cairo_surface_write_to_png_stream( |
60 surface, SnapshotCallback, png_representation); | 87 surface, SnapshotCallback, png_representation); |
61 cairo_surface_destroy(surface); | 88 cairo_surface_destroy(surface); |
62 | 89 |
63 return gfx::Rect(image->width, image->height); | 90 return gfx::Rect(image->width, image->height); |
64 } | 91 } |
65 | 92 |
66 } // namespace browser | 93 } // namespace browser |
OLD | NEW |