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