1 /* 2 * Copyright 2021 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 package org.skia.jetski; 9 10 import android.graphics.Bitmap; 11 import android.os.Build; 12 import androidx.annotation.RequiresApi; 13 14 public class Surface { 15 private long mNativeInstance; 16 17 /** 18 * Create a Surface backed by the provided Bitmap. 19 * 20 * The Bitmap must be mutable and its pixels are locked for the lifetime of the Surface. 21 */ Surface(Bitmap bitmap)22 public Surface(Bitmap bitmap) { 23 this(CreateBitmapInstance(bitmap)); 24 } 25 26 @RequiresApi(Build.VERSION_CODES.N) CreateVulkan(android.view.Surface surface)27 static public Surface CreateVulkan(android.view.Surface surface) { 28 return new Surface(nCreateVKSurface(surface)); 29 } 30 CreateGL(android.view.Surface surface)31 static public Surface CreateGL(android.view.Surface surface) { 32 return new Surface(nCreateGLSurface(surface)); 33 } 34 35 /** 36 * Create a Surface backed by the provided Android Surface (android.view.Surface). 37 * JetSki handles thread management. Assumes OpenGL backend. 38 */ createThreadedSurface(android.view.Surface surface)39 static public Surface createThreadedSurface(android.view.Surface surface) { 40 return new Surface(nCreateThreadedSurface(surface)); 41 } 42 43 /** 44 * The Canvas associated with this Surface. 45 */ getCanvas()46 public Canvas getCanvas() { 47 // TODO: given that canvases are now ephemeral, it would make sense to be more explicit 48 // e.g. lockCanvas/unlockCanvasAndPost? 49 return new Canvas(this, nGetNativeCanvas(mNativeInstance)); 50 } 51 52 53 /** 54 * Returns an Image capturing the Surface contents. 55 * Subsequent drawing to Surface contents are not captured. 56 */ makeImageSnapshot()57 public Image makeImageSnapshot() { 58 return new Image(nMakeImageSnapshot(mNativeInstance)); 59 } 60 61 /*** 62 * Triggers the immediate execution of all pending draw operations. 63 * 64 * Additionaly, if the backing device is multi-buffered, submits the current 65 * buffer to be displayed. 66 */ flushAndSubmit()67 public void flushAndSubmit() { 68 nFlushAndSubmit(mNativeInstance); 69 } 70 getWidth()71 public int getWidth() { 72 return nGetWidth(mNativeInstance); 73 } 74 getHeight()75 public int getHeight() { 76 return nGetHeight(mNativeInstance); 77 } 78 79 /** 80 * Releases any resources associated with this Surface. 81 */ release()82 public void release() { 83 nRelease(mNativeInstance); 84 mNativeInstance = 0; 85 } 86 87 @Override finalize()88 protected void finalize() throws Throwable 89 { 90 release(); 91 } 92 Surface(long native_instance)93 private Surface(long native_instance) { 94 mNativeInstance = native_instance; 95 } 96 CreateBitmapInstance(Bitmap bitmap)97 private static long CreateBitmapInstance(Bitmap bitmap) { 98 if (!bitmap.isMutable()) { 99 throw new IllegalStateException("Immutable bitmap passed to Surface constructor"); 100 } 101 return nCreateBitmap(bitmap); 102 } 103 nCreateBitmap(Bitmap bitmap)104 private static native long nCreateBitmap(Bitmap bitmap); nCreateThreadedSurface(android.view.Surface surface)105 private static native long nCreateThreadedSurface(android.view.Surface surface); nCreateVKSurface(android.view.Surface surface)106 private static native long nCreateVKSurface(android.view.Surface surface); nCreateGLSurface(android.view.Surface surface)107 private static native long nCreateGLSurface(android.view.Surface surface); 108 nRelease(long nativeInstance)109 private static native void nRelease(long nativeInstance); nGetNativeCanvas(long nativeInstance)110 private static native long nGetNativeCanvas(long nativeInstance); nFlushAndSubmit(long nativeInstance)111 private static native void nFlushAndSubmit(long nativeInstance); nGetWidth(long nativeInstance)112 private static native int nGetWidth(long nativeInstance); nGetHeight(long nativeInstance)113 private static native int nGetHeight(long nativeInstance); nMakeImageSnapshot(long nativeInstance)114 private static native long nMakeImageSnapshot(long nativeInstance); 115 } 116