-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v8binding: Support TypedArrays in [NoAllocDirectCall] methods
Data is passed as NADCTypedArray<T> which should be used for both the NoAllocDirectCall and the regular callbacks to avoid divergent implementation between NADC and non-NADC code. NADCTypedArray<T> can be implicitly constructed from the other typed array view types. Bug: 1052746 Change-Id: I506d9a164cf339f6b2b99eba5b0ddf9220f567c4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3111816 Commit-Queue: Austin Eng <enga@chromium.org> Reviewed-by: Yuki Shiino <yukishiino@chromium.org> Reviewed-by: Kentaro Hara <haraken@chromium.org> Cr-Commit-Position: refs/heads/main@{#940861}
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
third_party/blink/renderer/core/typed_arrays/nadc_typed_array_view.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_NADC_TYPED_ARRAY_VIEW_H_ | ||
#define THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_NADC_TYPED_ARRAY_VIEW_H_ | ||
|
||
#include "third_party/blink/renderer/core/core_export.h" | ||
#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h" | ||
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h" | ||
#include "third_party/blink/renderer/core/typed_arrays/typed_flexible_array_buffer_view.h" | ||
#include "v8/include/v8-fast-api-calls.h" | ||
|
||
namespace blink { | ||
|
||
// This class is for passing around un-owned bytes as a typed pointer + length | ||
// into functions tagged with extended attribute [NoAllocDirectCall]. | ||
// It supports implicit construction from several other MaybeShared typed array | ||
// data types. | ||
// | ||
// IMPORTANT: The data contained by NADCTypedArrayView is NOT OWNED, so caution | ||
// must be taken to ensure it is kept alive. | ||
template <typename T> | ||
class CORE_EXPORT NADCTypedArrayView { | ||
STACK_ALLOCATED(); | ||
|
||
public: | ||
NADCTypedArrayView(T* data, size_t size) : data_(data), size_(size) {} | ||
|
||
NADCTypedArrayView(const NADCTypedArrayView<T>& rhs) | ||
: data_(rhs.Data()), size_(rhs.Size()) {} | ||
|
||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
template <typename U> | ||
NADCTypedArrayView(const v8::FastApiTypedArray<U>& rhs) | ||
: size_(rhs.length()) { | ||
U* data = nullptr; | ||
bool is_aligned = rhs.getStorageIfAligned(&data); | ||
DCHECK(is_aligned); | ||
data_ = data; | ||
} | ||
|
||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
template <typename U, bool clamped> | ||
NADCTypedArrayView(const TypedFlexibleArrayBufferView<U, clamped>& rhs) | ||
: data_(rhs.DataMaybeOnStack()), size_(rhs.length()) {} | ||
|
||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
template <typename U, typename V8TypedArray, bool clamped> | ||
NADCTypedArrayView( | ||
const MaybeShared<DOMTypedArray<U, V8TypedArray, clamped>>& rhs) | ||
: data_(rhs.Get() ? rhs->DataMaybeShared() : nullptr), | ||
size_(rhs.Get() ? rhs->length() : 0) {} | ||
|
||
T* Data() const { return data_; } | ||
|
||
size_t Size() const { return size_; } | ||
|
||
bool IsEmpty() const { return size_ == 0; } | ||
|
||
bool IsNull() const { return data_ == nullptr; } | ||
|
||
private: | ||
T* data_; | ||
size_t size_; | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_NADC_TYPED_ARRAY_VIEW_H_ |