OLD | NEW |
---|---|
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/memory/ptr_util.h" | |
13 #include "ui/gfx/image/image.h" | |
13 | 14 |
14 #include "base/logging.h" | 15 @implementation CursorRendererMouseTracker |
16 | |
17 - (instancetype)initWithView:(NSView*)nsView { | |
18 self = [super init]; | |
Robert Sesek
2017/01/04 16:10:16
Style is:
if ((self = [super init])) {
// initi
braveyao
2017/01/04 18:05:07
Done.
| |
19 NSTrackingAreaOptions trackingOptions = | |
20 NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | | |
21 NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect; | |
22 tracking_area_.reset([[CrTrackingArea alloc] initWithRect:NSZeroRect | |
23 options:trackingOptions | |
24 owner:self | |
25 userInfo:nil]); | |
26 [nsView addTrackingArea:tracking_area_.get()]; | |
27 captured_view_ = nsView; | |
28 return self; | |
29 } | |
30 | |
31 - (void)stopTracking { | |
32 if (tracking_area_.get()) { | |
33 [captured_view_ removeTrackingArea:tracking_area_.get()]; | |
34 tracking_area_.reset(); | |
35 } | |
36 } | |
37 | |
38 - (void)registerMouseInteractionObserver:(const base::Closure&)observer { | |
39 mouseInteractionObserver_ = observer; | |
40 } | |
41 | |
42 - (void)mouseMoved:(NSEvent*)theEvent { | |
43 mouseInteractionObserver_.Run(); | |
44 } | |
45 | |
46 - (void)mouseEntered:(NSEvent*)theEvent { | |
47 mouseInteractionObserver_.Run(); | |
48 } | |
49 | |
50 - (void)mouseExited:(NSEvent*)theEvent { | |
51 } | |
52 @end | |
Robert Sesek
2017/01/04 16:10:16
nit: blank line before
braveyao
2017/01/04 18:05:07
Done.
| |
15 | 53 |
16 namespace content { | 54 namespace content { |
17 | 55 |
18 namespace { | 56 // static |
19 | 57 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) { |
20 // RGBA format on cursor bitmap | 58 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 } | 59 } |
26 | 60 |
27 inline int alpha_blend(int alpha, int src, int dst) { | 61 CursorRendererMac::CursorRendererMac(gfx::NativeView view) |
28 return (src * alpha + dst * (255 - alpha)) / 255; | 62 : CursorRenderer(view, kCursorEnabledOnMouseMovement), view_(view) { |
63 mouse_tracker_.reset([[CursorRendererMouseTracker alloc] initWithView:view]); | |
64 [mouse_tracker_ registerMouseInteractionObserver: | |
65 base::Bind(&CursorRendererMac::OnMouseEvent, base::Unretained(this))]; | |
29 } | 66 } |
30 | 67 |
31 } // namespace | 68 CursorRendererMac::~CursorRendererMac() { |
32 | 69 [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 } | 70 } |
37 | 71 |
38 CursorRendererMac::CursorRendererMac(NSView* view) | 72 bool CursorRendererMac::IsCapturedViewActive() { |
39 : view_(view), weak_factory_(this) { | 73 if (![[view_ window] isKeyWindow]) { |
40 Clear(); | 74 DVLOG(2) << "Window currently inactive"; |
Robert Sesek
2017/01/04 16:10:16
Remove this log message?
braveyao
2017/01/04 18:05:07
Done.
| |
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; | 75 return false; |
75 } | 76 } |
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; | 77 return true; |
138 } | 78 } |
139 | 79 |
140 // Helper function to composite a RGBA cursor bitmap on a YUV420 video frame. | 80 gfx::Size CursorRendererMac::GetCapturedViewSize() { |
141 void CursorRendererMac::RenderOnVideoFrame( | 81 NSRect frame_rect = [view_ bounds]; |
142 const scoped_refptr<media::VideoFrame>& target) const { | 82 return gfx::Size(frame_rect.size.width, frame_rect.size.height); |
143 DCHECK(target); | 83 } |
144 DCHECK(last_cursor_data_); | |
145 const uint8_t* cursor_data_ = | |
146 reinterpret_cast<const uint8_t*>(CFDataGetBytePtr(last_cursor_data_)); | |
147 | 84 |
148 gfx::Rect visible_rect = target->visible_rect(); | 85 gfx::Point CursorRendererMac::GetCursorPositionInView() { |
149 gfx::Rect rect = | 86 // Mouse location in window co-ordinates. |
150 gfx::IntersectRects(gfx::Rect(last_cursor_width_, last_cursor_height_) + | 87 NSPoint mouse_window_location = |
151 gfx::Vector2d(cursor_position_in_frame_.x(), | 88 [view_ window].mouseLocationOutsideOfEventStream; |
152 cursor_position_in_frame_.y()), | 89 // Mouse location with respect to the view within the window. |
153 visible_rect); | 90 NSPoint mouse_view_location = |
91 [view_ convertPoint:mouse_window_location fromView:nil]; | |
154 | 92 |
155 for (int y = rect.y() + 1; y <= rect.bottom(); ++y) { | 93 // Invert y coordinate to unify with Aura. |
156 int cursor_y = rect.bottom() - y; | 94 gfx::Point cursor_position_in_view( |
157 int inverted_y = visible_rect.bottom() - y; | 95 mouse_view_location.x, |
158 uint8_t* yplane = | 96 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 | 97 |
179 // Only sample U and V at even coordinates. | 98 return cursor_position_in_view; |
180 if ((x % 2 == 0) && (y % 2 == 0)) { | 99 } |
181 int color_u = clip_byte( | 100 |
182 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128); | 101 gfx::NativeCursor CursorRendererMac::GetLastKnownCursor() { |
183 int color_v = clip_byte( | 102 // Grab system cursor. |
184 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128); | 103 return [NSCursor currentSystemCursor]; |
185 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]); | 104 } |
186 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]); | 105 |
187 } | 106 SkBitmap CursorRendererMac::GetLastKnownCursorImage(gfx::Point* hot_point) { |
188 } | 107 // Grab system cursor. |
189 } | 108 NSCursor* nscursor = [NSCursor currentSystemCursor]; |
109 NSImage* nsimage = [nscursor image]; | |
110 NSPoint nshotspot = [nscursor hotSpot]; | |
111 | |
112 *hot_point = gfx::Point(nshotspot.x, nshotspot.y); | |
113 gfx::Image cursor_image = gfx::Image([nsimage retain]); | |
Robert Sesek
2017/01/04 16:10:16
Use skia::NSImageToSkBitmapWithColorSpace instead
braveyao
2017/01/04 18:05:07
Done.
| |
114 return *(cursor_image.ToSkBitmap()); | |
115 } | |
116 | |
117 void CursorRendererMac::OnMouseEvent() { | |
118 // Update cursor movement info to CursorRenderer | |
Robert Sesek
2017/01/04 16:10:16
nit: punctuation
braveyao
2017/01/04 18:05:07
Done.
| |
119 OnMouseMoved(GetCursorPositionInView(), base::TimeTicks::Now()); | |
190 } | 120 } |
191 | 121 |
192 } // namespace content | 122 } // namespace content |
OLD | NEW |