xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/cropped_desktop_frame.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
19 namespace webrtc {
20 
21 // A DesktopFrame that is a sub-rect of another DesktopFrame.
22 class CroppedDesktopFrame : public DesktopFrame {
23  public:
24   CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
25                       const DesktopRect& rect);
26 
27   CroppedDesktopFrame(const CroppedDesktopFrame&) = delete;
28   CroppedDesktopFrame& operator=(const CroppedDesktopFrame&) = delete;
29 
30  private:
31   const std::unique_ptr<DesktopFrame> frame_;
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   DesktopRect intersection = DesktopRect::MakeSize(frame->size());
40   intersection.IntersectWith(rect);
41   if (intersection.is_empty()) {
42     return nullptr;
43   }
44 
45   if (frame->size().equals(rect.size())) {
46     return frame;
47   }
48 
49   return std::unique_ptr<DesktopFrame>(
50       new CroppedDesktopFrame(std::move(frame), intersection));
51 }
52 
CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,const DesktopRect & rect)53 CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
54                                          const DesktopRect& rect)
55     : DesktopFrame(rect.size(),
56                    frame->stride(),
57                    frame->GetFrameDataAtPos(rect.top_left()),
58                    frame->shared_memory()),
59       frame_(std::move(frame)) {
60   MoveFrameInfoFrom(frame_.get());
61   set_top_left(frame_->top_left().add(rect.top_left()));
62   mutable_updated_region()->IntersectWith(rect);
63   mutable_updated_region()->Translate(-rect.left(), -rect.top());
64 }
65 
66 }  // namespace webrtc
67