| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
| 6 | 6 |
| 7 #include "base/win/scoped_gdi_object.h" | 7 #include "ui/snapshot/snapshot_hwnd_win.h" |
| 8 #include "base/win/scoped_hdc.h" | |
| 9 #include "base/win/scoped_select_object.h" | |
| 10 #include "ui/gfx/codec/png_codec.h" | |
| 11 #include "ui/gfx/gdi_util.h" | |
| 12 #include "ui/gfx/rect.h" | |
| 13 #include "ui/gfx/size.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 gfx::Rect GetWindowBounds(gfx::NativeWindow window_handle) { | |
| 18 RECT content_rect = {0, 0, 0, 0}; | |
| 19 if (window_handle) { | |
| 20 ::GetWindowRect(window_handle, &content_rect); | |
| 21 } else { | |
| 22 MONITORINFO monitor_info = {}; | |
| 23 monitor_info.cbSize = sizeof(monitor_info); | |
| 24 if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), | |
| 25 &monitor_info)) { | |
| 26 content_rect = monitor_info.rcMonitor; | |
| 27 } | |
| 28 } | |
| 29 content_rect.right++; // Match what PrintWindow wants. | |
| 30 | |
| 31 return gfx::Rect(content_rect.right - content_rect.left, | |
| 32 content_rect.bottom - content_rect.top); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | 8 |
| 37 namespace ui { | 9 namespace ui { |
| 38 | 10 |
| 39 bool GrabViewSnapshot(gfx::NativeView view_handle, | 11 bool GrabViewSnapshot(gfx::NativeView view_handle, |
| 40 std::vector<unsigned char>* png_representation, | 12 std::vector<unsigned char>* png_representation, |
| 41 const gfx::Rect& snapshot_bounds) { | 13 const gfx::Rect& snapshot_bounds) { |
| 42 return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds); | 14 return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds); |
| 43 } | 15 } |
| 44 | 16 |
| 45 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, | 17 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, |
| 46 std::vector<unsigned char>* png_representation, | 18 std::vector<unsigned char>* png_representation, |
| 47 const gfx::Rect& snapshot_bounds) { | 19 const gfx::Rect& snapshot_bounds) { |
| 48 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right()); | 20 DCHECK(window_handle); |
| 49 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom()); | 21 return internal::GrabHwndSnapshot(window_handle, png_representation, |
| 50 | 22 snapshot_bounds); |
| 51 // Create a memory DC that's compatible with the window. | |
| 52 HDC window_hdc = GetWindowDC(window_handle); | |
| 53 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); | |
| 54 | |
| 55 BITMAPINFOHEADER hdr; | |
| 56 gfx::CreateBitmapHeader(snapshot_bounds.width(), | |
| 57 snapshot_bounds.height(), | |
| 58 &hdr); | |
| 59 unsigned char *bit_ptr = NULL; | |
| 60 base::win::ScopedBitmap bitmap( | |
| 61 CreateDIBSection(mem_hdc, | |
| 62 reinterpret_cast<BITMAPINFO*>(&hdr), | |
| 63 DIB_RGB_COLORS, | |
| 64 reinterpret_cast<void **>(&bit_ptr), | |
| 65 NULL, 0)); | |
| 66 | |
| 67 base::win::ScopedSelectObject select_bitmap(mem_hdc, bitmap); | |
| 68 // Clear the bitmap to white (so that rounded corners on windows | |
| 69 // show up on a white background, and strangely-shaped windows | |
| 70 // look reasonable). Not capturing an alpha mask saves a | |
| 71 // bit of space. | |
| 72 PatBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(), | |
| 73 WHITENESS); | |
| 74 // Grab a copy of the window | |
| 75 // First, see if PrintWindow is defined (it's not in Windows 2000). | |
| 76 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); | |
| 77 PrintWindowPointer print_window = | |
| 78 reinterpret_cast<PrintWindowPointer>( | |
| 79 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); | |
| 80 | |
| 81 // If PrintWindow is defined, use it. It will work on partially | |
| 82 // obscured windows, and works better for out of process sub-windows. | |
| 83 // Otherwise grab the bits we can get with BitBlt; it's better | |
| 84 // than nothing and will work fine in the average case (window is | |
| 85 // completely on screen). Always BitBlt when grabbing the whole screen. | |
| 86 if (snapshot_bounds.origin() == gfx::Point() && print_window && window_handle) | |
| 87 (*print_window)(window_handle, mem_hdc, 0); | |
| 88 else | |
| 89 BitBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(), | |
| 90 window_hdc, snapshot_bounds.x(), snapshot_bounds.y(), SRCCOPY); | |
| 91 | |
| 92 // We now have a copy of the window contents in a DIB, so | |
| 93 // encode it into a useful format for posting to the bug report | |
| 94 // server. | |
| 95 gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA, | |
| 96 snapshot_bounds.size(), | |
| 97 snapshot_bounds.width() * 4, true, | |
| 98 std::vector<gfx::PNGCodec::Comment>(), | |
| 99 png_representation); | |
| 100 | |
| 101 ReleaseDC(window_handle, window_hdc); | |
| 102 | |
| 103 return true; | |
| 104 } | 23 } |
| 105 | 24 |
| 106 } // namespace ui | 25 } // namespace ui |
| OLD | NEW |