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

media / renderers / video_frame_shared_image_cache.h [blame]

// Copyright 2021 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_RENDERERS_VIDEO_FRAME_SHARED_IMAGE_CACHE_H_
#define MEDIA_RENDERERS_VIDEO_FRAME_SHARED_IMAGE_CACHE_H_

#include "media/base/media_export.h"
#include "media/base/video_frame.h"

namespace gpu {
class ClientSharedImage;
struct SyncToken;
}  // namespace gpu

namespace viz {
class RasterContextProvider;
}  // namespace viz

namespace media {

class MEDIA_EXPORT VideoFrameSharedImageCache {
 public:
  // Returns the cache Status on interaction with VideoFrameSharedImageCache.
  // Specifies whether a new SharedImage was created or not based on VideoFrame
  // id and SharedImage data.
  enum class Status {
    // The cached VideoFrame id matched.
    kMatchedVideoFrameId,
    // The cached SharedImage metadata (size, usage etc) matched.
    kMatchedSharedImageMetaData,
    // Mismatch of id/data, new SharedImage created.
    kCreatedNewSharedImage,
  };

  struct CachedData {
    CachedData(scoped_refptr<gpu::ClientSharedImage> shared_image,
               const gpu::SyncToken& sync_token,
               Status status);
    ~CachedData();

    scoped_refptr<gpu::ClientSharedImage> shared_image;
    gpu::SyncToken sync_token;
    Status status;
  };

  VideoFrameSharedImageCache();
  ~VideoFrameSharedImageCache();

  void ReleaseCachedData();

  // Creates a new shared image if the `video_frame` data differs from that in
  // `shared_image_`. This function can be called repeatedly to re-use
  // `shared_image_` in the case of CPU backed VideoFrames. Returns the
  // `shared_image_` along with Status on whether the shared image was created
  // or reused.
  CachedData GetSharedImage(const VideoFrame* video_frame,
                            viz::RasterContextProvider* raster_context_provider,
                            gpu::SharedImageUsageSet usage);

  // Update the `sync_token_` to wait for after performing raster/gles tasks.
  void UpdateSyncToken(const gpu::SyncToken& sync_token);

 private:
  scoped_refptr<viz::RasterContextProvider> provider_;

  // Populated by GetSharedImage.
  scoped_refptr<gpu::ClientSharedImage> shared_image_;
  VideoFrame::ID video_frame_id_;
  // The sync token initially generated on SharedImageInterface that can be
  // updated through UpdateSyncToken by the client based on raster/gles usages.
  // This allows to makes sure that on shared image destruction, we wait on
  // previous raster/gles tasks to be completed properly.
  gpu::SyncToken sync_token_;
};

}  // namespace media

#endif  // MEDIA_RENDERERS_VIDEO_FRAME_SHARED_IMAGE_CACHE_H_