• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/desktop_capture/desktop_and_cursor_composer.h"
12 
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/scoped_ptr.h"
15 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
16 #include "webrtc/modules/desktop_capture/desktop_frame.h"
17 #include "webrtc/modules/desktop_capture/mouse_cursor.h"
18 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
19 #include "webrtc/modules/desktop_capture/window_capturer.h"
20 #include "webrtc/system_wrappers/include/logging.h"
21 
22 namespace webrtc {
23 
24 namespace {
25 
26 const int kScreenWidth = 100;
27 const int kScreenHeight = 100;
28 const int kCursorWidth = 10;
29 const int kCursorHeight = 10;
30 
31 const int kTestCursorSize = 3;
32 const uint32_t kTestCursorData[kTestCursorSize][kTestCursorSize] = {
33   { 0xffffffff, 0x99990000, 0xaa222222, },
34   { 0x88008800, 0xaa0000aa, 0xaa333333, },
35   { 0x00000000, 0xaa0000aa, 0xaa333333, },
36 };
37 
GetFakeFramePixelValue(const DesktopVector & p)38 uint32_t GetFakeFramePixelValue(const DesktopVector& p) {
39   uint32_t r = 100 + p.x();
40   uint32_t g = 100 + p.y();
41   uint32_t b = 100 + p.x() + p.y();
42   return b + (g << 8) + (r << 16) + 0xff000000;
43 }
44 
GetFramePixel(const DesktopFrame & frame,const DesktopVector & pos)45 uint32_t GetFramePixel(const DesktopFrame& frame, const DesktopVector& pos) {
46   return *reinterpret_cast<uint32_t*>(frame.data() + pos.y() * frame.stride() +
47                                       pos.x() * DesktopFrame::kBytesPerPixel);
48 }
49 
50 // Blends two pixel values taking into account alpha.
BlendPixels(uint32_t dest,uint32_t src)51 uint32_t BlendPixels(uint32_t dest, uint32_t src) {
52   uint8_t alpha = 255 - ((src & 0xff000000) >> 24);
53   uint32_t r =
54       ((dest & 0x00ff0000) >> 16) * alpha / 255 + ((src & 0x00ff0000) >> 16);
55   uint32_t g =
56       ((dest & 0x0000ff00) >> 8) * alpha / 255 + ((src & 0x0000ff00) >> 8);
57   uint32_t b = (dest & 0x000000ff) * alpha / 255 + (src & 0x000000ff);
58   return b + (g << 8) + (r << 16) + 0xff000000;
59 }
60 
CreateTestFrame()61 DesktopFrame* CreateTestFrame() {
62   DesktopFrame* frame =
63       new BasicDesktopFrame(DesktopSize(kScreenWidth, kScreenHeight));
64   uint32_t* data = reinterpret_cast<uint32_t*>(frame->data());
65   for (int y = 0; y < kScreenHeight; ++y) {
66     for (int x = 0; x < kScreenWidth; ++x) {
67       *(data++) = GetFakeFramePixelValue(DesktopVector(x, y));
68     }
69   }
70   return frame;
71 }
72 
73 class FakeScreenCapturer : public DesktopCapturer {
74  public:
FakeScreenCapturer()75   FakeScreenCapturer() {}
76 
Start(Callback * callback)77   void Start(Callback* callback) override { callback_ = callback; }
78 
Capture(const DesktopRegion & region)79   void Capture(const DesktopRegion& region) override {
80     callback_->OnCaptureCompleted(next_frame_.release());
81   }
82 
SetNextFrame(DesktopFrame * next_frame)83   void SetNextFrame(DesktopFrame* next_frame) {
84     next_frame_.reset(next_frame);
85   }
86 
87  private:
88   Callback* callback_;
89 
90   rtc::scoped_ptr<DesktopFrame> next_frame_;
91 };
92 
93 class FakeMouseMonitor : public MouseCursorMonitor {
94  public:
FakeMouseMonitor()95   FakeMouseMonitor() : changed_(true) {}
96 
SetState(CursorState state,const DesktopVector & pos)97   void SetState(CursorState state, const DesktopVector& pos) {
98     state_ = state;
99     position_ = pos;
100   }
101 
SetHotspot(const DesktopVector & hotspot)102   void SetHotspot(const DesktopVector& hotspot) {
103     if (!hotspot_.equals(hotspot))
104       changed_ = true;
105     hotspot_ = hotspot;
106   }
107 
Init(Callback * callback,Mode mode)108   void Init(Callback* callback, Mode mode) override { callback_ = callback; }
109 
Capture()110   void Capture() override {
111     if (changed_) {
112       rtc::scoped_ptr<DesktopFrame> image(
113           new BasicDesktopFrame(DesktopSize(kCursorWidth, kCursorHeight)));
114       uint32_t* data = reinterpret_cast<uint32_t*>(image->data());
115       memset(data, 0, image->stride() * kCursorHeight);
116 
117       // Set four pixels near the hotspot and leave all other blank.
118       for (int y = 0; y < kTestCursorSize; ++y) {
119         for (int x = 0; x < kTestCursorSize; ++x) {
120           data[(hotspot_.y() + y) * kCursorWidth + (hotspot_.x() + x)] =
121               kTestCursorData[y][x];
122         }
123       }
124 
125       callback_->OnMouseCursor(new MouseCursor(image.release(), hotspot_));
126     }
127 
128     callback_->OnMouseCursorPosition(state_, position_);
129   }
130 
131  private:
132   Callback* callback_;
133   CursorState state_;
134   DesktopVector position_;
135   DesktopVector hotspot_;
136   bool changed_;
137 };
138 
VerifyFrame(const DesktopFrame & frame,MouseCursorMonitor::CursorState state,const DesktopVector & pos)139 void VerifyFrame(const DesktopFrame& frame,
140                  MouseCursorMonitor::CursorState state,
141                  const DesktopVector& pos) {
142   // Verify that all other pixels are set to their original values.
143   DesktopRect image_rect =
144       DesktopRect::MakeWH(kTestCursorSize, kTestCursorSize);
145   image_rect.Translate(pos);
146 
147   for (int y = 0; y < kScreenHeight; ++y) {
148     for (int x = 0; x < kScreenWidth; ++x) {
149       DesktopVector p(x, y);
150       if (state == MouseCursorMonitor::INSIDE && image_rect.Contains(p)) {
151         EXPECT_EQ(BlendPixels(GetFakeFramePixelValue(p),
152                               kTestCursorData[y - pos.y()][x - pos.x()]),
153                   GetFramePixel(frame, p));
154       } else {
155         EXPECT_EQ(GetFakeFramePixelValue(p), GetFramePixel(frame, p));
156       }
157     }
158   }
159 }
160 
161 class DesktopAndCursorComposerTest : public testing::Test,
162                                      public DesktopCapturer::Callback {
163  public:
DesktopAndCursorComposerTest()164   DesktopAndCursorComposerTest()
165       : fake_screen_(new FakeScreenCapturer()),
166         fake_cursor_(new FakeMouseMonitor()),
167         blender_(fake_screen_, fake_cursor_) {
168   }
169 
170   // DesktopCapturer::Callback interface
CreateSharedMemory(size_t size)171   SharedMemory* CreateSharedMemory(size_t size) override { return NULL; }
172 
OnCaptureCompleted(DesktopFrame * frame)173   void OnCaptureCompleted(DesktopFrame* frame) override { frame_.reset(frame); }
174 
175  protected:
176   // Owned by |blender_|.
177   FakeScreenCapturer* fake_screen_;
178   FakeMouseMonitor* fake_cursor_;
179 
180   DesktopAndCursorComposer blender_;
181   rtc::scoped_ptr<DesktopFrame> frame_;
182 };
183 
184 // Verify DesktopAndCursorComposer can handle the case when the screen capturer
185 // fails.
TEST_F(DesktopAndCursorComposerTest,Error)186 TEST_F(DesktopAndCursorComposerTest, Error) {
187   blender_.Start(this);
188 
189   fake_cursor_->SetHotspot(DesktopVector());
190   fake_cursor_->SetState(MouseCursorMonitor::INSIDE, DesktopVector());
191   fake_screen_->SetNextFrame(NULL);
192 
193   blender_.Capture(DesktopRegion());
194 
195   EXPECT_EQ(frame_, static_cast<DesktopFrame*>(NULL));
196 }
197 
TEST_F(DesktopAndCursorComposerTest,Blend)198 TEST_F(DesktopAndCursorComposerTest, Blend) {
199   struct {
200     int x, y;
201     int hotspot_x, hotspot_y;
202     bool inside;
203   } tests[] = {
204     {0, 0, 0, 0, true},
205     {50, 50, 0, 0, true},
206     {100, 50, 0, 0, true},
207     {50, 100, 0, 0, true},
208     {100, 100, 0, 0, true},
209     {0, 0, 2, 5, true},
210     {1, 1, 2, 5, true},
211     {50, 50, 2, 5, true},
212     {100, 100, 2, 5, true},
213     {0, 0, 5, 2, true},
214     {50, 50, 5, 2, true},
215     {100, 100, 5, 2, true},
216     {0, 0, 0, 0, false},
217   };
218 
219   blender_.Start(this);
220 
221   for (size_t i = 0; i < (sizeof(tests) / sizeof(tests[0])); ++i) {
222     SCOPED_TRACE(i);
223 
224     DesktopVector hotspot(tests[i].hotspot_x, tests[i].hotspot_y);
225     fake_cursor_->SetHotspot(hotspot);
226 
227     MouseCursorMonitor::CursorState state = tests[i].inside
228                                                 ? MouseCursorMonitor::INSIDE
229                                                 : MouseCursorMonitor::OUTSIDE;
230     DesktopVector pos(tests[i].x, tests[i].y);
231     fake_cursor_->SetState(state, pos);
232 
233     rtc::scoped_ptr<SharedDesktopFrame> frame(
234         SharedDesktopFrame::Wrap(CreateTestFrame()));
235     fake_screen_->SetNextFrame(frame->Share());
236 
237     blender_.Capture(DesktopRegion());
238 
239     VerifyFrame(*frame_, state, pos);
240 
241     // Verify that the cursor is erased before the frame buffer is returned to
242     // the screen capturer.
243     frame_.reset();
244     VerifyFrame(*frame, MouseCursorMonitor::OUTSIDE, DesktopVector());
245   }
246 }
247 
248 }  // namespace
249 
250 }  // namespace webrtc
251