Skip to content

Commit 1ca310d

Browse files
RaltyroMAJigsaw77
authored andcommitted
Fix useTexture from allocating a CPU BitmapData first
1 parent 3a40e68 commit 1ca310d

2 files changed

Lines changed: 57 additions & 35 deletions

File tree

source/hxvlc/openfl/Video.hx

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ class Video extends openfl.display.Bitmap
428428
@:noCompletion
429429
private var texturePlanes:Null<BytesData>;
430430

431+
@:noCompletion
432+
private var texturePlanesArray:Null<UInt8Array>;
433+
431434
#if lime_openal
432435
@:noCompletion
433436
private var alUseEXTFLOAT32:Null<Bool>;
@@ -870,9 +873,15 @@ class Video extends openfl.display.Bitmap
870873
bitmapData.dispose();
871874
}
872875

876+
if (texturePlanes != null)
877+
{
878+
texturePlanes.setSize(0);
879+
texturePlanes = null;
880+
}
881+
873882
textureWidth = 0;
874883
textureHeight = 0;
875-
texturePlanes = null;
884+
texturePlanesArray = null;
876885
}
877886

878887
textureMutex.release();
@@ -1339,7 +1348,9 @@ class Video extends openfl.display.Bitmap
13391348
if (texturePlanes == null)
13401349
texturePlanes = new BytesData();
13411350

1342-
texturePlanes.resize(textureWidth * textureHeight * 4);
1351+
texturePlanes.setSize(textureWidth * textureHeight * 4);
1352+
1353+
texturePlanesArray = UInt8Array.fromBytes(Bytes.ofData(texturePlanes));
13431354

13441355
pitches[0] = textureWidth * 4;
13451356
lines[0] = textureHeight;
@@ -1354,10 +1365,11 @@ class Video extends openfl.display.Bitmap
13541365
textureMutex.acquire();
13551366

13561367
final sizeMismatch:Bool = bitmapData != null && (bitmapData.width != textureWidth || bitmapData.height != textureHeight);
1357-
final textureMismatch:Bool = bitmapData != null && bitmapData.__texture != null && !useTexture;
1358-
final imageMismatch:Bool = bitmapData != null && bitmapData.image != null && useTexture;
1368+
final dataMismatch:Bool = bitmapData != null && (
1369+
if (useTexture) bitmapData.image != null && Lib.current.stage?.context3D != null
1370+
else bitmapData.__texture != null);
13591371

1360-
if (bitmapData == null || sizeMismatch || textureMismatch || imageMismatch)
1372+
if (bitmapData == null || sizeMismatch || dataMismatch)
13611373
{
13621374
if (bitmapData != null)
13631375
{
@@ -1367,28 +1379,32 @@ class Video extends openfl.display.Bitmap
13671379
bitmapData.dispose();
13681380
}
13691381

1370-
bitmapData = new BitmapData(textureWidth, textureHeight, true, 0);
1371-
1382+
if (useTexture)
13721383
{
13731384
@:privateAccess
1374-
if (useTexture)
1385+
if (Lib.current.stage?.context3D != null)
13751386
{
1376-
@:nullSafety(Off)
1377-
if (Lib.current.stage?.context3D != null)
1378-
{
1379-
bitmapData.disposeImage();
1387+
// This creates an image-less BitmapData
1388+
bitmapData = new BitmapData(0, 0, true, 0);
13801389

1381-
{
1382-
bitmapData.__texture = new VideoTexture(Lib.current.stage.context3D, bitmapData, textureWidth * textureHeight * 4);
1383-
bitmapData.__textureContext = bitmapData.__texture.__textureContext;
1384-
bitmapData.__surface = null;
1385-
}
1390+
// Because the BitmapData doesnt have an image we set the bounds of it here
1391+
bitmapData.rect.setTo(0, 0, textureWidth, textureHeight);
13861392

1387-
bitmapData.image = null;
1388-
}
1389-
else
1390-
trace('Unable to utilize GPU texture, resorting to CPU-based image rendering.');
1393+
// Allocates the Texture here so its a GPU BitmapData
1394+
bitmapData.__texture = new VideoTexture(Lib.current.stage.context3D, textureWidth, textureHeight, texturePlanesArray);
1395+
bitmapData.__textureContext = bitmapData.__texture.__textureContext;
1396+
bitmapData.__resize(textureWidth, textureHeight);
1397+
bitmapData.__isValid = true;
13911398
}
1399+
else
1400+
{
1401+
trace('Unable to utilize GPU texture, resorting to CPU-based image rendering.');
1402+
bitmapData = new BitmapData(textureWidth, textureHeight, true, 0);
1403+
}
1404+
}
1405+
else
1406+
{
1407+
bitmapData = new BitmapData(textureWidth, textureHeight, true, 0);
13921408
}
13931409

13941410
if (onFormatSetup != null)
@@ -1729,8 +1745,14 @@ class Video extends openfl.display.Bitmap
17291745

17301746
if (texturePlanes != null)
17311747
{
1732-
bitmapData.image.buffer.data = UInt8Array.fromBytes(Bytes.ofData(texturePlanes));
1748+
final dest:RawPointer<cpp.Void> = untyped bitmapData.image.buffer.data.buffer.getData().getBase().getBase();
1749+
1750+
final src:RawConstPointer<cpp.Void> = untyped texturePlanes.getBase().getBase();
1751+
1752+
Stdlib.nativeMemcpy(dest, src, bitmapData.image.buffer.data.buffer.byteLength);
1753+
17331754
bitmapData.image.dirty = true;
1755+
17341756
bitmapData.image.version++;
17351757
}
17361758

@@ -1747,7 +1769,7 @@ class Video extends openfl.display.Bitmap
17471769
{
17481770
MainLoop.runInMainThread(function():Void
17491771
{
1750-
if (!isValid() || bitmapData == null || texturePlanes == null)
1772+
if (!isValid() || bitmapData == null || texturePlanesArray == null)
17511773
return;
17521774

17531775
textureMutex.acquire();
@@ -1756,7 +1778,9 @@ class Video extends openfl.display.Bitmap
17561778

17571779
if (texture != null)
17581780
{
1759-
texture.uploadFromTypedArray(UInt8Array.fromBytes(Bytes.ofData(texturePlanes)));
1781+
texture.uploadFromTypedArray(texturePlanesArray);
1782+
1783+
bitmapData.__textureVersion++;
17601784

17611785
if (__renderable)
17621786
__setRenderDirty();

source/hxvlc/openfl/textures/VideoTexture.hx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,29 @@ class VideoTexture extends TextureBase
2222
* Initializes a VideoTexture object.
2323
*
2424
* @param context The context to use for texture operations.
25-
* @param bitmapData Initial bitmap data to populate the texture.
26-
* @param frameSize The size in bytes of a single video frame, used for buffer allocation.
27-
* @param optimizeForRenderToTexture Whether to optimize this texture for render-to-texture operations (used by default).
25+
* @param width The width dimension to allocate for the texture.
26+
* @param height The height dimension to allocate for the texture.
27+
* @param data The pixel data to initializes the texture with.
2828
*/
29-
public function new(context:Context3D, bitmapData:BitmapData, frameSize:Int, optimizeForRenderToTexture:Bool = true):Void
29+
public function new(context:Context3D, width:Int, height:Int, ?data:UInt8Array):Void
3030
{
3131
super(context);
3232

33-
__width = bitmapData.width;
34-
__height = bitmapData.height;
35-
__optimizeForRenderToTexture = optimizeForRenderToTexture;
33+
__width = width;
34+
__height = height;
3635
__textureTarget = __context.gl.TEXTURE_2D;
37-
__frameSize = frameSize;
36+
__frameSize = width * height * 4;
3837

3938
@:nullSafety(Off)
4039
{
4140
__context.__bindGLTexture2D(__textureID);
4241

43-
__context.gl.texImage2D(__textureTarget, 0, __internalFormat, __width, __height, 0, __format, __context.gl.UNSIGNED_BYTE, bitmapData.image.data);
42+
__context.gl.texImage2D(__textureTarget, 0, __internalFormat, __width, __height, 0, __format, __context.gl.UNSIGNED_BYTE, data);
4443

4544
__context.__bindGLTexture2D(null);
4645
}
4746

48-
if (optimizeForRenderToTexture)
49-
__getGLFramebuffer(true, 0, 0);
47+
__getGLFramebuffer(false, 0, 0);
5048
}
5149

5250
/**

0 commit comments

Comments
 (0)