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

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

Powered by Google App Engine
This is Rietveld 408576698