1/*
2 *  Copyright (c) 2018 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/mac/desktop_frame_provider.h"
12
13#include <utility>
14
15#include "modules/desktop_capture/mac/desktop_frame_cgimage.h"
16#include "modules/desktop_capture/mac/desktop_frame_iosurface.h"
17
18namespace webrtc {
19
20DesktopFrameProvider::DesktopFrameProvider(bool allow_iosurface)
21    : allow_iosurface_(allow_iosurface) {
22  thread_checker_.Detach();
23}
24
25DesktopFrameProvider::~DesktopFrameProvider() {
26  RTC_DCHECK(thread_checker_.IsCurrent());
27
28  Release();
29}
30
31std::unique_ptr<DesktopFrame> DesktopFrameProvider::TakeLatestFrameForDisplay(
32    CGDirectDisplayID display_id) {
33  RTC_DCHECK(thread_checker_.IsCurrent());
34
35  if (!allow_iosurface_ || !io_surfaces_[display_id]) {
36    // Regenerate a snapshot. If iosurface is on it will be empty until the
37    // stream handler is called.
38    return DesktopFrameCGImage::CreateForDisplay(display_id);
39  }
40
41  return io_surfaces_[display_id]->Share();
42}
43
44void DesktopFrameProvider::InvalidateIOSurface(CGDirectDisplayID display_id,
45                                               rtc::ScopedCFTypeRef<IOSurfaceRef> io_surface) {
46  RTC_DCHECK(thread_checker_.IsCurrent());
47
48  if (!allow_iosurface_) {
49    return;
50  }
51
52  std::unique_ptr<DesktopFrameIOSurface> desktop_frame_iosurface =
53      DesktopFrameIOSurface::Wrap(io_surface);
54
55  io_surfaces_[display_id] = desktop_frame_iosurface ?
56      SharedDesktopFrame::Wrap(std::move(desktop_frame_iosurface)) :
57      nullptr;
58}
59
60void DesktopFrameProvider::Release() {
61  RTC_DCHECK(thread_checker_.IsCurrent());
62
63  if (!allow_iosurface_) {
64    return;
65  }
66
67  io_surfaces_.clear();
68}
69
70}  // namespace webrtc
71