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

Side by Side Diff: webrtc/modules/desktop_capture/mouse_cursor_monitor_mac.mm

Issue 2908853002: desktopCapture: scale the cursor image according to screen scale factor on OSX (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 12 matching lines...) Expand all
23 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" 23 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
24 #include "webrtc/modules/desktop_capture/desktop_frame.h" 24 #include "webrtc/modules/desktop_capture/desktop_frame.h"
25 #include "webrtc/modules/desktop_capture/mac/desktop_configuration.h" 25 #include "webrtc/modules/desktop_capture/mac/desktop_configuration.h"
26 #include "webrtc/modules/desktop_capture/mac/desktop_configuration_monitor.h" 26 #include "webrtc/modules/desktop_capture/mac/desktop_configuration_monitor.h"
27 #include "webrtc/modules/desktop_capture/mac/full_screen_chrome_window_detector. h" 27 #include "webrtc/modules/desktop_capture/mac/full_screen_chrome_window_detector. h"
28 #include "webrtc/modules/desktop_capture/mouse_cursor.h" 28 #include "webrtc/modules/desktop_capture/mouse_cursor.h"
29 29
30 namespace webrtc { 30 namespace webrtc {
31 31
32 namespace { 32 namespace {
33 // Paint the image, so that we can get a bitmap representation compatible with 33 CGImageRef ResizeCGImage(CGImageRef image, int width, int height) {
erikchen 2017/05/26 23:09:04 please use scoped_cftyperefs everywhere. It preven
braveyao1 2017/05/30 17:08:19 scoped_cftyperefs is not available in WebRTC yet.
34 // current context. For example, in the retina display, we are going to get an 34 // create context, keeping original image properties
35 // image with same visual size but underlying pixel size conforms to the retina 35 CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
36 // setting. 36 CGContextRef context =
37 NSImage* PaintInCurrentContext(NSImage* source) { 37 CGBitmapContextCreate(NULL,
38 NSSize size = [source size]; 38 width,
39 NSImage* new_image = [[NSImage alloc] initWithSize:size]; 39 height,
40 [new_image lockFocus]; 40 CGImageGetBitsPerComponent(image),
41 NSRect frame = NSMakeRect(0, 0, size.width, size.height); 41 width * 4,
42 [source drawInRect:frame 42 colorspace,
43 fromRect:frame 43 CGImageGetBitmapInfo(image));
44 operation:NSCompositeCopy 44 CGColorSpaceRelease(colorspace);
erikchen 2017/05/30 17:27:46 Please read through https://developer.apple.com/li
braveyao1 2017/05/30 21:34:07 Done. It's weird that releasing the color space h
45 fraction:1.0]; 45
46 [new_image unlockFocus]; 46 if (context == NULL) return image;
47 return new_image; 47
48 // draw image to context (resizing it)
49 CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
50 // extract resulting image from context
51 CGImageRef imgRef = CGBitmapContextCreateImage(context);
52 CGContextRelease(context);
53
54 return imgRef;
48 } 55 }
49 } // namespace 56 } // namespace
50 57
51 class MouseCursorMonitorMac : public MouseCursorMonitor { 58 class MouseCursorMonitorMac : public MouseCursorMonitor {
52 public: 59 public:
53 MouseCursorMonitorMac(const DesktopCaptureOptions& options, 60 MouseCursorMonitorMac(const DesktopCaptureOptions& options,
54 CGWindowID window_id, 61 CGWindowID window_id,
55 ScreenId screen_id); 62 ScreenId screen_id);
56 ~MouseCursorMonitorMac() override; 63 ~MouseCursorMonitorMac() override;
57 64
58 void Init(Callback* callback, Mode mode) override; 65 void Init(Callback* callback, Mode mode) override;
59 void Capture() override; 66 void Capture() override;
60 67
61 private: 68 private:
62 static void DisplaysReconfiguredCallback(CGDirectDisplayID display, 69 static void DisplaysReconfiguredCallback(CGDirectDisplayID display,
63 CGDisplayChangeSummaryFlags flags, 70 CGDisplayChangeSummaryFlags flags,
64 void *user_parameter); 71 void *user_parameter);
65 void DisplaysReconfigured(CGDirectDisplayID display, 72 void DisplaysReconfigured(CGDirectDisplayID display,
66 CGDisplayChangeSummaryFlags flags); 73 CGDisplayChangeSummaryFlags flags);
67 74
68 void CaptureImage(float scale); 75 void CaptureImage(float scale);
69 76
70 rtc::scoped_refptr<DesktopConfigurationMonitor> configuration_monitor_; 77 rtc::scoped_refptr<DesktopConfigurationMonitor> configuration_monitor_;
71 CGWindowID window_id_; 78 CGWindowID window_id_;
72 ScreenId screen_id_; 79 ScreenId screen_id_;
73 Callback* callback_; 80 Callback* callback_;
74 Mode mode_; 81 Mode mode_;
75 std::unique_ptr<MouseCursor> last_cursor_; 82 NSImage* last_cursor_;
erikchen 2017/05/26 23:09:04 use a scoped_nsobject. You're not retaining this o
braveyao1 2017/05/30 17:08:19 scoped_nsobject is not available in WebRTC yet. "
76 rtc::scoped_refptr<FullScreenChromeWindowDetector> 83 rtc::scoped_refptr<FullScreenChromeWindowDetector>
77 full_screen_chrome_window_detector_; 84 full_screen_chrome_window_detector_;
78 }; 85 };
79 86
80 MouseCursorMonitorMac::MouseCursorMonitorMac( 87 MouseCursorMonitorMac::MouseCursorMonitorMac(
81 const DesktopCaptureOptions& options, 88 const DesktopCaptureOptions& options,
82 CGWindowID window_id, 89 CGWindowID window_id,
83 ScreenId screen_id) 90 ScreenId screen_id)
84 : configuration_monitor_(options.configuration_monitor()), 91 : configuration_monitor_(options.configuration_monitor()),
85 window_id_(window_id), 92 window_id_(window_id),
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 } 253 }
247 callback_->OnMouseCursorPosition(state, position); 254 callback_->OnMouseCursorPosition(state, position);
248 } 255 }
249 256
250 void MouseCursorMonitorMac::CaptureImage(float scale) { 257 void MouseCursorMonitorMac::CaptureImage(float scale) {
251 NSCursor* nscursor = [NSCursor currentSystemCursor]; 258 NSCursor* nscursor = [NSCursor currentSystemCursor];
252 259
253 NSImage* nsimage = [nscursor image]; 260 NSImage* nsimage = [nscursor image];
254 NSSize nssize = [nsimage size]; // DIP size 261 NSSize nssize = [nsimage size]; // DIP size
255 262
256 // For retina screen, we need to paint the cursor in current graphic context 263 if([[nsimage TIFFRepresentation] isEqual: [last_cursor_ TIFFRepresentation]])
257 // to get retina representation. 264 return;
258 if (scale != 1.0) 265 last_cursor_ = nsimage;
259 nsimage = PaintInCurrentContext(nsimage);
260 266
261 DesktopSize size(round(nssize.width * scale), 267 DesktopSize size(round(nssize.width * scale),
262 round(nssize.height * scale)); // Pixel size 268 round(nssize.height * scale)); // Pixel size
263 NSPoint nshotspot = [nscursor hotSpot]; 269 NSPoint nshotspot = [nscursor hotSpot];
264 DesktopVector hotspot( 270 DesktopVector hotspot(
265 std::max(0, 271 std::max(0,
266 std::min(size.width(), static_cast<int>(nshotspot.x * scale))), 272 std::min(size.width(), static_cast<int>(nshotspot.x * scale))),
267 std::max(0, 273 std::max(0,
268 std::min(size.height(), static_cast<int>(nshotspot.y * scale)))); 274 std::min(size.height(), static_cast<int>(nshotspot.y * scale))));
269 CGImageRef cg_image = 275 CGImageRef cg_image =
270 [nsimage CGImageForProposedRect:NULL context:nil hints:nil]; 276 [nsimage CGImageForProposedRect:NULL context:nil hints:nil];
271 if (!cg_image) 277 if (!cg_image)
272 return; 278 return;
273 279
280 // Before 10.12, OSX may report 1X cursor on Retina screen. (See
281 // crbug.com/632995.) After 10.12, OSX may report 2X cursor on non-Retina
282 // screen. (See crbug.com/671436.) So scaling the cursor if needed.
283 if (CGImageGetWidth(cg_image) != static_cast<size_t>(size.width()))
284 cg_image = ResizeCGImage(cg_image, size.width(), size.height());
285
274 if (CGImageGetBitsPerPixel(cg_image) != DesktopFrame::kBytesPerPixel * 8 || 286 if (CGImageGetBitsPerPixel(cg_image) != DesktopFrame::kBytesPerPixel * 8 ||
275 CGImageGetWidth(cg_image) != static_cast<size_t>(size.width()) || 287 CGImageGetWidth(cg_image) != static_cast<size_t>(size.width()) ||
276 CGImageGetBitsPerComponent(cg_image) != 8) { 288 CGImageGetBitsPerComponent(cg_image) != 8) {
277 return; 289 return;
278 } 290 }
279 291
280 CGDataProviderRef provider = CGImageGetDataProvider(cg_image); 292 CGDataProviderRef provider = CGImageGetDataProvider(cg_image);
281 CFDataRef image_data_ref = CGDataProviderCopyData(provider); 293 CFDataRef image_data_ref = CGDataProviderCopyData(provider);
282 if (image_data_ref == NULL) 294 if (image_data_ref == NULL)
283 return; 295 return;
284 296
285 const uint8_t* src_data = 297 const uint8_t* src_data =
286 reinterpret_cast<const uint8_t*>(CFDataGetBytePtr(image_data_ref)); 298 reinterpret_cast<const uint8_t*>(CFDataGetBytePtr(image_data_ref));
287 299
288 // Compare the cursor with the previous one.
289 if (last_cursor_.get() &&
290 last_cursor_->image()->size().equals(size) &&
291 last_cursor_->hotspot().equals(hotspot) &&
292 memcmp(last_cursor_->image()->data(), src_data,
293 last_cursor_->image()->stride() * size.height()) == 0) {
294 CFRelease(image_data_ref);
295 return;
296 }
297
298 // Create a MouseCursor that describes the cursor and pass it to 300 // Create a MouseCursor that describes the cursor and pass it to
299 // the client. 301 // the client.
300 std::unique_ptr<DesktopFrame> image( 302 std::unique_ptr<DesktopFrame> image(
301 new BasicDesktopFrame(DesktopSize(size.width(), size.height()))); 303 new BasicDesktopFrame(DesktopSize(size.width(), size.height())));
302 304
303 int src_stride = CGImageGetBytesPerRow(cg_image); 305 int src_stride = CGImageGetBytesPerRow(cg_image);
304 image->CopyPixelsFrom(src_data, src_stride, DesktopRect::MakeSize(size)); 306 image->CopyPixelsFrom(src_data, src_stride, DesktopRect::MakeSize(size));
305 307
306 CFRelease(image_data_ref); 308 CFRelease(image_data_ref);
307 309
308 std::unique_ptr<MouseCursor> cursor( 310 std::unique_ptr<MouseCursor> cursor(
309 new MouseCursor(image.release(), hotspot)); 311 new MouseCursor(image.release(), hotspot));
310 last_cursor_.reset(MouseCursor::CopyOf(*cursor));
311 312
312 callback_->OnMouseCursor(cursor.release()); 313 callback_->OnMouseCursor(cursor.release());
313 } 314 }
314 315
315 MouseCursorMonitor* MouseCursorMonitor::CreateForWindow( 316 MouseCursorMonitor* MouseCursorMonitor::CreateForWindow(
316 const DesktopCaptureOptions& options, WindowId window) { 317 const DesktopCaptureOptions& options, WindowId window) {
317 return new MouseCursorMonitorMac(options, window, kInvalidScreenId); 318 return new MouseCursorMonitorMac(options, window, kInvalidScreenId);
318 } 319 }
319 320
321 // ScreenCapture already contains current cursor in the captured frame on OSX.
320 MouseCursorMonitor* MouseCursorMonitor::CreateForScreen( 322 MouseCursorMonitor* MouseCursorMonitor::CreateForScreen(
321 const DesktopCaptureOptions& options, 323 const DesktopCaptureOptions& options,
322 ScreenId screen) { 324 ScreenId screen) {
323 return new MouseCursorMonitorMac(options, kCGNullWindowID, screen); 325 return nil;
324 } 326 }
325 327
326 } // namespace webrtc 328 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698