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
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138

media / gpu / chromeos / platform_video_frame_utils.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_GPU_CHROMEOS_PLATFORM_VIDEO_FRAME_UTILS_H_
#define MEDIA_GPU_CHROMEOS_PLATFORM_VIDEO_FRAME_UTILS_H_

#include <optional>

#include "base/memory/scoped_refptr.h"
#include "base/unguessable_token.h"
#include "media/base/video_frame.h"
#include "media/gpu/media_gpu_export.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/gpu_memory_buffer.h"
#include "ui/gfx/linux/native_pixmap_dmabuf.h"

namespace media {

// Used to create UnguessableTokens that are guaranteed to be unique with
// respect to others that are generated by this class.
class UniqueTrackingTokenHelper {
 public:
  UniqueTrackingTokenHelper();
  UniqueTrackingTokenHelper(const UniqueTrackingTokenHelper&) = delete;
  UniqueTrackingTokenHelper& operator=(const UniqueTrackingTokenHelper&) =
      delete;
  ~UniqueTrackingTokenHelper();

  // Clears all tokens. Used when releasing all frames.
  void ClearTokens();

  // Clears a token from the helper. Called when a token is no longer used to
  // ensure the helper does not leak tokens.
  void ClearToken(const base::UnguessableToken& token);

  // SetUniqueTrackingToken sets a unique |tracking_token| in |metadata|. Note
  // that this operation is not idempotent. Subsequent calls will always
  // produce a new, unique tracking token.
  void SetUniqueTrackingToken(VideoFrameMetadata& metadata);

 private:
  // Initializes |tokens_| with an empty UnguessableToken. This ensures that
  // users cannot insert an empty token.
  void Initialize();

  // Generates a unique UnguessableToken.
  base::UnguessableToken GenerateToken();

  // The expectation is that this class will be used for frame pools where the
  // number of tokens will be relatively small. I.e. tens of tokens at most.
  // If this is not true, then we should figure out why, so crashing Chrome is
  // reasonable.
  static constexpr size_t kMaxNumberOfTokens = 1000;

  std::set<base::UnguessableToken> tokens_;
};

// Returns a GpuMemoryBufferId that's guaranteed to be different from those
// returned by previous calls. This function is thread safe.
MEDIA_GPU_EXPORT gfx::GpuMemoryBufferId GetNextGpuMemoryBufferId();

// Creates a GpuMemoryBufferHandle. This function is thread safe.
gfx::GpuMemoryBufferHandle AllocateGpuMemoryBufferHandle(
    VideoPixelFormat pixel_format,
    const gfx::Size& coded_size,
    gfx::BufferUsage buffer_usage);

// Creates a STORAGE_GPU_MEMORY_BUFFER VideoFrame backed by a NATIVE_PIXMAP
// GpuMemoryBuffer allocated with |buffer_usage|. See //media/base/video_frame.h
// for the other parameters. This function is thread-safe.
MEDIA_GPU_EXPORT scoped_refptr<VideoFrame> CreateGpuMemoryBufferVideoFrame(
    VideoPixelFormat pixel_format,
    const gfx::Size& coded_size,
    const gfx::Rect& visible_rect,
    const gfx::Size& natural_size,
    base::TimeDelta timestamp,
    gfx::BufferUsage buffer_usage);

// Creates a STORAGE_GPU_MEMORY_BUFFER VideoFrame from a GpuMemoryBufferHandle.
// See //media/base/video_frame.h for the other parameters. This function is
// thread-safe.
scoped_refptr<VideoFrame> CreateVideoFrameFromGpuMemoryBufferHandle(
    gfx::GpuMemoryBufferHandle gmb_handle,
    VideoPixelFormat pixel_format,
    const gfx::Size& coded_size,
    const gfx::Rect& visible_rect,
    const gfx::Size& natural_size,
    base::TimeDelta timestamp,
    gfx::BufferUsage buffer_usage);

// Creates a STORAGE_DMABUFS VideoFrame whose buffer is allocated with
// |buffer_usage|. See //media/base/video_frame.h for the other parameters. This
// function is thread-safe.
MEDIA_GPU_EXPORT scoped_refptr<VideoFrame> CreatePlatformVideoFrame(
    VideoPixelFormat pixel_format,
    const gfx::Size& coded_size,
    const gfx::Rect& visible_rect,
    const gfx::Size& natural_size,
    base::TimeDelta timestamp,
    gfx::BufferUsage buffer_usage);

// Returns the VideoFrameLayout of a VideoFrame allocated with
// CreatePlatformVideoFrame(), i.e., all parameters are forwarded to that
// function (|visible_rect| is set to gfx::Rect(|coded_size|), |natural_size| is
// set to |coded_size|, and |timestamp| is set to base::TimeDelta()). This
// function is not cheap as it allocates a buffer. Returns std::nullopt if the
// buffer allocation fails. This function is thread-safe.
MEDIA_GPU_EXPORT std::optional<VideoFrameLayout> GetPlatformVideoFrameLayout(
    VideoPixelFormat pixel_format,
    const gfx::Size& coded_size,
    gfx::BufferUsage buffer_usage);

// Create a shared GPU memory handle to the |video_frame|'s data.
MEDIA_GPU_EXPORT gfx::GpuMemoryBufferHandle CreateGpuMemoryBufferHandle(
    const VideoFrame* video_frame);

// Create a NativePixmap that references the DMA Bufs of |video_frame|. The
// returned pixmap is only a DMA Buf container and should not be used for
// compositing/scanout.
MEDIA_GPU_EXPORT scoped_refptr<gfx::NativePixmapDmaBuf>
CreateNativePixmapDmaBuf(const VideoFrame* video_frame);

// Returns either the GPU MemoryBuffer ID or the FD of the first file
// descriptor depending on the storage type.
MEDIA_GPU_EXPORT gfx::GenericSharedMemoryId GetSharedMemoryId(
    const VideoFrame& frame);

// Returns true if |gmb_handle| can be imported into minigbm and false
// otherwise.
bool CanImportGpuMemoryBufferHandle(
    const gfx::Size& size,
    gfx::BufferFormat format,
    const gfx::GpuMemoryBufferHandle& gmb_handle);

}  // namespace media

#endif  // MEDIA_GPU_CHROMEOS_PLATFORM_VIDEO_FRAME_UTILS_H_