Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: content/browser/media/capture/cursor_renderer_aura.cc

Issue 2553763002: Fix cursor missing in tabCapture on OSX Sierra (Closed)
Patch Set: implement CursorRenderer Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 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 "content/browser/media/capture/cursor_renderer_aura.h" 5 #include "content/browser/media/capture/cursor_renderer_aura.h"
6 6
7 #include <stdint.h>
8
9 #include <algorithm>
10 #include <cmath>
11
12 #include "base/logging.h"
13 #include "base/time/default_tick_clock.h"
14 #include "skia/ext/image_operations.h"
15 #include "ui/aura/client/screen_position_client.h"
16 #include "ui/aura/env.h" 7 #include "ui/aura/env.h"
miu 2016/12/27 23:21:03 Please add: #include "base/memory/ptr_util.h" (s
braveyao 2017/01/04 01:57:49 Done.
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_tree_host.h" 8 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/cursor/cursors_aura.h" 9 #include "ui/base/cursor/cursors_aura.h"
20 #include "ui/compositor/dip_util.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/events/event_utils.h" 10 #include "ui/events/event_utils.h"
23 #include "ui/wm/public/activation_client.h" 11 #include "ui/wm/public/activation_client.h"
24 12
25 namespace content { 13 namespace content {
26 14
27 namespace {
28
29 inline int clip_byte(int x) {
30 return std::max(0, std::min(x, 255));
31 }
32
33 inline int alpha_blend(int alpha, int src, int dst) {
34 return (src * alpha + dst * (255 - alpha)) / 255;
35 }
36
37 } // namespace
38
39 // static 15 // static
40 std::unique_ptr<CursorRenderer> CursorRenderer::Create( 16 std::unique_ptr<CursorRenderer> CursorRenderer::Create(
41 gfx::NativeWindow window) { 17 gfx::NativeWindow window) {
42 return std::unique_ptr<CursorRenderer>( 18 return base::MakeUnique<CursorRendererAura>(window,
43 new CursorRendererAura(window, kCursorEnabledOnMouseMovement)); 19 kCursorEnabledOnMouseMovement);
44 } 20 }
45 21
46 CursorRendererAura::CursorRendererAura( 22 CursorRendererAura::CursorRendererAura(
47 aura::Window* window, 23 aura::Window* window,
48 CursorDisplaySetting cursor_display_setting) 24 CursorDisplaySetting cursor_display_setting)
49 : window_(window), 25 : CursorRenderer(window, cursor_display_setting),
50 cursor_display_setting_(cursor_display_setting), 26 window_(window),
51 tick_clock_(&default_tick_clock_), 27 last_cursor_hot_point_(gfx::Point()),
52 weak_factory_(this) { 28 cursor_display_setting_(cursor_display_setting) {
53 if (window_) { 29 if (window_) {
54 window_->AddObserver(this); 30 window_->AddObserver(this);
55 if (cursor_display_setting == kCursorEnabledOnMouseMovement) 31 if (cursor_display_setting == kCursorEnabledOnMouseMovement)
56 window_->AddPreTargetHandler(this); 32 window_->AddPreTargetHandler(this);
57 } 33 }
58 Clear();
59 } 34 }
60 35
61 CursorRendererAura::~CursorRendererAura() { 36 CursorRendererAura::~CursorRendererAura() {
62 if (window_) { 37 if (window_) {
63 window_->RemoveObserver(this); 38 window_->RemoveObserver(this);
64 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) 39 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement)
miu 2016/12/27 23:21:03 It's acceptable to just always call window_->Remov
braveyao 2017/01/04 01:57:49 Done.
65 window_->RemovePreTargetHandler(this); 40 window_->RemovePreTargetHandler(this);
66 } 41 }
67 } 42 }
68 43
69 base::WeakPtr<CursorRenderer> CursorRendererAura::GetWeakPtr() { 44 bool CursorRendererAura::IsCapturedViewActive() {
miu 2016/12/27 23:21:03 Please add a null-check for |window_| (and return
braveyao 2017/01/04 01:57:49 Done. There is such a check in the beginning of C
70 return weak_factory_.GetWeakPtr();
71 }
72
73 void CursorRendererAura::Clear() {
74 last_cursor_ = ui::Cursor();
75 window_size_when_cursor_last_updated_ = gfx::Size();
76 scaled_cursor_bitmap_.reset();
77 last_mouse_position_x_ = 0;
78 last_mouse_position_y_ = 0;
79 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) {
80 cursor_displayed_ = false;
81 } else {
82 cursor_displayed_ = true;
83 }
84 }
85
86 bool CursorRendererAura::SnapshotCursorState(const gfx::Rect& region_in_frame) {
87 if (!window_) {
88 DVLOG(2) << "Skipping update with no window being tracked";
89 return false;
90 }
91 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
92 gfx::Point cursor_position = aura::Env::GetInstance()->last_mouse_location();
93 if (!window_bounds.Contains(cursor_position)) {
94 // Return early if there is no need to draw the cursor.
95 DVLOG(2) << "Skipping update with cursor outside the window";
96 Clear();
97 return false;
98 }
99
100 // If we are sharing the root window, or namely the whole screen, we will 45 // If we are sharing the root window, or namely the whole screen, we will
101 // render the mouse cursor. For ordinary window, we only render the mouse 46 // render the mouse cursor. For ordinary window, we only render the mouse
102 // cursor when the window is active. 47 // cursor when the window is active.
103 if (!window_->IsRootWindow()) { 48 if (!window_->IsRootWindow()) {
104 aura::client::ActivationClient* activation_client = 49 aura::client::ActivationClient* activation_client =
105 aura::client::GetActivationClient(window_->GetRootWindow()); 50 aura::client::GetActivationClient(window_->GetRootWindow());
106 if (!activation_client) { 51 if (!activation_client) {
107 DVLOG(2) << "Assume window inactive with invalid activation_client"; 52 DVLOG(2) << "Assume window inactive with invalid activation_client";
108 Clear();
109 return false; 53 return false;
110 } 54 }
111 aura::Window* active_window = activation_client->GetActiveWindow(); 55 aura::Window* active_window = activation_client->GetActiveWindow();
112 if (!active_window) { 56 if (!active_window) {
113 DVLOG(2) << "Skipping update as there is no active window"; 57 DVLOG(2) << "Skipping update as there is no active window";
114 Clear();
115 return false; 58 return false;
116 } 59 }
117 if (!active_window->Contains(window_)) { 60 if (!active_window->Contains(window_)) {
118 // Return early if the target window is not active. 61 // Return early if the target window is not active.
119 DVLOG(2) << "Skipping update on an inactive window"; 62 DVLOG(2) << "Skipping update on an inactive window";
120 Clear();
121 return false; 63 return false;
122 } 64 }
123 } 65 }
124
125 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) {
126 if (cursor_displayed_) {
127 // Stop displaying cursor if there has been no mouse movement
128 base::TimeTicks now = tick_clock_->NowTicks();
129 if ((now - last_mouse_movement_timestamp_) >
130 base::TimeDelta::FromSeconds(MAX_IDLE_TIME_SECONDS)) {
131 cursor_displayed_ = false;
132 DVLOG(2) << "Turning off cursor display after idle time";
133 }
134 }
135 if (!cursor_displayed_)
136 return false;
137 }
138
139 gfx::NativeCursor cursor = window_->GetHost()->last_cursor();
140 gfx::Point cursor_hot_point;
141 if (last_cursor_ != cursor ||
142 window_size_when_cursor_last_updated_ != window_bounds.size()) {
143 SkBitmap cursor_bitmap;
144 if (ui::GetCursorBitmap(cursor, &cursor_bitmap, &cursor_hot_point)) {
145 const int scaled_width = cursor_bitmap.width() * region_in_frame.width() /
146 window_bounds.width();
147 const int scaled_height = cursor_bitmap.height() *
148 region_in_frame.height() /
149 window_bounds.height();
150 if (scaled_width <= 0 || scaled_height <= 0) {
151 DVLOG(2) << "scaled_width <= 0";
152 Clear();
153 return false;
154 }
155 scaled_cursor_bitmap_ = skia::ImageOperations::Resize(
156 cursor_bitmap, skia::ImageOperations::RESIZE_BEST, scaled_width,
157 scaled_height);
158 last_cursor_ = cursor;
159 window_size_when_cursor_last_updated_ = window_bounds.size();
160 } else {
161 // Clear cursor state if ui::GetCursorBitmap failed so that we do not
162 // render cursor on the captured frame.
163 Clear();
164 }
165 }
166
167 cursor_position.Offset(-window_bounds.x() - cursor_hot_point.x(),
168 -window_bounds.y() - cursor_hot_point.y());
169 cursor_position_in_frame_ = gfx::Point(
170 region_in_frame.x() +
171 cursor_position.x() * region_in_frame.width() / window_bounds.width(),
172 region_in_frame.y() +
173 cursor_position.y() * region_in_frame.height() /
174 window_bounds.height());
175
176 return true; 66 return true;
177 } 67 }
178 68
179 // Helper function to composite a cursor bitmap on a YUV420 video frame. 69 gfx::Size CursorRendererAura::GetCapturedViewSize() {
180 void CursorRendererAura::RenderOnVideoFrame( 70 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
181 const scoped_refptr<media::VideoFrame>& target) const { 71 const gfx::Size view_size(window_bounds.width(), window_bounds.height());
182 if (scaled_cursor_bitmap_.isNull()) 72 return view_size;
183 return; 73 }
184 74
185 DCHECK(target); 75 gfx::Point CursorRendererAura::GetCursorPositionInView() {
76 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
77 gfx::Point cursor_position = aura::Env::GetInstance()->last_mouse_location();
78 cursor_position.Offset(-window_bounds.x(), -window_bounds.y());
79 return cursor_position;
80 }
186 81
187 gfx::Rect rect = gfx::IntersectRects( 82 gfx::NativeCursor CursorRendererAura::GetLastKnownCursor() {
188 gfx::Rect(scaled_cursor_bitmap_.width(), scaled_cursor_bitmap_.height()) + 83 return window_->GetHost()->last_cursor();
189 gfx::Vector2d(cursor_position_in_frame_.x(), 84 }
190 cursor_position_in_frame_.y()),
191 target->visible_rect());
192 85
193 scaled_cursor_bitmap_.lockPixels(); 86 gfx::Point CursorRendererAura::GetLastKnownCursorHotPoint() {
194 for (int y = rect.y(); y < rect.bottom(); ++y) { 87 return last_cursor_hot_point_;
miu 2016/12/27 23:21:03 The result of this method depends on whether GetLa
braveyao 2017/01/04 01:57:49 Done.
195 int cursor_y = y - cursor_position_in_frame_.y(); 88 }
196 uint8_t* yplane = target->data(media::VideoFrame::kYPlane) +
197 y * target->row_bytes(media::VideoFrame::kYPlane);
198 uint8_t* uplane = target->data(media::VideoFrame::kUPlane) +
199 (y / 2) * target->row_bytes(media::VideoFrame::kUPlane);
200 uint8_t* vplane = target->data(media::VideoFrame::kVPlane) +
201 (y / 2) * target->row_bytes(media::VideoFrame::kVPlane);
202 for (int x = rect.x(); x < rect.right(); ++x) {
203 int cursor_x = x - cursor_position_in_frame_.x();
204 SkColor color = scaled_cursor_bitmap_.getColor(cursor_x, cursor_y);
205 int alpha = SkColorGetA(color);
206 int color_r = SkColorGetR(color);
207 int color_g = SkColorGetG(color);
208 int color_b = SkColorGetB(color);
209 int color_y = clip_byte(
210 ((color_r * 66 + color_g * 129 + color_b * 25 + 128) >> 8) + 16);
211 yplane[x] = alpha_blend(alpha, color_y, yplane[x]);
212 89
213 // Only sample U and V at even coordinates. 90 SkBitmap CursorRendererAura::GetLastKnownCursorImage() {
214 if ((x % 2 == 0) && (y % 2 == 0)) { 91 gfx::NativeCursor cursor = window_->GetHost()->last_cursor();
215 int color_u = clip_byte( 92 SkBitmap cursor_bitmap;
216 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128); 93 ui::GetCursorBitmap(cursor, &cursor_bitmap, &last_cursor_hot_point_);
217 int color_v = clip_byte( 94 return cursor_bitmap;
218 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128);
219 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]);
220 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]);
221 }
222 }
223 }
224 scaled_cursor_bitmap_.unlockPixels();
225 } 95 }
226 96
227 void CursorRendererAura::OnMouseEvent(ui::MouseEvent* event) { 97 void CursorRendererAura::OnMouseEvent(ui::MouseEvent* event) {
98 gfx::Point mouse_location(event->x(), event->y());
228 switch (event->type()) { 99 switch (event->type()) {
229 case ui::ET_MOUSE_MOVED: 100 case ui::ET_MOUSE_MOVED:
230 if (!cursor_displayed_) { 101 OnMouseMoved(mouse_location, event->time_stamp());
231 if (std::abs(event->x() - last_mouse_position_x_) >
232 MIN_MOVEMENT_PIXELS ||
233 std::abs(event->y() - last_mouse_position_y_) > MIN_MOVEMENT_PIXELS)
234 cursor_displayed_ = true;
235 }
236 break; 102 break;
237 case ui::ET_MOUSE_PRESSED: 103 case ui::ET_MOUSE_PRESSED:
238 case ui::ET_MOUSE_RELEASED: 104 case ui::ET_MOUSE_RELEASED:
239 case ui::ET_MOUSEWHEEL: 105 case ui::ET_MOUSEWHEEL:
240 cursor_displayed_ = true; 106 OnMouseClicked(mouse_location, event->time_stamp());
241 break; 107 break;
242 default: 108 default:
243 return; 109 return;
244 } 110 }
245 if (cursor_displayed_) {
246 last_mouse_movement_timestamp_ = event->time_stamp();
247 last_mouse_position_x_ = event->x();
248 last_mouse_position_y_ = event->y();
249 }
250 } 111 }
251 112
252 void CursorRendererAura::OnWindowDestroying(aura::Window* window) { 113 void CursorRendererAura::OnWindowDestroying(aura::Window* window) {
253 DCHECK_EQ(window_, window); 114 DCHECK_EQ(window_, window);
254 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) 115 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement)
255 window_->RemovePreTargetHandler(this); 116 window_->RemovePreTargetHandler(this);
256 window_->RemoveObserver(this); 117 window_->RemoveObserver(this);
257 window_ = nullptr; 118 window_ = nullptr;
258 } 119 }
259 120
260 } // namespace content 121 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698