cropped_desktop_frame.cc (2251B)
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_frame.h" 17 #include "modules/desktop_capture/desktop_geometry.h" 18 #include "modules/desktop_capture/desktop_region.h" 19 #include "rtc_base/checks.h" 20 21 namespace webrtc { 22 23 // A DesktopFrame that is a sub-rect of another DesktopFrame. 24 class CroppedDesktopFrame : public DesktopFrame { 25 public: 26 CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame, 27 const DesktopRect& rect); 28 29 CroppedDesktopFrame(const CroppedDesktopFrame&) = delete; 30 CroppedDesktopFrame& operator=(const CroppedDesktopFrame&) = delete; 31 32 private: 33 const std::unique_ptr<DesktopFrame> frame_; 34 }; 35 36 std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame( 37 std::unique_ptr<DesktopFrame> frame, 38 const DesktopRect& rect) { 39 RTC_DCHECK(frame); 40 41 DesktopRect intersection = DesktopRect::MakeSize(frame->size()); 42 intersection.IntersectWith(rect); 43 if (intersection.is_empty()) { 44 return nullptr; 45 } 46 47 if (frame->size().equals(rect.size())) { 48 return frame; 49 } 50 51 return std::unique_ptr<DesktopFrame>( 52 new CroppedDesktopFrame(std::move(frame), intersection)); 53 } 54 55 CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame, 56 const DesktopRect& rect) 57 : DesktopFrame(rect.size(), 58 frame->stride(), 59 frame->pixel_format(), 60 frame->GetFrameDataAtPos(rect.top_left()), 61 frame->shared_memory()), 62 frame_(std::move(frame)) { 63 MoveFrameInfoFrom(frame_.get()); 64 set_top_left(frame_->top_left().add(rect.top_left())); 65 mutable_updated_region()->IntersectWith(rect); 66 mutable_updated_region()->Translate(-rect.left(), -rect.top()); 67 } 68 69 } // namespace webrtc