refactor: make node Buffers more friendly to base::span
/ std::span
(#46724)
* refactor: add electron::Buffer namespace; move the Buffer as_byte_span() into it * feat: add electron::Buffer::Copy() a span-friendly version of node::Buffer::Copy() * refactor: use electron::Buffer::Copy() in electron_api_base_window.cc * refactor: use electron::Buffer::Copy() in electron_api_data_pipe_holder.cc * refactor: use electron::Buffer::Copy() in electron_api_safe_storage.cc * refactor: use electron::Buffer::Copy() in electron_api_clipboard.cc * refactor: use electron::Buffer::Copy() in osr_converter.cc * refactor: use electron::Buffer::Copy() in electron_api_native_image.cc * refactor: use electron::Buffer::Copy() in net_converter.cc * refactor: use electron::Buffer::Copy() in electron_api_web_contents.cc * refactor: make NewEmptyBuffer() return a Local<Value>
This commit is contained in:
parent
1976e935e7
commit
06a99d6770
10 changed files with 93 additions and 72 deletions
|
@ -13,6 +13,7 @@
|
|||
#include "shell/common/gin_converters/image_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "shell/common/node_util.h"
|
||||
#include "shell/common/process_util.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "ui/base/clipboard/clipboard_format_type.h"
|
||||
|
@ -99,8 +100,7 @@ std::string Clipboard::Read(const std::string& format_string) {
|
|||
v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
|
||||
gin_helper::Arguments* args) {
|
||||
std::string data = Read(format_string);
|
||||
return node::Buffer::Copy(args->isolate(), data.data(), data.length())
|
||||
.ToLocalChecked();
|
||||
return electron::Buffer::Copy(args->isolate(), data).ToLocalChecked();
|
||||
}
|
||||
|
||||
void Clipboard::WriteBuffer(const std::string& format,
|
||||
|
|
|
@ -122,6 +122,10 @@ base::win::ScopedGDIObject<HICON> ReadICOFromPath(int size,
|
|||
}
|
||||
#endif
|
||||
|
||||
[[nodiscard]] v8::Local<v8::Value> NewEmptyBuffer(v8::Isolate* isolate) {
|
||||
return node::Buffer::New(isolate, 0).ToLocalChecked();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
|
||||
|
@ -226,57 +230,48 @@ HICON NativeImage::GetHICON(int size) {
|
|||
#endif
|
||||
|
||||
v8::Local<v8::Value> NativeImage::ToPNG(gin::Arguments* args) {
|
||||
v8::Isolate* const isolate = args->isolate();
|
||||
float scale_factor = GetScaleFactorFromOptions(args);
|
||||
|
||||
if (scale_factor == 1.0f) {
|
||||
// Use raw 1x PNG bytes when available
|
||||
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
||||
if (png->size() > 0) {
|
||||
const char* data = reinterpret_cast<const char*>(png->front());
|
||||
size_t size = png->size();
|
||||
return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked();
|
||||
}
|
||||
const scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
|
||||
const base::span<const uint8_t> png_span = *png;
|
||||
if (!png_span.empty())
|
||||
return electron::Buffer::Copy(isolate, png_span).ToLocalChecked();
|
||||
}
|
||||
|
||||
const SkBitmap bitmap =
|
||||
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
|
||||
std::optional<std::vector<uint8_t>> encoded =
|
||||
const std::optional<std::vector<uint8_t>> encoded =
|
||||
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false);
|
||||
if (!encoded.has_value())
|
||||
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
|
||||
const char* data = reinterpret_cast<char*>(encoded->data());
|
||||
size_t size = encoded->size();
|
||||
return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked();
|
||||
return NewEmptyBuffer(isolate);
|
||||
|
||||
return electron::Buffer::Copy(isolate, *encoded).ToLocalChecked();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> NativeImage::ToBitmap(gin::Arguments* args) {
|
||||
float scale_factor = GetScaleFactorFromOptions(args);
|
||||
v8::Isolate* const isolate = args->isolate();
|
||||
|
||||
const SkBitmap bitmap =
|
||||
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
|
||||
const float scale = GetScaleFactorFromOptions(args);
|
||||
const auto src = image_.AsImageSkia().GetRepresentation(scale).GetBitmap();
|
||||
|
||||
SkImageInfo info =
|
||||
SkImageInfo::MakeN32Premul(bitmap.width(), bitmap.height());
|
||||
const auto dst_info = SkImageInfo::MakeN32Premul(src.dimensions());
|
||||
const size_t dst_n_bytes = dst_info.computeMinByteSize();
|
||||
auto dst_buf = v8::ArrayBuffer::New(isolate, dst_n_bytes);
|
||||
|
||||
auto array_buffer =
|
||||
v8::ArrayBuffer::New(args->isolate(), info.computeMinByteSize());
|
||||
if (bitmap.readPixels(info, array_buffer->Data(), info.minRowBytes(), 0, 0)) {
|
||||
return node::Buffer::New(args->isolate(), array_buffer, 0,
|
||||
info.computeMinByteSize())
|
||||
.ToLocalChecked();
|
||||
}
|
||||
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
|
||||
if (!src.readPixels(dst_info, dst_buf->Data(), dst_info.minRowBytes(), 0, 0))
|
||||
return NewEmptyBuffer(isolate);
|
||||
return node::Buffer::New(isolate, dst_buf, 0, dst_n_bytes).ToLocalChecked();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
|
||||
std::optional<std::vector<uint8_t>> encoded_image =
|
||||
const std::optional<std::vector<uint8_t>> encoded_image =
|
||||
gfx::JPEG1xEncodedDataFromImage(image_, quality);
|
||||
if (!encoded_image.has_value())
|
||||
return node::Buffer::New(isolate, 0).ToLocalChecked();
|
||||
return node::Buffer::Copy(
|
||||
isolate, reinterpret_cast<const char*>(&encoded_image->front()),
|
||||
encoded_image->size())
|
||||
.ToLocalChecked();
|
||||
if (!encoded_image)
|
||||
return NewEmptyBuffer(isolate);
|
||||
return electron::Buffer::Copy(isolate, *encoded_image).ToLocalChecked();
|
||||
}
|
||||
|
||||
std::string NativeImage::ToDataURL(gin::Arguments* args) {
|
||||
|
@ -301,17 +296,17 @@ v8::Local<v8::Value> NativeImage::GetBitmap(gin::Arguments* args) {
|
|||
|
||||
v8::Local<v8::Value> NativeImage::GetNativeHandle(
|
||||
gin_helper::ErrorThrower thrower) {
|
||||
v8::Isolate* const isolate = thrower.isolate();
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
if (IsEmpty())
|
||||
return node::Buffer::New(thrower.isolate(), 0).ToLocalChecked();
|
||||
return NewEmptyBuffer(isolate);
|
||||
|
||||
NSImage* ptr = image_.AsNSImage();
|
||||
return node::Buffer::Copy(thrower.isolate(), reinterpret_cast<char*>(ptr),
|
||||
sizeof(void*))
|
||||
return electron::Buffer::Copy(isolate, base::byte_span_from_ref(ptr))
|
||||
.ToLocalChecked();
|
||||
#else
|
||||
thrower.ThrowError("Not implemented");
|
||||
return v8::Undefined(thrower.isolate());
|
||||
return v8::Undefined(isolate);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -402,7 +397,7 @@ void NativeImage::AddRepresentation(const gin_helper::Dictionary& options) {
|
|||
GURL url;
|
||||
if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) {
|
||||
skia_rep_added = electron::util::AddImageSkiaRepFromBuffer(
|
||||
&image_skia, electron::util::as_byte_span(buffer), width, height,
|
||||
&image_skia, electron::Buffer::as_byte_span(buffer), width, height,
|
||||
scale_factor);
|
||||
} else if (options.Get("dataURL", &url)) {
|
||||
std::string mime_type, charset, data;
|
||||
|
@ -511,7 +506,7 @@ gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
|
|||
auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
|
||||
auto size_bytes = info.computeMinByteSize();
|
||||
|
||||
const auto buffer_data = electron::util::as_byte_span(buffer);
|
||||
const auto buffer_data = electron::Buffer::as_byte_span(buffer);
|
||||
if (size_bytes != buffer_data.size()) {
|
||||
thrower.ThrowError("invalid buffer size");
|
||||
return {};
|
||||
|
@ -552,7 +547,7 @@ gin::Handle<NativeImage> NativeImage::CreateFromBuffer(
|
|||
|
||||
gfx::ImageSkia image_skia;
|
||||
electron::util::AddImageSkiaRepFromBuffer(
|
||||
&image_skia, electron::util::as_byte_span(buffer), width, height,
|
||||
&image_skia, electron::Buffer::as_byte_span(buffer), width, height,
|
||||
scale_factor);
|
||||
return Create(args->isolate(), gfx::Image(image_skia));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue