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

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: rebase Created 3 years, 11 months 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> 7 #include "base/memory/ptr_util.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" 8 #include "ui/aura/env.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_tree_host.h" 9 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/cursor/cursors_aura.h" 10 #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" 11 #include "ui/events/event_utils.h"
23 #include "ui/wm/public/activation_client.h" 12 #include "ui/wm/public/activation_client.h"
24 13
25 namespace content { 14 namespace content {
26 15
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 16 // static
40 std::unique_ptr<CursorRenderer> CursorRenderer::Create( 17 std::unique_ptr<CursorRenderer> CursorRenderer::Create(
41 gfx::NativeWindow window) { 18 gfx::NativeWindow window) {
42 return std::unique_ptr<CursorRenderer>( 19 return base::MakeUnique<CursorRendererAura>(window,
43 new CursorRendererAura(window, kCursorEnabledOnMouseMovement)); 20 kCursorEnabledOnMouseMovement);
44 } 21 }
45 22
46 CursorRendererAura::CursorRendererAura( 23 CursorRendererAura::CursorRendererAura(
47 aura::Window* window, 24 aura::Window* window,
48 CursorDisplaySetting cursor_display_setting) 25 CursorDisplaySetting cursor_display_setting)
49 : window_(window), 26 : CursorRenderer(window, cursor_display_setting), window_(window) {
50 cursor_display_setting_(cursor_display_setting),
51 tick_clock_(&default_tick_clock_),
52 weak_factory_(this) {
53 if (window_) { 27 if (window_) {
54 window_->AddObserver(this); 28 window_->AddObserver(this);
55 if (cursor_display_setting == kCursorEnabledOnMouseMovement) 29 window_->AddPreTargetHandler(this);
56 window_->AddPreTargetHandler(this);
57 } 30 }
58 Clear();
59 } 31 }
60 32
61 CursorRendererAura::~CursorRendererAura() { 33 CursorRendererAura::~CursorRendererAura() {
62 if (window_) { 34 if (window_) {
63 window_->RemoveObserver(this); 35 window_->RemoveObserver(this);
64 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) 36 window_->RemovePreTargetHandler(this);
65 window_->RemovePreTargetHandler(this);
66 } 37 }
67 } 38 }
68 39
69 base::WeakPtr<CursorRenderer> CursorRendererAura::GetWeakPtr() { 40 bool CursorRendererAura::IsCapturedViewActive() {
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_) { 41 if (!window_) {
88 DVLOG(2) << "Skipping update with no window being tracked"; 42 DVLOG(2) << "Skipping update with no window being tracked";
89 return false; 43 return false;
90 } 44 }
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 45
100 // If we are sharing the root window, or namely the whole screen, we will 46 // 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 47 // render the mouse cursor. For ordinary window, we only render the mouse
102 // cursor when the window is active. 48 // cursor when the window is active.
103 if (!window_->IsRootWindow()) { 49 if (!window_->IsRootWindow()) {
104 aura::client::ActivationClient* activation_client = 50 aura::client::ActivationClient* activation_client =
105 aura::client::GetActivationClient(window_->GetRootWindow()); 51 aura::client::GetActivationClient(window_->GetRootWindow());
106 if (!activation_client) { 52 if (!activation_client) {
107 DVLOG(2) << "Assume window inactive with invalid activation_client"; 53 DVLOG(2) << "Assume window inactive with invalid activation_client";
108 Clear();
109 return false; 54 return false;
110 } 55 }
111 aura::Window* active_window = activation_client->GetActiveWindow(); 56 aura::Window* active_window = activation_client->GetActiveWindow();
112 if (!active_window) { 57 if (!active_window) {
113 DVLOG(2) << "Skipping update as there is no active window"; 58 DVLOG(2) << "Skipping update as there is no active window";
114 Clear();
115 return false; 59 return false;
116 } 60 }
117 if (!active_window->Contains(window_)) { 61 if (!active_window->Contains(window_)) {
118 // Return early if the target window is not active. 62 // Return early if the target window is not active.
119 DVLOG(2) << "Skipping update on an inactive window"; 63 DVLOG(2) << "Skipping update on an inactive window";
120 Clear();
121 return false; 64 return false;
122 } 65 }
123 } 66 }
67 return true;
68 }
124 69
125 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) { 70 gfx::Size CursorRendererAura::GetCapturedViewSize() {
126 if (cursor_displayed_) { 71 if (!window_) {
127 // Stop displaying cursor if there has been no mouse movement 72 return gfx::Size();
128 base::TimeTicks now = tick_clock_->NowTicks(); 73 }
129 if ((now - last_mouse_movement_timestamp_) > 74
130 base::TimeDelta::FromSeconds(MAX_IDLE_TIME_SECONDS)) { 75 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
131 cursor_displayed_ = false; 76 const gfx::Size view_size(window_bounds.width(), window_bounds.height());
132 DVLOG(2) << "Turning off cursor display after idle time"; 77 return view_size;
133 } 78 }
134 } 79
135 if (!cursor_displayed_) 80 gfx::Point CursorRendererAura::GetCursorPositionInView() {
136 return false; 81 if (!window_) {
82 return gfx::Point();
83 }
84
85 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
86 gfx::Point cursor_position = aura::Env::GetInstance()->last_mouse_location();
87 cursor_position.Offset(-window_bounds.x(), -window_bounds.y());
88 return cursor_position;
89 }
90
91 gfx::NativeCursor CursorRendererAura::GetLastKnownCursor() {
92 if (!window_) {
93 return gfx::NativeCursor();
94 }
95
96 return window_->GetHost()->last_cursor();
97 }
98
99 SkBitmap CursorRendererAura::GetLastKnownCursorImage(gfx::Point* hot_point) {
100 if (!window_) {
101 return SkBitmap();
137 } 102 }
138 103
139 gfx::NativeCursor cursor = window_->GetHost()->last_cursor(); 104 gfx::NativeCursor cursor = window_->GetHost()->last_cursor();
140 gfx::Point cursor_hot_point; 105 SkBitmap cursor_bitmap;
141 if (last_cursor_ != cursor || 106 ui::GetCursorBitmap(cursor, &cursor_bitmap, hot_point);
142 window_size_when_cursor_last_updated_ != window_bounds.size()) { 107 return cursor_bitmap;
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;
177 }
178
179 // Helper function to composite a cursor bitmap on a YUV420 video frame.
180 void CursorRendererAura::RenderOnVideoFrame(
181 const scoped_refptr<media::VideoFrame>& target) const {
182 if (scaled_cursor_bitmap_.isNull())
183 return;
184
185 DCHECK(target);
186
187 gfx::Rect rect = gfx::IntersectRects(
188 gfx::Rect(scaled_cursor_bitmap_.width(), scaled_cursor_bitmap_.height()) +
189 gfx::Vector2d(cursor_position_in_frame_.x(),
190 cursor_position_in_frame_.y()),
191 target->visible_rect());
192
193 scaled_cursor_bitmap_.lockPixels();
194 for (int y = rect.y(); y < rect.bottom(); ++y) {
195 int cursor_y = y - cursor_position_in_frame_.y();
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
213 // Only sample U and V at even coordinates.
214 if ((x % 2 == 0) && (y % 2 == 0)) {
215 int color_u = clip_byte(
216 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128);
217 int color_v = clip_byte(
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 } 108 }
226 109
227 void CursorRendererAura::OnMouseEvent(ui::MouseEvent* event) { 110 void CursorRendererAura::OnMouseEvent(ui::MouseEvent* event) {
111 gfx::Point mouse_location(event->x(), event->y());
228 switch (event->type()) { 112 switch (event->type()) {
229 case ui::ET_MOUSE_MOVED: 113 case ui::ET_MOUSE_MOVED:
230 if (!cursor_displayed_) { 114 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; 115 break;
237 case ui::ET_MOUSE_PRESSED: 116 case ui::ET_MOUSE_PRESSED:
238 case ui::ET_MOUSE_RELEASED: 117 case ui::ET_MOUSE_RELEASED:
239 case ui::ET_MOUSEWHEEL: 118 case ui::ET_MOUSEWHEEL:
240 cursor_displayed_ = true; 119 OnMouseClicked(mouse_location, event->time_stamp());
241 break; 120 break;
242 default: 121 default:
243 return; 122 return;
244 } 123 }
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 } 124 }
251 125
252 void CursorRendererAura::OnWindowDestroying(aura::Window* window) { 126 void CursorRendererAura::OnWindowDestroying(aura::Window* window) {
253 DCHECK_EQ(window_, window); 127 DCHECK_EQ(window_, window);
254 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) 128 window_->RemovePreTargetHandler(this);
255 window_->RemovePreTargetHandler(this);
256 window_->RemoveObserver(this); 129 window_->RemoveObserver(this);
257 window_ = nullptr; 130 window_ = nullptr;
258 } 131 }
259 132
260 } // namespace content 133 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/capture/cursor_renderer_aura.h ('k') | content/browser/media/capture/cursor_renderer_aura_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698