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.
5 #include "remoting/host/chromeos/aura_desktop_capturer.h"
8 #include "cc/output/copy_output_request.h"
9 #include "cc/output/copy_output_result.h"
10 #include "remoting/host/chromeos/skia_bitmap_desktop_frame.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_tree_host.h"
15 #include "ash/shell.h"
20 AuraDesktopCapturer::AuraDesktopCapturer()
21 : callback_(nullptr), desktop_window_(nullptr), weak_factory_(this) {
24 AuraDesktopCapturer::~AuraDesktopCapturer() {
27 void AuraDesktopCapturer::Start(webrtc::DesktopCapturer::Callback
* callback
) {
29 if (ash::Shell::HasInstance()) {
30 // TODO(kelvinp): Use ash::Shell::GetAllRootWindows() when multiple monitor
31 // support is implemented.
32 desktop_window_
= ash::Shell::GetPrimaryRootWindow();
33 DCHECK(desktop_window_
) << "Failed to retrieve the Aura Shell root window";
37 DCHECK(!callback_
) << "Start() can only be called once";
42 void AuraDesktopCapturer::Capture(const webrtc::DesktopRegion
&) {
43 scoped_ptr
<cc::CopyOutputRequest
> request
=
44 cc::CopyOutputRequest::CreateBitmapRequest(
46 &AuraDesktopCapturer::OnFrameCaptured
,
47 weak_factory_
.GetWeakPtr()));
49 gfx::Rect
window_rect(desktop_window_
->bounds().size());
51 request
->set_area(window_rect
);
52 desktop_window_
->layer()->RequestCopyOfOutput(request
.Pass());
55 void AuraDesktopCapturer::OnFrameCaptured(
56 scoped_ptr
<cc::CopyOutputResult
> result
) {
57 DCHECK(result
->HasBitmap());
59 scoped_ptr
<SkBitmap
> bitmap
= result
->TakeBitmap();
61 scoped_ptr
<webrtc::DesktopFrame
> frame(
62 SkiaBitmapDesktopFrame::Create(bitmap
.Pass()));
64 // |VideoFramePump| will not encode the frame if |updated_region| is empty.
65 const webrtc::DesktopRect
& rect
= webrtc::DesktopRect::MakeWH(
66 frame
->size().width(), frame
->size().height());
68 // TODO(kelvinp): Set Frame DPI according to the screen resolution.
69 // See cc::Layer::contents_scale_(x|y)() and frame->set_depi().
70 frame
->mutable_updated_region()->SetRect(rect
);
72 callback_
->OnCaptureCompleted(frame
.release());
75 } // namespace remoting