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

Side by Side Diff: content/browser/renderer_host/render_widget_host_impl.cc

Issue 2592983002: [devtools] Support different encodings for Page.CaptureScreenshot. (Closed)
Patch Set: Encode in ui snapshot methods instead. 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/render_widget_host_impl.h" 5 #include "content/browser/renderer_host/render_widget_host_impl.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <set> 9 #include <set>
10 #include <tuple> 10 #include <tuple>
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" 82 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
83 #include "ui/base/clipboard/clipboard.h" 83 #include "ui/base/clipboard/clipboard.h"
84 #include "ui/events/blink/web_input_event_traits.h" 84 #include "ui/events/blink/web_input_event_traits.h"
85 #include "ui/events/event.h" 85 #include "ui/events/event.h"
86 #include "ui/events/keycodes/keyboard_codes.h" 86 #include "ui/events/keycodes/keyboard_codes.h"
87 #include "ui/gfx/color_space.h" 87 #include "ui/gfx/color_space.h"
88 #include "ui/gfx/geometry/size_conversions.h" 88 #include "ui/gfx/geometry/size_conversions.h"
89 #include "ui/gfx/geometry/vector2d_conversions.h" 89 #include "ui/gfx/geometry/vector2d_conversions.h"
90 #include "ui/gfx/image/image_skia.h" 90 #include "ui/gfx/image/image_skia.h"
91 #include "ui/gfx/skbitmap_operations.h" 91 #include "ui/gfx/skbitmap_operations.h"
92 #include "ui/snapshot/snapshot.h"
93 92
94 #if defined(OS_MACOSX) 93 #if defined(OS_MACOSX)
95 #include "device/power_save_blocker/power_save_blocker.h" 94 #include "device/power_save_blocker/power_save_blocker.h"
96 #include "ui/accelerated_widget_mac/window_resize_helper_mac.h" 95 #include "ui/accelerated_widget_mac/window_resize_helper_mac.h"
97 #endif 96 #endif
98 97
99 using base::Time; 98 using base::Time;
100 using base::TimeDelta; 99 using base::TimeDelta;
101 using base::TimeTicks; 100 using base::TimeTicks;
102 using blink::WebDragOperation; 101 using blink::WebDragOperation;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 text_direction_canceled_(false), 266 text_direction_canceled_(false),
268 suppress_events_until_keydown_(false), 267 suppress_events_until_keydown_(false),
269 pending_mouse_lock_request_(false), 268 pending_mouse_lock_request_(false),
270 allow_privileged_mouse_lock_(false), 269 allow_privileged_mouse_lock_(false),
271 has_touch_handler_(false), 270 has_touch_handler_(false),
272 is_in_touchpad_gesture_scroll_(false), 271 is_in_touchpad_gesture_scroll_(false),
273 is_in_touchscreen_gesture_scroll_(false), 272 is_in_touchscreen_gesture_scroll_(false),
274 received_paint_after_load_(false), 273 received_paint_after_load_(false),
275 latency_tracker_(), 274 latency_tracker_(),
276 next_browser_snapshot_id_(1), 275 next_browser_snapshot_id_(1),
276 pending_browser_snapshots_(0),
277 owned_by_render_frame_host_(false), 277 owned_by_render_frame_host_(false),
278 is_focused_(false), 278 is_focused_(false),
279 hung_renderer_delay_( 279 hung_renderer_delay_(
280 base::TimeDelta::FromMilliseconds(kHungRendererDelayMs)), 280 base::TimeDelta::FromMilliseconds(kHungRendererDelayMs)),
281 hang_monitor_reason_( 281 hang_monitor_reason_(
282 RendererUnresponsiveType::RENDERER_UNRESPONSIVE_UNKNOWN), 282 RendererUnresponsiveType::RENDERER_UNRESPONSIVE_UNKNOWN),
283 hang_monitor_event_type_(blink::WebInputEvent::Undefined), 283 hang_monitor_event_type_(blink::WebInputEvent::Undefined),
284 last_event_type_(blink::WebInputEvent::Undefined), 284 last_event_type_(blink::WebInputEvent::Undefined),
285 new_content_rendering_delay_( 285 new_content_rendering_delay_(
286 base::TimeDelta::FromMilliseconds(kNewContentRenderingDelayMs)), 286 base::TimeDelta::FromMilliseconds(kNewContentRenderingDelayMs)),
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 // factor). 1423 // factor).
1424 WasResized(); 1424 WasResized();
1425 1425
1426 if (touch_emulator_) { 1426 if (touch_emulator_) {
1427 touch_emulator_->SetDeviceScaleFactor( 1427 touch_emulator_->SetDeviceScaleFactor(
1428 view_.get() ? content::GetScaleFactorForView(view_.get()) : 1.0f); 1428 view_.get() ? content::GetScaleFactorForView(view_.get()) : 1.0f);
1429 } 1429 }
1430 } 1430 }
1431 1431
1432 void RenderWidgetHostImpl::GetSnapshotFromBrowser( 1432 void RenderWidgetHostImpl::GetSnapshotFromBrowser(
1433 const GetSnapshotFromBrowserCallback& callback) { 1433 const GetSnapshotFromBrowserCallback& callback,
1434 ui::SnapshotEncoding encoding,
1435 ui::SnapshotQuality quality) {
1434 int id = next_browser_snapshot_id_++; 1436 int id = next_browser_snapshot_id_++;
1435 1437
1436 #if defined(OS_MACOSX) 1438 #if defined(OS_MACOSX)
1437 // MacOS version of underlying GrabViewSnapshot() blocks while 1439 // MacOS version of underlying GrabViewSnapshot() blocks while
1438 // display/GPU are in a power-saving mode, so make sure display 1440 // display/GPU are in a power-saving mode, so make sure display
1439 // does not go to sleep for the duration of reading a snapshot. 1441 // does not go to sleep for the duration of reading a snapshot.
1440 if (pending_browser_snapshots_.empty()) { 1442 if (pending_browser_snapshots_ == 0) {
1441 DCHECK(!power_save_blocker_); 1443 DCHECK(!power_save_blocker_);
1442 power_save_blocker_.reset(new device::PowerSaveBlocker( 1444 power_save_blocker_.reset(new device::PowerSaveBlocker(
1443 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, 1445 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep,
1444 device::PowerSaveBlocker::kReasonOther, "GetSnapshot", 1446 device::PowerSaveBlocker::kReasonOther, "GetSnapshot",
1445 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), 1447 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI),
1446 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); 1448 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)));
1447 } 1449 }
1448 #endif 1450 #endif
1449 pending_browser_snapshots_.insert(std::make_pair(id, callback)); 1451 pending_browser_snapshot_params_.insert(
1452 std::make_pair(id, PendingSnapshotParams(callback, encoding, quality)));
1453 pending_browser_snapshots_++;
1450 ui::LatencyInfo latency_info; 1454 ui::LatencyInfo latency_info;
1451 latency_info.AddLatencyNumber(ui::WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT, 0, 1455 latency_info.AddLatencyNumber(ui::WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT, 0,
1452 id); 1456 id);
1453 Send(new ViewMsg_ForceRedraw(GetRoutingID(), latency_info)); 1457 Send(new ViewMsg_ForceRedraw(GetRoutingID(), latency_info));
1454 } 1458 }
1455 1459
1456 const NativeWebKeyboardEvent* 1460 const NativeWebKeyboardEvent*
1457 RenderWidgetHostImpl::GetLastKeyboardEvent() const { 1461 RenderWidgetHostImpl::GetLastKeyboardEvent() const {
1458 return input_router_->GetLastKeyboardEvent(); 1462 return input_router_->GetLastKeyboardEvent();
1459 } 1463 }
(...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after
2347 void RenderWidgetHostImpl::DidReceiveRendererFrame() { 2351 void RenderWidgetHostImpl::DidReceiveRendererFrame() {
2348 view_->DidReceiveRendererFrame(); 2352 view_->DidReceiveRendererFrame();
2349 } 2353 }
2350 2354
2351 void RenderWidgetHostImpl::WindowSnapshotReachedScreen(int snapshot_id) { 2355 void RenderWidgetHostImpl::WindowSnapshotReachedScreen(int snapshot_id) {
2352 DCHECK(base::MessageLoopForUI::IsCurrent()); 2356 DCHECK(base::MessageLoopForUI::IsCurrent());
2353 2357
2354 gfx::Rect view_bounds = GetView()->GetViewBounds(); 2358 gfx::Rect view_bounds = GetView()->GetViewBounds();
2355 gfx::Rect snapshot_bounds(view_bounds.size()); 2359 gfx::Rect snapshot_bounds(view_bounds.size());
2356 2360
2357 std::vector<unsigned char> png; 2361 // Any pending snapshots with a lower ID than the one received are considered
2358 if (ui::GrabViewSnapshot( 2362 // to be implicitly complete. Grab snapshots in requested encodings.
2359 GetView()->GetNativeView(), &png, snapshot_bounds)) { 2363 PendingSnapshotParamsMap::iterator it =
2360 OnSnapshotDataReceived(snapshot_id, &png.front(), png.size()); 2364 pending_browser_snapshot_params_.begin();
2361 return; 2365 while (it != pending_browser_snapshot_params_.end()) {
2366 if (it->first <= snapshot_id) {
2367 std::vector<unsigned char> data;
2368 if (ui::GrabViewSnapshot(GetView()->GetNativeView(), &data,
pfeldman 2017/01/03 18:42:33 Why don't we grab it once and then encode here?
Eric Seckler 2017/01/03 19:18:09 What would that look like? I moved the encoding in
pfeldman 2017/01/04 00:10:50 There is nothing wrong with your assumption, but c
Eric Seckler 2017/01/06 21:06:01 I've changed ui::Grab*Snapshot* methods to return
2369 snapshot_bounds, it->second.encoding,
2370 it->second.quality)) {
2371 OnSnapshotDataReceived(it->second.callback, &data.front(), data.size());
2372 } else {
2373 ui::GrabViewSnapshotAsync(
2374 GetView()->GetNativeView(), snapshot_bounds,
2375 base::ThreadTaskRunnerHandle::Get(),
2376 base::Bind(&RenderWidgetHostImpl::OnSnapshotDataReceivedAsync,
2377 weak_factory_.GetWeakPtr(), it->second.callback),
2378 it->second.encoding, it->second.quality);
2379 }
2380 pending_browser_snapshot_params_.erase(it++);
2381 } else {
2382 ++it;
2383 }
2362 } 2384 }
2363
2364 ui::GrabViewSnapshotAsync(
2365 GetView()->GetNativeView(),
2366 snapshot_bounds,
2367 base::ThreadTaskRunnerHandle::Get(),
2368 base::Bind(&RenderWidgetHostImpl::OnSnapshotDataReceivedAsync,
2369 weak_factory_.GetWeakPtr(),
2370 snapshot_id));
2371 } 2385 }
2372 2386
2373 void RenderWidgetHostImpl::OnSnapshotDataReceived(int snapshot_id, 2387 void RenderWidgetHostImpl::OnSnapshotDataReceived(
2374 const unsigned char* data, 2388 const GetSnapshotFromBrowserCallback& callback,
2375 size_t size) { 2389 const unsigned char* data,
2376 // Any pending snapshots with a lower ID than the one received are considered 2390 size_t size) {
2377 // to be implicitly complete, and returned the same snapshot data. 2391 DCHECK_LT(0, pending_browser_snapshots_);
2378 PendingSnapshotMap::iterator it = pending_browser_snapshots_.begin(); 2392 callback.Run(data, size);
2379 while (it != pending_browser_snapshots_.end()) { 2393 pending_browser_snapshots_--;
2380 if (it->first <= snapshot_id) {
2381 it->second.Run(data, size);
2382 pending_browser_snapshots_.erase(it++);
2383 } else {
2384 ++it;
2385 }
2386 }
2387 #if defined(OS_MACOSX) 2394 #if defined(OS_MACOSX)
2388 if (pending_browser_snapshots_.empty()) 2395 if (pending_browser_snapshots_ == 0)
2389 power_save_blocker_.reset(); 2396 power_save_blocker_.reset();
2390 #endif 2397 #endif
2391 } 2398 }
2392 2399
2393 void RenderWidgetHostImpl::OnSnapshotDataReceivedAsync( 2400 void RenderWidgetHostImpl::OnSnapshotDataReceivedAsync(
2394 int snapshot_id, 2401 const GetSnapshotFromBrowserCallback& callback,
2395 scoped_refptr<base::RefCountedBytes> png_data) { 2402 scoped_refptr<base::RefCountedBytes> png_data) {
2396 if (png_data.get()) 2403 if (png_data.get())
2397 OnSnapshotDataReceived(snapshot_id, png_data->front(), png_data->size()); 2404 OnSnapshotDataReceived(callback, png_data->front(), png_data->size());
2398 else 2405 else
2399 OnSnapshotDataReceived(snapshot_id, NULL, 0); 2406 OnSnapshotDataReceived(callback, NULL, 0);
2400 } 2407 }
2401 2408
2402 // static 2409 // static
2403 void RenderWidgetHostImpl::CompositorFrameDrawn( 2410 void RenderWidgetHostImpl::CompositorFrameDrawn(
2404 const std::vector<ui::LatencyInfo>& latency_info) { 2411 const std::vector<ui::LatencyInfo>& latency_info) {
2405 for (size_t i = 0; i < latency_info.size(); i++) { 2412 for (size_t i = 0; i < latency_info.size(); i++) {
2406 std::set<RenderWidgetHostImpl*> rwhi_set; 2413 std::set<RenderWidgetHostImpl*> rwhi_set;
2407 for (const auto& lc : latency_info[i].latency_components()) { 2414 for (const auto& lc : latency_info[i].latency_components()) {
2408 if (lc.first.first == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT || 2415 if (lc.first.first == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT ||
2409 lc.first.first == ui::WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT || 2416 lc.first.first == ui::WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT ||
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
2511 // Note: We are using the origin URL provided by the sender here. It may be 2518 // Note: We are using the origin URL provided by the sender here. It may be
2512 // different from the receiver's. 2519 // different from the receiver's.
2513 file_system_file.url = 2520 file_system_file.url =
2514 GURL(storage::GetIsolatedFileSystemRootURIString( 2521 GURL(storage::GetIsolatedFileSystemRootURIString(
2515 file_system_url.origin(), filesystem_id, std::string()) 2522 file_system_url.origin(), filesystem_id, std::string())
2516 .append(register_name)); 2523 .append(register_name));
2517 file_system_file.filesystem_id = filesystem_id; 2524 file_system_file.filesystem_id = filesystem_id;
2518 } 2525 }
2519 } 2526 }
2520 2527
2528 RenderWidgetHostImpl::PendingSnapshotParams::PendingSnapshotParams(
2529 const GetSnapshotFromBrowserCallback& callback,
2530 ui::SnapshotEncoding encoding,
2531 ui::SnapshotQuality quality)
2532 : callback(callback), encoding(encoding), quality(quality) {}
2533
2534 RenderWidgetHostImpl::PendingSnapshotParams::PendingSnapshotParams(
2535 const PendingSnapshotParams& params) = default;
2536
2537 RenderWidgetHostImpl::PendingSnapshotParams::~PendingSnapshotParams() = default;
2538
2521 } // namespace content 2539 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698