OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
12 #include "cc/output/copy_output_request.h" | 12 #include "cc/output/copy_output_request.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
| 15 #include "ui/aura/window_tracker.h" |
15 #include "ui/compositor/compositor.h" | 16 #include "ui/compositor/compositor.h" |
16 #include "ui/compositor/dip_util.h" | 17 #include "ui/compositor/dip_util.h" |
17 #include "ui/compositor/layer.h" | 18 #include "ui/compositor/layer.h" |
18 #include "ui/snapshot/snapshot_async.h" | 19 #include "ui/snapshot/snapshot_async.h" |
19 | 20 |
20 namespace ui { | 21 namespace ui { |
21 | 22 |
22 bool GrabViewSnapshot(gfx::NativeView view, | 23 bool GrabViewSnapshot(gfx::NativeView view, |
23 std::vector<unsigned char>* png_representation, | 24 std::vector<unsigned char>* png_representation, |
24 const gfx::Rect& snapshot_bounds) { | 25 const gfx::Rect& snapshot_bounds) { |
(...skipping 10 matching lines...) Expand all Loading... |
35 static void MakeAsyncCopyRequest( | 36 static void MakeAsyncCopyRequest( |
36 gfx::NativeWindow window, | 37 gfx::NativeWindow window, |
37 const gfx::Rect& source_rect, | 38 const gfx::Rect& source_rect, |
38 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { | 39 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { |
39 std::unique_ptr<cc::CopyOutputRequest> request = | 40 std::unique_ptr<cc::CopyOutputRequest> request = |
40 cc::CopyOutputRequest::CreateBitmapRequest(callback); | 41 cc::CopyOutputRequest::CreateBitmapRequest(callback); |
41 request->set_area(source_rect); | 42 request->set_area(source_rect); |
42 window->layer()->RequestCopyOfOutput(std::move(request)); | 43 window->layer()->RequestCopyOfOutput(std::move(request)); |
43 } | 44 } |
44 | 45 |
| 46 static void FinishedAsyncCopyRequest( |
| 47 std::unique_ptr<aura::WindowTracker> tracker, |
| 48 const gfx::Rect& source_rect, |
| 49 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback, |
| 50 int retry_count, |
| 51 std::unique_ptr<cc::CopyOutputResult> result) { |
| 52 static const int kMaxRetries = 5; |
| 53 // Retry the copy request if the previous one failed for some reason. |
| 54 if (!tracker->windows().empty() && (retry_count < kMaxRetries) && |
| 55 result->IsEmpty()) { |
| 56 MakeAsyncCopyRequest( |
| 57 tracker->windows()[0], source_rect, |
| 58 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&tracker), |
| 59 source_rect, callback, retry_count + 1)); |
| 60 return; |
| 61 } |
| 62 |
| 63 callback.Run(std::move(result)); |
| 64 } |
| 65 |
| 66 static void MakeInitialAsyncCopyRequest( |
| 67 gfx::NativeWindow window, |
| 68 const gfx::Rect& source_rect, |
| 69 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { |
| 70 auto tracker = base::MakeUnique<aura::WindowTracker>(); |
| 71 tracker->Add(window); |
| 72 MakeAsyncCopyRequest( |
| 73 window, source_rect, |
| 74 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&tracker), source_rect, |
| 75 callback, 0)); |
| 76 } |
| 77 |
45 void GrabWindowSnapshotAndScaleAsync( | 78 void GrabWindowSnapshotAndScaleAsync( |
46 gfx::NativeWindow window, | 79 gfx::NativeWindow window, |
47 const gfx::Rect& source_rect, | 80 const gfx::Rect& source_rect, |
48 const gfx::Size& target_size, | 81 const gfx::Size& target_size, |
49 scoped_refptr<base::TaskRunner> background_task_runner, | 82 scoped_refptr<base::TaskRunner> background_task_runner, |
50 const GrabWindowSnapshotAsyncCallback& callback) { | 83 const GrabWindowSnapshotAsyncCallback& callback) { |
51 MakeAsyncCopyRequest(window, | 84 MakeInitialAsyncCopyRequest( |
52 source_rect, | 85 window, source_rect, |
53 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, | 86 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, callback, target_size, |
54 callback, | 87 background_task_runner)); |
55 target_size, | |
56 background_task_runner)); | |
57 } | 88 } |
58 | 89 |
59 void GrabWindowSnapshotAsync( | 90 void GrabWindowSnapshotAsync( |
60 gfx::NativeWindow window, | 91 gfx::NativeWindow window, |
61 const gfx::Rect& source_rect, | 92 const gfx::Rect& source_rect, |
62 scoped_refptr<base::TaskRunner> background_task_runner, | 93 scoped_refptr<base::TaskRunner> background_task_runner, |
63 const GrabWindowSnapshotAsyncPNGCallback& callback) { | 94 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
64 MakeAsyncCopyRequest(window, | 95 MakeInitialAsyncCopyRequest(window, source_rect, |
65 source_rect, | 96 base::Bind(&SnapshotAsync::EncodeCopyOutputResult, |
66 base::Bind(&SnapshotAsync::EncodeCopyOutputResult, | 97 callback, background_task_runner)); |
67 callback, | |
68 background_task_runner)); | |
69 } | 98 } |
70 | 99 |
71 void GrabViewSnapshotAsync( | 100 void GrabViewSnapshotAsync( |
72 gfx::NativeView view, | 101 gfx::NativeView view, |
73 const gfx::Rect& source_rect, | 102 const gfx::Rect& source_rect, |
74 scoped_refptr<base::TaskRunner> background_task_runner, | 103 scoped_refptr<base::TaskRunner> background_task_runner, |
75 const GrabWindowSnapshotAsyncPNGCallback& callback) { | 104 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
76 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback); | 105 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback); |
77 } | 106 } |
78 | 107 |
79 | 108 |
80 } // namespace ui | 109 } // namespace ui |
OLD | NEW |