[email protected] | c9bc8f1 | 2012-12-15 22:20:09 | [diff] [blame] | 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 | |
| 5 | #include "ui/snapshot/snapshot.h" |
| 6 | |
dcheng | 18ec0b54 | 2016-04-26 19:28:53 | [diff] [blame] | 7 | #include <memory> |
dcheng | 4de264f | 2015-12-30 02:07:29 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 10 | #include "base/bind.h" |
danakj | f20f450 | 2017-09-26 17:13:31 | [diff] [blame] | 11 | #include "components/viz/common/frame_sinks/copy_output_request.h" |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 12 | #include "third_party/skia/include/core/SkBitmap.h" |
jaekyun | e4f9eed | 2015-02-24 02:06:58 | [diff] [blame] | 13 | #include "ui/android/view_android.h" |
| 14 | #include "ui/android/window_android.h" |
| 15 | #include "ui/android/window_android_compositor.h" |
jinsukkim | 61bc73e74 | 2017-03-14 00:36:15 | [diff] [blame] | 16 | #include "ui/base/layout.h" |
[email protected] | 1a8376c7 | 2014-06-20 22:37:14 | [diff] [blame] | 17 | #include "ui/gfx/geometry/point_conversions.h" |
[email protected] | cec1232 | 2014-06-17 09:47:56 | [diff] [blame] | 18 | #include "ui/gfx/geometry/rect_conversions.h" |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 19 | #include "ui/snapshot/snapshot_async.h" |
[email protected] | c9bc8f1 | 2012-12-15 22:20:09 | [diff] [blame] | 20 | |
| 21 | namespace ui { |
| 22 | |
jinsukkim | 8f74b8e7 | 2017-01-10 01:35:17 | [diff] [blame] | 23 | // Sync versions are not supported in Android. Callers should fall back |
| 24 | // to the async version. |
[email protected] | c9bc8f1 | 2012-12-15 22:20:09 | [diff] [blame] | 25 | bool GrabViewSnapshot(gfx::NativeView view, |
eseckler | 7233c1a7 | 2017-01-25 15:07:54 | [diff] [blame] | 26 | const gfx::Rect& snapshot_bounds, |
| 27 | gfx::Image* image) { |
| 28 | return GrabWindowSnapshot(view->GetWindowAndroid(), snapshot_bounds, image); |
[email protected] | c9bc8f1 | 2012-12-15 22:20:09 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool GrabWindowSnapshot(gfx::NativeWindow window, |
eseckler | 7233c1a7 | 2017-01-25 15:07:54 | [diff] [blame] | 32 | const gfx::Rect& snapshot_bounds, |
| 33 | gfx::Image* image) { |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | |
Bo Liu | 76c9264 | 2019-03-13 01:18:31 | [diff] [blame] | 37 | static std::unique_ptr<viz::CopyOutputRequest> CreateCopyRequest( |
| 38 | gfx::NativeView view, |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 39 | const gfx::Rect& source_rect, |
Fady Samuel | dfecb7d | 2017-07-26 11:41:04 | [diff] [blame] | 40 | viz::CopyOutputRequest::CopyOutputRequestCallback callback) { |
| 41 | std::unique_ptr<viz::CopyOutputRequest> request = |
Yuri Wiitala | b9ad27a | 2017-09-06 19:13:50 | [diff] [blame] | 42 | std::make_unique<viz::CopyOutputRequest>( |
| 43 | viz::CopyOutputRequest::ResultFormat::RGBA_BITMAP, |
| 44 | std::move(callback)); |
Bo Liu | 76c9264 | 2019-03-13 01:18:31 | [diff] [blame] | 45 | float scale = ui::GetScaleFactorForNativeView(view); |
jinsukkim | 8f74b8e7 | 2017-01-10 01:35:17 | [diff] [blame] | 46 | request->set_area(gfx::ScaleToEnclosingRect(source_rect, scale)); |
Bo Liu | 76c9264 | 2019-03-13 01:18:31 | [diff] [blame] | 47 | return request; |
| 48 | } |
| 49 | |
| 50 | static void MakeAsyncCopyRequest( |
| 51 | gfx::NativeWindow window, |
| 52 | const gfx::Rect& source_rect, |
| 53 | std::unique_ptr<viz::CopyOutputRequest> copy_request) { |
| 54 | if (!window->GetCompositor()) |
| 55 | return; |
| 56 | window->GetCompositor()->RequestCopyOfOutputOnRootLayer( |
| 57 | std::move(copy_request)); |
[email protected] | c9bc8f1 | 2012-12-15 22:20:09 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | c051d5a5 | 2014-01-08 23:54:51 | [diff] [blame] | 60 | void GrabWindowSnapshotAndScaleAsync( |
[email protected] | 75a3f1b | 2013-12-27 00:13:43 | [diff] [blame] | 61 | gfx::NativeWindow window, |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 62 | const gfx::Rect& source_rect, |
[email protected] | 75a3f1b | 2013-12-27 00:13:43 | [diff] [blame] | 63 | const gfx::Size& target_size, |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 64 | const GrabWindowSnapshotAsyncCallback& callback) { |
Bo Liu | 76c9264 | 2019-03-13 01:18:31 | [diff] [blame] | 65 | MakeAsyncCopyRequest( |
| 66 | window, source_rect, |
| 67 | CreateCopyRequest(window, source_rect, |
| 68 | base::BindOnce(&SnapshotAsync::ScaleCopyOutputResult, |
| 69 | callback, target_size))); |
[email protected] | 75a3f1b | 2013-12-27 00:13:43 | [diff] [blame] | 70 | } |
| 71 | |
eseckler | 7233c1a7 | 2017-01-25 15:07:54 | [diff] [blame] | 72 | void GrabWindowSnapshotAsync(gfx::NativeWindow window, |
| 73 | const gfx::Rect& source_rect, |
| 74 | const GrabWindowSnapshotAsyncCallback& callback) { |
| 75 | MakeAsyncCopyRequest( |
| 76 | window, source_rect, |
Bo Liu | 76c9264 | 2019-03-13 01:18:31 | [diff] [blame] | 77 | CreateCopyRequest( |
| 78 | window, source_rect, |
| 79 | base::BindOnce(&SnapshotAsync::RunCallbackWithCopyOutputResult, |
| 80 | callback))); |
[email protected] | 2e6f39e | 2014-05-15 19:36:36 | [diff] [blame] | 81 | } |
| 82 | |
eseckler | 7233c1a7 | 2017-01-25 15:07:54 | [diff] [blame] | 83 | void GrabViewSnapshotAsync(gfx::NativeView view, |
| 84 | const gfx::Rect& source_rect, |
| 85 | const GrabWindowSnapshotAsyncCallback& callback) { |
Bo Liu | 76c9264 | 2019-03-13 01:18:31 | [diff] [blame] | 86 | std::unique_ptr<viz::CopyOutputRequest> copy_request = |
| 87 | view->MaybeRequestCopyOfView(CreateCopyRequest( |
| 88 | view, source_rect, |
| 89 | base::BindOnce(&SnapshotAsync::RunCallbackWithCopyOutputResult, |
| 90 | callback))); |
| 91 | if (!copy_request) |
| 92 | return; |
| 93 | |
| 94 | MakeAsyncCopyRequest(view->GetWindowAndroid(), source_rect, |
| 95 | std::move(copy_request)); |
[email protected] | c051d5a5 | 2014-01-08 23:54:51 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | c9bc8f1 | 2012-12-15 22:20:09 | [diff] [blame] | 98 | } // namespace ui |