Skip to content

Commit

Permalink
Move Rust bridge to use parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Apr 14, 2024
1 parent 1a88c71 commit 758ac09
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 126 deletions.
10 changes: 7 additions & 3 deletions lib/compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) {
callback(imageFile.copyWith(status: ImageFileStatus.compressing));
final timer = Stopwatch()..start();

var result = await processImg(
path: imageFile.path,
outPath: outPath,
var params = Parameters(
jpegQuality: config.qualityJPEG,
pngQuality: config.qualityPNG,
gifQuality: config.qualityGIF,
Expand All @@ -30,6 +28,12 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) {
resizeWidth: config.maxWidth,
resizeHeight: config.maxHeight,
);

var result = await processImg(
path: imageFile.path,
outPath: outPath,
parameters: params,
);
timer.stop();
debugPrint(
'Compressed ${imageFile.file} in ${timer.elapsedMilliseconds}ms');
Expand Down
62 changes: 45 additions & 17 deletions lib/src/rust/api/compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,50 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
Future<String> processImg(
{required String path,
required String outPath,
required int jpegQuality,
required int pngQuality,
required int webpQuality,
required int gifQuality,
required bool resize,
required int resizeWidth,
required int resizeHeight,
required Parameters parameters,
dynamic hint}) =>
RustLib.instance.api.processImg(
path: path,
outPath: outPath,
jpegQuality: jpegQuality,
pngQuality: pngQuality,
webpQuality: webpQuality,
gifQuality: gifQuality,
resize: resize,
resizeWidth: resizeWidth,
resizeHeight: resizeHeight,
hint: hint);
path: path, outPath: outPath, parameters: parameters, hint: hint);

class Parameters {
final int jpegQuality;
final int pngQuality;
final int webpQuality;
final int gifQuality;
final bool resize;
final int resizeWidth;
final int resizeHeight;

const Parameters({
required this.jpegQuality,
required this.pngQuality,
required this.webpQuality,
required this.gifQuality,
required this.resize,
required this.resizeWidth,
required this.resizeHeight,
});

@override
int get hashCode =>
jpegQuality.hashCode ^
pngQuality.hashCode ^
webpQuality.hashCode ^
gifQuality.hashCode ^
resize.hashCode ^
resizeWidth.hashCode ^
resizeHeight.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Parameters &&
runtimeType == other.runtimeType &&
jpegQuality == other.jpegQuality &&
pngQuality == other.pngQuality &&
webpQuality == other.webpQuality &&
gifQuality == other.gifQuality &&
resize == other.resize &&
resizeWidth == other.resizeWidth &&
resizeHeight == other.resizeHeight;
}
30 changes: 0 additions & 30 deletions lib/src/rust/api/simple.dart

This file was deleted.

116 changes: 73 additions & 43 deletions lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ abstract class RustLibApi extends BaseApi {
Future<String> processImg(
{required String path,
required String outPath,
required int jpegQuality,
required int pngQuality,
required int webpQuality,
required int gifQuality,
required bool resize,
required int resizeWidth,
required int resizeHeight,
required Parameters parameters,
dynamic hint});
}

Expand Down Expand Up @@ -115,26 +109,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
Future<String> processImg(
{required String path,
required String outPath,
required int jpegQuality,
required int pngQuality,
required int webpQuality,
required int gifQuality,
required bool resize,
required int resizeWidth,
required int resizeHeight,
required Parameters parameters,
dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(path, serializer);
sse_encode_String(outPath, serializer);
sse_encode_u_32(jpegQuality, serializer);
sse_encode_u_32(pngQuality, serializer);
sse_encode_u_32(webpQuality, serializer);
sse_encode_u_32(gifQuality, serializer);
sse_encode_bool(resize, serializer);
sse_encode_u_32(resizeWidth, serializer);
sse_encode_u_32(resizeHeight, serializer);
sse_encode_box_autoadd_parameters(parameters, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 1, port: port_);
},
Expand All @@ -143,35 +125,15 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
decodeErrorData: null,
),
constMeta: kProcessImgConstMeta,
argValues: [
path,
outPath,
jpegQuality,
pngQuality,
webpQuality,
gifQuality,
resize,
resizeWidth,
resizeHeight
],
argValues: [path, outPath, parameters],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kProcessImgConstMeta => const TaskConstMeta(
debugName: "process_img",
argNames: [
"path",
"outPath",
"jpegQuality",
"pngQuality",
"webpQuality",
"gifQuality",
"resize",
"resizeWidth",
"resizeHeight"
],
argNames: ["path", "outPath", "parameters"],
);

@protected
Expand All @@ -186,12 +148,35 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return raw as bool;
}

@protected
Parameters dco_decode_box_autoadd_parameters(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return dco_decode_parameters(raw);
}

@protected
Uint8List dco_decode_list_prim_u_8_strict(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return raw as Uint8List;
}

@protected
Parameters dco_decode_parameters(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
final arr = raw as List<dynamic>;
if (arr.length != 7)
throw Exception('unexpected arr length: expect 7 but see ${arr.length}');
return Parameters(
jpegQuality: dco_decode_u_32(arr[0]),
pngQuality: dco_decode_u_32(arr[1]),
webpQuality: dco_decode_u_32(arr[2]),
gifQuality: dco_decode_u_32(arr[3]),
resize: dco_decode_bool(arr[4]),
resizeWidth: dco_decode_u_32(arr[5]),
resizeHeight: dco_decode_u_32(arr[6]),
);
}

@protected
int dco_decode_u_32(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
Expand Down Expand Up @@ -223,13 +208,39 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return deserializer.buffer.getUint8() != 0;
}

@protected
Parameters sse_decode_box_autoadd_parameters(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
return (sse_decode_parameters(deserializer));
}

@protected
Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var len_ = sse_decode_i_32(deserializer);
return deserializer.buffer.getUint8List(len_);
}

@protected
Parameters sse_decode_parameters(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var var_jpegQuality = sse_decode_u_32(deserializer);
var var_pngQuality = sse_decode_u_32(deserializer);
var var_webpQuality = sse_decode_u_32(deserializer);
var var_gifQuality = sse_decode_u_32(deserializer);
var var_resize = sse_decode_bool(deserializer);
var var_resizeWidth = sse_decode_u_32(deserializer);
var var_resizeHeight = sse_decode_u_32(deserializer);
return Parameters(
jpegQuality: var_jpegQuality,
pngQuality: var_pngQuality,
webpQuality: var_webpQuality,
gifQuality: var_gifQuality,
resize: var_resize,
resizeWidth: var_resizeWidth,
resizeHeight: var_resizeHeight);
}

@protected
int sse_decode_u_32(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -265,6 +276,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer.buffer.putUint8(self ? 1 : 0);
}

@protected
void sse_encode_box_autoadd_parameters(
Parameters self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_parameters(self, serializer);
}

@protected
void sse_encode_list_prim_u_8_strict(
Uint8List self, SseSerializer serializer) {
Expand All @@ -273,6 +291,18 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer.buffer.putUint8List(self);
}

@protected
void sse_encode_parameters(Parameters self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_u_32(self.jpegQuality, serializer);
sse_encode_u_32(self.pngQuality, serializer);
sse_encode_u_32(self.webpQuality, serializer);
sse_encode_u_32(self.gifQuality, serializer);
sse_encode_bool(self.resize, serializer);
sse_encode_u_32(self.resizeWidth, serializer);
sse_encode_u_32(self.resizeHeight, serializer);
}

@protected
void sse_encode_u_32(int self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down
19 changes: 19 additions & 0 deletions lib/src/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
bool dco_decode_bool(dynamic raw);

@protected
Parameters dco_decode_box_autoadd_parameters(dynamic raw);

@protected
Uint8List dco_decode_list_prim_u_8_strict(dynamic raw);

@protected
Parameters dco_decode_parameters(dynamic raw);

@protected
int dco_decode_u_32(dynamic raw);

Expand All @@ -42,9 +48,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
bool sse_decode_bool(SseDeserializer deserializer);

@protected
Parameters sse_decode_box_autoadd_parameters(SseDeserializer deserializer);

@protected
Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer);

@protected
Parameters sse_decode_parameters(SseDeserializer deserializer);

@protected
int sse_decode_u_32(SseDeserializer deserializer);

Expand All @@ -63,10 +75,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_bool(bool self, SseSerializer serializer);

@protected
void sse_encode_box_autoadd_parameters(
Parameters self, SseSerializer serializer);

@protected
void sse_encode_list_prim_u_8_strict(
Uint8List self, SseSerializer serializer);

@protected
void sse_encode_parameters(Parameters self, SseSerializer serializer);

@protected
void sse_encode_u_32(int self, SseSerializer serializer);

Expand Down
Loading

0 comments on commit 758ac09

Please sign in to comment.