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

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

Issue 1412173003: cast: support cursor rendering for tab capture (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed left over logs 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.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
18 namespace content {
19
20 namespace {
21
22 int clip_byte(int x) {
23 return std::max(0, std::min(x, 255));
24 }
25
26 int alpha_blend(int alpha, int src, int dst) {
27 return (src * alpha + dst * (255 - alpha)) / 255;
28 }
29
30 } // namespace
31
32 CursorRenderer::CursorRenderer(aura::Window* window, bool enable_mouse_events)
33 : window_(window), enable_mouse_events_(enable_mouse_events) {
34 if (enable_mouse_events_)
35 window_->AddPreTargetHandler(this);
36 Clear();
37 }
38
39 CursorRenderer::~CursorRenderer() {
40 if (enable_mouse_events_)
41 window_->RemovePreTargetHandler(this);
42 }
43
44 void CursorRenderer::Clear() {
45 last_cursor_ = ui::Cursor();
46 window_size_when_cursor_last_updated_ = gfx::Size();
47 scaled_cursor_bitmap_.reset();
48 }
49
50 bool CursorRenderer::Update(const gfx::Rect& region_in_frame) {
51 const gfx::Rect window_bounds = window_->GetBoundsInScreen();
52 gfx::Point cursor_position = aura::Env::GetInstance()->last_mouse_location();
53 if (!window_bounds.Contains(cursor_position)) {
54 // Return early if there is no need to draw the cursor.
55 Clear();
56 return false;
57 }
58
miu 2015/10/20 02:12:44 Please include changes in: https://codereview.chro
Irfan 2015/10/21 23:01:07 Done.
59 gfx::NativeCursor cursor = window_->GetHost()->last_cursor();
60 gfx::Point cursor_hot_point;
61 if (last_cursor_ != cursor ||
62 window_size_when_cursor_last_updated_ != window_bounds.size()) {
63 SkBitmap cursor_bitmap;
64 if (ui::GetCursorBitmap(cursor, &cursor_bitmap, &cursor_hot_point)) {
65 const int scaled_width = cursor_bitmap.width() * region_in_frame.width() /
66 window_bounds.width();
67 const int scaled_height = cursor_bitmap.height() *
68 region_in_frame.height() /
69 window_bounds.height();
70 if (scaled_width <= 0 || scaled_height <= 0) {
71 Clear();
72 return false;
73 }
74 scaled_cursor_bitmap_ = skia::ImageOperations::Resize(
75 cursor_bitmap, skia::ImageOperations::RESIZE_BEST, scaled_width,
76 scaled_height);
77 last_cursor_ = cursor;
78 window_size_when_cursor_last_updated_ = window_bounds.size();
79 } else {
80 // Clear cursor state if ui::GetCursorBitmap failed so that we do not
81 // render cursor on the captured frame.
82 Clear();
83 }
84 }
85
86 cursor_position.Offset(-window_bounds.x() - cursor_hot_point.x(),
87 -window_bounds.y() - cursor_hot_point.y());
88 cursor_position_in_frame_ = gfx::Point(
89 region_in_frame.x() +
90 cursor_position.x() * region_in_frame.width() / window_bounds.width(),
91 region_in_frame.y() +
92 cursor_position.y() * region_in_frame.height() /
93 window_bounds.height());
94
95 if (cursor_displayed_) {
96 // Stop displaying cursor if there has been no mouse movement
97 base::TimeDelta now = base::TimeDelta::FromInternalValue(
miu 2015/10/20 02:12:44 FromInternalValue() and ToInternalValue() are only
Irfan 2015/10/21 23:01:07 Done.
98 base::TimeTicks::Now().ToInternalValue());
99 if ((now - last_mouse_movement_timestamp_) >
100 base::TimeDelta::FromSeconds(MAX_IDLE_TIME))
101 cursor_displayed_ = false;
102 }
103 return cursor_displayed_;
104 }
105
106 // Helper function to composite a cursor bitmap on a YUV420 video frame.
107 void CursorRenderer::RenderOnVideoFrame(
108 const scoped_refptr<media::VideoFrame>& target) const {
109 if (scaled_cursor_bitmap_.isNull())
110 return;
111
112 DCHECK(target);
113
114 gfx::Rect rect = gfx::IntersectRects(
115 gfx::Rect(scaled_cursor_bitmap_.width(), scaled_cursor_bitmap_.height()) +
116 gfx::Vector2d(cursor_position_in_frame_.x(),
117 cursor_position_in_frame_.y()),
118 target->visible_rect());
119
120 scaled_cursor_bitmap_.lockPixels();
121 for (int y = rect.y(); y < rect.bottom(); ++y) {
122 int cursor_y = y - cursor_position_in_frame_.y();
123 uint8* yplane = target->data(media::VideoFrame::kYPlane) +
124 y * target->row_bytes(media::VideoFrame::kYPlane);
125 uint8* uplane = target->data(media::VideoFrame::kUPlane) +
126 (y / 2) * target->row_bytes(media::VideoFrame::kUPlane);
127 uint8* vplane = target->data(media::VideoFrame::kVPlane) +
128 (y / 2) * target->row_bytes(media::VideoFrame::kVPlane);
129 for (int x = rect.x(); x < rect.right(); ++x) {
130 int cursor_x = x - cursor_position_in_frame_.x();
131 SkColor color = scaled_cursor_bitmap_.getColor(cursor_x, cursor_y);
132 int alpha = SkColorGetA(color);
133 int color_r = SkColorGetR(color);
134 int color_g = SkColorGetG(color);
135 int color_b = SkColorGetB(color);
136 int color_y = clip_byte(
137 ((color_r * 66 + color_g * 129 + color_b * 25 + 128) >> 8) + 16);
138 yplane[x] = alpha_blend(alpha, color_y, yplane[x]);
139
140 // Only sample U and V at even coordinates.
141 if ((x % 2 == 0) && (y % 2 == 0)) {
142 int color_u = clip_byte(
143 ((color_r * -38 + color_g * -74 + color_b * 112 + 128) >> 8) + 128);
144 int color_v = clip_byte(
145 ((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) + 128);
146 uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]);
147 vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]);
148 }
149 }
150 }
151 scaled_cursor_bitmap_.unlockPixels();
152 }
153
154 void CursorRenderer::OnMouseEvent(ui::MouseEvent* event) {
155 switch (event->type()) {
156 case ui::ET_MOUSE_MOVED:
157 if (!cursor_displayed_) {
158 if ((event->x() - last_mouse_position_x_) > MIN_MOVEMENT ||
159 (event->y() - last_mouse_position_y_) > MIN_MOVEMENT)
160 cursor_displayed_ = true;
161 }
162 if (cursor_displayed_) {
163 last_mouse_movement_timestamp_ = event->time_stamp();
164 last_mouse_position_x_ = event->x();
165 last_mouse_position_y_ = event->y();
166 }
167 break;
168 default:
169 break;
170 }
171 }
172
173 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698