Skip to content

Commit 03fc09e

Browse files
committed
feat: initial imagebitmap support
fix(ios): video texture performance
1 parent 47b6dcc commit 03fc09e

File tree

46 files changed

+1407
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1407
-123
lines changed

packages/canvas-media/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/canvas-media",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"description": "Canvas media",
55
"main": "index",
66
"typings": "index.d.ts",

packages/canvas-media/video/index.ios.ts

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ export class Video extends VideoBase {
7474
_suspendListenerId: any;
7575
_isInForground = true;
7676
_assetOutput: AVPlayerItemVideoOutput;
77-
_previousTS: any;
7877
_isPlaying = false;
7978
#src: string;
8079
#muted: boolean;
@@ -87,6 +86,7 @@ export class Video extends VideoBase {
8786
_ctx: any;
8887
_asset: AVURLAsset;
8988
_videoSize: any;
89+
_render: any;
9090
get _player() {
9191
return this.#player;
9292
}
@@ -129,39 +129,11 @@ export class Video extends VideoBase {
129129
}
130130
if (this._assetOutput) {
131131
try {
132-
// _ player: AVPlayer, _ output: AVPlayerItemVideoOutput,_ videoSize: CGSize
133-
Utils.drawFrame(this.#player, this._assetOutput, this._videoSize);
134-
/*
135-
const currentTime = this.#player.currentTime();
136-
if (!this._assetOutput.hasNewPixelBufferForItemTime(currentTime)) {
137-
return;
138-
}
139-
const pixel_buffer = this._assetOutput.copyPixelBufferForItemTimeItemTimeForDisplay(currentTime, null);
140-
if (pixel_buffer !== 0) {
141-
CVPixelBufferLockBaseAddress(pixel_buffer, 0);
142-
const bytesPerRow = CVPixelBufferGetBytesPerRow(pixel_buffer);
143-
const line_base = CVPixelBufferGetBaseAddress(pixel_buffer) as any;
144-
145-
if (bytesPerRow / BYTES_PER_TEXEL === this._videoSize.width) {
146-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this._videoSize.width, this._videoSize.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, line_base);
147-
} else {
148-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this._videoSize.width, this._videoSize.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, null);
149-
for (let i = 0; i < this._videoSize.height; ++i) {
150-
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, i, this._videoSize.width, 1, GL_BGRA, GL_UNSIGNED_BYTE, line_base.add(i * bytesPerRow));
151-
}
152-
}
153-
154-
CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);
155-
// may not need to release
156-
157-
// Unlike regular Core Foundation objects, toll-free bridged types are automatically memory managed by NativeScript,
158-
// so there is no need to retain or release them using CFRetain and CFRelease.
159-
160-
// https://docs.nativescript.org/core-concepts/ios-runtime/marshalling-overview#corefoundation-objects
161-
//CFRelease(sampleBuffer);
162-
}
163-
164-
*/
132+
if (!this._render) {
133+
this._render = Utils.setupRender();
134+
this._render.createSurface();
135+
}
136+
Utils.drawFrame(this.#player, this._assetOutput, this._videoSize, this._render, arguments[4], arguments[5], false);
165137
} catch (e) {
166138
console.log('getCurrentFrame error:', e);
167139
}
@@ -242,8 +214,10 @@ export class Video extends VideoBase {
242214
}
243215

244216
set src(value: string) {
245-
this.#src = value;
246-
this._loadSrc(value);
217+
if(value !== this._currentUrl){
218+
this.#src = value;
219+
this._loadSrc(value);
220+
}
247221
}
248222

249223
get controls() {
@@ -298,7 +272,8 @@ export class Video extends VideoBase {
298272
this._notifyVideoFrameCallbacks();
299273
}
300274
});
301-
275+
this._render?.destroy();
276+
this._render = undefined;
302277
const item = AVPlayerItem.alloc().initWithAsset(this._asset);
303278
const settings: any = {};
304279
settings[kCVPixelBufferPixelFormatTypeKey] = NSNumber.numberWithUnsignedInt(kCVPixelFormatType_32BGRA);
@@ -317,7 +292,6 @@ export class Video extends VideoBase {
317292
this.#player.replaceCurrentItemWithPlayerItem(item);
318293
this._readyState = Video.HAVE_METADATA;
319294
this._currentUrl = src;
320-
this._previousTS = undefined;
321295
if (this.autoplay) {
322296
this.play();
323297
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ImageAsset } from '@nativescript/canvas';
2+
3+
export class ImageBitmap {
4+
_asset: ImageAsset;
5+
#width: number = 0;
6+
#height: number = 0;
7+
get width(): number {
8+
return this.#width;
9+
}
10+
get height(): number {
11+
return this.#height;
12+
}
13+
14+
close(){}
15+
}

packages/canvas-polyfill/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import './process';
1616
import { TextDecoder, TextEncoder } from '@nativescript/canvas';
1717
import { URL } from './URL';
1818
(global as any).document = (global as any).window.document = (global as any).document || new Document();
19+
20+
(global as any).window.createImageBitmap = (global as any).createImageBitmap = (image) =>{
21+
22+
}
23+
1924
Object.defineProperty(global, 'Element', {
2025
value: Element,
2126
configurable: true,

packages/canvas/WebGL/WebGLRenderingContext/index.ios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,9 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
979979
}
980980
} else if (arguments.length === 6) {
981981
if (border && typeof border.tagName === 'string' && (border.tagName === 'VID' || border.tagName === 'VIDEO') && border._video && typeof border._video.getCurrentFrame === 'function') {
982-
border._video.getCurrentFrame(this.context);
982+
border._video.getCurrentFrame(this.context,this, target, level, internalformat, width, height);
983983
} else if (border && typeof border.getCurrentFrame === 'function') {
984-
border.getCurrentFrame(this.context);
984+
border.getCurrentFrame(this.context, this, target, level, internalformat, width, height);
985985
} else if (border instanceof ImageAsset) {
986986
this.context.texImage2DAsset(target, level, internalformat, width, height, border.native);
987987
} else if (border instanceof ImageSource) {

packages/canvas/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/canvas",
3-
"version": "0.9.16",
3+
"version": "0.9.17",
44
"description": "DOM Canvas API for NativeScript",
55
"main": "index",
66
"typings": "index.d.ts",

packages/canvas/platforms/ios/Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use_frameworks!
22
platform :ios, '11.0'
3-
pod 'CanvasNative' , '~> 0.9.16'
4-
#pod 'CanvasNative', :path => "$(SRCROOT)/../../../../../"
3+
#pod 'CanvasNative' , '~> 0.9.16'
4+
pod 'CanvasNative', :path => "$(SRCROOT)/../../../../../"
55

66

packages/canvas/src-native/canvas-android/canvas/publish.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'maven-publish'
22
apply plugin: 'com.jfrog.bintray'
33

44
group 'com.github.triniwiz'
5-
version '0.9.13'
5+
version '0.9.14'
66

77
afterEvaluate {
88
publishing {
@@ -15,7 +15,7 @@ afterEvaluate {
1515
// You can then customize attributes of the publication as shown below.
1616
groupId = 'com.github.triniwiz'
1717
artifactId = 'canvas'
18-
version = '0.9.13'
18+
version = '0.9.14'
1919
}
2020
}
2121
}

packages/canvas/src-native/canvas-android/canvas/src/main/java/com/github/triniwiz/canvas/TNSImageAsset.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ class TNSImageAsset {
284284
@JvmStatic
285285
private external fun nativeDestroy(asset: Long)
286286

287+
@JvmStatic
288+
internal fun nativeDestroyImpl(asset: Long) {
289+
nativeDestroy(asset)
290+
}
291+
287292
private val executorService = Executors.newCachedThreadPool()
288293
}
289294

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.github.triniwiz.canvas
2+
3+
import java.nio.ByteBuffer
4+
5+
class TNSImageBitmap {
6+
internal var nativeImageAsset: Long
7+
8+
internal constructor(asset: Long) {
9+
nativeImageAsset = asset
10+
}
11+
12+
fun createFromBytes(bytes: ByteArray) {
13+
// TNSImageBitmap(asset)
14+
}
15+
16+
fun createFromBytes(bytes: ByteArray, sx: Float, sy: Float,sWidth: Float,sHeight: Float) {
17+
// TNSImageBitmap(asset)
18+
}
19+
20+
private var widthCache: Int = 0
21+
val width: Int
22+
get() {
23+
return widthCache
24+
}
25+
26+
private var heightCache: Int = 0
27+
val height: Int
28+
get() {
29+
return heightCache
30+
}
31+
32+
fun close() {
33+
TNSImageAsset.nativeDestroyImpl(nativeImageAsset)
34+
widthCache = 0
35+
heightCache = 0
36+
nativeImageAsset = 0
37+
}
38+
39+
40+
@Throws(Throwable::class)
41+
protected fun finalize() {
42+
if (nativeImageAsset > 0) {
43+
TNSImageAsset.nativeDestroyImpl(nativeImageAsset)
44+
nativeImageAsset = 0
45+
}
46+
}
47+
48+
49+
object Companion {
50+
@JvmStatic
51+
private external fun nativeCreateFromBuffer(
52+
imageBuffer: ByteBuffer,
53+
imageWidth: Float,
54+
imageHeight: Float,
55+
flipY: Boolean,
56+
premultiplyAlpha: Int,
57+
colorSpaceConversion: Int,
58+
resizeQuality: Int,
59+
resizeWidth: Float,
60+
resizeHeight: Float,
61+
)
62+
63+
@JvmStatic
64+
private external fun nativeCreateFromBufferSrcRect(
65+
imageBuffer: ByteBuffer,
66+
imageWidth: Float,
67+
imageHeight: Float,
68+
sx: Float,
69+
sy: Float,
70+
sWidth: Float,
71+
sHeight: Float,
72+
flipY: Boolean,
73+
premultiplyAlpha: Int,
74+
colorSpaceConversion: Int,
75+
resizeQuality: Int,
76+
resizeWidth: Float,
77+
resizeHeight: Float,
78+
)
79+
80+
@JvmStatic
81+
private external fun nativeCreateFromBytes(
82+
imageBuffer: ByteArray,
83+
imageWidth: Float,
84+
imageHeight: Float,
85+
flipY: Boolean,
86+
premultiplyAlpha: Int,
87+
colorSpaceConversion: Int,
88+
resizeQuality: Int,
89+
resizeWidth: Float,
90+
resizeHeight: Float,
91+
)
92+
93+
@JvmStatic
94+
private external fun nativeCreateFromBytesSrcRect(
95+
imageBuffer: ByteArray,
96+
imageWidth: Float,
97+
imageHeight: Float,
98+
sx: Float,
99+
sy: Float,
100+
sWidth: Float,
101+
sHeight: Float,
102+
flipY: Boolean,
103+
premultiplyAlpha: Int,
104+
colorSpaceConversion: Int,
105+
resizeQuality: Int,
106+
resizeWidth: Float,
107+
resizeHeight: Float,
108+
)
109+
}
110+
}

0 commit comments

Comments
 (0)