Photo processing was slow on most devices because blurhash was encoded from the full-resolution image, the original file was decoded up to five times, and the Android fallback path skipped native resizing. - Encode blurhash from a <=64px canvas instead of full resolution - Process main image, thumbnail and blurhash in a single worker pass, decoding the original only once and deriving the thumbnail from the resized main image - Retry createImageBitmap via an in-memory Blob before falling back to stepped software scaling, and validate the browser honored the resize - Avoid the redundant full-resolution canvas and release intermediate canvases during stepped scaling - Swap the preview to the processed thumbnail and decode it async - Lower main image JPEG quality to 0.85 Adds unit tests for the image-processor service and documents the design in doc/dev/photo-upload-performance.md.
131 lines
3.7 KiB
JavaScript
131 lines
3.7 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'marco/tests/helpers';
|
|
import { fitWithin } from 'marco/services/image-processor';
|
|
|
|
module('Unit | Service | image-processor', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
function setupService(context, dimensions = { width: 100, height: 100 }) {
|
|
const service = context.owner.lookup('service:image-processor');
|
|
// Don't leave a real worker running during tests.
|
|
service._worker?.terminate?.();
|
|
service._getImageDimensions = async () => dimensions;
|
|
return service;
|
|
}
|
|
|
|
test('fitWithin preserves aspect ratio and never upscales', function (assert) {
|
|
assert.deepEqual(fitWithin(4000, 3000, 1920), {
|
|
width: 1920,
|
|
height: 1440,
|
|
});
|
|
assert.deepEqual(fitWithin(3000, 4000, 1920), {
|
|
width: 1440,
|
|
height: 1920,
|
|
});
|
|
assert.deepEqual(fitWithin(4000, 4000, 1920), {
|
|
width: 1920,
|
|
height: 1920,
|
|
});
|
|
assert.deepEqual(fitWithin(800, 600, 1920), { width: 800, height: 600 });
|
|
assert.deepEqual(fitWithin(1920, 1080, 1920), {
|
|
width: 1920,
|
|
height: 1080,
|
|
});
|
|
});
|
|
|
|
test('fitWithin rounds the derived dimension', function (assert) {
|
|
const { width, height } = fitWithin(4032, 3024, 1920);
|
|
assert.strictEqual(width, 1920);
|
|
assert.strictEqual(height, 1440);
|
|
});
|
|
|
|
test('processPhoto sends both main and thumbnail dimensions to the worker', async function (assert) {
|
|
const service = setupService(this, { width: 4000, height: 3000 });
|
|
|
|
let sent;
|
|
service._worker = {
|
|
postMessage(message) {
|
|
sent = message;
|
|
// Respond synchronously; the resolver is registered before postMessage.
|
|
service._handleMessage({
|
|
data: {
|
|
id: message.id,
|
|
success: true,
|
|
mainBlob: 'main',
|
|
mainDim: '1920x1440',
|
|
thumbBlob: 'thumb',
|
|
thumbDim: '350x263',
|
|
blurhash: 'LKO2',
|
|
},
|
|
});
|
|
},
|
|
terminate() {},
|
|
};
|
|
|
|
const result = await service.processPhoto(new Blob(['x']), {
|
|
maxDimension: 1920,
|
|
quality: 0.9,
|
|
thumbMaxDimension: 350,
|
|
thumbQuality: 0.9,
|
|
computeBlurhash: true,
|
|
});
|
|
|
|
assert.strictEqual(sent.type, 'PROCESS_PHOTO');
|
|
assert.strictEqual(sent.origWidth, 4000);
|
|
assert.strictEqual(sent.origHeight, 3000);
|
|
assert.strictEqual(sent.mainWidth, 1920);
|
|
assert.strictEqual(sent.mainHeight, 1440);
|
|
assert.strictEqual(sent.thumbWidth, 350);
|
|
assert.strictEqual(sent.thumbHeight, 263);
|
|
assert.strictEqual(sent.quality, 0.9);
|
|
assert.strictEqual(sent.thumbQuality, 0.9);
|
|
assert.strictEqual(sent.computeBlurhash, true);
|
|
|
|
assert.deepEqual(result, {
|
|
mainBlob: 'main',
|
|
mainDim: '1920x1440',
|
|
thumbBlob: 'thumb',
|
|
thumbDim: '350x263',
|
|
blurhash: 'LKO2',
|
|
});
|
|
});
|
|
|
|
test('processPhoto rejects when the worker is unavailable', async function (assert) {
|
|
const service = setupService(this);
|
|
service._worker = null;
|
|
|
|
await assert.rejects(
|
|
service.processPhoto(new Blob(['x']), {
|
|
maxDimension: 1920,
|
|
quality: 0.9,
|
|
thumbMaxDimension: 350,
|
|
thumbQuality: 0.9,
|
|
}),
|
|
/not available/
|
|
);
|
|
});
|
|
|
|
test('processPhoto rejects when the worker reports an error', async function (assert) {
|
|
const service = setupService(this);
|
|
|
|
service._worker = {
|
|
postMessage(message) {
|
|
service._handleMessage({
|
|
data: { id: message.id, success: false, error: 'boom' },
|
|
});
|
|
},
|
|
terminate() {},
|
|
};
|
|
|
|
await assert.rejects(
|
|
service.processPhoto(new Blob(['x']), {
|
|
maxDimension: 1920,
|
|
quality: 0.9,
|
|
thumbMaxDimension: 350,
|
|
thumbQuality: 0.9,
|
|
}),
|
|
/boom/
|
|
);
|
|
});
|
|
});
|