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

Side by Side Diff: content/browser/media/capture/cursor_renderer.cc

Issue 2553763002: Fix cursor missing in tabCapture on OSX Sierra (Closed)
Patch Set: rebase Created 3 years, 11 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
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/media/capture/cursor_renderer.h"
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "skia/ext/image_operations.h"
13
14 namespace content {
15
16 namespace {
17
18 inline int clip_byte(int x) {
19 return std::max(0, std::min(x, 255));
20 }
21
22 inline int alpha_blend(int alpha, int src, int dst) {
23 return (src * alpha + dst * (255 - alpha)) / 255;
24 }
25
26 } // namespace
27
28 CursorRenderer::CursorRenderer(gfx::NativeView view,
29 CursorDisplaySetting cursor_display_setting)
30 : captured_view_(view),
31 cursor_display_setting_(cursor_display_setting),
32 tick_clock_(&default_tick_clock_),
33 weak_factory_(this) {
34 Clear();
35 }
36
37 CursorRenderer::~CursorRenderer() {}
38
39 base::WeakPtr<CursorRenderer> CursorRenderer::GetWeakPtr() {
40 return weak_factory_.GetWeakPtr();
41 }
42
43 void CursorRenderer::Clear() {
44 last_x_scale_ = 0.0f;
45 last_y_scale_ = 0.0f;
46 last_cursor_ = gfx::NativeCursor();
47 last_cursor_hot_point_ = gfx::Point();
48 scaled_cursor_bitmap_.reset();
49 last_mouse_position_x_ = 0;
50 last_mouse_position_y_ = 0;
51 last_mouse_movement_timestamp_ = base::TimeTicks::Now();
52 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) {
53 cursor_displayed_ = false;
54 } else {
55 cursor_displayed_ = true;
56 }
57 }
58
59 bool CursorRenderer::SnapshotCursorState(const gfx::Rect& region_in_frame) {
60 if (!captured_view_) {
61 DVLOG(2) << "Skipping update with no view being tracked";
62 return false;
63 }
64
65 // If we are sharing the root view, or namely the whole screen, we will
66 // render the mouse cursor. For ordinary view, we only render the mouse
67 // cursor when the view is active.
68 if (!IsCapturedViewActive()) {
69 // Return early if the target view is not active.
70 DVLOG(2) << "Skipping update on an inactive view";
71 Clear();
72 return false;
73 }
74
75 gfx::Size view_size = GetCapturedViewSize();
76 if (view_size.IsEmpty()) {
77 DVLOG(2) << "Skipping update on an empty view";
78 Clear();
79 return false;
80 }
81
82 gfx::Point cursor_position = GetCursorPositionInView();
83 if (!gfx::Rect(view_size).Contains(cursor_position)) {
84 DVLOG(2) << "Skipping update with cursor outside the view";
85 Clear();
86 return false;
87 }
88
89 if (cursor_display_setting_ == kCursorEnabledOnMouseMovement) {
90 if (cursor_displayed_) {
91 // Stop displaying cursor if there has been no mouse movement
92 base::TimeTicks now = tick_clock_->NowTicks();
93 if ((now - last_mouse_movement_timestamp_) >
94 base::TimeDelta::FromSeconds(MAX_IDLE_TIME_SECONDS)) {
95 cursor_displayed_ = false;
96 DVLOG(2) << "Turning off cursor display after idle time";
97 }
98 }
99 if (!cursor_displayed_)
100 return false;
101 }
102
103 const float x_scale =
104 static_cast<float>(region_in_frame.width()) / view_size.width();
105 const float y_scale =
106 static_cast<float>(region_in_frame.height()) / view_size.height();
107
108 gfx::NativeCursor cursor = GetLastKnownCursor();
109 if (last_cursor_ != cursor || last_x_scale_ != x_scale ||
110 last_y_scale_ != y_scale) {
111 SkBitmap cursor_bitmap = GetLastKnownCursorImage(&last_cursor_hot_point_);
112 const int scaled_width = cursor_bitmap.width() * x_scale;
113 const int scaled_height = cursor_bitmap.height() * y_scale;
114 if (scaled_width <= 0 || scaled_height <= 0) {
115 DVLOG(2) << "scaled_width <= 0";
116 Clear();
117 return false;
118 }
119 scaled_cursor_bitmap_ = skia::ImageOperations::Resize(
120 cursor_bitmap, skia::ImageOperations::RESIZE_BEST, scaled_width,
121 scaled_height);
122
123 last_cursor_ = cursor;
124 last_x_scale_ = x_scale;
125 last_y_scale_ = y_scale;
126 }
127
128 cursor_position.Offset(-last_cursor_hot_point_.x(),
129 -last_cursor_hot_point_.y());
130 cursor_position_in_frame_ =
131 gfx::Point(region_in_frame.x() + cursor_position.x() * x_scale,
132 region_in_frame.y() + cursor_position.y() * y_scale);
133 return true;
134 }
135
136 // Helper function to composite a cursor bitmap on a YUV420 video frame.
137 void CursorRenderer::RenderOnVideoFrame(media::VideoFrame* target) const {
138 if (scaled_cursor_bitmap_.isNull())
139 return;
140
141 DCHECK(target);
142
143 gfx::Rect rect = gfx::IntersectRects(
144 gfx::Rect(scaled_cursor_bitmap_.width(), scaled_cursor_bitmap_.height()) +
145 gfx::Vector2d(cursor_position_in_frame_.x(),
146 cursor_position_in_frame_.y()),
147 target->visible_rect());
148
149 scaled_cursor_bitmap_.lockPixels();
150 for (int y = rect.y(); y < rect.bottom(); ++y) {
151 int cursor_y = y - cursor_position_in_frame_.y();
152 uint8_t* yplane = target->data(media::VideoFrame::kYPlane) +
153 y * target->row_bytes(media::VideoFrame::kYPlane);
154 uint8_t* uplane = target->data(media::VideoFrame::kUPlane) +
155 (y / 2) * target->row_bytes(media::VideoFrame::kUPlane);
156 uint8_t* vplane = target->data(media::VideoFrame::kVPlane) +
157 (y / 2) * target->row_bytes(media::VideoFrame::kVPlane);
158 for (int x = rect.x(); x < rect.right(); ++x) {
159 int cursor_x = x - cursor_position_in_frame_.x();
160 SkColor color = scaled_cursor_bitmap_.getColor(cursor_x, cursor_y);
161 int alpha = SkColorGetA(color);
162 int color_r = SkColorGetR(color);
163 int color_g = SkColorGetG(color);
164 int color_b = SkColorGetB(color);
165 int color_y = clip_byte(
166 ((color_r * 66 + color_g * 129 + color_b * 25 + 128) >> 8) + 16);
167 yplane[x] = alpha_blend(alpha, color_y, yplane[x]);
168
169 // Only sample U and V at even coordinates.
170 if ((x % 2 == 0) && (y % 2 == 0)) {
171 int color_u = clip_byte(
172 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128);
173 int color_v = clip_byte(
174 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128);
175 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]);
176 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]);
177 }
178 }
179 }
180 scaled_cursor_bitmap_.unlockPixels();
181 }
182
183 void CursorRenderer::OnMouseMoved(const gfx::Point& location,
184 base::TimeTicks timestamp) {
185 if (!cursor_displayed_) {
186 if (std::abs(location.x() - last_mouse_position_x_) > MIN_MOVEMENT_PIXELS ||
187 std::abs(location.y() - last_mouse_position_y_) > MIN_MOVEMENT_PIXELS)
188 cursor_displayed_ = true;
189 }
190
191 if (cursor_displayed_) {
192 last_mouse_movement_timestamp_ = timestamp;
193 last_mouse_position_x_ = location.x();
194 last_mouse_position_y_ = location.y();
195 }
196 }
197
198 void CursorRenderer::OnMouseClicked(const gfx::Point& location,
199 base::TimeTicks timestamp) {
200 cursor_displayed_ = true;
201 last_mouse_movement_timestamp_ = timestamp;
202 last_mouse_position_x_ = location.x();
203 last_mouse_position_y_ = location.y();
204 }
205
206 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/capture/cursor_renderer.h ('k') | content/browser/media/capture/cursor_renderer_aura.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698