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_cgimage.h"
12
13#include "rtc_base/checks.h"
14#include "rtc_base/logging.h"
15
16namespace webrtc {
17
18// static
19std::unique_ptr<DesktopFrameCGImage> DesktopFrameCGImage::CreateForDisplay(
20    CGDirectDisplayID display_id) {
21  // Create an image containing a snapshot of the display.
22  rtc::ScopedCFTypeRef<CGImageRef> cg_image(CGDisplayCreateImage(display_id));
23  if (!cg_image) {
24    return nullptr;
25  }
26
27  return DesktopFrameCGImage::CreateFromCGImage(cg_image);
28}
29
30// static
31std::unique_ptr<DesktopFrameCGImage> DesktopFrameCGImage::CreateForWindow(CGWindowID window_id) {
32  rtc::ScopedCFTypeRef<CGImageRef> cg_image(
33      CGWindowListCreateImage(CGRectNull,
34                              kCGWindowListOptionIncludingWindow,
35                              window_id,
36                              kCGWindowImageBoundsIgnoreFraming));
37  if (!cg_image) {
38    return nullptr;
39  }
40
41  return DesktopFrameCGImage::CreateFromCGImage(cg_image);
42}
43
44// static
45std::unique_ptr<DesktopFrameCGImage> DesktopFrameCGImage::CreateFromCGImage(
46    rtc::ScopedCFTypeRef<CGImageRef> cg_image) {
47  // Verify that the image has 32-bit depth.
48  int bits_per_pixel = CGImageGetBitsPerPixel(cg_image.get());
49  if (bits_per_pixel / 8 != DesktopFrame::kBytesPerPixel) {
50    RTC_LOG(LS_ERROR) << "CGDisplayCreateImage() returned imaged with " << bits_per_pixel
51                      << " bits per pixel. Only 32-bit depth is supported.";
52    return nullptr;
53  }
54
55  // Request access to the raw pixel data via the image's DataProvider.
56  CGDataProviderRef cg_provider = CGImageGetDataProvider(cg_image.get());
57  RTC_DCHECK(cg_provider);
58
59  // CGDataProviderCopyData returns a new data object containing a copy of the provider’s
60  // data.
61  rtc::ScopedCFTypeRef<CFDataRef> cg_data(CGDataProviderCopyData(cg_provider));
62  RTC_DCHECK(cg_data);
63
64  // CFDataGetBytePtr returns a read-only pointer to the bytes of a CFData object.
65  uint8_t* data = const_cast<uint8_t*>(CFDataGetBytePtr(cg_data.get()));
66  RTC_DCHECK(data);
67
68  DesktopSize size(CGImageGetWidth(cg_image.get()), CGImageGetHeight(cg_image.get()));
69  int stride = CGImageGetBytesPerRow(cg_image.get());
70
71  std::unique_ptr<DesktopFrameCGImage> frame(
72      new DesktopFrameCGImage(size, stride, data, cg_image, cg_data));
73
74  CGColorSpaceRef cg_color_space = CGImageGetColorSpace(cg_image.get());
75  if (cg_color_space) {
76    rtc::ScopedCFTypeRef<CFDataRef> cf_icc_profile(CGColorSpaceCopyICCProfile(cg_color_space));
77    if (cf_icc_profile) {
78      const uint8_t* data_as_byte =
79          reinterpret_cast<const uint8_t*>(CFDataGetBytePtr(cf_icc_profile.get()));
80      const size_t data_size = CFDataGetLength(cf_icc_profile.get());
81      if (data_as_byte && data_size > 0) {
82        frame->set_icc_profile(std::vector<uint8_t>(data_as_byte, data_as_byte + data_size));
83      }
84    }
85  }
86
87  return frame;
88}
89
90DesktopFrameCGImage::DesktopFrameCGImage(DesktopSize size,
91                                         int stride,
92                                         uint8_t* data,
93                                         rtc::ScopedCFTypeRef<CGImageRef> cg_image,
94                                         rtc::ScopedCFTypeRef<CFDataRef> cg_data)
95    : DesktopFrame(size, stride, data, nullptr), cg_image_(cg_image), cg_data_(cg_data) {
96  RTC_DCHECK(cg_image_);
97  RTC_DCHECK(cg_data_);
98}
99
100DesktopFrameCGImage::~DesktopFrameCGImage() = default;
101
102}  // namespace webrtc
103