Skip to content

Commit

Permalink
Merge pull request #759 from openkraken/fix/image-stream-compeleter
Browse files Browse the repository at this point in the history
Fix/image stream compeleter
  • Loading branch information
andycall committed Nov 1, 2021
2 parents 25edf15 + 6484274 commit e2bc0d6
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
3 changes: 1 addition & 2 deletions bridge/bindings/qjs/dom/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ JSValue Element::toBlob(QjsContext *ctx, JSValue this_val, int argc, JSValue *ar
} else {
JSValue errorObject = JS_NewError(ctx);
JSValue errorMessage = JS_NewString(ctx, error);
JS_DefinePropertyValueStr(ctx, errorObject, "message", errorMessage,
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
JS_SetPropertyStr(ctx, errorObject, "message", errorMessage);
JSValue ret = JS_Call(ctx, promiseContext->rejectFunc, promiseContext->promise, 1, &errorObject);
JS_FreeValue(ctx, errorObject);
JS_FreeValue(ctx, errorMessage);
Expand Down
17 changes: 12 additions & 5 deletions bridge/bindings/qjs/js_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,28 +247,35 @@ JSRuntime *JSContext::runtime() {
void JSContext::reportError(JSValueConst error) {
if (!JS_IsError(m_ctx, error)) return;

const char *title = JS_ToCString(m_ctx, error);
JSValue messageValue = JS_GetPropertyStr(m_ctx, error, "message");
JSValue errorTypeValue = JS_GetPropertyStr(m_ctx, error, "name");
const char *title = JS_ToCString(m_ctx, messageValue);
const char *type = JS_ToCString(m_ctx, errorTypeValue);
const char *stack = nullptr;
JSValue stackValue = JS_GetPropertyStr(m_ctx, error, "stack");
if (!JS_IsUndefined(stackValue)) {
stack = JS_ToCString(m_ctx, stackValue);
}

uint32_t messageLength = strlen(title) + 2;
uint32_t messageLength = strlen(type) + strlen(title);
if (stack != nullptr) {
messageLength += strlen(stack);
messageLength += 4 + strlen(stack);
char message[messageLength];
sprintf(message, "%s\n%s", title, stack);
sprintf(message, "%s: %s\n%s", type, title, stack);
_handler(contextId, message);
} else {
messageLength += 3;
char message[messageLength];
sprintf(message, "%s", title);
sprintf(message, "%s: %s", type, title);
_handler(contextId, message);
}

JS_FreeValue(m_ctx, errorTypeValue);
JS_FreeValue(m_ctx, messageValue);
JS_FreeValue(m_ctx, stackValue);
JS_FreeCString(m_ctx, title);
JS_FreeCString(m_ctx, stack);
JS_FreeCString(m_ctx, type);
}

void JSContext::drainPendingPromiseJobs() {
Expand Down
4 changes: 4 additions & 0 deletions integration_tests/lib/bridge/match_snapshots.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ num diffBetweenPixels(firstPixel, secondPixel, ignoreAlpha) {
}

bool matchImage(Uint8List imageA, List<int> imageB, String filename) {
if (imageA.length == 0 || imageB.length == 0) {
return false;
}

Image a = decodeImage(imageA.toList())!;
Image b = decodeImage(imageB.toList())!;
if (!haveSameSize(a, b)) {
Expand Down
Binary file modified integration_tests/snapshots/dom/elements/img.ts.ff77830b2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion integration_tests/specs/css/css-flexbox/css-flexbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('css-flexbox', () => {
BODY.appendChild(p);
BODY.appendChild(flexbox);

await snapshot(0.5);
await snapshot(1);
});
it('row-ref', async () => {
let p;
Expand Down
10 changes: 6 additions & 4 deletions kraken/lib/src/dom/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1441,19 +1441,21 @@ class Element extends Node
dispatchEvent(clickEvent);
}

Future<Uint8List> toBlob({double? devicePixelRatio}) {
Future<Uint8List> toBlob({ double? devicePixelRatio }) {
devicePixelRatio ??= window.devicePixelRatio;

Completer<Uint8List> completer = Completer();
if (nodeName != 'HTML') {
if (targetId != HTML_ID) {
convertToRepaintBoundary();
}
renderBoxModel!.owner!.flushLayout();

SchedulerBinding.instance!.addPostFrameCallback((_) async {
Uint8List captured;
RenderBoxModel? renderObject = nodeName == 'HTML' ? elementManager.viewportElement.renderBoxModel : renderBoxModel;
if (renderObject!.hasSize && renderObject.size == Size.zero) {
RenderBoxModel? renderObject = targetId == HTML_ID
? elementManager.viewportElement.renderBoxModel
: renderBoxModel;
if (renderObject!.hasSize && renderObject.size.isEmpty) {
// Return a blob with zero length.
captured = Uint8List(0);
} else {
Expand Down
35 changes: 16 additions & 19 deletions kraken/lib/src/dom/elements/img.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class ImageElement extends Element {
_imageProvider?.evict();
_imageProvider = null;

_removeStreamListener();
_renderImage?.image = null;
}

Expand All @@ -118,7 +117,8 @@ class ImageElement extends Element {
_imageProvider?.evict();
_imageProvider = null;

_removeStreamListener();
_imageStream?.removeListener(_renderStreamListener);
_imageStream = null;

_renderImage = null;
}
Expand Down Expand Up @@ -197,6 +197,8 @@ class ImageElement extends Element {
@override
bool get shouldConvertToRepaintBoundary => _frameCount > 2 || super.shouldConvertToRepaintBoundary;

bool get shouldConvertToNonRepaintBoundary => _frameCount == 1;

void _renderImageStream(ImageInfo imageInfo, bool synchronousCall) {
_frameCount++;
_imageInfo = imageInfo;
Expand All @@ -213,17 +215,19 @@ class ImageElement extends Element {
}
}

// @HACK Flutter image cache will cause image steam listener to trigger twice when page reload
// so use two frames to tell multi frame image from static image, note this optimization will fail
// at multi frame image with only two frames which is not common.
if (shouldConvertToRepaintBoundary) {
convertToRepaintBoundary();
} else {
convertToNonRepaintBoundary();
}
if (isRendererAttached) {
// @HACK Flutter image cache will cause image steam listener to trigger twice when page reload
// so use two frames to tell multi frame image from static image, note this optimization will fail
// at multi frame image with only two frames which is not common.
if (shouldConvertToRepaintBoundary) {
convertToRepaintBoundary();
} else if (shouldConvertToNonRepaintBoundary) {
convertToNonRepaintBoundary();
}

_resize();
_renderImage?.image = image;
_resize();
_renderImage?.image = image;
}
}

// Mark if the same src loaded.
Expand Down Expand Up @@ -298,11 +302,6 @@ class ImageElement extends Element {
}
}

void _removeStreamListener() {
_imageStream?.removeListener(_renderStreamListener);
_imageStream = null;
}

RenderImage createRenderImageBox() {
RenderStyle renderStyle = renderBoxModel!.renderStyle;
BoxFit objectFit = renderStyle.objectFit;
Expand Down Expand Up @@ -377,8 +376,6 @@ class ImageElement extends Element {
_resetImage();

if (_source != null) {
_removeStreamListener();

Uri base = Uri.parse(elementManager.controller.href);
Uri resolvedUri = elementManager.controller.uriParser!.resolve(base, Uri.parse(_source!));

Expand Down

0 comments on commit e2bc0d6

Please sign in to comment.