| 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_win.h" |
| 6 | 6 |
| 7 #include "base/win/scoped_gdi_object.h" | 7 #include "base/win/scoped_gdi_object.h" |
| 8 #include "base/win/scoped_hdc.h" | 8 #include "base/win/scoped_hdc.h" |
| 9 #include "base/win/scoped_select_object.h" | 9 #include "base/win/scoped_select_object.h" |
| 10 #include "ui/gfx/codec/png_codec.h" | 10 #include "ui/gfx/codec/png_codec.h" |
| 11 #include "ui/gfx/gdi_util.h" | 11 #include "ui/gfx/gdi_util.h" |
| 12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
| 13 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
| 14 #include "ui/snapshot/snapshot.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 gfx::Rect GetWindowBounds(gfx::NativeWindow window_handle) { | 18 gfx::Rect GetWindowBounds(HWND window_handle) { |
| 18 RECT content_rect = {0, 0, 0, 0}; | 19 RECT content_rect = {0, 0, 0, 0}; |
| 19 if (window_handle) { | 20 if (window_handle) { |
| 20 ::GetWindowRect(window_handle, &content_rect); | 21 ::GetWindowRect(window_handle, &content_rect); |
| 21 } else { | 22 } else { |
| 22 MONITORINFO monitor_info = {}; | 23 MONITORINFO monitor_info = {}; |
| 23 monitor_info.cbSize = sizeof(monitor_info); | 24 monitor_info.cbSize = sizeof(monitor_info); |
| 24 if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), | 25 if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), |
| 25 &monitor_info)) { | 26 &monitor_info)) { |
| 26 content_rect = monitor_info.rcMonitor; | 27 content_rect = monitor_info.rcMonitor; |
| 27 } | 28 } |
| 28 } | 29 } |
| 29 content_rect.right++; // Match what PrintWindow wants. | 30 content_rect.right++; // Match what PrintWindow wants. |
| 30 | 31 |
| 31 return gfx::Rect(content_rect.right - content_rect.left, | 32 return gfx::Rect(content_rect.right - content_rect.left, |
| 32 content_rect.bottom - content_rect.top); | 33 content_rect.bottom - content_rect.top); |
| 33 } | 34 } |
| 34 | 35 |
| 35 } // namespace | 36 } // namespace |
| 36 | 37 |
| 37 namespace ui { | 38 namespace ui { |
| 38 | 39 |
| 39 bool GrabViewSnapshot(gfx::NativeView view_handle, | 40 namespace internal { |
| 40 std::vector<unsigned char>* png_representation, | |
| 41 const gfx::Rect& snapshot_bounds) { | |
| 42 return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds); | |
| 43 } | |
| 44 | 41 |
| 45 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, | 42 bool GrabHwndSnapshot(HWND window_handle, |
| 46 std::vector<unsigned char>* png_representation, | 43 const gfx::Rect& snapshot_bounds, |
| 47 const gfx::Rect& snapshot_bounds) { | 44 std::vector<unsigned char>* png_representation) { |
| 48 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right()); | 45 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right()); |
| 49 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom()); | 46 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom()); |
| 50 | 47 |
| 51 // Create a memory DC that's compatible with the window. | 48 // Create a memory DC that's compatible with the window. |
| 52 HDC window_hdc = GetWindowDC(window_handle); | 49 HDC window_hdc = GetWindowDC(window_handle); |
| 53 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); | 50 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); |
| 54 | 51 |
| 55 BITMAPINFOHEADER hdr; | 52 BITMAPINFOHEADER hdr; |
| 56 gfx::CreateBitmapHeader(snapshot_bounds.width(), | 53 gfx::CreateBitmapHeader(snapshot_bounds.width(), |
| 57 snapshot_bounds.height(), | 54 snapshot_bounds.height(), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 snapshot_bounds.size(), | 93 snapshot_bounds.size(), |
| 97 snapshot_bounds.width() * 4, true, | 94 snapshot_bounds.width() * 4, true, |
| 98 std::vector<gfx::PNGCodec::Comment>(), | 95 std::vector<gfx::PNGCodec::Comment>(), |
| 99 png_representation); | 96 png_representation); |
| 100 | 97 |
| 101 ReleaseDC(window_handle, window_hdc); | 98 ReleaseDC(window_handle, window_hdc); |
| 102 | 99 |
| 103 return true; | 100 return true; |
| 104 } | 101 } |
| 105 | 102 |
| 103 } // namespace internal |
| 104 |
| 105 #if !defined(USE_AURA) |
| 106 bool GrabViewSnapshot(gfx::NativeView view_handle, |
| 107 std::vector<unsigned char>* png_representation, |
| 108 const gfx::Rect& snapshot_bounds) { |
| 109 return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds); |
| 110 } |
| 111 |
| 112 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, |
| 113 std::vector<unsigned char>* png_representation, |
| 114 const gfx::Rect& snapshot_bounds) { |
| 115 DCHECK(window_handle); |
| 116 return internal::GrabHwndSnapshot(window_handle, snapshot_bounds, |
| 117 png_representation); |
| 118 } |
| 119 #endif // !defined(USE_AURA) |
| 120 |
| 106 } // namespace ui | 121 } // namespace ui |
| OLD | NEW |