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

[DONT MERGE] feat(webworker): Reworking Web Worker API and new WebWorkerManager #880

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
*/
"mainEntryPointFilePath": "<projectFolder>/dist/cjs/index.d.ts",
"mainEntryPointFilePath": "<projectFolder>/dist/esm/index.d.ts",

/**
* A list of NPM package names whose exports should be treated as part of this package.
Expand Down
5 changes: 5 additions & 0 deletions common/reviews/api/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,9 @@ function getVolumeViewportScrollInfo(viewport: IVolumeViewport, volumeId: string
};
};

// @public (undocumented)
export function getWebWorkerManager(): any;

// @public (undocumented)
function hasNaNValues(input: number[] | number): boolean;

Expand Down Expand Up @@ -2271,6 +2274,8 @@ function renderToCanvasGPU(canvas: HTMLCanvasElement, image: IImage, modality?:

// @public (undocumented)
enum RequestType {
// (undocumented)
Compute = "compute",
// (undocumented)
Interaction = "interaction",
// (undocumented)
Expand Down
1 change: 1 addition & 0 deletions common/reviews/api/streaming-image-volume-loader.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,7 @@ type PublicViewportInput = {

// @public
enum RequestType {
Compute = 'compute',
Interaction = 'interaction',
Prefetch = 'prefetch',
Thumbnail = 'thumbnail',
Expand Down
18 changes: 9 additions & 9 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ module.exports = function (config) {
},
// NOTE: For better debugging you can comment out the
// istanbul-instrumenter-loader below
{
test: /\.ts$/,
exclude: [path.resolve(__dirname, 'test')],
enforce: 'post',
use: {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true },
},
},
// {
// test: /\.ts$/,
// exclude: [path.resolve(__dirname, 'test')],
// enforce: 'post',
// use: {
// loader: 'istanbul-instrumenter-loader',
// options: { esModules: true },
// },
// },
],
},
resolve: {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
"fs-extra": "^10.0.0",
"html-webpack-plugin": "^5.5.1",
"husky": "^8.0.3",
"istanbul-instrumenter-loader": "^3.0.1",
"jasmine": "^4.6.0",
"jest": "^29.5.0",
"jsdoc": "^3.6.7",
Expand Down Expand Up @@ -148,7 +147,7 @@
"webpack-cli": "^5.0.2",
"webpack-dev-server": "^4.13.3",
"webpack-merge": "5.8.0",
"worker-loader": "^3.0.8",
"webpack": "5.81.0",
"xml2js": "^0.5.0"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../api-extractor.json",
"projectFolder": ".",
"mainEntryPointFilePath": "<projectFolder>/dist/cjs/index.d.ts",
"mainEntryPointFilePath": "<projectFolder>/dist/esm/index.d.ts",
"apiReport": {
"reportFileName": "<unscopedPackageName>.api.md",
"reportFolder": "../../common/reviews/api"
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"build:esm": "tsc --project ./tsconfig.esm.json",
"build:esm:watch": "tsc --project ./tsconfig.esm.json --watch",
"build:umd": "cross-env NODE_ENV=production webpack --config .webpack/webpack.prod.js",
"build:all": "yarn run build:umd && yarn run build:cjs && yarn run build:esm",
"build:all": "yarn run build:umd && yarn run build:esm",
"clean": "shx rm -rf dist",
"copy-dts": "copyfiles -u 1 \"src/**/*.d.ts\" dist/cjs && copyfiles -u 1 \"src/**/*.d.ts\" dist/esm",
"build": "yarn run build:all && yarn run copy-dts",
Expand All @@ -33,7 +33,8 @@
"@kitware/vtk.js": "27.3.1",
"detect-gpu": "^5.0.22",
"gl-matrix": "^3.4.3",
"lodash.clonedeep": "4.5.0"
"lodash.clonedeep": "4.5.0",
"comlink": "^4.4.1"
},
"contributors": [
{
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/RenderingEngine/StackViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransf
import * as metaData from '../metaData';
import Viewport from './Viewport';
import eventTarget from '../eventTarget';

import {
triggerEvent,
isEqual,
Expand Down Expand Up @@ -1914,6 +1915,7 @@ class StackViewport extends Viewport implements IStackViewport {
enabled: true,
},
useRGBA: true,
requestType,
};

const eventDetail: EventTypes.PreStackNewImageEventDetail = {
Expand Down Expand Up @@ -2024,6 +2026,7 @@ class StackViewport extends Viewport implements IStackViewport {
enabled: true,
},
useRGBA: false,
requestType,
};

const eventDetail: EventTypes.PreStackNewImageEventDetail = {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/enums/RequestType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ enum RequestType {
Interaction = 'interaction',
/** Second highest priority for loading*/
Thumbnail = 'thumbnail',
/** Lowest priority for loading*/
/** Third highest priority for loading, usually used for image loading in the background*/
Prefetch = 'prefetch',
/** Lower priority, often used for background computations in the worker */
Compute = 'compute',
}

export default RequestType;
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
resetUseSharedArrayBuffer,
getConfiguration,
setConfiguration,
getWebWorkerManager,
} from './init';

// Classes
Expand Down Expand Up @@ -69,6 +70,7 @@ export {
// configs
getConfiguration,
setConfiguration,
getWebWorkerManager,
// enums
Enums,
CONSTANTS,
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ let useSharedArrayBuffer = true;
let sharedArrayBufferMode = SharedArrayBufferModes.TRUE;
import { deepMerge } from './utilities';
import { Cornerstone3DConfig } from './types';
import CentralizedWebWorkerManager from './webWorkerManager/webWorkerManager';

// TODO: move sharedArrayBuffer into config.
// TODO: change config into a class with methods to better control get/set
const defaultConfig: Cornerstone3DConfig = {
Expand Down Expand Up @@ -36,6 +38,8 @@ let config: Cornerstone3DConfig = {
// ...
};

let webWorkerManager = null;

function _getGLContext(): RenderingContext {
// Create canvas element. The canvas is not added to the
// document itself, so it is never displayed in the
Expand Down Expand Up @@ -133,6 +137,11 @@ async function init(configuration = config): Promise<boolean> {
setUseSharedArrayBuffer(sharedArrayBufferMode);

csRenderInitialized = true;

if (!webWorkerManager) {
webWorkerManager = new CentralizedWebWorkerManager();
}

return csRenderInitialized;
}

Expand Down Expand Up @@ -256,6 +265,14 @@ function _updateRenderingPipelinesForAllViewports(): void {
);
}

function getWebWorkerManager() {
if (!webWorkerManager) {
webWorkerManager = new CentralizedWebWorkerManager();
}

return webWorkerManager;
}

export {
init,
getShouldUseCPURendering,
Expand All @@ -268,4 +285,5 @@ export {
resetUseSharedArrayBuffer,
getConfiguration,
setConfiguration,
getWebWorkerManager,
};
22 changes: 9 additions & 13 deletions packages/core/src/requestPool/requestPoolManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ class RequestPoolManager {
interaction: 0,
thumbnail: 0,
prefetch: 0,
compute: 0,
};
/* maximum number of requests of each type. */
public maxNumRequests: {
interaction: number;
thumbnail: number;
prefetch: number;
compute: number;
};
/* A public property that is used to set the delay between requests. */
public grabDelay: number;
Expand All @@ -101,6 +103,7 @@ class RequestPoolManager {
interaction: { 0: [] },
thumbnail: { 0: [] },
prefetch: { 0: [] },
compute: { 0: [] },
};

this.grabDelay = 5;
Expand All @@ -110,12 +113,14 @@ class RequestPoolManager {
interaction: 0,
thumbnail: 0,
prefetch: 0,
compute: 0,
};

this.maxNumRequests = {
interaction: 6,
thumbnail: 6,
prefetch: 5,
compute: 15,
};
}

Expand Down Expand Up @@ -185,15 +190,7 @@ class RequestPoolManager {
// Adding the request to the correct priority group of the request type
this.requestPool[type][priority].push(requestDetails);

// Wake up
if (!this.awake) {
this.awake = true;
this.startGrabbing();
} else if (type === RequestType.Interaction) {
// Todo: this is a hack for interaction right now, we should separate
// the grabbing from the adding requests
this.startGrabbing();
}
this.startGrabbing();
}

/**
Expand Down Expand Up @@ -273,11 +270,13 @@ class RequestPoolManager {
const hasRemainingPrefetchRequests = this.sendRequests(
RequestType.Prefetch
);
const hasRemainingComputeRequests = this.sendRequests(RequestType.Compute);

if (
!hasRemainingInteractionRequests &&
!hasRemainingThumbnailRequests &&
!hasRemainingPrefetchRequests
!hasRemainingPrefetchRequests &&
!hasRemainingComputeRequests
) {
this.awake = false;
}
Expand Down Expand Up @@ -323,7 +322,4 @@ class RequestPoolManager {
}
}

const requestPoolManager = new RequestPoolManager();

export { RequestPoolManager };
export default requestPoolManager;