snapshot_android.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  3. // found in the LICENSE file.
  4. #include "ui/snapshot/snapshot.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "components/viz/common/frame_sinks/copy_output_request.h"
  9. #include "third_party/skia/include/core/SkBitmap.h"
  10. #include "ui/android/view_android.h"
  11. #include "ui/android/window_android.h"
  12. #include "ui/android/window_android_compositor.h"
  13. #include "ui/base/layout.h"
  14. #include "ui/gfx/geometry/point_conversions.h"
  15. #include "ui/gfx/geometry/rect_conversions.h"
  16. #include "ui/snapshot/snapshot_async.h"
  17. namespace ui {
  18. // Sync versions are not supported in Android. Callers should fall back
  19. // to the async version.
  20. bool GrabViewSnapshot(gfx::NativeView view,
  21. const gfx::Rect& snapshot_bounds,
  22. gfx::Image* image) {
  23. return GrabWindowSnapshot(view->GetWindowAndroid(), snapshot_bounds, image);
  24. }
  25. bool GrabWindowSnapshot(gfx::NativeWindow window,
  26. const gfx::Rect& snapshot_bounds,
  27. gfx::Image* image) {
  28. return false;
  29. }
  30. static std::unique_ptr<viz::CopyOutputRequest> CreateCopyRequest(
  31. gfx::NativeView view,
  32. const gfx::Rect& source_rect,
  33. viz::CopyOutputRequest::CopyOutputRequestCallback callback) {
  34. std::unique_ptr<viz::CopyOutputRequest> request =
  35. std::make_unique<viz::CopyOutputRequest>(
  36. viz::CopyOutputRequest::ResultFormat::RGBA,
  37. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  38. std::move(callback));
  39. float scale = ui::GetScaleFactorForNativeView(view);
  40. request->set_area(gfx::ScaleToEnclosingRect(source_rect, scale));
  41. return request;
  42. }
  43. static void MakeAsyncCopyRequest(
  44. gfx::NativeWindow window,
  45. const gfx::Rect& source_rect,
  46. std::unique_ptr<viz::CopyOutputRequest> copy_request) {
  47. if (!window->GetCompositor())
  48. return;
  49. window->GetCompositor()->RequestCopyOfOutputOnRootLayer(
  50. std::move(copy_request));
  51. }
  52. void GrabWindowSnapshotAndScaleAsync(gfx::NativeWindow window,
  53. const gfx::Rect& source_rect,
  54. const gfx::Size& target_size,
  55. GrabWindowSnapshotAsyncCallback callback) {
  56. MakeAsyncCopyRequest(
  57. window, source_rect,
  58. CreateCopyRequest(window, source_rect,
  59. base::BindOnce(&SnapshotAsync::ScaleCopyOutputResult,
  60. std::move(callback), target_size)));
  61. }
  62. void GrabWindowSnapshotAsync(gfx::NativeWindow window,
  63. const gfx::Rect& source_rect,
  64. GrabWindowSnapshotAsyncCallback callback) {
  65. MakeAsyncCopyRequest(
  66. window, source_rect,
  67. CreateCopyRequest(
  68. window, source_rect,
  69. base::BindOnce(&SnapshotAsync::RunCallbackWithCopyOutputResult,
  70. std::move(callback))));
  71. }
  72. void GrabViewSnapshotAsync(gfx::NativeView view,
  73. const gfx::Rect& source_rect,
  74. GrabWindowSnapshotAsyncCallback callback) {
  75. std::unique_ptr<viz::CopyOutputRequest> copy_request =
  76. view->MaybeRequestCopyOfView(CreateCopyRequest(
  77. view, source_rect,
  78. base::BindOnce(&SnapshotAsync::RunCallbackWithCopyOutputResult,
  79. std::move(callback))));
  80. if (!copy_request)
  81. return;
  82. MakeAsyncCopyRequest(view->GetWindowAndroid(), source_rect,
  83. std::move(copy_request));
  84. }
  85. } // namespace ui