#ifndef MEDIA_GPU_WINDOWS_D3D11_TEXTURE_WRAPPER_H_
#define MEDIA_GPU_WINDOWS_D3D11_TEXTURE_WRAPPER_H_
#include <memory>
#include <optional>
#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/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_status.h"
#include "media/gpu/windows/d3d_com_defs.h"
#include "ui/gfx/color_space.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 GetCommandBufferHelperCB =
base::RepeatingCallback<CommandBufferHelperPtr()>;
class MEDIA_GPU_EXPORT Texture2DWrapper {
public:
using PictureBufferGPUResourceInitDoneCB =
base::OnceCallback<void(scoped_refptr<media::D3D11PictureBuffer>)>;
Texture2DWrapper();
virtual ~Texture2DWrapper();
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;
virtual D3D11Status BeginSharedImageAccess() = 0;
virtual D3D11Status ProcessTexture(
const gfx::ColorSpace& input_color_space,
scoped_refptr<gpu::ClientSharedImage>& shared_image_dest_out) = 0;
};
class MEDIA_GPU_EXPORT DefaultTexture2DWrapper : public Texture2DWrapper {
public:
using OnErrorCB = base::OnceCallback<void(D3D11Status)>;
using GPUResourceInitCB =
base::OnceCallback<void(scoped_refptr<media::D3D11PictureBuffer>,
std::unique_ptr<gpu::VideoImageRepresentation>,
scoped_refptr<gpu::ClientSharedImage>)>;
DefaultTexture2DWrapper(const gfx::Size& size,
const gfx::ColorSpace& output_color_space,
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,
scoped_refptr<gpu::ClientSharedImage>& shared_image_dest) override;
void OnGPUResourceInitDone(
scoped_refptr<media::D3D11PictureBuffer> picture_buffer,
std::unique_ptr<gpu::VideoImageRepresentation> shared_image_rep,
scoped_refptr<gpu::ClientSharedImage> client_shared_image);
ComD3D11Device GetVideoDevice() { return video_device_; }
private:
class GpuResources {
public:
GpuResources(OnErrorCB on_error_cb,
GetCommandBufferHelperCB get_helper_cb,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
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::unique_ptr<gpu::SharedImageRepresentationFactoryRef> shared_image_;
base::WeakPtrFactory<GpuResources> weak_factory_{this};
};
void OnError(D3D11Status status);
std::optional<D3D11Status> received_error_;
const gfx::Size size_;
const gfx::ColorSpace output_color_space_;
base::SequenceBound<GpuResources> gpu_resources_;
scoped_refptr<gpu::ClientSharedImage> shared_image_;
DXGI_FORMAT dxgi_format_;
std::unique_ptr<gpu::VideoImageRepresentation> shared_image_rep_;
std::unique_ptr<gpu::VideoImageRepresentation::ScopedWriteAccess>
shared_image_access_;
ComD3D11Device video_device_;
Texture2DWrapper::PictureBufferGPUResourceInitDoneCB
picture_buffer_gpu_resource_init_done_cb_;
base::WeakPtrFactory<DefaultTexture2DWrapper> weak_factory_{this};
};
}
#endif