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: Improve autoDetectRenderer #8475

Merged
merged 4 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions packages/canvas-renderer/src/CanvasRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import type { DisplayObject } from '@pixi/display';
import type {
IRendererOptions,
IRendererPlugins,
IRendererRenderOptions
,
IRendererRenderOptions,
RenderTexture,
IRenderableObject,
GenerateTextureSystem, IRenderer,
GenerateTextureSystem,
IRenderer,
BackgroundSystem,
ViewSystem,
PluginSystem,
StartupSystem,
StartupOptions,
ExtensionMetadata,
IGenerateTextureOptions } from '@pixi/core';

import type { SmoothingEnabledProperties } from './CanvasContextSystem';
Expand Down Expand Up @@ -71,6 +72,21 @@ export interface CanvasRenderer extends GlobalMixins.CanvasRenderer {}
*/
export class CanvasRenderer extends SystemManager<CanvasRenderer> implements IRenderer
{
/** @ignore */
static extension: ExtensionMetadata = {
type: ExtensionType.Renderer,
priority: 0,
};

/**
* Used with autoDetectRenderer, this is always supported for any environment, so return true.
* @ignore
*/
static test(): boolean
{
return true;
}

/**
* Fired after rendering finishes.
* @event PIXI.CanvasRenderer#postrender
Expand Down Expand Up @@ -577,6 +593,7 @@ extensions.handle(
);

extensions.add(
CanvasRenderer,
CanvasMaskSystem,
CanvasContextSystem,
CanvasObjectRendererSystem
Expand Down
33 changes: 0 additions & 33 deletions packages/canvas-renderer/src/Renderer.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/canvas-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from './CanvasContextSystem';
export * from './utils/canUseNewCanvasBlendModes';
export * from './canvasUtils';

import './Renderer';
import './BaseTexture';
16 changes: 12 additions & 4 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { Rectangle } from '@pixi/math';
import { Matrix } from '@pixi/math';
import { BufferSystem } from './geometry/BufferSystem';
import type { RenderTexture } from './renderTexture/RenderTexture';
import type { ExtensionMetadata } from './extensions';
import { extensions, ExtensionType } from './extensions';
import type { IRendererPlugins } from './plugin/PluginSystem';
import { PluginSystem } from './plugin/PluginSystem';
Expand Down Expand Up @@ -92,6 +93,12 @@ export interface Renderer extends GlobalMixins.Renderer {}
*/
export class Renderer extends SystemManager<Renderer> implements IRenderer
{
/** @ignore */
static extension: ExtensionMetadata = {
type: ExtensionType.Renderer,
priority: 1,
};

/**
* The type of the renderer. will be PIXI.RENDERER_TYPE.CANVAS
* @member {number}
Expand Down Expand Up @@ -276,14 +283,14 @@ export class Renderer extends SystemManager<Renderer> implements IRenderer
* @param options
* @private
*/
static create(options?: IRendererOptions): IRenderer
static test(options?: IRendererOptions): boolean
{
if (isWebGLSupported())
if (options?.forceCanvas)
{
return new Renderer(options);
return false;
}

throw new Error('WebGL unsupported in this browser, use "pixi.js-legacy" for fallback canvas2d support.');
return isWebGLSupported();
}

/**
Expand Down Expand Up @@ -655,6 +662,7 @@ extensions.handle(
);

extensions.add(
Renderer,
GenerateTextureSystem,
BackgroundSystem,
ViewSystem,
Expand Down
35 changes: 33 additions & 2 deletions packages/core/src/autoDetectRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import type { ExtensionFormat } from './extensions';
import { extensions, ExtensionType } from './extensions';
import type { IRenderer, IRendererOptions } from './IRenderer';
import { Renderer } from './Renderer';

export interface IRendererOptionsAuto extends IRendererOptions
{
forceCanvas?: boolean;
}

/**
* Collection of installed Renderers.
* @ignore
*/
const renderers: ExtensionFormat[] = [];

extensions.handle(ExtensionType.Renderer,
(extension) =>
{
renderers.push(extension);
renderers.sort((a, b) => b.priority - a.priority); // highest priority first
},
(extension) =>
{
if (renderers.includes(extension))
{
renderers.splice(renderers.indexOf(extension), 1);
}
}
);

/**
* This helper function will automatically detect which renderer you should be using.
* WebGL is the preferred renderer as it is a lot faster. If WebGL is not supported by
Expand Down Expand Up @@ -38,5 +61,13 @@ export interface IRendererOptionsAuto extends IRendererOptions
*/
export function autoDetectRenderer(options?: IRendererOptionsAuto): IRenderer
{
return Renderer.create(options);
for (const renderer of renderers)
{
if (renderer.ref.test(options))
{
return new renderer.ref(options) as IRenderer;
}
}

throw new Error('Unable to auto-detect a suitable renderer.');
}
4 changes: 4 additions & 0 deletions packages/core/src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
enum ExtensionType
// eslint-disable-next-line @typescript-eslint/indent
{
Renderer = 'renderer',
Application = 'application',
RendererSystem = 'renderer-webgl-system',
RendererPlugin = 'renderer-webgl-plugin',
Expand All @@ -24,6 +25,7 @@ interface ExtensionMetadataDetails
{
type: ExtensionType | ExtensionType[];
name?: string;
priority?: number;
}

type ExtensionMetadata = ExtensionType | ExtensionMetadataDetails;
Expand All @@ -40,6 +42,8 @@ interface ExtensionFormatLoose
type: ExtensionType | ExtensionType[];
/** Optional. Some plugins provide an API name/property, such as Renderer plugins */
name?: string;
/** Optional, used for sorting the plugins in a particular order */
priority?: number;
/** Reference to the plugin object/class */
ref: any;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/events/test/EventSystem.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function createRenderer(view?: HTMLCanvasElement, supportsPointerEvents?: boolea
events: any
}

const renderer = Renderer.create({
const renderer = new Renderer({
width: 100,
height: 100,
view,
Expand Down