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

Side by Side Diff: content/browser/media/capture/cursor_renderer_mac.mm

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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_mac.h" 5 #include "content/browser/media/capture/cursor_renderer_mac.h"
6 6
7 #include <ApplicationServices/ApplicationServices.h>
8 #include <Cocoa/Cocoa.h> 7 #include <Cocoa/Cocoa.h>
9 #include <CoreFoundation/CoreFoundation.h> 8 #include <CoreFoundation/CoreFoundation.h>
10 #include <stdint.h>
11 9
12 #include <cmath> 10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/mac/mac_util.h"
13 #include "base/memory/ptr_util.h"
14 #include "skia/ext/skia_utils_mac.h"
15 #include "ui/gfx/image/image.h"
13 16
14 #include "base/logging.h" 17 @implementation CursorRendererMouseTracker
18
19 - (instancetype)initWithView:(NSView*)nsView {
20 if ((self = [super init])) {
21 NSTrackingAreaOptions trackingOptions =
22 NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited |
23 NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect;
24 trackingArea_.reset([[CrTrackingArea alloc] initWithRect:NSZeroRect
25 options:trackingOptions
26 owner:self
27 userInfo:nil]);
28 [nsView addTrackingArea:trackingArea_.get()];
29 capturedView_.reset([nsView retain]);
30 }
31 return self;
32 }
33
34 - (void)stopTracking {
35 if (trackingArea_.get()) {
36 [capturedView_ removeTrackingArea:trackingArea_.get()];
37 trackingArea_.reset();
38 capturedView_.reset();
39 }
40 }
41
42 - (void)registerMouseInteractionObserver:(const base::Closure&)observer {
43 mouseInteractionObserver_ = observer;
44 }
45
46 - (void)mouseMoved:(NSEvent*)theEvent {
47 mouseInteractionObserver_.Run();
48 }
49
50 - (void)mouseEntered:(NSEvent*)theEvent {
51 mouseInteractionObserver_.Run();
52 }
53
54 - (void)mouseExited:(NSEvent*)theEvent {
55 }
56
57 @end
15 58
16 namespace content { 59 namespace content {
17 60
18 namespace { 61 // static
19 62 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) {
20 // RGBA format on cursor bitmap 63 return base::MakeUnique<CursorRendererMac>(view);
21 const int kBytesPerPixel = 4;
22
23 inline int clip_byte(int x) {
24 return std::max(0, std::min(x, 255));
25 } 64 }
26 65
27 inline int alpha_blend(int alpha, int src, int dst) { 66 CursorRendererMac::CursorRendererMac(gfx::NativeView view)
28 return (src * alpha + dst * (255 - alpha)) / 255; 67 : CursorRenderer(view, kCursorEnabledOnMouseMovement), view_(view) {
68 mouse_tracker_.reset([[CursorRendererMouseTracker alloc] initWithView:view]);
69 [mouse_tracker_ registerMouseInteractionObserver:
70 base::Bind(&CursorRendererMac::OnMouseEvent, base::Unretained(this))];
29 } 71 }
30 72
31 } // namespace 73 CursorRendererMac::~CursorRendererMac() {
32 74 [mouse_tracker_ stopTracking];
33 // static
34 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) {
35 return std::unique_ptr<CursorRenderer>(new CursorRendererMac(view));
36 } 75 }
37 76
38 CursorRendererMac::CursorRendererMac(NSView* view) 77 bool CursorRendererMac::IsCapturedViewActive() {
39 : view_(view), weak_factory_(this) { 78 if (![[view_ window] isKeyWindow]) {
40 Clear();
41 }
42
43 CursorRendererMac::~CursorRendererMac() {}
44
45 base::WeakPtr<CursorRenderer> CursorRendererMac::GetWeakPtr() {
46 return weak_factory_.GetWeakPtr();
47 }
48
49 void CursorRendererMac::Clear() {
50 last_cursor_data_.reset();
51 last_cursor_width_ = 0;
52 last_cursor_height_ = 0;
53 last_mouse_location_x_ = 0;
54 last_mouse_location_y_ = 0;
55 last_mouse_movement_timestamp_ = base::TimeTicks();
56 }
57
58 // Polls mouse cursor location and image and returns whether the mouse
59 // cursor should be rendered on the frame.
60 bool CursorRendererMac::SnapshotCursorState(const gfx::Rect& region_in_frame) {
61 // Mouse location in window co-ordinates.
62 NSPoint mouse_window_location =
63 [view_ window].mouseLocationOutsideOfEventStream;
64 // Mouse location with respect to the web contents.
65 NSPoint mouse_tab_location =
66 [view_ convertPoint:mouse_window_location fromView:nil];
67
68 // Mouse co-ordinates directly comparable against frame co-ordinates
69 // after translation.
70 if (mouse_tab_location.x < 0 || mouse_tab_location.y < 0 ||
71 mouse_tab_location.x > region_in_frame.width() ||
72 mouse_tab_location.y > region_in_frame.height()) {
73 VLOG(2) << "Mouse outside content region";
74 return false; 79 return false;
75 } 80 }
76
77 if (![[view_ window] isKeyWindow]) {
78 VLOG(2) << "Window currently inactive";
79 return false;
80 }
81
82 if ((base::TimeTicks::Now() - last_mouse_movement_timestamp_).InSeconds() >
83 MAX_IDLE_TIME_SECONDS &&
84 std::abs(mouse_tab_location.x - last_mouse_location_x_) <
85 MIN_MOVEMENT_PIXELS &&
86 std::abs(mouse_tab_location.y - last_mouse_location_y_) <
87 MIN_MOVEMENT_PIXELS) {
88 VLOG(2) << "No mouse movement in a while";
89 return false;
90 }
91
92 // Mouse cursor position within the frame.
93 cursor_position_in_frame_ =
94 gfx::Point(region_in_frame.x() + mouse_tab_location.x,
95 region_in_frame.y() + mouse_tab_location.y);
96
97 // Grab system cursor.
98 NSCursor* nscursor = [NSCursor currentSystemCursor];
99 NSPoint nshotspot = [nscursor hotSpot];
100 NSImage* nsimage = [nscursor image];
101 NSSize nssize = [nsimage size];
102
103 // The cursor co-ordinates in the window and the video frame co-ordinates are
104 // inverted along y-axis. We render the cursor inverse vertically on the
105 // frame. Hence the inversion on hotspot offset here.
106 cursor_position_in_frame_.Offset(-nshotspot.x,
107 -(nssize.height - nshotspot.y));
108 last_cursor_width_ = nssize.width;
109 last_cursor_height_ = nssize.height;
110
111 CGImageRef cg_image =
112 [nsimage CGImageForProposedRect:NULL context:nil hints:nil];
113 if (!cg_image)
114 return false;
115
116 if (CGImageGetBitsPerPixel(cg_image) != kBytesPerPixel * 8 ||
117 CGImageGetBytesPerRow(cg_image) !=
118 static_cast<size_t>(kBytesPerPixel * nssize.width) ||
119 CGImageGetBitsPerComponent(cg_image) != 8) {
120 return false;
121 }
122
123 CGDataProviderRef provider = CGImageGetDataProvider(cg_image);
124 CFDataRef image_data_ref = CGDataProviderCopyData(provider);
125 if (!image_data_ref)
126 return false;
127 last_cursor_data_.reset(image_data_ref, base::scoped_policy::ASSUME);
128
129 if (std::abs(mouse_tab_location.x - last_mouse_location_x_) >
130 MIN_MOVEMENT_PIXELS ||
131 std::abs(mouse_tab_location.y - last_mouse_location_y_) >
132 MIN_MOVEMENT_PIXELS) {
133 last_mouse_movement_timestamp_ = base::TimeTicks::Now();
134 last_mouse_location_x_ = mouse_tab_location.x;
135 last_mouse_location_y_ = mouse_tab_location.y;
136 }
137 return true; 81 return true;
138 } 82 }
139 83
140 // Helper function to composite a RGBA cursor bitmap on a YUV420 video frame. 84 gfx::Size CursorRendererMac::GetCapturedViewSize() {
141 void CursorRendererMac::RenderOnVideoFrame( 85 NSRect frame_rect = [view_ bounds];
142 const scoped_refptr<media::VideoFrame>& target) const { 86 return gfx::Size(frame_rect.size.width, frame_rect.size.height);
143 DCHECK(target); 87 }
144 DCHECK(last_cursor_data_);
145 const uint8_t* cursor_data_ =
146 reinterpret_cast<const uint8_t*>(CFDataGetBytePtr(last_cursor_data_));
147 88
148 gfx::Rect visible_rect = target->visible_rect(); 89 gfx::Point CursorRendererMac::GetCursorPositionInView() {
149 gfx::Rect rect = 90 // Mouse location in window co-ordinates.
150 gfx::IntersectRects(gfx::Rect(last_cursor_width_, last_cursor_height_) + 91 NSPoint mouse_window_location =
151 gfx::Vector2d(cursor_position_in_frame_.x(), 92 [view_ window].mouseLocationOutsideOfEventStream;
152 cursor_position_in_frame_.y()), 93 // Mouse location with respect to the view within the window.
153 visible_rect); 94 NSPoint mouse_view_location =
95 [view_ convertPoint:mouse_window_location fromView:nil];
154 96
155 for (int y = rect.y() + 1; y <= rect.bottom(); ++y) { 97 // Invert y coordinate to unify with Aura.
156 int cursor_y = rect.bottom() - y; 98 gfx::Point cursor_position_in_view(
157 int inverted_y = visible_rect.bottom() - y; 99 mouse_view_location.x,
158 uint8_t* yplane = 100 GetCapturedViewSize().height() - mouse_view_location.y);
159 target->data(media::VideoFrame::kYPlane) +
160 inverted_y * target->row_bytes(media::VideoFrame::kYPlane);
161 uint8_t* uplane =
162 target->data(media::VideoFrame::kUPlane) +
163 (inverted_y / 2) * target->row_bytes(media::VideoFrame::kUPlane);
164 uint8_t* vplane =
165 target->data(media::VideoFrame::kVPlane) +
166 (inverted_y / 2) * target->row_bytes(media::VideoFrame::kVPlane);
167 for (int x = rect.x(); x < rect.right(); ++x) {
168 int cursor_x = x - rect.x();
169 int byte_pos = cursor_y * last_cursor_width_ * kBytesPerPixel +
170 cursor_x * kBytesPerPixel;
171 int color_r = cursor_data_[byte_pos];
172 int color_g = cursor_data_[byte_pos + 1];
173 int color_b = cursor_data_[byte_pos + 2];
174 int alpha = cursor_data_[byte_pos + 3];
175 int color_y = clip_byte(
176 ((color_r * 66 + color_g * 129 + color_b * 25 + 128) >> 8) + 16);
177 yplane[x] = alpha_blend(alpha, color_y, yplane[x]);
178 101
179 // Only sample U and V at even coordinates. 102 return cursor_position_in_view;
180 if ((x % 2 == 0) && (y % 2 == 0)) { 103 }
181 int color_u = clip_byte( 104
182 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128); 105 gfx::NativeCursor CursorRendererMac::GetLastKnownCursor() {
183 int color_v = clip_byte( 106 // Grab system cursor.
184 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128); 107 return [NSCursor currentSystemCursor];
185 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]); 108 }
186 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]); 109
187 } 110 SkBitmap CursorRendererMac::GetLastKnownCursorImage(gfx::Point* hot_point) {
188 } 111 // Grab system cursor.
189 } 112 NSCursor* nscursor = [NSCursor currentSystemCursor];
113 NSImage* nsimage = [nscursor image];
114 NSPoint nshotspot = [nscursor hotSpot];
115
116 *hot_point = gfx::Point(nshotspot.x, nshotspot.y);
117 return skia::NSImageToSkBitmapWithColorSpace(
118 nsimage, /*is_opaque=*/false, base::mac::GetSystemColorSpace());
119 }
120
121 void CursorRendererMac::OnMouseEvent() {
122 // Update cursor movement info to CursorRenderer.
123 OnMouseMoved(GetCursorPositionInView(), base::TimeTicks::Now());
190 } 124 }
191 125
192 } // namespace content 126 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698