Commit b80801c5 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Introduce ExplicitArgumentBarrier to make_span

This change adds ExplicitArgumentBarrier to base::make_span, blocking
callers from explicitly specifying types the compiler can deduce for
them. It also performs some other minor clean-ups in effected code,
such as getting rid of base::make_span completely when an implicit
conversion would have been performed anyway.

TBR=khushalsagar,caseq,mcasas,kouhei,rockot@google.com

Bug: 828324

Change-Id: I4c462b9802c477baf6f601ee5651a90a6d929fbf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1871699
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709426}
parent b6f109c6
...@@ -468,32 +468,33 @@ constexpr void swap(span<T, X>& lhs, span<T, X>& rhs) noexcept { ...@@ -468,32 +468,33 @@ constexpr void swap(span<T, X>& lhs, span<T, X>& rhs) noexcept {
} }
// Type-deducing helpers for constructing a span. // Type-deducing helpers for constructing a span.
template <typename T> template <int&... ExplicitArgumentBarrier, typename T>
constexpr span<T> make_span(T* data, size_t size) noexcept { constexpr span<T> make_span(T* data, size_t size) noexcept {
return {data, size}; return {data, size};
} }
template <typename T> template <int&... ExplicitArgumentBarrier, typename T>
constexpr span<T> make_span(T* begin, T* end) noexcept { constexpr span<T> make_span(T* begin, T* end) noexcept {
return {begin, end}; return {begin, end};
} }
template <typename T, size_t N> template <int&... ExplicitArgumentBarrier, typename T, size_t N>
constexpr span<T, N> make_span(T (&array)[N]) noexcept { constexpr span<T, N> make_span(T (&array)[N]) noexcept {
return array; return array;
} }
template <typename T, size_t N> template <int&... ExplicitArgumentBarrier, typename T, size_t N>
constexpr span<T, N> make_span(std::array<T, N>& array) noexcept { constexpr span<T, N> make_span(std::array<T, N>& array) noexcept {
return array; return array;
} }
template <typename T, size_t N> template <int&... ExplicitArgumentBarrier, typename T, size_t N>
constexpr span<const T, N> make_span(const std::array<T, N>& array) noexcept { constexpr span<const T, N> make_span(const std::array<T, N>& array) noexcept {
return array; return array;
} }
template <typename Container, template <int&... ExplicitArgumentBarrier,
typename Container,
typename T = std::remove_pointer_t< typename T = std::remove_pointer_t<
decltype(base::data(std::declval<Container&>()))>, decltype(base::data(std::declval<Container&>()))>,
typename = internal::EnableIfSpanCompatibleContainer<Container&, T>> typename = internal::EnableIfSpanCompatibleContainer<Container&, T>>
...@@ -502,6 +503,7 @@ constexpr span<T> make_span(Container& container) noexcept { ...@@ -502,6 +503,7 @@ constexpr span<T> make_span(Container& container) noexcept {
} }
template < template <
int&... ExplicitArgumentBarrier,
typename Container, typename Container,
typename T = std::remove_pointer_t< typename T = std::remove_pointer_t<
decltype(base::data(std::declval<const Container&>()))>, decltype(base::data(std::declval<const Container&>()))>,
...@@ -511,6 +513,7 @@ constexpr span<T> make_span(const Container& container) noexcept { ...@@ -511,6 +513,7 @@ constexpr span<T> make_span(const Container& container) noexcept {
} }
template <size_t N, template <size_t N,
int&... ExplicitArgumentBarrier,
typename Container, typename Container,
typename T = std::remove_pointer_t< typename T = std::remove_pointer_t<
decltype(base::data(std::declval<Container&>()))>, decltype(base::data(std::declval<Container&>()))>,
...@@ -521,6 +524,7 @@ constexpr span<T, N> make_span(Container& container) noexcept { ...@@ -521,6 +524,7 @@ constexpr span<T, N> make_span(Container& container) noexcept {
template < template <
size_t N, size_t N,
int&... ExplicitArgumentBarrier,
typename Container, typename Container,
typename T = std::remove_pointer_t< typename T = std::remove_pointer_t<
decltype(base::data(std::declval<const Container&>()))>, decltype(base::data(std::declval<const Container&>()))>,
...@@ -529,7 +533,7 @@ constexpr span<T, N> make_span(const Container& container) noexcept { ...@@ -529,7 +533,7 @@ constexpr span<T, N> make_span(const Container& container) noexcept {
return span<T, N>(container); return span<T, N>(container);
} }
template <typename T, size_t X> template <int&... ExplicitArgumentBarrier, typename T, size_t X>
constexpr span<T, X> make_span(const span<T, X>& span) noexcept { constexpr span<T, X> make_span(const span<T, X>& span) noexcept {
return span; return span;
} }
......
...@@ -2026,8 +2026,7 @@ void GpuImageDecodeCache::UploadImageIfNecessary(const DrawImage& draw_image, ...@@ -2026,8 +2026,7 @@ void GpuImageDecodeCache::UploadImageIfNecessary(const DrawImage& draw_image,
ClientImageTransferCacheEntry::GetNextId(); ClientImageTransferCacheEntry::GetNextId();
const gpu::SyncToken decode_sync_token = const gpu::SyncToken decode_sync_token =
context_->RasterInterface()->ScheduleImageDecode( context_->RasterInterface()->ScheduleImageDecode(
base::make_span<const uint8_t>(encoded_data->bytes(), base::make_span(encoded_data->bytes(), encoded_data->size()),
encoded_data->size()),
output_size, transfer_cache_id, output_size, transfer_cache_id,
color_space ? gfx::ColorSpace(*color_space) : gfx::ColorSpace(), color_space ? gfx::ColorSpace(*color_space) : gfx::ColorSpace(),
image_data->needs_mips); image_data->needs_mips);
......
...@@ -212,8 +212,7 @@ Response WebAuthnHandler::AddCredential( ...@@ -212,8 +212,7 @@ Response WebAuthnHandler::AddCredential(
} else { } else {
credential_created = authenticator->AddRegistration( credential_created = authenticator->AddRegistration(
CopyBinaryToVector(credential->GetCredentialId()), CopyBinaryToVector(credential->GetCredentialId()),
base::make_span<uint8_t, device::kRpIdHashLength>( device::fido_parsing_utils::CreateSHA256Hash(rp_id),
device::fido_parsing_utils::CreateSHA256Hash(rp_id)),
CopyBinaryToVector(credential->GetPrivateKey()), CopyBinaryToVector(credential->GetPrivateKey()),
credential->GetSignCount()); credential->GetSignCount());
} }
......
...@@ -91,10 +91,10 @@ TEST_P(SignedExchangeEnvelopeTest, ParseGoldenFile) { ...@@ -91,10 +91,10 @@ TEST_P(SignedExchangeEnvelopeTest, ParseGoldenFile) {
base::StringPiece signature_header_field( base::StringPiece signature_header_field(
contents.data() + signature_header_field_offset, contents.data() + signature_header_field_offset,
prologue_b.signature_header_field_length()); prologue_b.signature_header_field_length());
const auto cbor_bytes = base::make_span<const uint8_t>( const auto cbor_bytes =
contents_bytes + signature_header_field_offset + base::make_span(contents_bytes + signature_header_field_offset +
prologue_b.signature_header_field_length(), prologue_b.signature_header_field_length(),
prologue_b.cbor_header_length()); prologue_b.cbor_header_length());
const base::Optional<SignedExchangeEnvelope> envelope = const base::Optional<SignedExchangeEnvelope> envelope =
SignedExchangeEnvelope::Parse( SignedExchangeEnvelope::Parse(
SignedExchangeVersion::kB3, prologue_b.fallback_url(), SignedExchangeVersion::kB3, prologue_b.fallback_url(),
......
...@@ -13,9 +13,8 @@ MessageView::MessageView() = default; ...@@ -13,9 +13,8 @@ MessageView::MessageView() = default;
MessageView::MessageView( MessageView::MessageView(
const Message& message, const Message& message,
base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles) base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles)
: buffer_view_(base::make_span<const uint8_t>( : buffer_view_(base::make_span(static_cast<const uint8_t*>(message.data()),
static_cast<const uint8_t*>(message.data()), message.size())),
message.size())),
handles_(std::move(handles)) {} handles_(std::move(handles)) {}
MessageView::MessageView( MessageView::MessageView(
......
...@@ -73,8 +73,7 @@ void DecodeTask( ...@@ -73,8 +73,7 @@ void DecodeTask(
DVLOGF(1) << "No decoder is available for supplied image"; DVLOGF(1) << "No decoder is available for supplied image";
return; return;
} }
VaapiImageDecodeStatus status = decoder->Decode( VaapiImageDecodeStatus status = decoder->Decode(encoded_data);
base::make_span<const uint8_t>(encoded_data.data(), encoded_data.size()));
if (status != VaapiImageDecodeStatus::kSuccess) { if (status != VaapiImageDecodeStatus::kSuccess) {
DVLOGF(1) << "Failed to decode - status = " DVLOGF(1) << "Failed to decode - status = "
<< static_cast<uint32_t>(status); << static_cast<uint32_t>(status);
...@@ -177,17 +176,15 @@ VaapiImageDecodeAcceleratorWorker::GetSupportedProfiles() { ...@@ -177,17 +176,15 @@ VaapiImageDecodeAcceleratorWorker::GetSupportedProfiles() {
VaapiImageDecoder* VaapiImageDecodeAcceleratorWorker::GetDecoderForImage( VaapiImageDecoder* VaapiImageDecodeAcceleratorWorker::GetDecoderForImage(
const std::vector<uint8_t>& encoded_data) { const std::vector<uint8_t>& encoded_data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(io_sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(io_sequence_checker_);
auto encoded_data_span =
base::make_span<const uint8_t>(encoded_data.data(), encoded_data.size());
auto result = decoders_.end(); auto result = decoders_.end();
if (base::FeatureList::IsEnabled( if (base::FeatureList::IsEnabled(
features::kVaapiJpegImageDecodeAcceleration) && features::kVaapiJpegImageDecodeAcceleration) &&
IsJpegImage(encoded_data_span)) { IsJpegImage(encoded_data)) {
result = decoders_.find(gpu::ImageDecodeAcceleratorType::kJpeg); result = decoders_.find(gpu::ImageDecodeAcceleratorType::kJpeg);
} else if (base::FeatureList::IsEnabled( } else if (base::FeatureList::IsEnabled(
features::kVaapiWebPImageDecodeAcceleration) && features::kVaapiWebPImageDecodeAcceleration) &&
IsLossyWebPImage(encoded_data_span)) { IsLossyWebPImage(encoded_data)) {
result = decoders_.find(gpu::ImageDecodeAcceleratorType::kWebP); result = decoders_.find(gpu::ImageDecodeAcceleratorType::kWebP);
} }
......
...@@ -349,8 +349,7 @@ TEST_P(VaapiJpegDecoderTest, DecodeSucceeds) { ...@@ -349,8 +349,7 @@ TEST_P(VaapiJpegDecoderTest, DecodeSucceeds) {
std::string jpeg_data; std::string jpeg_data;
ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data)) ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data))
<< "failed to read input data from " << input_file.value(); << "failed to read input data from " << input_file.value();
const auto encoded_image = base::make_span<const uint8_t>( const auto encoded_image = base::as_bytes(base::make_span(jpeg_data));
reinterpret_cast<const uint8_t*>(jpeg_data.data()), jpeg_data.size());
// Skip the image if the VAAPI driver doesn't claim to support its chroma // Skip the image if the VAAPI driver doesn't claim to support its chroma
// subsampling format. However, we expect at least 4:2:0 and 4:2:2 support. // subsampling format. However, we expect at least 4:2:0 and 4:2:2 support.
...@@ -468,8 +467,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeSucceedsForSupportedSizes) { ...@@ -468,8 +467,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeSucceedsForSupportedSizes) {
{max_width, max_height}}; {max_width, max_height}};
for (const auto& test_size : test_sizes) { for (const auto& test_size : test_sizes) {
const std::vector<unsigned char> jpeg_data = GenerateJpegImage(test_size); const std::vector<unsigned char> jpeg_data = GenerateJpegImage(test_size);
auto jpeg_data_span = base::make_span<const uint8_t>( auto jpeg_data_span = base::as_bytes(base::make_span(jpeg_data));
reinterpret_cast<const uint8_t*>(jpeg_data.data()), jpeg_data.size());
ASSERT_FALSE(jpeg_data.empty()); ASSERT_FALSE(jpeg_data.empty());
std::unique_ptr<ScopedVAImage> scoped_image = Decode(jpeg_data_span); std::unique_ptr<ScopedVAImage> scoped_image = Decode(jpeg_data_span);
ASSERT_TRUE(scoped_image) ASSERT_TRUE(scoped_image)
...@@ -513,8 +511,7 @@ TEST_P(VaapiJpegDecoderWithDmaBufsTest, DecodeSucceeds) { ...@@ -513,8 +511,7 @@ TEST_P(VaapiJpegDecoderWithDmaBufsTest, DecodeSucceeds) {
std::string jpeg_data; std::string jpeg_data;
ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data)) ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data))
<< "failed to read input data from " << input_file.value(); << "failed to read input data from " << input_file.value();
const auto encoded_image = base::make_span<const uint8_t>( const auto encoded_image = base::as_bytes(base::make_span(jpeg_data));
reinterpret_cast<const uint8_t*>(jpeg_data.data()), jpeg_data.size());
// Decode into a VAAPI-allocated surface. // Decode into a VAAPI-allocated surface.
const VaapiImageDecodeStatus decode_status = decoder_.Decode(encoded_image); const VaapiImageDecodeStatus decode_status = decoder_.Decode(encoded_image);
...@@ -667,10 +664,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeFailsForBelowMinSize) { ...@@ -667,10 +664,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeFailsForBelowMinSize) {
const std::vector<unsigned char> jpeg_data = GenerateJpegImage(test_size); const std::vector<unsigned char> jpeg_data = GenerateJpegImage(test_size);
ASSERT_FALSE(jpeg_data.empty()); ASSERT_FALSE(jpeg_data.empty());
VaapiImageDecodeStatus status = VaapiImageDecodeStatus::kSuccess; VaapiImageDecodeStatus status = VaapiImageDecodeStatus::kSuccess;
ASSERT_FALSE(Decode(base::make_span<const uint8_t>( ASSERT_FALSE(Decode(base::as_bytes(base::make_span(jpeg_data)), &status))
reinterpret_cast<const uint8_t*>(jpeg_data.data()),
jpeg_data.size()),
&status))
<< "Decode unexpectedly succeeded for size = " << test_size.ToString(); << "Decode unexpectedly succeeded for size = " << test_size.ToString();
EXPECT_EQ(VaapiImageDecodeStatus::kUnsupportedImage, status); EXPECT_EQ(VaapiImageDecodeStatus::kUnsupportedImage, status);
EXPECT_FALSE(decoder_.GetScopedVASurface()); EXPECT_FALSE(decoder_.GetScopedVASurface());
...@@ -717,10 +711,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeFailsForAboveMaxSize) { ...@@ -717,10 +711,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeFailsForAboveMaxSize) {
const std::vector<unsigned char> jpeg_data = GenerateJpegImage(test_size); const std::vector<unsigned char> jpeg_data = GenerateJpegImage(test_size);
ASSERT_FALSE(jpeg_data.empty()); ASSERT_FALSE(jpeg_data.empty());
VaapiImageDecodeStatus status = VaapiImageDecodeStatus::kSuccess; VaapiImageDecodeStatus status = VaapiImageDecodeStatus::kSuccess;
ASSERT_FALSE(Decode(base::make_span<const uint8_t>( ASSERT_FALSE(Decode(base::as_bytes(base::make_span(jpeg_data)), &status))
reinterpret_cast<const uint8_t*>(jpeg_data.data()),
jpeg_data.size()),
&status))
<< "Decode unexpectedly succeeded for size = " << test_size.ToString(); << "Decode unexpectedly succeeded for size = " << test_size.ToString();
EXPECT_EQ(VaapiImageDecodeStatus::kUnsupportedImage, status); EXPECT_EQ(VaapiImageDecodeStatus::kUnsupportedImage, status);
EXPECT_FALSE(decoder_.GetScopedVASurface()); EXPECT_FALSE(decoder_.GetScopedVASurface());
...@@ -734,10 +725,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeFails) { ...@@ -734,10 +725,7 @@ TEST_F(VaapiJpegDecoderTest, DecodeFails) {
ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data)) ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data))
<< "failed to read input data from " << input_file.value(); << "failed to read input data from " << input_file.value();
VaapiImageDecodeStatus status = VaapiImageDecodeStatus::kSuccess; VaapiImageDecodeStatus status = VaapiImageDecodeStatus::kSuccess;
ASSERT_FALSE(Decode( ASSERT_FALSE(Decode(base::as_bytes(base::make_span(jpeg_data)), &status));
base::make_span<const uint8_t>(
reinterpret_cast<const uint8_t*>(jpeg_data.data()), jpeg_data.size()),
&status));
EXPECT_EQ(VaapiImageDecodeStatus::kUnsupportedSubsampling, status); EXPECT_EQ(VaapiImageDecodeStatus::kUnsupportedSubsampling, status);
EXPECT_FALSE(decoder_.GetScopedVASurface()); EXPECT_FALSE(decoder_.GetScopedVASurface());
} }
......
...@@ -138,8 +138,7 @@ TEST_P(VaapiWebPDecoderTest, DecodeAndExportAsNativePixmapDmaBuf) { ...@@ -138,8 +138,7 @@ TEST_P(VaapiWebPDecoderTest, DecodeAndExportAsNativePixmapDmaBuf) {
std::string webp_data; std::string webp_data;
ASSERT_TRUE(base::ReadFileToString(input_file, &webp_data)) ASSERT_TRUE(base::ReadFileToString(input_file, &webp_data))
<< "failed to read input data from " << input_file.value(); << "failed to read input data from " << input_file.value();
const auto encoded_image = base::make_span<const uint8_t>( const auto encoded_image = base::as_bytes(base::make_span(webp_data));
reinterpret_cast<const uint8_t*>(webp_data.data()), webp_data.size());
// Decode the image using the VA-API and wrap the decoded image in a // Decode the image using the VA-API and wrap the decoded image in a
// DecodedImage object. // DecodedImage object.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment