#ifndef COMPONENTS_VIZ_COMMON_RESOURCES_SHARED_IMAGE_FORMAT_H_
#define COMPONENTS_VIZ_COMMON_RESOURCES_SHARED_IMAGE_FORMAT_H_
#include <stdint.h>
#include <string>
#include "base/check.h"
#include "components/viz/common/resources/resource_format.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/cpp/bindings/union_traits.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gfx/geometry/size.h"
namespace viz {
namespace mojom {
class SharedImageFormatDataView;
class MultiplanarFormatDataView;
}
class SharedImageFormat {
public:
enum class PlaneConfig : uint8_t {
kY_V_U,
kY_UV,
kY_UV_A,
};
enum class Subsampling : uint8_t {
k420,
};
enum class ChannelFormat : uint8_t {
k8,
k10,
k16,
k16F
};
SharedImageFormat() = default;
static constexpr SharedImageFormat SinglePlane(
ResourceFormat resource_format) {
return SharedImageFormat(resource_format);
}
static constexpr SharedImageFormat MultiPlane(PlaneConfig plane_config,
Subsampling subsampling,
ChannelFormat channel_format) {
return SharedImageFormat(plane_config, subsampling, channel_format);
}
ResourceFormat resource_format() const {
DCHECK(is_single_plane());
return format_.resource_format;
}
PlaneConfig plane_config() const {
DCHECK(is_multi_plane());
return format_.multiplanar_format.plane_config;
}
Subsampling subsampling() const {
DCHECK(is_multi_plane());
return format_.multiplanar_format.subsampling;
}
ChannelFormat channel_format() const {
DCHECK(is_multi_plane());
return format_.multiplanar_format.channel_format;
}
bool is_single_plane() const {
return plane_type_ == PlaneType::kSinglePlane;
}
bool is_multi_plane() const { return plane_type_ == PlaneType::kMultiPlane; }
bool PrefersExternalSampler() const { return false; }
bool IsBitmapFormatSupported() const;
int NumberOfPlanes() const;
bool IsValidPlaneIndex(int plane_index) const;
gfx::Size GetPlaneSize(int plane_index, const gfx::Size& size) const;
absl::optional<size_t> MaybeEstimatedSizeInBytes(const gfx::Size& size) const;
size_t EstimatedSizeInBytes(const gfx::Size& size) const;
int NumChannelsInPlane(int plane_index) const;
int MultiplanarBitDepth() const;
std::string ToString() const;
std::string ToTestParamString() const;
bool HasAlpha() const;
bool IsCompressed() const;
bool IsLegacyMultiplanar() const;
bool operator==(const SharedImageFormat& o) const;
bool operator!=(const SharedImageFormat& o) const;
bool operator<(const SharedImageFormat& o) const;
private:
enum class PlaneType : uint8_t {
kUnknown,
kSinglePlane,
kMultiPlane,
};
union SharedImageFormatUnion {
struct MultiplanarFormat {
PlaneConfig plane_config;
Subsampling subsampling;
ChannelFormat channel_format;
bool operator==(const MultiplanarFormat& o) const;
bool operator!=(const MultiplanarFormat& o) const;
bool operator<(const MultiplanarFormat& o) const;
};
SharedImageFormatUnion() = default;
explicit constexpr SharedImageFormatUnion(ResourceFormat resource_format)
: resource_format(resource_format) {}
constexpr SharedImageFormatUnion(PlaneConfig plane_config,
Subsampling subsampling,
ChannelFormat channel_format)
: multiplanar_format({plane_config, subsampling, channel_format}) {}
ResourceFormat resource_format;
MultiplanarFormat multiplanar_format;
};
friend struct mojo::UnionTraits<mojom::SharedImageFormatDataView,
SharedImageFormat>;
friend struct mojo::StructTraits<mojom::MultiplanarFormatDataView,
SharedImageFormatUnion::MultiplanarFormat>;
explicit constexpr SharedImageFormat(ResourceFormat resource_format)
: plane_type_(PlaneType::kSinglePlane), format_(resource_format) {}
constexpr SharedImageFormat(PlaneConfig plane_config,
Subsampling subsampling,
ChannelFormat channel_format)
: plane_type_(PlaneType::kMultiPlane),
format_(plane_config, subsampling, channel_format) {}
PlaneType plane_type() const { return plane_type_; }
SharedImageFormatUnion::MultiplanarFormat multiplanar_format() const {
DCHECK(is_multi_plane());
return format_.multiplanar_format;
}
PlaneType plane_type_ = PlaneType::kUnknown;
SharedImageFormatUnion format_;
};
namespace SinglePlaneFormat {
inline constexpr SharedImageFormat kRGBA_8888 =
SharedImageFormat::SinglePlane(ResourceFormat::RGBA_8888);
inline constexpr SharedImageFormat kRGBA_4444 =
SharedImageFormat::SinglePlane(ResourceFormat::RGBA_4444);
inline constexpr SharedImageFormat kBGRA_8888 =
SharedImageFormat::SinglePlane(ResourceFormat::BGRA_8888);
inline constexpr SharedImageFormat kALPHA_8 =
SharedImageFormat::SinglePlane(ResourceFormat::ALPHA_8);
inline constexpr SharedImageFormat kLUMINANCE_8 =
SharedImageFormat::SinglePlane(ResourceFormat::LUMINANCE_8);
inline constexpr SharedImageFormat kRGB_565 =
SharedImageFormat::SinglePlane(ResourceFormat::RGB_565);
inline constexpr SharedImageFormat kBGR_565 =
SharedImageFormat::SinglePlane(ResourceFormat::BGR_565);
inline constexpr SharedImageFormat kETC1 =
SharedImageFormat::SinglePlane(ResourceFormat::ETC1);
inline constexpr SharedImageFormat kR_8 =
SharedImageFormat::SinglePlane(ResourceFormat::RED_8);
inline constexpr SharedImageFormat kRG_88 =
SharedImageFormat::SinglePlane(ResourceFormat::RG_88);
inline constexpr SharedImageFormat kLUMINANCE_F16 =
SharedImageFormat::SinglePlane(ResourceFormat::LUMINANCE_F16);
inline constexpr SharedImageFormat kRGBA_F16 =
SharedImageFormat::SinglePlane(ResourceFormat::RGBA_F16);
inline constexpr SharedImageFormat kR_16 =
SharedImageFormat::SinglePlane(ResourceFormat::R16_EXT);
inline constexpr SharedImageFormat kRG_1616 =
SharedImageFormat::SinglePlane(ResourceFormat::RG16_EXT);
inline constexpr SharedImageFormat kRGBX_8888 =
SharedImageFormat::SinglePlane(ResourceFormat::RGBX_8888);
inline constexpr SharedImageFormat kBGRX_8888 =
SharedImageFormat::SinglePlane(ResourceFormat::BGRX_8888);
inline constexpr SharedImageFormat kRGBA_1010102 =
SharedImageFormat::SinglePlane(ResourceFormat::RGBA_1010102);
inline constexpr SharedImageFormat kBGRA_1010102 =
SharedImageFormat::SinglePlane(ResourceFormat::BGRA_1010102);
}
namespace LegacyMultiPlaneFormat {
inline constexpr SharedImageFormat kYV12 =
SharedImageFormat::SinglePlane(ResourceFormat::YVU_420);
inline constexpr SharedImageFormat kNV12 =
SharedImageFormat::SinglePlane(ResourceFormat::YUV_420_BIPLANAR);
inline constexpr SharedImageFormat kNV12A =
SharedImageFormat::SinglePlane(ResourceFormat::YUVA_420_TRIPLANAR);
inline constexpr SharedImageFormat kP010 =
SharedImageFormat::SinglePlane(ResourceFormat::P010);
}
namespace MultiPlaneFormat {
inline constexpr SharedImageFormat kYV12 =
SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_V_U,
SharedImageFormat::Subsampling::k420,
SharedImageFormat::ChannelFormat::k8);
inline constexpr SharedImageFormat kNV12 =
SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_UV,
SharedImageFormat::Subsampling::k420,
SharedImageFormat::ChannelFormat::k8);
inline constexpr SharedImageFormat kNV12A =
SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_UV_A,
SharedImageFormat::Subsampling::k420,
SharedImageFormat::ChannelFormat::k8);
inline constexpr SharedImageFormat kP010 =
SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_UV,
SharedImageFormat::Subsampling::k420,
SharedImageFormat::ChannelFormat::k10);
}
}
#endif