Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / host / chromeos / aura_desktop_capturer.cc
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 "remoting/host/chromeos/aura_desktop_capturer.h"
6
7 #include "base/bind.h"
8 #include "cc/output/copy_output_request.h"
9 #include "cc/output/copy_output_result.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_tree_host.h"
14
15 #if defined(USE_ASH)
16 #include "ash/shell.h"
17 #endif
18
19 namespace remoting {
20
21 namespace {
22
23 // DesktopFrame implementation used by screen capture on ChromeOS.
24 // Frame data is stored in a SkBitmap.
25 class SkiaBitmapDesktopFrame : public webrtc::DesktopFrame {
26  public:
27   static SkiaBitmapDesktopFrame* Create(scoped_ptr<SkBitmap> bitmap);
28   virtual ~SkiaBitmapDesktopFrame();
29
30  private:
31   SkiaBitmapDesktopFrame(webrtc::DesktopSize size,
32                          int stride,
33                          uint8_t* data,
34                          scoped_ptr<SkBitmap> bitmap);
35
36   scoped_ptr<SkBitmap> bitmap_;
37
38   DISALLOW_COPY_AND_ASSIGN(SkiaBitmapDesktopFrame);
39 };
40
41 // static
42 SkiaBitmapDesktopFrame* SkiaBitmapDesktopFrame::Create(
43     scoped_ptr<SkBitmap> bitmap) {
44
45   webrtc::DesktopSize size(bitmap->width(), bitmap->height());
46   DCHECK_EQ(kBGRA_8888_SkColorType, bitmap->info().colorType())
47       << "DesktopFrame objects always hold RGBA data.";
48
49   uint8_t* bitmap_data = reinterpret_cast<uint8_t*>(bitmap->getPixels());
50
51   SkiaBitmapDesktopFrame* result = new SkiaBitmapDesktopFrame(
52       size, bitmap->rowBytes(), bitmap_data, bitmap.Pass());
53
54   return result;
55 }
56
57 SkiaBitmapDesktopFrame::SkiaBitmapDesktopFrame(webrtc::DesktopSize size,
58                                                int stride,
59                                                uint8_t* data,
60                                                scoped_ptr<SkBitmap> bitmap)
61     : DesktopFrame(size, stride, data, NULL), bitmap_(bitmap.Pass()) {
62 }
63
64 SkiaBitmapDesktopFrame::~SkiaBitmapDesktopFrame() {
65 }
66
67 }  // namespace
68
69 AuraDesktopCapturer::AuraDesktopCapturer()
70     : callback_(NULL), desktop_window_(NULL), weak_factory_(this) {
71 }
72
73 AuraDesktopCapturer::~AuraDesktopCapturer() {
74 }
75
76 void AuraDesktopCapturer::Start(webrtc::DesktopCapturer::Callback* callback) {
77 #if defined(USE_ASH)
78   if (ash::Shell::HasInstance()) {
79     // TODO(kelvinp): Use ash::Shell::GetAllRootWindows() when multiple monitor
80     // support is implemented.
81     desktop_window_ = ash::Shell::GetPrimaryRootWindow();
82     DCHECK(desktop_window_) << "Failed to retrieve the Aura Shell root window";
83   }
84 #endif
85
86   DCHECK(!callback_) << "Start() can only be called once";
87   callback_ = callback;
88   DCHECK(callback_);
89 }
90
91 void AuraDesktopCapturer::Capture(const webrtc::DesktopRegion&) {
92   scoped_ptr<cc::CopyOutputRequest> request =
93       cc::CopyOutputRequest::CreateBitmapRequest(
94           base::Bind(
95               &AuraDesktopCapturer::OnFrameCaptured,
96               weak_factory_.GetWeakPtr()));
97
98   gfx::Rect window_rect(desktop_window_->bounds().size());
99
100   request->set_area(window_rect);
101   desktop_window_->layer()->RequestCopyOfOutput(request.Pass());
102 }
103
104 void AuraDesktopCapturer::OnFrameCaptured(
105     scoped_ptr<cc::CopyOutputResult> result) {
106   DCHECK(result->HasBitmap());
107
108   scoped_ptr<SkBitmap> bitmap = result->TakeBitmap();
109
110   scoped_ptr<webrtc::DesktopFrame> frame(
111       SkiaBitmapDesktopFrame::Create(bitmap.Pass()));
112
113   // |VideoScheduler| will not encode the frame if |updated_region| is empty.
114   const webrtc::DesktopRect& rect = webrtc::DesktopRect::MakeWH(
115       frame->size().width(), frame->size().height());
116
117   // TODO(kelvinp): Set Frame DPI according to the screen resolution.
118   // See cc::Layer::contents_scale_(x|y)() and frame->set_depi().
119   frame->mutable_updated_region()->SetRect(rect);
120
121   callback_->OnCaptureCompleted(frame.release());
122 }
123
124 }  // namespace remoting