Chromium Code Reviews| 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> | 7 #include <ApplicationServices/ApplicationServices.h> |
| 8 #include <Cocoa/Cocoa.h> | 8 #include <Cocoa/Cocoa.h> |
| 9 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 const int kBytesPerPixel = 4; | 21 const int kBytesPerPixel = 4; |
| 22 | 22 |
| 23 inline int clip_byte(int x) { | 23 inline int clip_byte(int x) { |
| 24 return std::max(0, std::min(x, 255)); | 24 return std::max(0, std::min(x, 255)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 inline int alpha_blend(int alpha, int src, int dst) { | 27 inline int alpha_blend(int alpha, int src, int dst) { |
| 28 return (src * alpha + dst * (255 - alpha)) / 255; | 28 return (src * alpha + dst * (255 - alpha)) / 255; |
| 29 } | 29 } |
| 30 | 30 |
| 31 CGImageRef ResizeCGImage(CGImageRef image, int width, int height) { | |
| 32 // create context, keeping original image properties | |
| 33 CGColorSpaceRef colorspace = CGImageGetColorSpace(image); | |
| 34 CGContextRef context = CGBitmapContextCreate( | |
| 35 NULL, width, height, CGImageGetBitsPerComponent(image), width * 4, | |
| 36 colorspace, CGImageGetBitmapInfo(image)); | |
| 37 CGColorSpaceRelease(colorspace); | |
| 38 | |
| 39 if (context == NULL) | |
| 40 return nil; | |
| 41 | |
| 42 // draw image to context (resizing it) | |
| 43 CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); | |
| 44 // extract resulting image from context | |
| 45 CGImageRef imgRef = CGBitmapContextCreateImage(context); | |
| 46 CGContextRelease(context); | |
| 47 | |
| 48 return imgRef; | |
| 49 } | |
| 50 | |
| 31 } // namespace | 51 } // namespace |
| 32 | 52 |
| 33 // static | 53 // static |
| 34 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) { | 54 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) { |
| 35 return std::unique_ptr<CursorRenderer>(new CursorRendererMac(view)); | 55 return std::unique_ptr<CursorRenderer>(new CursorRendererMac(view)); |
| 36 } | 56 } |
| 37 | 57 |
| 38 CursorRendererMac::CursorRendererMac(NSView* view) | 58 CursorRendererMac::CursorRendererMac(NSView* view) |
| 39 : view_(view), weak_factory_(this) { | 59 : view_(view), weak_factory_(this) { |
| 40 Clear(); | 60 Clear(); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 55 last_mouse_movement_timestamp_ = base::TimeTicks(); | 75 last_mouse_movement_timestamp_ = base::TimeTicks(); |
| 56 } | 76 } |
| 57 | 77 |
| 58 // Polls mouse cursor location and image and returns whether the mouse | 78 // Polls mouse cursor location and image and returns whether the mouse |
| 59 // cursor should be rendered on the frame. | 79 // cursor should be rendered on the frame. |
| 60 bool CursorRendererMac::SnapshotCursorState(const gfx::Rect& region_in_frame) { | 80 bool CursorRendererMac::SnapshotCursorState(const gfx::Rect& region_in_frame) { |
| 61 // Mouse location in window co-ordinates. | 81 // Mouse location in window co-ordinates. |
| 62 NSPoint mouse_window_location = | 82 NSPoint mouse_window_location = |
| 63 [view_ window].mouseLocationOutsideOfEventStream; | 83 [view_ window].mouseLocationOutsideOfEventStream; |
| 64 // Mouse location with respect to the web contents. | 84 // Mouse location with respect to the web contents. |
| 65 NSPoint mouse_tab_location = | 85 NSPoint mouse_tab_location = |
|
miu
2016/12/06 22:06:23
This variable must be renamed. Code in src/content
| |
| 66 [view_ convertPoint:mouse_window_location fromView:nil]; | 86 [view_ convertPoint:mouse_window_location fromView:nil]; |
| 67 | 87 |
| 88 // The |view_| is in window coordinates. And the |region_in_frame| is in | |
| 89 // physical coordinates, which also might be clamped by the configured | |
| 90 // |max_capture_size|. So we need to get the final scale ratio and unify | |
| 91 // all the comparison and calculation below. | |
| 92 NSRect frame_rect = [view_ bounds]; | |
| 93 float x_scale = region_in_frame.width() / frame_rect.size.width; | |
|
miu
2016/12/06 22:06:23
Please add divide-by-zero checks here, since it's
| |
| 94 float y_scale = region_in_frame.height() / frame_rect.size.height; | |
| 95 | |
|
miu
2016/12/06 22:06:23
Instead of multiplying by these scaling factors ev
| |
| 68 // Mouse co-ordinates directly comparable against frame co-ordinates | 96 // Mouse co-ordinates directly comparable against frame co-ordinates |
| 69 // after translation. | 97 // after translation. |
| 70 if (mouse_tab_location.x < 0 || mouse_tab_location.y < 0 || | 98 if (mouse_tab_location.x < 0 || mouse_tab_location.y < 0 || |
| 71 mouse_tab_location.x > region_in_frame.width() || | 99 mouse_tab_location.x * x_scale > region_in_frame.width() || |
| 72 mouse_tab_location.y > region_in_frame.height()) { | 100 mouse_tab_location.y * y_scale > region_in_frame.height()) { |
| 73 VLOG(2) << "Mouse outside content region"; | 101 VLOG(2) << "Mouse outside content region"; |
| 74 return false; | 102 return false; |
| 75 } | 103 } |
| 76 | 104 |
| 77 if (![[view_ window] isKeyWindow]) { | 105 if (![[view_ window] isKeyWindow]) { |
| 78 VLOG(2) << "Window currently inactive"; | 106 VLOG(2) << "Window currently inactive"; |
| 79 return false; | 107 return false; |
| 80 } | 108 } |
| 81 | 109 |
| 82 if ((base::TimeTicks::Now() - last_mouse_movement_timestamp_).InSeconds() > | 110 if ((base::TimeTicks::Now() - last_mouse_movement_timestamp_).InSeconds() > |
| 83 MAX_IDLE_TIME_SECONDS && | 111 MAX_IDLE_TIME_SECONDS && |
| 84 std::abs(mouse_tab_location.x - last_mouse_location_x_) < | 112 std::abs(mouse_tab_location.x - last_mouse_location_x_) < |
| 85 MIN_MOVEMENT_PIXELS && | 113 MIN_MOVEMENT_PIXELS && |
| 86 std::abs(mouse_tab_location.y - last_mouse_location_y_) < | 114 std::abs(mouse_tab_location.y - last_mouse_location_y_) < |
| 87 MIN_MOVEMENT_PIXELS) { | 115 MIN_MOVEMENT_PIXELS) { |
| 88 VLOG(2) << "No mouse movement in a while"; | 116 VLOG(2) << "No mouse movement in a while"; |
| 89 return false; | 117 return false; |
| 90 } | 118 } |
| 91 | 119 |
| 92 // Mouse cursor position within the frame. | 120 // Mouse cursor position within the frame. |
| 93 cursor_position_in_frame_ = | 121 cursor_position_in_frame_ = |
| 94 gfx::Point(region_in_frame.x() + mouse_tab_location.x, | 122 gfx::Point(region_in_frame.x() + mouse_tab_location.x * x_scale, |
| 95 region_in_frame.y() + mouse_tab_location.y); | 123 region_in_frame.y() + mouse_tab_location.y * y_scale); |
| 96 | 124 |
| 97 // Grab system cursor. | 125 // Grab system cursor. |
| 98 NSCursor* nscursor = [NSCursor currentSystemCursor]; | 126 NSCursor* nscursor = [NSCursor currentSystemCursor]; |
| 99 NSPoint nshotspot = [nscursor hotSpot]; | 127 NSPoint nshotspot = [nscursor hotSpot]; |
| 100 NSImage* nsimage = [nscursor image]; | 128 NSImage* nsimage = [nscursor image]; |
| 101 NSSize nssize = [nsimage size]; | 129 NSSize nssize = [nsimage size]; |
| 102 | 130 |
| 131 gfx::Point scaled_hotspot = | |
| 132 gfx::Point(nshotspot.x * x_scale, nshotspot.y * y_scale); | |
| 133 last_cursor_width_ = nssize.width * x_scale; | |
|
miu
2016/12/06 22:06:23
To simplify the code after this point, let's make
braveyao
2016/12/07 00:56:03
I don't think we should divide the |nssize| by |de
miu
2016/12/07 20:53:43
Sounds good. I didn't realize nssize was in view c
| |
| 134 last_cursor_height_ = nssize.height * y_scale; | |
| 135 | |
| 103 // The cursor co-ordinates in the window and the video frame co-ordinates are | 136 // 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 | 137 // inverted along y-axis. We render the cursor inverse vertically on the |
| 105 // frame. Hence the inversion on hotspot offset here. | 138 // frame. Hence the inversion on hotspot offset here. |
| 106 cursor_position_in_frame_.Offset(-nshotspot.x, | 139 cursor_position_in_frame_.Offset(-scaled_hotspot.x(), |
| 107 -(nssize.height - nshotspot.y)); | 140 -(last_cursor_height_ - scaled_hotspot.y())); |
| 108 last_cursor_width_ = nssize.width; | |
| 109 last_cursor_height_ = nssize.height; | |
| 110 | 141 |
| 142 // Since OSX Sierra (10.12), the CGImageRef of the cursor image is | |
| 143 // double-size with Retina display. So we give a half-size hint to get the | |
| 144 // CGImageRef with orignal cursor image size on both Retina and non-Retina. | |
| 145 NSRect image_rect = NSMakeRect(0, 0, nssize.width / 2, nssize.height / 2); | |
|
miu
2016/12/06 22:06:23
Instead of doing this here, just allow the higher-
| |
| 111 CGImageRef cg_image = | 146 CGImageRef cg_image = |
| 112 [nsimage CGImageForProposedRect:NULL context:nil hints:nil]; | 147 [nsimage CGImageForProposedRect:&image_rect context:nil hints:nil]; |
| 113 if (!cg_image) | 148 if (!cg_image) |
| 114 return false; | 149 return false; |
| 115 | 150 |
| 116 if (CGImageGetBitsPerPixel(cg_image) != kBytesPerPixel * 8 || | 151 if (CGImageGetBitsPerPixel(cg_image) != kBytesPerPixel * 8 || |
| 117 CGImageGetBytesPerRow(cg_image) != | 152 CGImageGetBytesPerRow(cg_image) != |
| 118 static_cast<size_t>(kBytesPerPixel * nssize.width) || | 153 static_cast<size_t>(kBytesPerPixel * nssize.width) || |
| 119 CGImageGetBitsPerComponent(cg_image) != 8) { | 154 CGImageGetBitsPerComponent(cg_image) != 8) { |
| 120 return false; | 155 return false; |
| 121 } | 156 } |
| 122 | 157 |
| 158 if (x_scale != 1.0f || y_scale != 1.0f) { | |
|
miu
2016/12/06 22:06:23
Now, the only reason to resize is if the cursor im
| |
| 159 // Resize the cursor image to fit in the scaled target frame. | |
| 160 cg_image = ResizeCGImage(cg_image, last_cursor_width_, last_cursor_height_); | |
|
miu
2016/12/06 22:06:23
This is not super-expensive, but not cheap either.
braveyao
2016/12/07 00:56:03
What does 'caching' mean?
I suppose what I should
miu
2016/12/07 20:53:43
I mean 'memoize': Only resize the image whenever t
| |
| 161 if (!cg_image) | |
| 162 return false; | |
| 163 } | |
| 164 | |
| 123 CGDataProviderRef provider = CGImageGetDataProvider(cg_image); | 165 CGDataProviderRef provider = CGImageGetDataProvider(cg_image); |
| 124 CFDataRef image_data_ref = CGDataProviderCopyData(provider); | 166 CFDataRef image_data_ref = CGDataProviderCopyData(provider); |
| 125 if (!image_data_ref) | 167 if (!image_data_ref) |
| 126 return false; | 168 return false; |
| 127 last_cursor_data_.reset(image_data_ref, base::scoped_policy::ASSUME); | 169 last_cursor_data_.reset(image_data_ref, base::scoped_policy::ASSUME); |
| 128 | 170 |
| 129 if (std::abs(mouse_tab_location.x - last_mouse_location_x_) > | 171 if (std::abs(mouse_tab_location.x - last_mouse_location_x_) > |
| 130 MIN_MOVEMENT_PIXELS || | 172 MIN_MOVEMENT_PIXELS || |
| 131 std::abs(mouse_tab_location.y - last_mouse_location_y_) > | 173 std::abs(mouse_tab_location.y - last_mouse_location_y_) > |
| 132 MIN_MOVEMENT_PIXELS) { | 174 MIN_MOVEMENT_PIXELS) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 int color_v = clip_byte( | 225 int color_v = clip_byte( |
| 184 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128); | 226 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128); |
| 185 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]); | 227 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]); |
| 186 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]); | 228 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]); |
| 187 } | 229 } |
| 188 } | 230 } |
| 189 } | 231 } |
| 190 } | 232 } |
| 191 | 233 |
| 192 } // namespace content | 234 } // namespace content |
| OLD | NEW |