Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(): rm fabric.filterBackend => getFilterBackend #8487

Merged
merged 9 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- chore(): rm `fabric.filterBackend` => `getFilterBackend` [#8487](https://github.com/fabricjs/fabric.js/pull/8487)
- chore(): refactor `Object.__uid++` => `uid()` [#8482](https://github.com/fabricjs/fabric.js/pull/8482)
- chore(TS): migrate object mixins to TS [#8414](https://github.com/fabricjs/fabric.js/pull/8414)
- chore(TS): migrate filters [#8474](https://github.com/fabricjs/fabric.js/pull/8474)
Expand Down
15 changes: 10 additions & 5 deletions src/filters/FilterBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { webGLProbe } from './WebGLProbe';

export type FilterBackend = WebGLFilterBackend | Canvas2dFilterBackend;

let filterBackend: FilterBackend;

export function initFilterBackend(): FilterBackend {
webGLProbe.queryWebGL();
if (config.enableGLFiltering && webGLProbe.isSupported(config.textureSize)) {
Expand All @@ -16,15 +18,18 @@ export function initFilterBackend(): FilterBackend {
}

/**
* @todo refactor to a module w/o assigning to fabric
*
* @param [strict] pass `true` to create the backend if it wasn't created yet (default behavior),
* pass `false` to get the backend ref without mutating it
*/
export function getFilterBackend(): FilterBackend {
if (!fabric.filterBackend) {
fabric.filterBackend = initFilterBackend();
export function getFilterBackend(strict = true): FilterBackend {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want strict option here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image#removeTexture: we don't want it to create the backend, only to probe it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so is the issue that initElement calls removeTexture? and that we don't want to strictly couple Image with having a filter Backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems correct

if (!filterBackend && strict) {
filterBackend = initFilterBackend();
}
return fabric.filterBackend;
return filterBackend;
}

fabric.getFilterBackend = getFilterBackend;
fabric.Canvas2dFilterBackend = Canvas2dFilterBackend;
fabric.WebglFilterBackend = WebGLFilterBackend;
fabric.initFilterBackend = initFilterBackend;
2 changes: 1 addition & 1 deletion src/shapes/image.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class Image extends FabricObject {
* Delete a single texture if in webgl mode
*/
removeTexture(key: string) {
const backend = fabric.filterBackend;
const backend = getFilterBackend(false);
if (backend && backend.evictCachesForKey) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use instanceof WebGL?

backend.evictCachesForKey(key);
}
Expand Down
44 changes: 27 additions & 17 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,23 +377,33 @@
});
});

QUnit.test('setElement resets the webgl cache', function(assert) {
var done = assert.async();
var fabricBackend = fabric.filterBackend;
createImageObject(function(image) {
fabric.filterBackend = {
textureCache: {},
evictCachesForKey: function(key) {
delete this.textureCache[key];
}
};
var elImage = _createImageElement();
fabric.filterBackend.textureCache[image.cacheKey] = 'something';
image.setElement(elImage);
assert.equal(fabric.filterBackend.textureCache[image.cacheKey], undefined);
fabric.filterBackend = fabricBackend;
done();
});
QUnit.test('setElement calls `removeTexture`', function (assert) {
const done = assert.async();
const keys = [];
createImageObject((image) => {
image.cacheKey = 'TEST';
// use sinon replace or something one day
image.removeTexture = (key) => keys.push(key);
image.setElement(_createImageElement());
assert.deepEqual(keys, ['TEST', 'TEST_filtered'], 'should try to remove caches');
done();
});
});

QUnit.test('setElement resets the webgl cache', function (assert) {
const backend = fabric.getFilterBackend();
if (backend instanceof fabric.WebglFilterBackend) {
const done = assert.async();
createImageObject((image) => {
backend.textureCache[image.cacheKey] = backend.createTexture(backend.gl, 50, 50);
assert.ok(backend.textureCache[image.cacheKey]);
image.setElement(_createImageElement());
assert.equal(backend.textureCache[image.cacheKey], undefined);
done();
});
} else {
assert.expect(0);
}
});

QUnit.test('crossOrigin', function(assert) {
Expand Down