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> | |
| 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 | |
| 12 #include <cmath> | |
| 13 | 9 |
| 14 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "ui/gfx/image/image.h" | |
| 15 | 13 |
| 16 namespace content { | 14 namespace content { |
| 17 | 15 |
| 18 namespace { | 16 // static |
| 19 | 17 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) { |
| 20 // RGBA format on cursor bitmap | 18 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 } | 19 } |
| 26 | 20 |
| 27 inline int alpha_blend(int alpha, int src, int dst) { | 21 CursorRendererMac::CursorRendererMac(gfx::NativeView view) |
| 28 return (src * alpha + dst * (255 - alpha)) / 255; | 22 : CursorRenderer(view, kCursorEnabledOnMouseMovement), |
| 29 } | 23 view_(view), |
| 30 | 24 last_known_cursor_location_(gfx::Point()) {} |
| 31 } // namespace | |
| 32 | |
| 33 // static | |
| 34 std::unique_ptr<CursorRenderer> CursorRenderer::Create(gfx::NativeView view) { | |
| 35 return std::unique_ptr<CursorRenderer>(new CursorRendererMac(view)); | |
| 36 } | |
| 37 | |
| 38 CursorRendererMac::CursorRendererMac(NSView* view) | |
| 39 : view_(view), weak_factory_(this) { | |
| 40 Clear(); | |
| 41 } | |
| 42 | 25 |
| 43 CursorRendererMac::~CursorRendererMac() {} | 26 CursorRendererMac::~CursorRendererMac() {} |
| 44 | 27 |
| 45 base::WeakPtr<CursorRenderer> CursorRendererMac::GetWeakPtr() { | 28 bool CursorRendererMac::IsCapturedViewActive() { |
| 46 return weak_factory_.GetWeakPtr(); | 29 if (![[view_ window] isKeyWindow]) { |
| 30 DVLOG(2) << "Window currently inactive"; | |
| 31 return false; | |
| 32 } | |
| 33 return true; | |
| 47 } | 34 } |
| 48 | 35 |
| 49 void CursorRendererMac::Clear() { | 36 gfx::Size CursorRendererMac::GetCapturedViewSize() { |
| 50 last_cursor_data_.reset(); | 37 NSRect frame_rect = [view_ bounds]; |
| 51 last_cursor_width_ = 0; | 38 return gfx::Size(frame_rect.size.width, frame_rect.size.height); |
| 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 } | 39 } |
| 57 | 40 |
| 58 // Polls mouse cursor location and image and returns whether the mouse | 41 gfx::Point CursorRendererMac::GetCursorPositionInView() { |
| 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. | 42 // Mouse location in window co-ordinates. |
| 62 NSPoint mouse_window_location = | 43 NSPoint mouse_window_location = |
| 63 [view_ window].mouseLocationOutsideOfEventStream; | 44 [view_ window].mouseLocationOutsideOfEventStream; |
| 64 // Mouse location with respect to the web contents. | 45 // Mouse location with respect to the web contents. |
|
miu
2016/12/27 23:21:03
nit: s/the web contents/the view within the window
braveyao
2017/01/04 01:57:49
Done.
| |
| 65 NSPoint mouse_tab_location = | 46 NSPoint mouse_view_location = |
| 66 [view_ convertPoint:mouse_window_location fromView:nil]; | 47 [view_ convertPoint:mouse_window_location fromView:nil]; |
| 67 | 48 |
| 68 // Mouse co-ordinates directly comparable against frame co-ordinates | 49 // Revert y coordinate to unify with Aura. |
|
miu
2016/12/27 23:21:03
s/Revert/Invert/
braveyao
2017/01/04 01:57:49
Done.
| |
| 69 // after translation. | 50 gfx::Point cursor_position_in_view( |
| 70 if (mouse_tab_location.x < 0 || mouse_tab_location.y < 0 || | 51 mouse_view_location.x, |
| 71 mouse_tab_location.x > region_in_frame.width() || | 52 GetCapturedViewSize().height() - mouse_view_location.y); |
| 72 mouse_tab_location.y > region_in_frame.height()) { | 53 |
| 73 VLOG(2) << "Mouse outside content region"; | 54 // Update cursor movement info to CursorRenderer. |
|
miu
2016/12/27 23:21:03
This is an unexpected side-effect of calling GetCu
braveyao
2017/01/04 01:57:49
Done.
| |
| 74 return false; | 55 if (cursor_position_in_view != last_known_cursor_location_) { |
| 56 last_known_cursor_location_ = cursor_position_in_view; | |
| 57 OnMouseMoved(cursor_position_in_view, base::TimeTicks::Now()); | |
| 75 } | 58 } |
| 76 | 59 |
| 77 if (![[view_ window] isKeyWindow]) { | 60 return cursor_position_in_view; |
| 78 VLOG(2) << "Window currently inactive"; | 61 } |
| 79 return false; | |
| 80 } | |
| 81 | 62 |
| 82 if ((base::TimeTicks::Now() - last_mouse_movement_timestamp_).InSeconds() > | 63 gfx::NativeCursor CursorRendererMac::GetLastKnownCursor() { |
| 83 MAX_IDLE_TIME_SECONDS && | 64 // Grab system cursor. |
| 84 std::abs(mouse_tab_location.x - last_mouse_location_x_) < | 65 return [NSCursor currentSystemCursor]; |
| 85 MIN_MOVEMENT_PIXELS && | 66 } |
| 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 | 67 |
| 92 // Mouse cursor position within the frame. | 68 SkBitmap CursorRendererMac::GetLastKnownCursorImage() { |
| 93 cursor_position_in_frame_ = | 69 // Grab system cursor. |
| 94 gfx::Point(region_in_frame.x() + mouse_tab_location.x, | 70 NSCursor* nscursor = [NSCursor currentSystemCursor]; |
| 95 region_in_frame.y() + mouse_tab_location.y); | 71 NSImage* nsimage = [nscursor image]; |
| 96 | 72 |
| 73 gfx::Image cursor_image = gfx::Image([nsimage retain]); | |
| 74 return *(cursor_image.ToSkBitmap()); | |
| 75 } | |
| 76 | |
| 77 gfx::Point CursorRendererMac::GetLastKnownCursorHotPoint() { | |
| 97 // Grab system cursor. | 78 // Grab system cursor. |
| 98 NSCursor* nscursor = [NSCursor currentSystemCursor]; | 79 NSCursor* nscursor = [NSCursor currentSystemCursor]; |
| 99 NSPoint nshotspot = [nscursor hotSpot]; | 80 NSPoint nshotspot = [nscursor hotSpot]; |
| 100 NSImage* nsimage = [nscursor image]; | |
| 101 NSSize nssize = [nsimage size]; | |
| 102 | 81 |
| 103 // The cursor co-ordinates in the window and the video frame co-ordinates are | 82 return gfx::Point(nshotspot.x, nshotspot.y); |
| 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; | |
| 138 } | |
| 139 | |
| 140 // Helper function to composite a RGBA cursor bitmap on a YUV420 video frame. | |
| 141 void CursorRendererMac::RenderOnVideoFrame( | |
| 142 const scoped_refptr<media::VideoFrame>& target) const { | |
| 143 DCHECK(target); | |
| 144 DCHECK(last_cursor_data_); | |
| 145 const uint8_t* cursor_data_ = | |
| 146 reinterpret_cast<const uint8_t*>(CFDataGetBytePtr(last_cursor_data_)); | |
| 147 | |
| 148 gfx::Rect visible_rect = target->visible_rect(); | |
| 149 gfx::Rect rect = | |
| 150 gfx::IntersectRects(gfx::Rect(last_cursor_width_, last_cursor_height_) + | |
| 151 gfx::Vector2d(cursor_position_in_frame_.x(), | |
| 152 cursor_position_in_frame_.y()), | |
| 153 visible_rect); | |
| 154 | |
| 155 for (int y = rect.y() + 1; y <= rect.bottom(); ++y) { | |
| 156 int cursor_y = rect.bottom() - y; | |
| 157 int inverted_y = visible_rect.bottom() - y; | |
| 158 uint8_t* yplane = | |
| 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 | |
| 179 // Only sample U and V at even coordinates. | |
| 180 if ((x % 2 == 0) && (y % 2 == 0)) { | |
| 181 int color_u = clip_byte( | |
| 182 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128); | |
| 183 int color_v = clip_byte( | |
| 184 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128); | |
| 185 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]); | |
| 186 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]); | |
| 187 } | |
| 188 } | |
| 189 } | |
| 190 } | 83 } |
| 191 | 84 |
| 192 } // namespace content | 85 } // namespace content |
| OLD | NEW |