Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/browser/ui/window_snapshot/window_snapshot_win.cc

Issue 8523022: Change snasphotting API to take in a bounds Rect (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes as requested Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "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 14
15 namespace browser { 15 namespace browser {
16 16
17 namespace {
jonathan.backer 2011/11/14 13:45:02 No need to nest anon namespace. Customary to follo
18 gfx::Rect GetWindowBounds(gfx::NativeWindow window_handle) {
19 RECT content_rect = {0, 0, 0, 0};
20 ::GetWindowRect(window_handle, &content_rect);
21 content_rect.right++; // Match what PrintWindow wants.
22
23 return gfx::Rect(content_rect.right - content_rect.left,
jonathan.backer 2011/11/14 13:45:02 Feel free to ignore, but aren't you really returni
24 content_rect.bottom - content_rect.top);
25 }
26
27 } // namespace
28
17 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window_handle, 29 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window_handle,
18 std::vector<unsigned char>* png_representation) { 30 std::vector<unsigned char>* png_representation) {
31 return GrabWindowSnapshot(window_handle, png_representation,
32 GetWindowBounds(window_handle));
33 }
34
35 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window_handle,
36 std::vector<unsigned char>* png_representation,
37 const gfx::Rect snapshot_bounds) {
38 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right());
39 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom());
40
19 // Create a memory DC that's compatible with the window. 41 // Create a memory DC that's compatible with the window.
20 HDC window_hdc = GetWindowDC(window_handle); 42 HDC window_hdc = GetWindowDC(window_handle);
21 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); 43 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc));
22 44
23 // Create a DIB that's the same size as the window.
24 RECT content_rect = {0, 0, 0, 0};
25 ::GetWindowRect(window_handle, &content_rect);
26 content_rect.right++; // Match what PrintWindow wants.
27 int width = content_rect.right - content_rect.left;
28 int height = content_rect.bottom - content_rect.top;
29 BITMAPINFOHEADER hdr; 45 BITMAPINFOHEADER hdr;
30 gfx::CreateBitmapHeader(width, height, &hdr); 46 gfx::CreateBitmapHeader(snapshot_bounds.width(),
47 snapshot_bounds.height(),
48 &hdr);
31 unsigned char *bit_ptr = NULL; 49 unsigned char *bit_ptr = NULL;
32 base::win::ScopedBitmap bitmap( 50 base::win::ScopedBitmap bitmap(
33 CreateDIBSection(mem_hdc, 51 CreateDIBSection(mem_hdc,
34 reinterpret_cast<BITMAPINFO*>(&hdr), 52 reinterpret_cast<BITMAPINFO*>(&hdr),
35 DIB_RGB_COLORS, 53 DIB_RGB_COLORS,
36 reinterpret_cast<void **>(&bit_ptr), 54 reinterpret_cast<void **>(&bit_ptr),
37 NULL, 0)); 55 NULL, 0));
38 56
39 base::win::ScopedSelectObject select_bitmap(mem_hdc, bitmap); 57 base::win::ScopedSelectObject select_bitmap(mem_hdc, bitmap);
40 // Clear the bitmap to white (so that rounded corners on windows 58 // Clear the bitmap to white (so that rounded corners on windows
41 // show up on a white background, and strangely-shaped windows 59 // show up on a white background, and strangely-shaped windows
42 // look reasonable). Not capturing an alpha mask saves a 60 // look reasonable). Not capturing an alpha mask saves a
43 // bit of space. 61 // bit of space.
44 PatBlt(mem_hdc, 0, 0, width, height, WHITENESS); 62 PatBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(),
63 WHITENESS);
45 // Grab a copy of the window 64 // Grab a copy of the window
46 // First, see if PrintWindow is defined (it's not in Windows 2000). 65 // First, see if PrintWindow is defined (it's not in Windows 2000).
47 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); 66 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT);
48 PrintWindowPointer print_window = 67 PrintWindowPointer print_window =
49 reinterpret_cast<PrintWindowPointer>( 68 reinterpret_cast<PrintWindowPointer>(
50 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); 69 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow"));
51 70
52 // If PrintWindow is defined, use it. It will work on partially 71 // If PrintWindow is defined, use it. It will work on partially
53 // obscured windows, and works better for out of process sub-windows. 72 // obscured windows, and works better for out of process sub-windows.
54 // Otherwise grab the bits we can get with BitBlt; it's better 73 // Otherwise grab the bits we can get with BitBlt; it's better
55 // than nothing and will work fine in the average case (window is 74 // than nothing and will work fine in the average case (window is
56 // completely on screen). 75 // completely on screen).
57 if (print_window) 76 if (snapshot_bounds.origin() == gfx::Point() && print_window)
58 (*print_window)(window_handle, mem_hdc, 0); 77 (*print_window)(window_handle, mem_hdc, 0);
59 else 78 else
60 BitBlt(mem_hdc, 0, 0, width, height, window_hdc, 0, 0, SRCCOPY); 79 BitBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(),
80 window_hdc, snapshot_bounds.x(), snapshot_bounds.y(), SRCCOPY);
61 81
62 // We now have a copy of the window contents in a DIB, so 82 // We now have a copy of the window contents in a DIB, so
63 // encode it into a useful format for posting to the bug report 83 // encode it into a useful format for posting to the bug report
64 // server. 84 // server.
65 gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA, 85 gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA,
66 gfx::Size(width, height), width * 4, true, 86 snapshot_bounds.size(),
87 snapshot_bounds.width() * 4, true,
67 std::vector<gfx::PNGCodec::Comment>(), 88 std::vector<gfx::PNGCodec::Comment>(),
68 png_representation); 89 png_representation);
69 90
70 ReleaseDC(window_handle, window_hdc); 91 ReleaseDC(window_handle, window_hdc);
71 92
72 return gfx::Rect(width, height); 93 return gfx::Rect(snapshot_bounds.size());
73 } 94 }
74 95
75 } // namespace browser 96 } // namespace browser
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698