dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 1 | // Copyright 2014 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 "chrome/browser/devtools/devtools_eye_dropper.h" |
| 6 | |
Yuri Wiitala | fc5fe70 | 2018-06-20 06:14:00 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 9 | #include "base/bind.h" |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 10 | #include "base/memory/shared_memory_mapping.h" |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 11 | #include "build/build_config.h" |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 12 | #include "cc/paint/skia_paint_canvas.h" |
| 13 | #include "components/viz/common/features.h" |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 14 | #include "content/public/browser/render_view_host.h" |
| 15 | #include "content/public/browser/render_widget_host.h" |
| 16 | #include "content/public/browser/render_widget_host_view.h" |
| 17 | #include "content/public/browser/web_contents.h" |
Saman Sami | d81a0e64 | 2018-03-19 04:51:15 | [diff] [blame] | 18 | #include "content/public/common/content_features.h" |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 19 | #include "content/public/common/cursor_info.h" |
| 20 | #include "content/public/common/screen_info.h" |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 21 | #include "media/base/limits.h" |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 22 | #include "media/base/video_frame.h" |
Takuto Ikuta | 0ee5934a | 2019-01-31 05:49:38 | [diff] [blame] | 23 | #include "media/capture/mojom/video_capture_types.mojom.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 24 | #include "third_party/blink/public/platform/web_input_event.h" |
| 25 | #include "third_party/blink/public/platform/web_mouse_event.h" |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 26 | #include "third_party/skia/include/core/SkCanvas.h" |
| 27 | #include "third_party/skia/include/core/SkPaint.h" |
| 28 | #include "third_party/skia/include/core/SkPath.h" |
Mike Klein | 7008dab | 2018-09-28 21:58:51 | [diff] [blame] | 29 | #include "third_party/skia/include/core/SkPixmap.h" |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 30 | #include "ui/gfx/geometry/size_conversions.h" |
| 31 | |
| 32 | DevToolsEyeDropper::DevToolsEyeDropper(content::WebContents* web_contents, |
| 33 | EyeDropperCallback callback) |
| 34 | : content::WebContentsObserver(web_contents), |
| 35 | callback_(callback), |
| 36 | last_cursor_x_(-1), |
| 37 | last_cursor_y_(-1), |
| 38 | host_(nullptr), |
| 39 | weak_factory_(this) { |
| 40 | mouse_event_callback_ = |
| 41 | base::Bind(&DevToolsEyeDropper::HandleMouseEvent, base::Unretained(this)); |
| 42 | content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); |
Saman Sami | 5cc093d | 2018-08-08 12:12:25 | [diff] [blame] | 43 | if (rvh) |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 44 | AttachToHost(rvh->GetWidget()); |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | DevToolsEyeDropper::~DevToolsEyeDropper() { |
| 48 | DetachFromHost(); |
| 49 | } |
| 50 | |
| 51 | void DevToolsEyeDropper::AttachToHost(content::RenderWidgetHost* host) { |
| 52 | host_ = host; |
| 53 | host_->AddMouseEventCallback(mouse_event_callback_); |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 54 | |
Saman Sami | e1e8576 | 2018-06-01 18:38:33 | [diff] [blame] | 55 | // The view can be null if the renderer process has crashed. |
| 56 | // (https://crbug.com/847363) |
| 57 | if (!host_->GetView()) |
| 58 | return; |
| 59 | |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 60 | // Capturing a full-page screenshot can be costly so we shouldn't do it too |
| 61 | // often. We can capture at a lower frame rate without hurting the user |
| 62 | // experience. |
| 63 | constexpr static int kMaxFrameRate = 15; |
| 64 | |
| 65 | // Create and configure the video capturer. |
| 66 | video_capturer_ = host_->GetView()->CreateVideoCapturer(); |
| 67 | video_capturer_->SetResolutionConstraints( |
Saman Sami | 493222f | 2018-04-05 03:41:17 | [diff] [blame] | 68 | host_->GetView()->GetViewBounds().size(), |
| 69 | host_->GetView()->GetViewBounds().size(), true); |
Saman Sami | 561d774 | 2018-03-17 02:22:32 | [diff] [blame] | 70 | video_capturer_->SetAutoThrottlingEnabled(false); |
| 71 | video_capturer_->SetMinSizeChangePeriod(base::TimeDelta()); |
Saman Sami | 00d3544 | 2018-03-20 20:22:39 | [diff] [blame] | 72 | video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB, |
Fredrik Hubinette | 088f6dc | 2018-10-04 19:42:30 | [diff] [blame] | 73 | gfx::ColorSpace::CreateREC709()); |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 74 | video_capturer_->SetMinCapturePeriod(base::TimeDelta::FromSeconds(1) / |
| 75 | kMaxFrameRate); |
Saman Sami | d2dc25f | 2018-05-24 20:41:07 | [diff] [blame] | 76 | video_capturer_->Start(this); |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void DevToolsEyeDropper::DetachFromHost() { |
| 80 | if (!host_) |
| 81 | return; |
| 82 | host_->RemoveMouseEventCallback(mouse_event_callback_); |
| 83 | content::CursorInfo cursor_info; |
| 84 | cursor_info.type = blink::WebCursorInfo::kTypePointer; |
| 85 | host_->SetCursor(cursor_info); |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 86 | video_capturer_.reset(); |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 87 | host_ = nullptr; |
| 88 | } |
| 89 | |
| 90 | void DevToolsEyeDropper::RenderViewCreated(content::RenderViewHost* host) { |
Saman Sami | 5cc093d | 2018-08-08 12:12:25 | [diff] [blame] | 91 | if (!host_) |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 92 | AttachToHost(host->GetWidget()); |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void DevToolsEyeDropper::RenderViewDeleted(content::RenderViewHost* host) { |
| 96 | if (host->GetWidget() == host_) { |
| 97 | DetachFromHost(); |
| 98 | ResetFrame(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void DevToolsEyeDropper::RenderViewHostChanged( |
| 103 | content::RenderViewHost* old_host, |
| 104 | content::RenderViewHost* new_host) { |
| 105 | if ((old_host && old_host->GetWidget() == host_) || (!old_host && !host_)) { |
| 106 | DetachFromHost(); |
| 107 | AttachToHost(new_host->GetWidget()); |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 111 | void DevToolsEyeDropper::ResetFrame() { |
| 112 | frame_.reset(); |
| 113 | last_cursor_x_ = -1; |
| 114 | last_cursor_y_ = -1; |
| 115 | } |
| 116 | |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 117 | bool DevToolsEyeDropper::HandleMouseEvent(const blink::WebMouseEvent& event) { |
| 118 | last_cursor_x_ = event.PositionInWidget().x; |
| 119 | last_cursor_y_ = event.PositionInWidget().y; |
| 120 | if (frame_.drawsNothing()) |
| 121 | return true; |
| 122 | |
| 123 | if (event.button == blink::WebMouseEvent::Button::kLeft && |
| 124 | (event.GetType() == blink::WebInputEvent::kMouseDown || |
| 125 | event.GetType() == blink::WebInputEvent::kMouseMove)) { |
| 126 | if (last_cursor_x_ < 0 || last_cursor_x_ >= frame_.width() || |
| 127 | last_cursor_y_ < 0 || last_cursor_y_ >= frame_.height()) { |
| 128 | return true; |
| 129 | } |
| 130 | |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 131 | SkColor sk_color = frame_.getColor(last_cursor_x_, last_cursor_y_); |
Christopher Cameron | 95fb5ee6 | 2017-08-23 12:50:58 | [diff] [blame] | 132 | |
Mike Klein | 7008dab | 2018-09-28 21:58:51 | [diff] [blame] | 133 | // The picked colors are expected to be sRGB. Convert from |frame_|'s color |
| 134 | // space to sRGB. |
Mike Klein | 7008dab | 2018-09-28 21:58:51 | [diff] [blame] | 135 | SkPixmap pm( |
| 136 | SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, |
Yuri Wiitala | ed44fc4 | 2018-12-15 03:14:24 | [diff] [blame] | 137 | frame_.refColorSpace()), |
Mike Klein | 7008dab | 2018-09-28 21:58:51 | [diff] [blame] | 138 | &sk_color, sizeof(sk_color)); |
Mike Klein | 7008dab | 2018-09-28 21:58:51 | [diff] [blame] | 139 | uint8_t rgba_color[4]; |
| 140 | bool ok = pm.readPixels( |
| 141 | SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, |
| 142 | SkColorSpace::MakeSRGB()), |
| 143 | rgba_color, sizeof(rgba_color)); |
| 144 | DCHECK(ok); |
Christopher Cameron | 95fb5ee6 | 2017-08-23 12:50:58 | [diff] [blame] | 145 | |
| 146 | callback_.Run(rgba_color[0], rgba_color[1], rgba_color[2], rgba_color[3]); |
dgozman | 1137e62 | 2017-04-17 19:49:12 | [diff] [blame] | 147 | } |
| 148 | UpdateCursor(); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | void DevToolsEyeDropper::UpdateCursor() { |
| 153 | if (!host_ || frame_.drawsNothing()) |
| 154 | return; |
| 155 | |
| 156 | if (last_cursor_x_ < 0 || last_cursor_x_ >= frame_.width() || |
| 157 | last_cursor_y_ < 0 || last_cursor_y_ >= frame_.height()) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Due to platform limitations, we are using two different cursors |
| 162 | // depending on the platform. Mac and Win have large cursors with two circles |
| 163 | // for original spot and its magnified projection; Linux gets smaller (64 px) |
| 164 | // magnified projection only with centered hotspot. |
| 165 | // Mac Retina requires cursor to be > 120px in order to render smoothly. |
| 166 | |
| 167 | #if defined(OS_LINUX) |
| 168 | const float kCursorSize = 63; |
| 169 | const float kDiameter = 63; |
| 170 | const float kHotspotOffset = 32; |
| 171 | const float kHotspotRadius = 0; |
| 172 | const float kPixelSize = 9; |
| 173 | #else |
| 174 | const float kCursorSize = 150; |
| 175 | const float kDiameter = 110; |
| 176 | const float kHotspotOffset = 25; |
| 177 | const float kHotspotRadius = 5; |
| 178 | const float kPixelSize = 10; |
| 179 | #endif |
| 180 | |
| 181 | content::ScreenInfo screen_info; |
| 182 | host_->GetScreenInfo(&screen_info); |
| 183 | double device_scale_factor = screen_info.device_scale_factor; |
| 184 | |
| 185 | SkBitmap result; |
| 186 | result.allocN32Pixels(kCursorSize * device_scale_factor, |
| 187 | kCursorSize * device_scale_factor); |
| 188 | result.eraseARGB(0, 0, 0, 0); |
| 189 | |
| 190 | SkCanvas canvas(result); |
| 191 | canvas.scale(device_scale_factor, device_scale_factor); |
| 192 | canvas.translate(0.5f, 0.5f); |
| 193 | |
| 194 | SkPaint paint; |
| 195 | |
| 196 | // Paint original spot with cross. |
| 197 | if (kHotspotRadius > 0) { |
| 198 | paint.setStrokeWidth(1); |
| 199 | paint.setAntiAlias(false); |
| 200 | paint.setColor(SK_ColorDKGRAY); |
| 201 | paint.setStyle(SkPaint::kStroke_Style); |
| 202 | |
| 203 | canvas.drawLine(kHotspotOffset, kHotspotOffset - 2 * kHotspotRadius, |
| 204 | kHotspotOffset, kHotspotOffset - kHotspotRadius, paint); |
| 205 | canvas.drawLine(kHotspotOffset, kHotspotOffset + kHotspotRadius, |
| 206 | kHotspotOffset, kHotspotOffset + 2 * kHotspotRadius, paint); |
| 207 | canvas.drawLine(kHotspotOffset - 2 * kHotspotRadius, kHotspotOffset, |
| 208 | kHotspotOffset - kHotspotRadius, kHotspotOffset, paint); |
| 209 | canvas.drawLine(kHotspotOffset + kHotspotRadius, kHotspotOffset, |
| 210 | kHotspotOffset + 2 * kHotspotRadius, kHotspotOffset, paint); |
| 211 | |
| 212 | paint.setStrokeWidth(2); |
| 213 | paint.setAntiAlias(true); |
| 214 | canvas.drawCircle(kHotspotOffset, kHotspotOffset, kHotspotRadius, paint); |
| 215 | } |
| 216 | |
| 217 | // Clip circle for magnified projection. |
| 218 | float padding = (kCursorSize - kDiameter) / 2; |
| 219 | SkPath clip_path; |
| 220 | clip_path.addOval(SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter)); |
| 221 | clip_path.close(); |
| 222 | canvas.clipPath(clip_path, SkClipOp::kIntersect, true); |
| 223 | |
| 224 | // Project pixels. |
| 225 | int pixel_count = kDiameter / kPixelSize; |
| 226 | SkRect src_rect = SkRect::MakeXYWH(last_cursor_x_ - pixel_count / 2, |
| 227 | last_cursor_y_ - pixel_count / 2, |
| 228 | pixel_count, pixel_count); |
| 229 | SkRect dst_rect = SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter); |
| 230 | canvas.drawBitmapRect(frame_, src_rect, dst_rect, NULL); |
| 231 | |
| 232 | // Paint grid. |
| 233 | paint.setStrokeWidth(1); |
| 234 | paint.setAntiAlias(false); |
| 235 | paint.setColor(SK_ColorGRAY); |
| 236 | for (int i = 0; i < pixel_count; ++i) { |
| 237 | canvas.drawLine(padding + i * kPixelSize, padding, padding + i * kPixelSize, |
| 238 | kCursorSize - padding, paint); |
| 239 | canvas.drawLine(padding, padding + i * kPixelSize, kCursorSize - padding, |
| 240 | padding + i * kPixelSize, paint); |
| 241 | } |
| 242 | |
| 243 | // Paint central pixel in red. |
| 244 | SkRect pixel = |
| 245 | SkRect::MakeXYWH((kCursorSize - kPixelSize) / 2, |
| 246 | (kCursorSize - kPixelSize) / 2, kPixelSize, kPixelSize); |
| 247 | paint.setColor(SK_ColorRED); |
| 248 | paint.setStyle(SkPaint::kStroke_Style); |
| 249 | canvas.drawRect(pixel, paint); |
| 250 | |
| 251 | // Paint outline. |
| 252 | paint.setStrokeWidth(2); |
| 253 | paint.setColor(SK_ColorDKGRAY); |
| 254 | paint.setAntiAlias(true); |
| 255 | canvas.drawCircle(kCursorSize / 2, kCursorSize / 2, kDiameter / 2, paint); |
| 256 | |
| 257 | content::CursorInfo cursor_info; |
| 258 | cursor_info.type = blink::WebCursorInfo::kTypeCustom; |
| 259 | cursor_info.image_scale_factor = device_scale_factor; |
| 260 | cursor_info.custom_image = result; |
| 261 | cursor_info.hotspot = gfx::Point(kHotspotOffset * device_scale_factor, |
| 262 | kHotspotOffset * device_scale_factor); |
| 263 | host_->SetCursor(cursor_info); |
| 264 | } |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 265 | |
| 266 | void DevToolsEyeDropper::OnFrameCaptured( |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 267 | base::ReadOnlySharedMemoryRegion data, |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 268 | ::media::mojom::VideoFrameInfoPtr info, |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 269 | const gfx::Rect& content_rect, |
| 270 | viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) { |
Saman Sami | 493222f | 2018-04-05 03:41:17 | [diff] [blame] | 271 | gfx::Size view_size = host_->GetView()->GetViewBounds().size(); |
| 272 | if (view_size != content_rect.size()) { |
| 273 | video_capturer_->SetResolutionConstraints(view_size, view_size, true); |
| 274 | video_capturer_->RequestRefreshFrame(); |
| 275 | return; |
| 276 | } |
| 277 | |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 278 | if (!data.IsValid()) { |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 279 | callbacks->Done(); |
| 280 | return; |
| 281 | } |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 282 | base::ReadOnlySharedMemoryMapping mapping = data.Map(); |
| 283 | if (!mapping.IsValid()) { |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 284 | DLOG(ERROR) << "Shared memory mapping failed."; |
| 285 | return; |
| 286 | } |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 287 | if (mapping.size() < |
| 288 | media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) { |
| 289 | DLOG(ERROR) << "Shared memory size was less than expected."; |
| 290 | return; |
| 291 | } |
Yuri Wiitala | ed44fc4 | 2018-12-15 03:14:24 | [diff] [blame] | 292 | if (!info->color_space) { |
| 293 | DLOG(ERROR) << "Missing mandatory color space info."; |
| 294 | return; |
| 295 | } |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 296 | |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 297 | // The SkBitmap's pixels will be marked as immutable, but the installPixels() |
| 298 | // API requires a non-const pointer. So, cast away the const. |
| 299 | void* const pixels = const_cast<void*>(mapping.memory()); |
| 300 | |
| 301 | // Call installPixels() with a |releaseProc| that: 1) notifies the capturer |
| 302 | // that this consumer has finished with the frame, and 2) releases the shared |
| 303 | // memory mapping. |
| 304 | struct FramePinner { |
| 305 | // Keeps the shared memory that backs |frame_| mapped. |
| 306 | base::ReadOnlySharedMemoryMapping mapping; |
| 307 | // Prevents FrameSinkVideoCapturer from recycling the shared memory that |
| 308 | // backs |frame_|. |
| 309 | viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr releaser; |
| 310 | }; |
| 311 | frame_.installPixels( |
| 312 | SkImageInfo::MakeN32(content_rect.width(), content_rect.height(), |
Yuri Wiitala | ed44fc4 | 2018-12-15 03:14:24 | [diff] [blame] | 313 | kPremul_SkAlphaType, |
| 314 | info->color_space->ToSkColorSpace()), |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 315 | pixels, |
| 316 | media::VideoFrame::RowBytes(media::VideoFrame::kARGBPlane, |
| 317 | info->pixel_format, info->coded_size.width()), |
| 318 | [](void* addr, void* context) { |
| 319 | delete static_cast<FramePinner*>(context); |
| 320 | }, |
| 321 | new FramePinner{std::move(mapping), std::move(callbacks)}); |
| 322 | frame_.setImmutable(); |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 323 | |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 324 | UpdateCursor(); |
| 325 | } |
| 326 | |
Saman Sami | 2acabd1 | 2018-03-10 00:51:09 | [diff] [blame] | 327 | void DevToolsEyeDropper::OnStopped() {} |