snapshot_win.cc 5.6 KB

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