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_observer.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 |
23 namespace { | |
24 | |
25 class WindowCapturer : public aura::WindowObserver { | |
26 public: | |
27 WindowCapturer(aura::Window* window) : window_(window) { | |
sadrul
2016/09/29 04:45:09
explicit
| |
28 window_->AddObserver(this); | |
29 } | |
30 ~WindowCapturer() override { | |
31 if (window_) | |
32 window_->RemoveObserver(this); | |
33 } | |
34 | |
35 void OnWindowDestroyed(aura::Window* window) override { window_ = nullptr; } | |
36 aura::Window* window() const { return window_; } | |
37 | |
38 private: | |
39 aura::Window* window_; | |
sadrul
2016/09/29 04:45:09
DISALLOW_COPY_AND_ASSIGN
| |
40 }; | |
41 | |
42 } // namespace | |
43 | |
22 bool GrabViewSnapshot(gfx::NativeView view, | 44 bool GrabViewSnapshot(gfx::NativeView view, |
23 std::vector<unsigned char>* png_representation, | 45 std::vector<unsigned char>* png_representation, |
24 const gfx::Rect& snapshot_bounds) { | 46 const gfx::Rect& snapshot_bounds) { |
25 return GrabWindowSnapshot(view, png_representation, snapshot_bounds); | 47 return GrabWindowSnapshot(view, png_representation, snapshot_bounds); |
26 } | 48 } |
27 | 49 |
28 bool GrabWindowSnapshot(gfx::NativeWindow window, | 50 bool GrabWindowSnapshot(gfx::NativeWindow window, |
29 std::vector<unsigned char>* png_representation, | 51 std::vector<unsigned char>* png_representation, |
30 const gfx::Rect& snapshot_bounds) { | 52 const gfx::Rect& snapshot_bounds) { |
31 // Not supported in Aura. Callers should fall back to the async version. | 53 // Not supported in Aura. Callers should fall back to the async version. |
32 return false; | 54 return false; |
33 } | 55 } |
34 | 56 |
35 static void MakeAsyncCopyRequest( | 57 static void MakeAsyncCopyRequest( |
36 gfx::NativeWindow window, | 58 gfx::NativeWindow window, |
37 const gfx::Rect& source_rect, | 59 const gfx::Rect& source_rect, |
38 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { | 60 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { |
39 std::unique_ptr<cc::CopyOutputRequest> request = | 61 std::unique_ptr<cc::CopyOutputRequest> request = |
40 cc::CopyOutputRequest::CreateBitmapRequest(callback); | 62 cc::CopyOutputRequest::CreateBitmapRequest(callback); |
41 request->set_area(source_rect); | 63 request->set_area(source_rect); |
42 window->layer()->RequestCopyOfOutput(std::move(request)); | 64 window->layer()->RequestCopyOfOutput(std::move(request)); |
43 } | 65 } |
44 | 66 |
67 static void FinishedAsyncCopyRequest( | |
68 std::unique_ptr<WindowCapturer> capturer, | |
69 const gfx::Rect& source_rect, | |
70 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback, | |
71 int retry_count, | |
72 std::unique_ptr<cc::CopyOutputResult> result) { | |
73 static const int kMaxRetries = 5; | |
74 aura::Window* window = capturer->window(); | |
75 | |
76 // Retry the copy request if the previous one failed for some reason. | |
77 if (window && (retry_count < kMaxRetries) && result->IsEmpty()) { | |
78 MakeAsyncCopyRequest( | |
sadrul
2016/09/29 04:45:09
Do we want to delay this request? Does the failure
| |
79 window, source_rect, | |
80 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&capturer), | |
81 source_rect, callback, retry_count + 1)); | |
82 return; | |
83 } | |
84 | |
85 callback.Run(std::move(result)); | |
86 } | |
87 | |
88 static void MakeInitialAsyncCopyRequest( | |
89 gfx::NativeWindow window, | |
90 const gfx::Rect& source_rect, | |
91 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { | |
92 std::unique_ptr<WindowCapturer> capturer = | |
93 base::MakeUnique<WindowCapturer>(window); | |
sadrul
2016/09/29 04:45:09
You can use aura::WindowTracker if you want instea
| |
94 MakeAsyncCopyRequest( | |
95 window, source_rect, | |
96 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&capturer), | |
97 source_rect, callback, 0)); | |
98 } | |
99 | |
45 void GrabWindowSnapshotAndScaleAsync( | 100 void GrabWindowSnapshotAndScaleAsync( |
46 gfx::NativeWindow window, | 101 gfx::NativeWindow window, |
47 const gfx::Rect& source_rect, | 102 const gfx::Rect& source_rect, |
48 const gfx::Size& target_size, | 103 const gfx::Size& target_size, |
49 scoped_refptr<base::TaskRunner> background_task_runner, | 104 scoped_refptr<base::TaskRunner> background_task_runner, |
50 const GrabWindowSnapshotAsyncCallback& callback) { | 105 const GrabWindowSnapshotAsyncCallback& callback) { |
51 MakeAsyncCopyRequest(window, | 106 MakeInitialAsyncCopyRequest( |
52 source_rect, | 107 window, source_rect, |
53 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, | 108 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, callback, target_size, |
54 callback, | 109 background_task_runner)); |
55 target_size, | |
56 background_task_runner)); | |
57 } | 110 } |
58 | 111 |
59 void GrabWindowSnapshotAsync( | 112 void GrabWindowSnapshotAsync( |
60 gfx::NativeWindow window, | 113 gfx::NativeWindow window, |
61 const gfx::Rect& source_rect, | 114 const gfx::Rect& source_rect, |
62 scoped_refptr<base::TaskRunner> background_task_runner, | 115 scoped_refptr<base::TaskRunner> background_task_runner, |
63 const GrabWindowSnapshotAsyncPNGCallback& callback) { | 116 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
64 MakeAsyncCopyRequest(window, | 117 MakeInitialAsyncCopyRequest(window, source_rect, |
65 source_rect, | 118 base::Bind(&SnapshotAsync::EncodeCopyOutputResult, |
66 base::Bind(&SnapshotAsync::EncodeCopyOutputResult, | 119 callback, background_task_runner)); |
67 callback, | |
68 background_task_runner)); | |
69 } | 120 } |
70 | 121 |
71 void GrabViewSnapshotAsync( | 122 void GrabViewSnapshotAsync( |
72 gfx::NativeView view, | 123 gfx::NativeView view, |
73 const gfx::Rect& source_rect, | 124 const gfx::Rect& source_rect, |
74 scoped_refptr<base::TaskRunner> background_task_runner, | 125 scoped_refptr<base::TaskRunner> background_task_runner, |
75 const GrabWindowSnapshotAsyncPNGCallback& callback) { | 126 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
76 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback); | 127 GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback); |
77 } | 128 } |
78 | 129 |
79 | 130 |
80 } // namespace ui | 131 } // namespace ui |
OLD | NEW |