tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

desktop_frame.h (11238B)


      1 /*
      2 *  Copyright (c) 2013 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 #ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_
     12 #define MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_
     13 
     14 #include <stdint.h>
     15 
     16 #include <memory>
     17 #include <optional>
     18 #include <vector>
     19 
     20 #include "media/base/video_common.h"
     21 #include "modules/desktop_capture/desktop_geometry.h"
     22 #include "modules/desktop_capture/desktop_region.h"
     23 #include "modules/desktop_capture/shared_memory.h"
     24 #include "rtc_base/system/rtc_export.h"
     25 
     26 namespace webrtc {
     27 
     28 const float kStandardDPI = 96.0f;
     29 
     30 // DesktopFrame represents a video frame captured from the screen.
     31 class RTC_EXPORT DesktopFrame {
     32 public:
     33  // DesktopFrame objects always hold BGRA data.
     34  static const int kBytesPerPixel = 4;
     35 
     36  virtual ~DesktopFrame();
     37 
     38  DesktopFrame(const DesktopFrame&) = delete;
     39  DesktopFrame& operator=(const DesktopFrame&) = delete;
     40 
     41  // Returns the rectangle in full desktop coordinates to indicate it covers
     42  // the area of top_left() to top_letf() + size() / scale_factor().
     43  DesktopRect rect() const;
     44 
     45  // Returns the scale factor from DIPs to physical pixels of the frame.
     46  // Assumes same scale in both X and Y directions at present.
     47  float scale_factor() const;
     48 
     49  // Size of the frame. In physical coordinates, mapping directly from the
     50  // underlying buffer.
     51  const DesktopSize& size() const { return size_; }
     52 
     53  // The top-left of the frame in full desktop coordinates. E.g. the top left
     54  // monitor should start from (0, 0). The desktop coordinates may be scaled by
     55  // OS, but this is always consistent with the MouseCursorMonitor.
     56  const DesktopVector& top_left() const { return top_left_; }
     57  void set_top_left(const DesktopVector& top_left) { top_left_ = top_left; }
     58 
     59  // Distance in the buffer between two neighboring rows in bytes.
     60  int stride() const { return stride_; }
     61 
     62  // The pixel format the `DesktopFrame` is stored in.
     63  FourCC pixel_format() const { return pixel_format_; }
     64 
     65  // Data buffer used for the frame.
     66  uint8_t* data() const { return data_; }
     67 
     68  // SharedMemory used for the buffer or NULL if memory is allocated on the
     69  // heap. The result is guaranteed to be deleted only after the frame is
     70  // deleted (classes that inherit from DesktopFrame must ensure it).
     71  SharedMemory* shared_memory() const { return shared_memory_; }
     72 
     73  // Indicates region of the screen that has changed since the previous frame.
     74  const DesktopRegion& updated_region() const { return updated_region_; }
     75  DesktopRegion* mutable_updated_region() { return &updated_region_; }
     76 
     77  // DPI of the screen being captured. May be set to zero, e.g. if DPI is
     78  // unknown.
     79  const DesktopVector& dpi() const { return dpi_; }
     80  void set_dpi(const DesktopVector& dpi) { dpi_ = dpi; }
     81 
     82  std::optional<float> device_scale_factor() const {
     83    return device_scale_factor_;
     84  }
     85  void set_device_scale_factor(std::optional<float> device_scale_factor) {
     86    device_scale_factor_ = device_scale_factor;
     87  }
     88  // Indicates if this frame may have the mouse cursor in it. Capturers that
     89  // support cursor capture may set this to true. If the cursor was
     90  // outside of the captured area, this may be true even though the cursor is
     91  // not in the image.
     92  bool may_contain_cursor() const { return may_contain_cursor_; }
     93  void set_may_contain_cursor(bool may_contain_cursor) {
     94    may_contain_cursor_ = may_contain_cursor;
     95  }
     96 
     97  // Time taken to capture the frame in milliseconds.
     98  int64_t capture_time_ms() const { return capture_time_ms_; }
     99  void set_capture_time_ms(int64_t time_ms) { capture_time_ms_ = time_ms; }
    100 
    101  // Copies pixels from a buffer or another frame. `dest_rect` rect must lay
    102  // within bounds of this frame.
    103  void CopyPixelsFrom(const uint8_t* src_buffer,
    104                      int src_stride,
    105                      const DesktopRect& dest_rect);
    106  void CopyPixelsFrom(const DesktopFrame& src_frame,
    107                      const DesktopVector& src_pos,
    108                      const DesktopRect& dest_rect);
    109 
    110  // Copies pixels from another frame, with the copied & overwritten regions
    111  // representing the intersection between the two frames. Returns true if
    112  // pixels were copied, or false if there's no intersection. The scale factors
    113  // represent the ratios between pixel space & offset coordinate space (e.g.
    114  // 2.0 would indicate the frames are scaled down by 50% for display, so any
    115  // offset between their origins should be doubled).
    116  bool CopyIntersectingPixelsFrom(const DesktopFrame& src_frame,
    117                                  double horizontal_scale,
    118                                  double vertical_scale);
    119 
    120  // A helper to return the data pointer of a frame at the specified position.
    121  uint8_t* GetFrameDataAtPos(const DesktopVector& pos) const;
    122 
    123  // The DesktopCapturer implementation which generates current DesktopFrame.
    124  // Not all DesktopCapturer implementations set this field; it's set to
    125  // kUnknown by default.
    126  uint32_t capturer_id() const { return capturer_id_; }
    127  void set_capturer_id(uint32_t capturer_id) { capturer_id_ = capturer_id; }
    128 
    129  // Copies various information from `other`. Anything initialized in
    130  // constructor are not copied.
    131  // This function is usually used when sharing a source DesktopFrame with
    132  // several clients: the original DesktopFrame should be kept unchanged. For
    133  // example, BasicDesktopFrame::CopyOf() and SharedDesktopFrame::Share().
    134  void CopyFrameInfoFrom(const DesktopFrame& other);
    135 
    136  // Copies various information from `other`. Anything initialized in
    137  // constructor are not copied. Not like CopyFrameInfoFrom() function, this
    138  // function uses swap or move constructor to avoid data copy. It won't break
    139  // the `other`, but some of its information may be missing after this
    140  // operation. E.g. other->updated_region_;
    141  // This function is usually used when wrapping a DesktopFrame: the wrapper
    142  // instance takes the ownership of `other`, so other components cannot access
    143  // `other` anymore. For example, CroppedDesktopFrame and
    144  // DesktopFrameWithCursor.
    145  void MoveFrameInfoFrom(DesktopFrame* other);
    146 
    147  // Set and get the ICC profile of the frame data pixels. Useful to build the
    148  // a ColorSpace object from clients of webrtc library like chromium. The
    149  // format of an ICC profile is defined in the following specification
    150  // http://www.color.org/specification/ICC1v43_2010-12.pdf.
    151  const std::vector<uint8_t>& icc_profile() const { return icc_profile_; }
    152  void set_icc_profile(const std::vector<uint8_t>& icc_profile) {
    153    icc_profile_ = icc_profile;
    154  }
    155 
    156  // Sets all pixel values in the data buffer to zero.
    157  void SetFrameDataToBlack();
    158 
    159  // Returns true if all pixel values in the data buffer are zero or false
    160  // otherwise. Also returns false if the frame is empty.
    161  bool FrameDataIsBlack() const;
    162 
    163 protected:
    164  // TODO(bugs.webrtc.org/436974448): Remove.
    165  // Deprecated, use the next constructor.
    166  DesktopFrame(DesktopSize size,
    167               int stride,
    168               uint8_t* data,
    169               SharedMemory* shared_memory);
    170 
    171  DesktopFrame(DesktopSize size,
    172               int stride,
    173               FourCC pixel_format,
    174               uint8_t* data,
    175               SharedMemory* shared_memory);
    176 
    177  // Ownership of the buffers is defined by the classes that inherit from this
    178  // class. They must guarantee that the buffer is not deleted before the frame
    179  // is deleted.
    180  uint8_t* const data_;
    181  SharedMemory* const shared_memory_;
    182 
    183 private:
    184  const DesktopSize size_;
    185  const int stride_;
    186 
    187  // The pixel format for the data stored. Currently, only 4 byte per pixel
    188  // formats are supported.
    189  const FourCC pixel_format_;
    190 
    191  DesktopRegion updated_region_;
    192  DesktopVector top_left_;
    193  DesktopVector dpi_;
    194  bool may_contain_cursor_ = false;
    195  int64_t capture_time_ms_;
    196  uint32_t capturer_id_;
    197  std::vector<uint8_t> icc_profile_;
    198  // Currently only used on Windows. It stores the device scale factor of the
    199  // captured surface and has distinct values possible in the range of
    200  // [1,5].
    201  std::optional<float> device_scale_factor_;
    202 };
    203 
    204 // A DesktopFrame that stores data in the heap.
    205 class RTC_EXPORT BasicDesktopFrame : public DesktopFrame {
    206 public:
    207  // TODO(bugs.webrtc.org/436974448): Remove.
    208  // Deprecated, use the last constructor.
    209  explicit BasicDesktopFrame(DesktopSize size);
    210 
    211  // The entire data buffer used for the frame is initialized with zeros.
    212  BasicDesktopFrame(DesktopSize size, FourCC pixel_format);
    213 
    214  ~BasicDesktopFrame() override;
    215 
    216  BasicDesktopFrame(const BasicDesktopFrame&) = delete;
    217  BasicDesktopFrame& operator=(const BasicDesktopFrame&) = delete;
    218 
    219  // Creates a BasicDesktopFrame that contains copy of `frame`.
    220  // TODO(zijiehe): Return std::unique_ptr<DesktopFrame>
    221  static DesktopFrame* CopyOf(const DesktopFrame& frame);
    222 };
    223 
    224 // A DesktopFrame that stores data in shared memory.
    225 class RTC_EXPORT SharedMemoryDesktopFrame : public DesktopFrame {
    226 public:
    227  // May return nullptr if `shared_memory_factory` failed to create a
    228  // SharedMemory instance.
    229  // `shared_memory_factory` should not be nullptr.
    230  static std::unique_ptr<DesktopFrame> Create(
    231      DesktopSize size,
    232      FourCC pixel_format,
    233      SharedMemoryFactory* shared_memory_factory);
    234 
    235  // TODO(bugs.webrtc.org/436974448): Remove.
    236  // Deprecated, use the last constructor.
    237  SharedMemoryDesktopFrame(DesktopSize size,
    238                           int stride,
    239                           std::unique_ptr<SharedMemory> shared_memory);
    240 
    241  // Takes ownership of `shared_memory`.
    242  // Deprecated, use the last constructor.
    243  SharedMemoryDesktopFrame(DesktopSize size,
    244                           int stride,
    245                           FourCC pixel_format,
    246                           SharedMemory* shared_memory);
    247 
    248  // Preferred.
    249  SharedMemoryDesktopFrame(DesktopSize size,
    250                           int stride,
    251                           FourCC pixel_format,
    252                           std::unique_ptr<SharedMemory> shared_memory);
    253 
    254  ~SharedMemoryDesktopFrame() override;
    255 
    256  SharedMemoryDesktopFrame(const SharedMemoryDesktopFrame&) = delete;
    257  SharedMemoryDesktopFrame& operator=(const SharedMemoryDesktopFrame&) = delete;
    258 
    259 private:
    260  // Avoid unexpected order of parameter evaluation.
    261  // Executing both std::unique_ptr<T>::operator->() and
    262  // std::unique_ptr<T>::release() in the member initializer list is not safe.
    263  // Depends on the order of parameter evaluation,
    264  // std::unique_ptr<T>::operator->() may trigger assertion failure if it has
    265  // been evaluated after std::unique_ptr<T>::release(). By using this
    266  // constructor, std::unique_ptr<T>::operator->() won't be involved anymore.
    267  SharedMemoryDesktopFrame(DesktopRect rect,
    268                           int stride,
    269                           FourCC pixel_format,
    270                           SharedMemory* shared_memory);
    271 };
    272 
    273 }  // namespace webrtc
    274 
    275 #endif  // MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_