Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit fb6c2a6

Browse files
Addressed more of Yegor's comments.
1 parent 4a2f1e5 commit fb6c2a6

5 files changed

Lines changed: 58 additions & 30 deletions

File tree

lib/web_ui/lib/painting.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,19 @@ class Shadow {
743743
}
744744

745745
abstract class ImageShader implements Shader {
746-
factory ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4, {
746+
factory ImageShader(
747+
Image image,
748+
TileMode tmx,
749+
TileMode tmy,
750+
Float64List matrix4, {
747751
FilterQuality? filterQuality,
748-
}) => engine.renderer.createImageShader(image, tmx, tmy, matrix4, filterQuality);
752+
}) => engine.renderer.createImageShader(
753+
image,
754+
tmx,
755+
tmy,
756+
matrix4,
757+
filterQuality
758+
);
749759

750760
@override
751761
void dispose();

lib/web_ui/lib/src/engine/canvaskit/image.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ void skiaDecodeImageFromPixels(
6262
}
6363

6464
if (targetWidth != null || targetHeight != null) {
65-
if (!validUpscale(allowUpscaling, targetWidth, targetHeight, width, height)) {
66-
domWindow.console.warn('Cannot apply targetWidth/targetHeight when allowUpscaling is false.');
67-
} else {
65+
if (validUpscale(allowUpscaling, targetWidth, targetHeight, width, height)) {
6866
return callback(scaleImage(skImage, targetWidth, targetHeight));
6967
}
7068
}

lib/web_ui/lib/src/engine/skwasm/skwasm_impl/renderer.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,16 @@ class SkwasmRenderer implements Renderer {
301301
int? targetWidth,
302302
int? targetHeight,
303303
) {
304-
if (targetWidth == null && targetHeight == null) {
305-
return null;
306-
}
307304
if (targetWidth == width && targetHeight == height) {
308305
// Not scaled
309306
return null;
310307
}
311308
if (targetWidth == null) {
312-
if (targetHeight == height) {
309+
if (targetHeight == null || targetHeight == height) {
313310
// Not scaled.
314311
return null;
315312
}
316-
targetWidth = (width * targetHeight! / height).round();
313+
targetWidth = (width * targetHeight / height).round();
317314
} else if (targetHeight == null) {
318315
if (targetWidth == targetWidth) {
319316
// Not scaled.
@@ -336,16 +333,15 @@ class SkwasmRenderer implements Renderer {
336333
int? targetHeight,
337334
bool allowUpscaling = true
338335
}) {
339-
final ui.Size? scaledSize = _scaledSize(
336+
ui.Size? scaledSize = _scaledSize(
340337
width,
341338
height,
342339
targetWidth,
343340
targetHeight
344341
);
345342
if (!allowUpscaling && scaledSize != null &&
346343
(scaledSize.width > width || scaledSize.height > height)) {
347-
domWindow.console.warn('Cannot apply targetWidth/targetHeight when allowUpscaling is false.');
348-
return;
344+
scaledSize = null;
349345
}
350346
final SkwasmImage pixelImage = SkwasmImage.fromPixels(
351347
pixels,

lib/web_ui/lib/src/engine/skwasm/skwasm_impl/shaders.dart

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,30 @@ class SkwasmImageShader extends SkwasmShader implements ui.ImageShader {
162162
SkwasmImage image,
163163
ui.TileMode tmx,
164164
ui.TileMode tmy,
165-
Float64List matrix4,
165+
Float64List? matrix4,
166166
ui.FilterQuality? filterQuality,
167-
) => withStackScope((StackScope scope) {
168-
final RawMatrix33 localMatrix = scope.convertMatrix4toSkMatrix(matrix4);
169-
return SkwasmImageShader._(shaderCreateFromImage(
170-
image.handle,
171-
tmx.index,
172-
tmy.index,
173-
(filterQuality ?? ui.FilterQuality.medium).index,
174-
localMatrix,
175-
));
176-
});
167+
) {
168+
if (matrix4 != null) {
169+
return withStackScope((StackScope scope) {
170+
final RawMatrix33 localMatrix = scope.convertMatrix4toSkMatrix(matrix4);
171+
return SkwasmImageShader._(shaderCreateFromImage(
172+
image.handle,
173+
tmx.index,
174+
tmy.index,
175+
(filterQuality ?? ui.FilterQuality.medium).index,
176+
localMatrix,
177+
));
178+
});
179+
} else {
180+
return SkwasmImageShader._(shaderCreateFromImage(
181+
image.handle,
182+
tmx.index,
183+
tmy.index,
184+
(filterQuality ?? ui.FilterQuality.medium).index,
185+
nullptr,
186+
));
187+
}
188+
}
177189

178190
@override
179191
ShaderHandle handle;
@@ -214,18 +226,22 @@ class SkwasmFragmentProgram implements ui.FragmentProgram {
214226
);
215227
}
216228

217-
RuntimeEffectHandle handle;
218-
String name;
219-
int floatUniformCount;
220-
int childShaderCount;
229+
final RuntimeEffectHandle handle;
230+
final String name;
231+
final int floatUniformCount;
232+
final int childShaderCount;
233+
bool _isDisposed = false;
221234

222235
@override
223236
ui.FragmentShader fragmentShader() => SkwasmFragmentShader(this);
224237

225238
int get uniformSize => runtimeEffectGetUniformSize(handle);
226239

227240
void dispose() {
228-
runtimeEffectDispose(handle);
241+
if (!_isDisposed) {
242+
runtimeEffectDispose(handle);
243+
_isDisposed = true;
244+
}
229245
}
230246
}
231247

@@ -284,7 +300,7 @@ class SkwasmFragmentShader extends SkwasmShader implements ui.FragmentShader {
284300
image as SkwasmImage,
285301
ui.TileMode.clamp,
286302
ui.TileMode.clamp,
287-
toMatrix64(Matrix4.identity().storage),
303+
null,
288304
ui.FilterQuality.none,
289305
);
290306
final SkwasmShader? oldShader = _childShaders[index];

lib/web_ui/skwasm/surface.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <emscripten/html5_webgl.h>
99
#include <emscripten/threading.h>
1010
#include <webgl/webgl1.h>
11+
#include <cassert>
1112
#include "export.h"
1213
#include "third_party/skia/include/core/SkCanvas.h"
1314
#include "third_party/skia/include/core/SkColorSpace.h"
@@ -51,6 +52,8 @@ class Surface {
5152

5253
// Main thread only
5354
Surface(const char* canvasID) : _canvasID(canvasID) {
55+
assert(emscripten_is_main_browser_thread());
56+
5457
pthread_attr_t attr;
5558
pthread_attr_init(&attr);
5659
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@@ -67,20 +70,23 @@ class Surface {
6770

6871
// Main thread only
6972
void dispose() {
73+
assert(emscripten_is_main_browser_thread());
7074
emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VI,
7175
reinterpret_cast<void*>(fDispose), nullptr,
7276
this);
7377
}
7478

7579
// Main thread only
7680
void setCanvasSize(int width, int height) {
81+
assert(emscripten_is_main_browser_thread());
7782
emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VIII,
7883
reinterpret_cast<void*>(fSetCanvasSize),
7984
nullptr, this, width, height);
8085
}
8186

8287
// Main thread only
8388
uint32_t renderPicture(SkPicture* picture) {
89+
assert(emscripten_is_main_browser_thread());
8490
uint32_t callbackId = ++_currentCallbackId;
8591
picture->ref();
8692
emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VII,
@@ -110,6 +116,7 @@ class Surface {
110116

111117
// Main thread only
112118
void setCallbackHandler(CallbackHandler* callbackHandler) {
119+
assert(emscripten_is_main_browser_thread());
113120
_callbackHandler = callbackHandler;
114121
}
115122

@@ -244,6 +251,7 @@ class Surface {
244251

245252
// Main thread only
246253
void _onRenderComplete(uint32_t callbackId) {
254+
assert(emscripten_is_main_browser_thread());
247255
_callbackHandler(callbackId, nullptr);
248256
}
249257

0 commit comments

Comments
 (0)