1 /*
2  *  Copyright (c) 2014 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 "modules/desktop_capture/cropped_desktop_frame.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "modules/desktop_capture/desktop_region.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/constructor_magic.h"
19 
20 namespace webrtc {
21 
22 // A DesktopFrame that is a sub-rect of another DesktopFrame.
23 class CroppedDesktopFrame : public DesktopFrame {
24  public:
25   CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
26                       const DesktopRect& rect);
27 
28  private:
29   const std::unique_ptr<DesktopFrame> frame_;
30 
31   RTC_DISALLOW_COPY_AND_ASSIGN(CroppedDesktopFrame);
32 };
33 
CreateCroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,const DesktopRect & rect)34 std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame(
35     std::unique_ptr<DesktopFrame> frame,
36     const DesktopRect& rect) {
37   RTC_DCHECK(frame);
38 
39   if (!DesktopRect::MakeSize(frame->size()).ContainsRect(rect)) {
40     return nullptr;
41   }
42 
43   if (frame->size().equals(rect.size())) {
44     return frame;
45   }
46 
47   return std::unique_ptr<DesktopFrame>(
48       new CroppedDesktopFrame(std::move(frame), rect));
49 }
50 
CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,const DesktopRect & rect)51 CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
52                                          const DesktopRect& rect)
53     : DesktopFrame(rect.size(),
54                    frame->stride(),
55                    frame->GetFrameDataAtPos(rect.top_left()),
56                    frame->shared_memory()),
57       frame_(std::move(frame)) {
58   MoveFrameInfoFrom(frame_.get());
59   set_top_left(frame_->top_left().add(rect.top_left()));
60   mutable_updated_region()->IntersectWith(rect);
61   mutable_updated_region()->Translate(-rect.left(), -rect.top());
62 }
63 
64 }  // namespace webrtc
65