Skip to content

Commit eef69c0

Browse files
committed
fix: enable HWAcc on android >= 28 by default
1 parent 8e8c8a2 commit eef69c0

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

Diff for: src/canvas.android.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ export {
2929
PorterDuffXfermode,
3030
};
3131

32-
const isPostLVar = lazy(() => android.os.Build.VERSION.SDK_INT >= 24);
32+
let SDK_INT = -1;
33+
function getSDK() {
34+
if (SDK_INT === -1) {
35+
SDK_INT = android.os.Build.VERSION.SDK_INT;
36+
}
37+
return SDK_INT;
38+
}
3339

3440
function createArrayBuffer(length: number, useInts = false) {
3541
let bb: java.nio.ByteBuffer;
@@ -461,7 +467,7 @@ export class StaticLayout {
461467
if (!(text instanceof java.lang.CharSequence) && !(typeof text === 'string')) {
462468
text = text + '';
463469
}
464-
if (isPostLVar()) {
470+
if (getSDK() >=24) {
465471
this._native = android.text.StaticLayout.Builder.obtain(
466472
text,
467473
0,
@@ -555,12 +561,21 @@ class CanvasView extends CanvasBase {
555561
}
556562
nativeViewProtected: com.akylas.canvas.CanvasView;
557563
createNativeView() {
558-
// initAndroidCanvasViewClass();
559564
const view = new com.akylas.canvas.CanvasView(this._context);
560-
// const view = new NativeCanvasView(this._context, this);
561-
view.setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null);
565+
if (getSDK() >= 28) {
566+
view.setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null);
567+
} else {
568+
view.setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, null);
569+
}
562570
return view;
563571
}
572+
[hardwareAcceleratedProperty.getDefault](value) {
573+
if (getSDK() >= 28) {
574+
return true;
575+
} else {
576+
return false;
577+
}
578+
}
564579

565580
[hardwareAcceleratedProperty.setNative](value) {
566581
this.nativeViewProtected.setLayerType(value ? android.view.View.LAYER_TYPE_HARDWARE : android.view.View.LAYER_TYPE_SOFTWARE, null);

Diff for: src/canvas.common.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ export function createRectF(x: number, y: number, w: number, h: number) {
1717
return new RectF(x, y, x + w, y + h);
1818
}
1919

20+
export const DEFAULT_SCALE = Screen.mainScreen.scale;
2021
export const cachedProperty = new Property<CanvasBase, boolean>({ name: 'cached', defaultValue: false, valueConverter: booleanConverter });
21-
export const hardwareAcceleratedProperty = new Property<CanvasBase, boolean>({ name: 'hardwareAccelerated', defaultValue: true, valueConverter: booleanConverter });
22+
export const hardwareAcceleratedProperty = new Property<CanvasBase, boolean>({ name: 'hardwareAccelerated', valueConverter: booleanConverter });
2223
export const callDrawBeforeShapesProperty = new Property<CanvasBase, boolean>({ name: 'callDrawBeforeShapes', defaultValue: false, valueConverter: booleanConverter });
23-
export const densityProperty = new Property<CanvasBase, number>({ name: 'density', valueConverter: parseFloat });
24+
export const densityProperty = new Property<CanvasBase, number>({ name: 'density', defaultValue:DEFAULT_SCALE, valueConverter: parseFloat });
2425

2526
function throttle(fn, limit) {
2627
let waiting = false;
@@ -34,7 +35,6 @@ function throttle(fn, limit) {
3435
}
3536
};
3637
}
37-
export const DEFAULT_SCALE = Screen.mainScreen.scale;
3838
export abstract class CanvasBase extends View {
3939
protected _shapes: ObservableArray<Shape>;
4040

@@ -43,9 +43,9 @@ export abstract class CanvasBase extends View {
4343
get shapes() {
4444
return this._shapes;
4545
}
46-
public cached = false;
47-
public density = DEFAULT_SCALE;
48-
public hardwareAccelerated = true;
46+
public cached;
47+
public density;
48+
public hardwareAccelerated ;
4949

5050
drawFameRate = false;
5151

0 commit comments

Comments
 (0)