From e27ea3cf705ecc07afd5631cfa1eac0c58f05923 Mon Sep 17 00:00:00 2001 From: alemart <221477+alemart@users.noreply.github.com> Date: Wed, 12 Jun 2024 19:14:30 -0300 Subject: [PATCH] Remove class SpeedyTextureUploader --- src/gpu/speedy-gpu.js | 8 +--- src/gpu/speedy-texture-uploader.js | 75 ------------------------------ src/gpu/speedy-texture.js | 19 ++++++-- 3 files changed, 17 insertions(+), 85 deletions(-) delete mode 100644 src/gpu/speedy-texture-uploader.js diff --git a/src/gpu/speedy-gpu.js b/src/gpu/speedy-gpu.js index 3ddcde02..36ff93e2 100644 --- a/src/gpu/speedy-gpu.js +++ b/src/gpu/speedy-gpu.js @@ -23,7 +23,6 @@ import { SpeedyGL } from './speedy-gl'; import { SpeedyTexture } from './speedy-texture'; import { SpeedyProgramCenter } from './speedy-program-center'; import { SpeedyTexturePool } from './speedy-texture-pool'; -import { SpeedyTextureUploader } from './speedy-texture-uploader'; import { SpeedyMediaSource } from '../core/speedy-media-source'; import { SpeedyPromise } from '../core/speedy-promise'; import { Utils } from '../utils/utils'; @@ -51,9 +50,6 @@ export class SpeedyGPU extends Observable /** @type {SpeedyTexturePool} texture pool */ this._texturePool = new SpeedyTexturePool(this); - /** @type {SpeedyTextureUploader} texture uploader */ - this._textureUploader = new SpeedyTextureUploader(this); - // recreate the state if necessary @@ -131,7 +127,7 @@ export class SpeedyGPU extends Observable */ upload(source, outputTexture) { - return this._textureUploader.upload(source, outputTexture); + return outputTexture.upload(source.data, source.width, source.height); } /** @@ -145,7 +141,6 @@ export class SpeedyGPU extends Observable // release internal components this._programs = this._programs.release(); this._texturePool = this._texturePool.release(); - this._textureUploader = this._textureUploader.release(); // unsubscribe this._speedyGL.unsubscribe(this._reset); @@ -181,7 +176,6 @@ export class SpeedyGPU extends Observable this._programs = new SpeedyProgramCenter(this); this._texturePool = new SpeedyTexturePool(this); - this._textureUploader = new SpeedyTextureUploader(this); this._notify(); } diff --git a/src/gpu/speedy-texture-uploader.js b/src/gpu/speedy-texture-uploader.js deleted file mode 100644 index 73eb5f13..00000000 --- a/src/gpu/speedy-texture-uploader.js +++ /dev/null @@ -1,75 +0,0 @@ -/* - * speedy-vision.js - * GPU-accelerated Computer Vision for JavaScript - * Copyright 2020-2023 Alexandre Martins - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * speedy-texture-uploader.js - * A utility that helps uploading data to textures - */ - -import { SpeedyGPU } from './speedy-gpu'; -import { SpeedyTexture } from './speedy-texture'; -import { SpeedyMediaSource } from '../core/speedy-media-source'; - -/** - * A utility that helps uploading data to textures - */ -export class SpeedyTextureUploader -{ - /** - * Constructor - * @param {SpeedyGPU} gpu - */ - constructor(gpu) - { - /** @type {SpeedyGPU} GPU instance */ - this._gpu = gpu; - } - - /** - * Upload an image to the GPU - * @param {SpeedyMediaSource} source - * @param {SpeedyTexture} outputTexture - * @returns {SpeedyTexture} output texture - */ - upload(source, outputTexture) - { - const data = source.data; - - // bugfix: if the media is a video, we can't really - // upload it to the GPU unless it's ready - //if(data.constructor.name == 'HTMLVideoElement') { - if(data instanceof HTMLVideoElement) { - if(data.readyState < 2) { - // this may happen when the video loops (Firefox) - // return the previously uploaded texture - //Utils.warning(`Trying to process a video that isn't ready yet`); - return outputTexture; - } - } - - // upload to the output texture - return outputTexture.upload(data, source.width, source.height); - } - - /** - * Release the texture uploader - * @returns {null} - */ - release() - { - return null; - } -} \ No newline at end of file diff --git a/src/gpu/speedy-texture.js b/src/gpu/speedy-texture.js index 1304f592..8e8ded16 100644 --- a/src/gpu/speedy-texture.js +++ b/src/gpu/speedy-texture.js @@ -116,14 +116,26 @@ export class SpeedyTexture /** * Upload pixel data to the texture. The texture will be resized if needed. - * @param {TexImageSource} pixels + * @param {TexImageSource} data * @param {number} [width] in pixels * @param {number} [height] in pixels * @return {SpeedyTexture} this */ - upload(pixels, width = this._width, height = this._height) + upload(data, width = this._width, height = this._height) { const gl = this._gl; + + // bugfix: if the media is a video, we can't really + // upload it to the GPU unless it's ready + if(data instanceof HTMLVideoElement) { + if(data.readyState < 2) { + // this may happen when the video loops (Firefox) + // keep the previously uploaded texture + //Utils.warning(`Trying to process a video that isn't ready yet`); + return this; + } + } + Utils.assert(width > 0 && height > 0); this.discardMipmaps(); @@ -133,7 +145,7 @@ export class SpeedyTexture this._format = gl.RGBA; this._dataType = gl.UNSIGNED_BYTE; - SpeedyTexture._upload(gl, this._glTexture, this._width, this._height, pixels, 0, this._format, this._internalFormat, this._dataType); + SpeedyTexture._upload(gl, this._glTexture, this._width, this._height, data, 0, this._format, this._internalFormat, this._dataType); return this; } @@ -356,6 +368,7 @@ export class SpeedyTexture gl.bindTexture(gl.TEXTURE_2D, null); return texture; } + /** * Upload pixel data to a WebGL texture * @param {WebGL2RenderingContext} gl