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_aura.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/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
13 #include "cc/output/copy_output_request.h" | 13 #include "cc/output/copy_output_request.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
16 #include "ui/aura/window_tracker.h" | 16 #include "ui/aura/window_tracker.h" |
17 #include "ui/compositor/compositor.h" | 17 #include "ui/compositor/compositor.h" |
18 #include "ui/compositor/dip_util.h" | 18 #include "ui/compositor/dip_util.h" |
19 #include "ui/compositor/layer.h" | 19 #include "ui/compositor/layer.h" |
20 #include "ui/snapshot/snapshot_async.h" | 20 #include "ui/snapshot/snapshot_async.h" |
21 | 21 |
22 namespace ui { | 22 namespace ui { |
23 | 23 |
24 bool GrabViewSnapshot(gfx::NativeView view, | 24 bool GrabWindowSnapshotAura(aura::Window* window, |
25 const gfx::Rect& snapshot_bounds, | 25 const gfx::Rect& snapshot_bounds, |
26 gfx::Image* image) { | 26 gfx::Image* image) { |
27 return GrabWindowSnapshot(view, snapshot_bounds, image); | |
28 } | |
29 | |
30 bool GrabWindowSnapshot(gfx::NativeWindow window, | |
31 const gfx::Rect& snapshot_bounds, | |
32 gfx::Image* image) { | |
33 // Not supported in Aura. Callers should fall back to the async version. | 27 // Not supported in Aura. Callers should fall back to the async version. |
34 return false; | 28 return false; |
35 } | 29 } |
36 | 30 |
37 static void MakeAsyncCopyRequest( | 31 static void MakeAsyncCopyRequest( |
38 gfx::NativeWindow window, | 32 aura::Window* window, |
39 const gfx::Rect& source_rect, | 33 const gfx::Rect& source_rect, |
40 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { | 34 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { |
41 std::unique_ptr<cc::CopyOutputRequest> request = | 35 std::unique_ptr<cc::CopyOutputRequest> request = |
42 cc::CopyOutputRequest::CreateBitmapRequest(callback); | 36 cc::CopyOutputRequest::CreateBitmapRequest(callback); |
43 request->set_area(source_rect); | 37 request->set_area(source_rect); |
44 window->layer()->RequestCopyOfOutput(std::move(request)); | 38 window->layer()->RequestCopyOfOutput(std::move(request)); |
45 } | 39 } |
46 | 40 |
47 static void FinishedAsyncCopyRequest( | 41 static void FinishedAsyncCopyRequest( |
48 std::unique_ptr<aura::WindowTracker> tracker, | 42 std::unique_ptr<aura::WindowTracker> tracker, |
49 const gfx::Rect& source_rect, | 43 const gfx::Rect& source_rect, |
50 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback, | 44 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback, |
51 int retry_count, | 45 int retry_count, |
52 std::unique_ptr<cc::CopyOutputResult> result) { | 46 std::unique_ptr<cc::CopyOutputResult> result) { |
53 static const int kMaxRetries = 5; | 47 static const int kMaxRetries = 5; |
54 // Retry the copy request if the previous one failed for some reason. | 48 // Retry the copy request if the previous one failed for some reason. |
55 if (!tracker->windows().empty() && (retry_count < kMaxRetries) && | 49 if (!tracker->windows().empty() && (retry_count < kMaxRetries) && |
56 result->IsEmpty()) { | 50 result->IsEmpty()) { |
57 // Look up window before calling MakeAsyncRequest. Otherwise, due | 51 // Look up window before calling MakeAsyncRequest. Otherwise, due |
58 // to undefined (favorably right to left) argument evaluation | 52 // to undefined (favorably right to left) argument evaluation |
59 // order, the tracker might have been passed and set to NULL | 53 // order, the tracker might have been passed and set to NULL |
60 // before the window is looked up which results in a NULL pointer | 54 // before the window is looked up which results in a NULL pointer |
61 // dereference. | 55 // dereference. |
62 gfx::NativeWindow window = tracker->windows()[0]; | 56 aura::Window* window = tracker->windows()[0]; |
63 MakeAsyncCopyRequest( | 57 MakeAsyncCopyRequest( |
64 window, source_rect, | 58 window, source_rect, |
65 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&tracker), | 59 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&tracker), |
66 source_rect, callback, retry_count + 1)); | 60 source_rect, callback, retry_count + 1)); |
67 return; | 61 return; |
68 } | 62 } |
69 | 63 |
70 callback.Run(std::move(result)); | 64 callback.Run(std::move(result)); |
71 } | 65 } |
72 | 66 |
73 static void MakeInitialAsyncCopyRequest( | 67 static void MakeInitialAsyncCopyRequest( |
74 gfx::NativeWindow window, | 68 aura::Window* window, |
75 const gfx::Rect& source_rect, | 69 const gfx::Rect& source_rect, |
76 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { | 70 const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) { |
77 auto tracker = base::MakeUnique<aura::WindowTracker>(); | 71 auto tracker = base::MakeUnique<aura::WindowTracker>(); |
78 tracker->Add(window); | 72 tracker->Add(window); |
79 MakeAsyncCopyRequest( | 73 MakeAsyncCopyRequest( |
80 window, source_rect, | 74 window, source_rect, |
81 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&tracker), source_rect, | 75 base::Bind(&FinishedAsyncCopyRequest, base::Passed(&tracker), source_rect, |
82 callback, 0)); | 76 callback, 0)); |
83 } | 77 } |
84 | 78 |
85 void GrabWindowSnapshotAndScaleAsync( | 79 void GrabWindowSnapshotAndScaleAsyncAura( |
86 gfx::NativeWindow window, | 80 aura::Window* window, |
87 const gfx::Rect& source_rect, | 81 const gfx::Rect& source_rect, |
88 const gfx::Size& target_size, | 82 const gfx::Size& target_size, |
89 scoped_refptr<base::TaskRunner> background_task_runner, | 83 scoped_refptr<base::TaskRunner> background_task_runner, |
90 const GrabWindowSnapshotAsyncCallback& callback) { | 84 const GrabWindowSnapshotAsyncCallback& callback) { |
91 MakeInitialAsyncCopyRequest( | 85 MakeInitialAsyncCopyRequest( |
92 window, source_rect, | 86 window, source_rect, |
93 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, callback, target_size, | 87 base::Bind(&SnapshotAsync::ScaleCopyOutputResult, callback, target_size, |
94 background_task_runner)); | 88 background_task_runner)); |
95 } | 89 } |
96 | 90 |
| 91 void GrabWindowSnapshotAsyncAura( |
| 92 aura::Window* window, |
| 93 const gfx::Rect& source_rect, |
| 94 const GrabWindowSnapshotAsyncCallback& callback) { |
| 95 MakeInitialAsyncCopyRequest( |
| 96 window, source_rect, |
| 97 base::Bind(&SnapshotAsync::RunCallbackWithCopyOutputResult, callback)); |
| 98 } |
| 99 |
| 100 #if !defined(OS_WIN) |
| 101 bool GrabWindowSnapshot(gfx::NativeWindow window, |
| 102 const gfx::Rect& snapshot_bounds, |
| 103 gfx::Image* image) { |
| 104 // Not supported in Aura. Callers should fall back to the async version. |
| 105 return false; |
| 106 } |
| 107 |
| 108 bool GrabViewSnapshot(gfx::NativeView view, |
| 109 const gfx::Rect& snapshot_bounds, |
| 110 gfx::Image* image) { |
| 111 return GrabWindowSnapshot(view, snapshot_bounds, image); |
| 112 } |
| 113 |
| 114 void GrabWindowSnapshotAndScaleAsync( |
| 115 gfx::NativeWindow window, |
| 116 const gfx::Rect& source_rect, |
| 117 const gfx::Size& target_size, |
| 118 scoped_refptr<base::TaskRunner> background_task_runner, |
| 119 const GrabWindowSnapshotAsyncCallback& callback) { |
| 120 GrabWindowSnapshotAndScaleAsyncAura(window, source_rect, target_size, |
| 121 background_task_runner, callback); |
| 122 } |
| 123 |
97 void GrabWindowSnapshotAsync(gfx::NativeWindow window, | 124 void GrabWindowSnapshotAsync(gfx::NativeWindow window, |
98 const gfx::Rect& source_rect, | 125 const gfx::Rect& source_rect, |
99 const GrabWindowSnapshotAsyncCallback& callback) { | 126 const GrabWindowSnapshotAsyncCallback& callback) { |
100 MakeInitialAsyncCopyRequest( | 127 GrabWindowSnapshotAsyncAura(window, source_rect, callback); |
101 window, source_rect, | |
102 base::Bind(&SnapshotAsync::RunCallbackWithCopyOutputResult, callback)); | |
103 } | 128 } |
104 | 129 |
105 void GrabViewSnapshotAsync(gfx::NativeView view, | 130 void GrabViewSnapshotAsync(gfx::NativeView view, |
106 const gfx::Rect& source_rect, | 131 const gfx::Rect& source_rect, |
107 const GrabWindowSnapshotAsyncCallback& callback) { | 132 const GrabWindowSnapshotAsyncCallback& callback) { |
108 GrabWindowSnapshotAsync(view, source_rect, callback); | 133 GrabWindowSnapshotAsyncAura(view, source_rect, callback); |
109 } | 134 } |
110 | 135 |
| 136 #endif |
111 | 137 |
112 } // namespace ui | 138 } // namespace ui |
OLD | NEW |