From fb6b87d656a1911787f7daca55392ab2b09981b3 Mon Sep 17 00:00:00 2001 From: Hajime-san <41257923+Hajime-san@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:09:05 +0900 Subject: [PATCH] avoid copying string --- ext/canvas/01_image.js | 30 +++++------ ext/canvas/op_create_image_bitmap.rs | 76 +++++++--------------------- 2 files changed, 33 insertions(+), 73 deletions(-) diff --git a/ext/canvas/01_image.js b/ext/canvas/01_image.js index 4f54a2aadfbc75..0e0fa62ba4fdbe 100644 --- a/ext/canvas/01_image.js +++ b/ext/canvas/01_image.js @@ -256,22 +256,20 @@ function createImageBitmap( // TODO(Hajime-san): this should be real async const processedImage = op_create_image_bitmap( buf, - { - width, - height, - sx, - sy, - sw, - sh, - imageOrientation: options.imageOrientation ?? "from-image", - premultiplyAlpha: options.premultiplyAlpha ?? "default", - colorSpaceConversion: options.colorSpaceConversion ?? "default", - resizeWidth: options.resizeWidth, - resizeHeight: options.resizeHeight, - resizeQuality: options.resizeQuality ?? "low", - imageBitmapSource, - mimeType, - }, + width, + height, + sx, + sy, + sw, + sh, + options.imageOrientation ?? "from-image", + options.premultiplyAlpha ?? "default", + options.colorSpaceConversion ?? "default", + options.resizeWidth, + options.resizeHeight, + options.resizeQuality ?? "low", + imageBitmapSource, + mimeType, ); imageBitmap[_bitmapData] = processedImage.data; imageBitmap[_width] = processedImage.width; diff --git a/ext/canvas/op_create_image_bitmap.rs b/ext/canvas/op_create_image_bitmap.rs index c11fb518d82d2e..71bdf8203e47aa 100644 --- a/ext/canvas/op_create_image_bitmap.rs +++ b/ext/canvas/op_create_image_bitmap.rs @@ -71,25 +71,6 @@ enum ResizeQuality { High, } -#[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -struct OpCreateImageBitmapArgs { - width: u32, - height: u32, - sx: Option, - sy: Option, - sw: Option, - sh: Option, - image_orientation: ImageOrientation, - premultiply_alpha: PremultiplyAlpha, - color_space_conversion: ColorSpaceConversion, - resize_width: Option, - resize_height: Option, - resize_quality: ResizeQuality, - image_bitmap_source: ImageBitmapSource, - mime_type: String, -} - #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct OpCreateImageBitmapReturn { @@ -105,7 +86,7 @@ fn decode_bitmap_data( width: u32, height: u32, image_bitmap_source: &ImageBitmapSource, - mime_type: String, + mime_type: &str, ) -> Result { let (image, width, height, icc_profile) = match image_bitmap_source { ImageBitmapSource::Blob => { @@ -116,7 +97,7 @@ fn decode_bitmap_data( )) .into() } - let (image, icc_profile) = match &*mime_type { + let (image, icc_profile) = match mime_type { // Should we support the "image/apng" MIME type here? "image/png" => { let mut decoder: PngDecoder = @@ -284,46 +265,27 @@ fn apply_premultiply_alpha( #[op2] #[serde] +#[allow(clippy::too_many_arguments)] pub(super) fn op_create_image_bitmap( - #[buffer] zero_copy: JsBuffer, - #[serde] args: OpCreateImageBitmapArgs, + #[buffer] buf: JsBuffer, + width: u32, + height: u32, + sx: Option, + sy: Option, + sw: Option, + sh: Option, + #[serde] image_orientation: ImageOrientation, + #[serde] premultiply_alpha: PremultiplyAlpha, + #[serde] color_space_conversion: ColorSpaceConversion, + resize_width: Option, + resize_height: Option, + #[serde] resize_quality: ResizeQuality, + #[serde] image_bitmap_source: ImageBitmapSource, + #[string] mime_type: &str, ) -> Result { - let buf = &*zero_copy; - let OpCreateImageBitmapArgs { - width, - height, - sh, - sw, - sx, - sy, - image_orientation, - premultiply_alpha, - color_space_conversion, - resize_width, - resize_height, - resize_quality, - image_bitmap_source, - mime_type, - } = OpCreateImageBitmapArgs { - width: args.width, - height: args.height, - sx: args.sx, - sy: args.sy, - sw: args.sw, - sh: args.sh, - image_orientation: args.image_orientation, - premultiply_alpha: args.premultiply_alpha, - color_space_conversion: args.color_space_conversion, - resize_width: args.resize_width, - resize_height: args.resize_height, - resize_quality: args.resize_quality, - image_bitmap_source: args.image_bitmap_source, - mime_type: args.mime_type, - }; - // 6. Switch on image: let (image, width, height, icc_profile) = - decode_bitmap_data(buf, width, height, &image_bitmap_source, mime_type)?; + decode_bitmap_data(&buf, width, height, &image_bitmap_source, mime_type)?; // crop bitmap data // 2.