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

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

Issue 1412173003: cast: support cursor rendering for tab capture (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing cursor renderer files in repo Created 5 years, 2 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) 2015 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_aura.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "ui/aura/client/screen_position_client.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_tree_host.h"
14 #include "ui/base/cursor/cursors_aura.h"
15 #include "ui/compositor/dip_util.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/wm/public/activation_client.h"
19
20 namespace content {
21
22 namespace {
23
24 int clip_byte(int x) {
miu 2015/10/23 01:55:12 Suggestion: Use 'inline' keyword for clip_byte() a
Irfan 2015/10/26 23:10:31 Done.
25 return std::max(0, std::min(x, 255));
26 }
27
28 int alpha_blend(int alpha, int src, int dst) {
29 return (src * alpha + dst * (255 - alpha)) / 255;
30 }
31
32 } // namespace
33
34 CursorRendererAura::CursorRendererAura(aura::Window* window) :
35 window_(window),
36 weak_factory_(this) {
37 if (window_) {
38 window_->AddObserver(this);
39 window_->AddPreTargetHandler(this);
40 }
41 Clear();
42 }
43
44 CursorRendererAura::~CursorRendererAura() {
45 if (window_) {
46 window_->RemoveObserver(this);
47 window_->RemovePreTargetHandler(this);
48 }
49 }
50
51 base::WeakPtr<CursorRenderer> CursorRendererAura::GetWeakPtr() {
52 return weak_factory_.GetWeakPtr();
53 }
54
55 void CursorRendererAura::Clear() {
56 last_cursor_ = ui::Cursor();
57 window_size_when_cursor_last_updated_ = gfx::Size();
58 scaled_cursor_bitmap_.reset();
59 last_mouse_position_x_ = 0;
60 last_mouse_position_y_ = 0;
61 cursor_displayed_ = false;
62 }
63
64 bool CursorRendererAura::Update(const gfx::Rect& region_in_frame) {
65 if (!window_) {
66 DVLOG(2) << "Skipping update with window empty";
miu 2015/10/23 01:55:12 Suggestion: s/window empty/no window being tracked
Irfan 2015/10/26 23:10:31 Done.
67 return false;
68 }
69 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
70 gfx::Point cursor_position = aura::Env::GetInstance()->last_mouse_location();
71 if (!window_bounds.Contains(cursor_position)) {
72 // Return early if there is no need to draw the cursor.
73 Clear();
74 return false;
75 }
76
77
xjz 2015/10/22 17:08:42 nit: remove one blank line
Irfan 2015/10/26 23:10:31 Done.
78 aura::client::ActivationClient* activation_client =
79 aura::client::GetActivationClient(window_->GetRootWindow());
80 DCHECK(activation_client);
81 aura::Window* active_window = activation_client->GetActiveWindow();
82 if (!active_window->Contains(window_)) {
83 // Return early if the target window is not active.
84 Clear();
85 return false;
86 }
87
88 gfx::NativeCursor cursor = window_->GetHost()->last_cursor();
89 gfx::Point cursor_hot_point;
90 if (last_cursor_ != cursor ||
91 window_size_when_cursor_last_updated_ != window_bounds.size()) {
92 SkBitmap cursor_bitmap;
93 if (ui::GetCursorBitmap(cursor, &cursor_bitmap, &cursor_hot_point)) {
94 const int scaled_width = cursor_bitmap.width() * region_in_frame.width() /
95 window_bounds.width();
96 const int scaled_height = cursor_bitmap.height() *
97 region_in_frame.height() /
98 window_bounds.height();
99 if (scaled_width <= 0 || scaled_height <= 0) {
100 Clear();
101 return false;
102 }
103 scaled_cursor_bitmap_ = skia::ImageOperations::Resize(
104 cursor_bitmap, skia::ImageOperations::RESIZE_BEST, scaled_width,
105 scaled_height);
106 last_cursor_ = cursor;
107 window_size_when_cursor_last_updated_ = window_bounds.size();
108 } else {
109 // Clear cursor state if ui::GetCursorBitmap failed so that we do not
110 // render cursor on the captured frame.
111 Clear();
112 }
113 }
114
115 cursor_position.Offset(-window_bounds.x() - cursor_hot_point.x(),
116 -window_bounds.y() - cursor_hot_point.y());
117 cursor_position_in_frame_ = gfx::Point(
118 region_in_frame.x() +
119 cursor_position.x() * region_in_frame.width() / window_bounds.width(),
120 region_in_frame.y() +
121 cursor_position.y() * region_in_frame.height() /
122 window_bounds.height());
123
124 if (cursor_displayed_) {
125 // Stop displaying cursor if there has been no mouse movement
126 base::TimeDelta now = ui::EventTimeForNow();
127 if ((now - last_mouse_movement_timestamp_) >
128 base::TimeDelta::FromSeconds(MAX_IDLE_TIME_SECONDS)) {
129 cursor_displayed_ = false;
130 DVLOG(2) << "Turning off cursor display after idle time";
131 }
132 }
133 return cursor_displayed_;
134 }
135
136 // Helper function to composite a cursor bitmap on a YUV420 video frame.
137 void CursorRendererAura::RenderOnVideoFrame(
138 const scoped_refptr<media::VideoFrame>& target) const {
139 if (scaled_cursor_bitmap_.isNull())
140 return;
141
142 DCHECK(target);
143
144 gfx::Rect rect = gfx::IntersectRects(
145 gfx::Rect(scaled_cursor_bitmap_.width(), scaled_cursor_bitmap_.height()) +
146 gfx::Vector2d(cursor_position_in_frame_.x(),
147 cursor_position_in_frame_.y()),
148 target->visible_rect());
149
150 scaled_cursor_bitmap_.lockPixels();
151 for (int y = rect.y(); y < rect.bottom(); ++y) {
152 int cursor_y = y - cursor_position_in_frame_.y();
153 uint8* yplane = target->data(media::VideoFrame::kYPlane) +
154 y * target->row_bytes(media::VideoFrame::kYPlane);
155 uint8* uplane = target->data(media::VideoFrame::kUPlane) +
156 (y / 2) * target->row_bytes(media::VideoFrame::kUPlane);
157 uint8* vplane = target->data(media::VideoFrame::kVPlane) +
158 (y / 2) * target->row_bytes(media::VideoFrame::kVPlane);
159 for (int x = rect.x(); x < rect.right(); ++x) {
160 int cursor_x = x - cursor_position_in_frame_.x();
161 SkColor color = scaled_cursor_bitmap_.getColor(cursor_x, cursor_y);
162 int alpha = SkColorGetA(color);
163 int color_r = SkColorGetR(color);
164 int color_g = SkColorGetG(color);
165 int color_b = SkColorGetB(color);
166 int color_y = clip_byte(
167 ((color_r * 66 + color_g * 129 + color_b * 25 + 128) >> 8) + 16);
168 yplane[x] = alpha_blend(alpha, color_y, yplane[x]);
169
170 // Only sample U and V at even coordinates.
171 if ((x % 2 == 0) && (y % 2 == 0)) {
172 int color_u = clip_byte(
173 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128);
174 int color_v = clip_byte(
175 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128);
176 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]);
177 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]);
178 }
179 }
180 }
181 scaled_cursor_bitmap_.unlockPixels();
182 }
183
184 void CursorRendererAura::OnMouseEvent(ui::MouseEvent* event) {
185 switch (event->type()) {
186 case ui::ET_MOUSE_MOVED:
187 if (!cursor_displayed_) {
188 if ((event->x() - last_mouse_position_x_) > MIN_MOVEMENT_PIXELS ||
189 (event->y() - last_mouse_position_y_) > MIN_MOVEMENT_PIXELS)
190 cursor_displayed_ = true;
191 }
192 if (cursor_displayed_) {
193 last_mouse_movement_timestamp_ = event->time_stamp();
194 last_mouse_position_x_ = event->x();
195 last_mouse_position_y_ = event->y();
196 }
197 break;
198 default:
199 break;
200 }
201 }
202
203 void CursorRendererAura::OnWindowDestroying(aura::Window* window) {
204 DCHECK_EQ(window_, window);
205 window_->RemovePreTargetHandler(this);
206 window_->RemoveObserver(this);
207 window_ = nullptr;
208 }
209
210 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698