OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_win.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/task_runner.h" |
| 9 #include "base/win/windows_version.h" |
| 10 #include "skia/ext/platform_canvas.h" |
| 11 #include "skia/ext/skia_utils_win.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/geometry/size.h" |
| 16 #include "ui/gfx/image/image.h" |
| 17 #include "ui/snapshot/snapshot.h" |
| 18 #include "ui/snapshot/snapshot_aura.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Windows 8.1 is the first version that supports PW_RENDERFULLCONTENT. |
| 23 // Without that flag PrintWindow may not correctly capture what's actually |
| 24 // onscreen. |
| 25 bool UseAuraSnapshot() { |
| 26 return (base::win::GetVersion() < base::win::VERSION_WIN8_1); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 namespace ui { |
| 32 |
| 33 namespace internal { |
| 34 |
| 35 bool GrabHwndSnapshot(HWND window_handle, |
| 36 const gfx::Rect& snapshot_bounds_in_pixels, |
| 37 const gfx::Rect& clip_rect_in_pixels, |
| 38 gfx::Image* image) { |
| 39 gfx::Rect snapshot_bounds_in_window = |
| 40 snapshot_bounds_in_pixels + clip_rect_in_pixels.OffsetFromOrigin(); |
| 41 gfx::Size bitmap_size(snapshot_bounds_in_window.right(), |
| 42 snapshot_bounds_in_window.bottom()); |
| 43 |
| 44 std::unique_ptr<SkCanvas> canvas = skia::CreatePlatformCanvas( |
| 45 bitmap_size.width(), bitmap_size.height(), false); |
| 46 HDC mem_hdc = skia::GetNativeDrawingContext(canvas.get()); |
| 47 |
| 48 // Grab a copy of the window. Use PrintWindow because it works even when the |
| 49 // window's partially occluded. The PW_RENDERFULLCONTENT flag is undocumented, |
| 50 // but works starting in Windows 8.1. It allows for capturing the contents of |
| 51 // the window that are drawn using DirectComposition. |
| 52 UINT flags = PW_CLIENTONLY | PW_RENDERFULLCONTENT; |
| 53 |
| 54 BOOL result = PrintWindow(window_handle, mem_hdc, flags); |
| 55 if (!result) { |
| 56 PLOG(ERROR) << "Failed to print window"; |
| 57 return false; |
| 58 } |
| 59 |
| 60 SkBitmap bitmap; |
| 61 canvas->readPixels(gfx::RectToSkIRect(snapshot_bounds_in_window), &bitmap); |
| 62 |
| 63 // Clear the region of the bitmap outside the clip rect to white. |
| 64 SkCanvas image_canvas(bitmap); |
| 65 SkPaint paint; |
| 66 paint.setColor(SK_ColorWHITE); |
| 67 |
| 68 SkRegion region; |
| 69 gfx::Rect clip_in_bitmap(clip_rect_in_pixels.size()); |
| 70 clip_in_bitmap.Offset(-snapshot_bounds_in_pixels.OffsetFromOrigin()); |
| 71 region.setRect( |
| 72 gfx::RectToSkIRect(gfx::Rect(snapshot_bounds_in_pixels.size()))); |
| 73 region.op(gfx::RectToSkIRect(clip_in_bitmap), SkRegion::kDifference_Op); |
| 74 image_canvas.drawRegion(region, paint); |
| 75 |
| 76 *image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 77 |
| 78 return true; |
| 79 } |
| 80 |
| 81 } // namespace internal |
| 82 |
| 83 bool GrabViewSnapshot(gfx::NativeView view_handle, |
| 84 const gfx::Rect& snapshot_bounds, |
| 85 gfx::Image* image) { |
| 86 return GrabWindowSnapshot(view_handle, snapshot_bounds, image); |
| 87 } |
| 88 |
| 89 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, |
| 90 const gfx::Rect& snapshot_bounds, |
| 91 gfx::Image* image) { |
| 92 if (UseAuraSnapshot()) { |
| 93 // Not supported in Aura. Callers should fall back to the async version. |
| 94 return false; |
| 95 } |
| 96 |
| 97 DCHECK(window_handle); |
| 98 gfx::Rect window_bounds = window_handle->GetBoundsInRootWindow(); |
| 99 aura::WindowTreeHost* host = window_handle->GetHost(); |
| 100 DCHECK(host); |
| 101 HWND hwnd = host->GetAcceleratedWidget(); |
| 102 |
| 103 gfx::RectF window_bounds_in_pixels(window_bounds); |
| 104 host->GetRootTransform().TransformRect(&window_bounds_in_pixels); |
| 105 gfx::RectF snapshot_bounds_in_pixels(snapshot_bounds); |
| 106 host->GetRootTransform().TransformRect(&snapshot_bounds_in_pixels); |
| 107 |
| 108 gfx::Rect expanded_window_bounds_in_pixels = |
| 109 gfx::ToEnclosingRect(window_bounds_in_pixels); |
| 110 RECT client_area; |
| 111 ::GetClientRect(hwnd, &client_area); |
| 112 gfx::Rect client_area_rect(client_area); |
| 113 client_area_rect.set_origin(gfx::Point()); |
| 114 |
| 115 expanded_window_bounds_in_pixels.Intersect(client_area_rect); |
| 116 |
| 117 return internal::GrabHwndSnapshot( |
| 118 hwnd, gfx::ToEnclosingRect(snapshot_bounds_in_pixels), |
| 119 expanded_window_bounds_in_pixels, image); |
| 120 } |
| 121 |
| 122 void GrabWindowSnapshotAsync(gfx::NativeWindow window, |
| 123 const gfx::Rect& source_rect, |
| 124 const GrabWindowSnapshotAsyncCallback& callback) { |
| 125 if (UseAuraSnapshot()) { |
| 126 GrabWindowSnapshotAsyncAura(window, source_rect, callback); |
| 127 return; |
| 128 } |
| 129 gfx::Image image; |
| 130 GrabWindowSnapshot(window, source_rect, &image); |
| 131 callback.Run(image); |
| 132 } |
| 133 |
| 134 void GrabViewSnapshotAsync(gfx::NativeView view, |
| 135 const gfx::Rect& source_rect, |
| 136 const GrabWindowSnapshotAsyncCallback& callback) { |
| 137 if (UseAuraSnapshot()) { |
| 138 GrabWindowSnapshotAsyncAura(view, source_rect, callback); |
| 139 return; |
| 140 } |
| 141 NOTIMPLEMENTED(); |
| 142 callback.Run(gfx::Image()); |
| 143 } |
| 144 |
| 145 void GrabWindowSnapshotAndScaleAsync( |
| 146 gfx::NativeWindow window, |
| 147 const gfx::Rect& source_rect, |
| 148 const gfx::Size& target_size, |
| 149 scoped_refptr<base::TaskRunner> background_task_runner, |
| 150 const GrabWindowSnapshotAsyncCallback& callback) { |
| 151 if (UseAuraSnapshot()) { |
| 152 GrabWindowSnapshotAndScaleAsyncAura(window, source_rect, target_size, |
| 153 background_task_runner, callback); |
| 154 return; |
| 155 } |
| 156 NOTIMPLEMENTED(); |
| 157 callback.Run(gfx::Image()); |
| 158 } |
| 159 |
| 160 } // namespace ui |
OLD | NEW |