#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/service/copy_shared_image_helper.h"
#include <memory>
#include <vector>
#include "base/check.h"
#include "base/strings/string_number_conversions.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/service/feature_info.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/shared_image/shared_image_factory.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/skia_utils.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/config/gpu_finch_features.h"
#include "skia/ext/rgba_to_yuva.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "third_party/skia/include/gpu/GrTypes.h"
#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "third_party/skia/include/gpu/gl/GrGLTypes.h"
#include "third_party/skia/include/gpu/graphite/Context.h"
#include "third_party/skia/include/gpu/graphite/Image.h"
#include "third_party/skia/include/gpu/graphite/Recorder.h"
#include "third_party/skia/include/gpu/graphite/YUVABackendTextures.h"
#include "third_party/skia/include/private/chromium/GrPromiseImageTexture.h"
#include "ui/gfx/geometry/skia_conversions.h"
namespace gpu {
GLError;
namespace {
SkColorType GetCompatibleSurfaceColorType(GrGLenum format) { … }
GrGLenum GetSurfaceColorFormat(GrGLenum format, GrGLenum type) { … }
sk_sp<SkSurface> CreateSkSurfaceWrappingGLTexture(
SharedContextState* shared_context_state,
GLuint texture_id,
GLenum target,
GLuint internal_format,
GLenum type,
GLsizei width,
GLsizei height,
GLboolean flip_y) { … }
base::expected<void, GLError> ConvertYUVACommon(
const char* function_name,
GLenum yuv_color_space_in,
GLenum plane_config_in,
GLenum subsampling_in,
const volatile GLbyte* mailboxes_in,
SharedImageRepresentationFactory* representation_factory,
SharedContextState* shared_context_state,
SkYUVColorSpace& sk_yuv_color_space,
SkYUVAInfo::PlaneConfig& sk_plane_config,
SkYUVAInfo::Subsampling& sk_subsampling,
int& num_yuva_planes,
std::array<std::unique_ptr<SkiaImageRepresentation>,
SkYUVAInfo::kMaxPlanes>& yuva_images) { … }
sk_sp<SkColorSpace> ReadSkColorSpace(const volatile GLbyte* bytes) { … }
bool TryCopySubTextureINTERNALMemory(
GLint xoffset,
GLint yoffset,
GLint x,
GLint y,
GLsizei width,
GLsizei height,
gfx::Rect dest_cleared_rect,
GLboolean unpack_flip_y,
const Mailbox& source_mailbox,
SkiaImageRepresentation* dest_shared_image,
SkiaImageRepresentation::ScopedWriteAccess* dest_scoped_access,
SharedImageRepresentationFactory* representation_factory,
SharedContextState* shared_context_state,
const std::vector<GrBackendSemaphore>& begin_semaphores,
std::vector<GrBackendSemaphore>& end_semaphores) { … }
struct ReadPixelsContext { … };
void OnReadPixelsDone(
void* raw_ctx,
std::unique_ptr<const SkImage::AsyncReadResult> async_result) { … }
}
CopySharedImageHelper::CopySharedImageHelper(
SharedImageRepresentationFactory* representation_factory,
SharedContextState* shared_context_state)
: … { … }
CopySharedImageHelper::~CopySharedImageHelper() = default;
CopySharedImageHelper::GLError::GLError(GLenum gl_error,
std::string function_name,
std::string msg)
: … { … }
base::expected<void, GLError> CopySharedImageHelper::ConvertRGBAToYUVAMailboxes(
GLenum yuv_color_space,
GLenum plane_config,
GLenum subsampling,
const volatile GLbyte* mailboxes_in) { … }
base::expected<void, GLError> CopySharedImageHelper::ConvertYUVAMailboxesToRGB(
GLint src_x,
GLint src_y,
GLsizei width,
GLsizei height,
GLenum planes_yuv_color_space,
GLenum plane_config,
GLenum subsampling,
const volatile GLbyte* bytes_in) { … }
base::expected<void, GLError>
CopySharedImageHelper::ConvertYUVAMailboxesToSkSurface(
const char* function_name,
GLint src_x,
GLint src_y,
GLsizei width,
GLsizei height,
GLenum planes_yuv_color_space,
GLenum plane_config,
GLenum subsampling,
const volatile GLbyte* bytes_in,
SkSurface* dest_surface,
bool dest_need_graphite_submit,
std::vector<GrBackendSemaphore>& begin_semaphores,
std::vector<GrBackendSemaphore>& end_semaphores,
sk_sp<SkColorSpace> src_rgb_color_space,
base::FunctionRef<void()> flush_dest_surface_function) { … }
base::expected<void, GLError>
CopySharedImageHelper::ConvertYUVAMailboxesToGLTexture(
GLuint dest_texture_id,
GLenum target,
GLuint internal_format,
GLenum type,
GLint src_x,
GLint src_y,
GLsizei width,
GLsizei height,
bool flip_y,
GLenum planes_yuv_color_space,
GLenum plane_config,
GLenum subsampling,
const volatile GLbyte* bytes_in) { … }
base::expected<void, GLError> CopySharedImageHelper::CopySharedImage(
GLint xoffset,
GLint yoffset,
GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLboolean unpack_flip_y,
const volatile GLbyte* mailboxes) { … }
base::expected<void, GLError> CopySharedImageHelper::CopySharedImageToGLTexture(
GLuint dest_texture_id,
GLenum target,
GLuint internal_format,
GLenum type,
GLint src_x,
GLint src_y,
GLsizei width,
GLsizei height,
GLboolean flip_y,
const volatile GLbyte* src_mailbox) { … }
base::expected<void, GLError> CopySharedImageHelper::ReadPixels(
GLint src_x,
GLint src_y,
GLint plane_index,
GLuint row_bytes,
SkImageInfo dst_info,
void* pixel_address,
std::unique_ptr<SkiaImageRepresentation> source_shared_image) { … }
base::expected<void, GLError> CopySharedImageHelper::WritePixelsYUV(
GLuint src_width,
GLuint src_height,
std::array<SkPixmap, SkYUVAInfo::kMaxPlanes> pixmaps,
std::vector<GrBackendSemaphore> end_semaphores,
std::unique_ptr<SkiaImageRepresentation> dest_shared_image,
std::unique_ptr<SkiaImageRepresentation::ScopedWriteAccess>
dest_scoped_access) { … }
}