blob: 00e46097a24558d3220c737aeef96f89c433384d [file] [log] [blame]
// Copyright 2019 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_WINDOWS_D3D11_TEXTURE_WRAPPER_H_
#define MEDIA_GPU_WINDOWS_D3D11_TEXTURE_WRAPPER_H_
#include <d3d11.h>
#include <wrl/client.h>
#include <memory>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "gpu/command_buffer/service/mailbox_manager.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "media/base/video_frame.h"
#include "media/gpu/command_buffer_helper.h"
#include "media/gpu/media_gpu_export.h"
#include "media/gpu/windows/d3d11_com_defs.h"
#include "media/gpu/windows/d3d11_status.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/hdr_metadata.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_surface_egl.h"
#include "ui/gl/scoped_binders.h"
namespace media {
class D3D11PictureBuffer;
using CommandBufferHelperPtr = scoped_refptr<CommandBufferHelper>;
using MailboxHolderArray = gpu::MailboxHolder[VideoFrame::kMaxPlanes];
using GetCommandBufferHelperCB =
base::RepeatingCallback<CommandBufferHelperPtr()>;
// Support different strategies for processing pictures - some may need copying,
// for example. Each wrapper owns the resources for a single texture, so it's
// up to you not to re-use a wrapper for a second image before a previously
// processed image is no longer needed.
class MEDIA_GPU_EXPORT Texture2DWrapper {
public:
using PictureBufferGPUResourceInitDoneCB =
base::OnceCallback<void(scoped_refptr<media::D3D11PictureBuffer>)>;
Texture2DWrapper();
virtual ~Texture2DWrapper();
// Initialize the wrapper.
virtual D3D11Status Init(
scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner,
GetCommandBufferHelperCB get_helper_cb,
ComD3D11Texture2D texture,
size_t array_size,
scoped_refptr<media::D3D11PictureBuffer> picture_buffer,
PictureBufferGPUResourceInitDoneCB
picture_buffer_gpu_resource_init_done_cb) = 0;
// "If the |texture| is shared and needs synchronization, it is important for
// shared image to call begin access before any usage. This API is required to
// be called:
// - Before reading or writing to the texture via views on the texture or
// other means.
// - Before calling ProcessTexture.
// And need reset the scoped access object to end access.
virtual D3D11Status BeginSharedImageAccess() = 0;
// Import |texture|, |array_slice| and return the mailbox(es) that can be
// used to refer to it.
virtual D3D11Status ProcessTexture(const gfx::ColorSpace& input_color_space,
MailboxHolderArray* mailbox_dest_out,
gfx::ColorSpace* output_color_space) = 0;
virtual void SetStreamHDRMetadata(
const gfx::HDRMetadata& stream_metadata) = 0;
virtual void SetDisplayHDRMetadata(
const DXGI_HDR_METADATA_HDR10& dxgi_display_metadata) = 0;
};
// The default texture wrapper that uses GPUResources to talk to hardware
// on behalf of a Texture2D. Each DefaultTexture2DWrapper owns GL textures
// that it uses to bind the provided input texture. Thus, one needs one wrapper
// instance for each concurrently outstanding texture.
class MEDIA_GPU_EXPORT DefaultTexture2DWrapper : public Texture2DWrapper {
public:
// Error callback for GpuResource to notify us of errors.
using OnErrorCB = base::OnceCallback<void(D3D11Status)>;
// Callback for setting shared image representation and resume picture buffer
// after gpu resource initialization.
using GPUResourceInitCB = base::OnceCallback<void(
scoped_refptr<media::D3D11PictureBuffer>,
std::unique_ptr<gpu::VideoDecodeImageRepresentation>)>;
// While the specific texture instance can change on every call to
// ProcessTexture, the dxgi format must be the same for all of them.
DefaultTexture2DWrapper(const gfx::Size& size,
DXGI_FORMAT dxgi_format,
ComD3D11Device device);
~DefaultTexture2DWrapper() override;
D3D11Status Init(scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner,
GetCommandBufferHelperCB get_helper_cb,
ComD3D11Texture2D in_texture,
size_t array_slice,
scoped_refptr<media::D3D11PictureBuffer> picture_buffer,
Texture2DWrapper::PictureBufferGPUResourceInitDoneCB
picture_buffer_gpu_resource_init_done_cb) override;
D3D11Status BeginSharedImageAccess() override;
D3D11Status ProcessTexture(const gfx::ColorSpace& input_color_space,
MailboxHolderArray* mailbox_dest,
gfx::ColorSpace* output_color_space) override;
void SetStreamHDRMetadata(const gfx::HDRMetadata& stream_metadata) override;
void SetDisplayHDRMetadata(
const DXGI_HDR_METADATA_HDR10& dxgi_display_metadata) override;
void OnGPUResourceInitDone(
scoped_refptr<media::D3D11PictureBuffer> picture_buffer,
std::unique_ptr<gpu::VideoDecodeImageRepresentation> shared_image_rep);
ComD3D11Device GetVideoDevice() { return video_device_; }
private:
// Things that are to be accessed / freed only on the main thread. In
// addition to setting up the textures to render from a D3D11 texture,
// these also hold the chrome GL Texture objects so that the client
// can use the mailbox.
class GpuResources {
public:
GpuResources(OnErrorCB on_error_cb,
GetCommandBufferHelperCB get_helper_cb,
const std::vector<gpu::Mailbox>& mailboxes,
const gfx::Size& size,
DXGI_FORMAT dxgi_format,
ComD3D11Device video_device,
ComD3D11Texture2D texture,
size_t array_slice,
scoped_refptr<media::D3D11PictureBuffer> picture_buffer,
GPUResourceInitCB gpu_resource_init_cb);
GpuResources(const GpuResources&) = delete;
GpuResources& operator=(const GpuResources&) = delete;
~GpuResources();
private:
scoped_refptr<CommandBufferHelper> helper_;
std::vector<std::unique_ptr<gpu::SharedImageRepresentationFactoryRef>>
shared_images_;
base::WeakPtrFactory<GpuResources> weak_factory_{this};
};
// Receive an error from |gpu_resources_| and store it in |received_error_|.
void OnError(D3D11Status status);
// The first error status that we've received from |gpu_resources_|, if any.
absl::optional<D3D11Status> received_error_;
gfx::Size size_;
base::SequenceBound<GpuResources> gpu_resources_;
MailboxHolderArray mailbox_holders_;
DXGI_FORMAT dxgi_format_;
std::unique_ptr<gpu::VideoDecodeImageRepresentation> shared_image_rep_;
std::unique_ptr<gpu::VideoDecodeImageRepresentation::ScopedWriteAccess>
shared_image_access_;
ComD3D11Device video_device_;
Texture2DWrapper::PictureBufferGPUResourceInitDoneCB
picture_buffer_gpu_resource_init_done_cb_;
base::WeakPtrFactory<DefaultTexture2DWrapper> weak_factory_{this};
};
} // namespace media
#endif // MEDIA_GPU_WINDOWS_D3D11_TEXTURE_WRAPPER_H_