summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/mac/pixel_buffer_pool_mac.h
blob: 162204b67065669ab0d0f74f8cf688d08cde3912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CAPTURE_VIDEO_MAC_PIXEL_BUFFER_POOL_MAC_H_
#define MEDIA_CAPTURE_VIDEO_MAC_PIXEL_BUFFER_POOL_MAC_H_

#import <VideoToolbox/VideoToolbox.h>
#include <memory>

#include "base/mac/scoped_cftyperef.h"
#include "base/optional.h"
#include "media/capture/capture_export.h"

namespace media {

class CAPTURE_EXPORT PixelBufferPool {
 public:
  // All buffers created by the pool will have the specified properties.
  // If specified, the pool allows at most |max_buffers| to exist at a time by
  // having CreateBuffer() returns null if this threshold would be exceeded.
  //
  // If an unsupportd |format| is specified, or the pool cannot be created for
  // unknown reasons, null is returned.
  static std::unique_ptr<PixelBufferPool> Create(
      OSType format,
      int width,
      int height,
      base::Optional<size_t> max_buffers);
  ~PixelBufferPool();

  // Creates a new buffer from the pool, or returns null if |max_buffers_| would
  // be exceeded. The underlying buffers may be recycled.
  //
  // Freeing all buffer references returns the underlying buffer to the pool. In
  // order to free memory, you must both release all buffers and call Flush() or
  // delete the pool. It is safe for a buffer to outlive its pool.
  //
  // Retaining a pixel buffer and preventing it from returning to the pool can
  // be done either by keeping a reference directly to the CVPixelBuffer, e.g.
  // with a base::ScopedCFTypeRef<CVPixelBufferRef>, or by incrementing the use
  // count of the IOSurface, i.e. with IOSurfaceIncrementUseCount().
  //
  // WARNING: Retaining references to the pixel buffer's IOSurface (e.g. with
  // base::ScopedCFTypeRef<IOSurfaceRef>) without incrementing its use count
  // does NOT prevent it from being recycled!
  base::ScopedCFTypeRef<CVPixelBufferRef> CreateBuffer();

  // Frees the memory of any released buffers returned to the pool.
  void Flush();

 private:
  friend std::unique_ptr<PixelBufferPool> std::make_unique<PixelBufferPool>(
      base::ScopedCFTypeRef<CVPixelBufferPoolRef>&& buffer_pool,
      base::Optional<size_t>&& max_buffers);

  PixelBufferPool(base::ScopedCFTypeRef<CVPixelBufferPoolRef> buffer_pool,
                  base::Optional<size_t> max_buffers);

  base::ScopedCFTypeRef<CVPixelBufferPoolRef> buffer_pool_;
  const base::Optional<size_t> max_buffers_;
};

}  // namespace media

#endif  // MEDIA_CAPTURE_VIDEO_MAC_PIXEL_BUFFER_POOL_MAC_H_