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