1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.graphics;
18 
19 import android.annotation.Nullable;
20 import android.view.Surface;
21 import android.view.SurfaceControl;
22 
23 /**
24  * @hide
25  */
26 public final class BLASTBufferQueue {
27     // Note: This field is accessed by native code.
28     public long mNativeObject; // BLASTBufferQueue*
29 
nativeCreateAndUpdate(String name, long surfaceControl, long width, long height, int format)30     private static native long nativeCreateAndUpdate(String name, long surfaceControl, long width,
31             long height, int format);
nativeCreate(String name)32     private static native long nativeCreate(String name);
nativeDestroy(long ptr)33     private static native void nativeDestroy(long ptr);
nativeGetSurface(long ptr, boolean includeSurfaceControlHandle)34     private static native Surface nativeGetSurface(long ptr, boolean includeSurfaceControlHandle);
nativeSetNextTransaction(long ptr, long transactionPtr)35     private static native void nativeSetNextTransaction(long ptr, long transactionPtr);
nativeUpdate(long ptr, long surfaceControl, long width, long height, int format, long transactionPtr)36     private static native void nativeUpdate(long ptr, long surfaceControl, long width, long height,
37             int format, long transactionPtr);
nativeMergeWithNextTransaction(long ptr, long transactionPtr, long frameNumber)38     private static native void nativeMergeWithNextTransaction(long ptr, long transactionPtr,
39                                                               long frameNumber);
nativeSetTransactionCompleteCallback(long ptr, long frameNumber, TransactionCompleteCallback callback)40     private static native void nativeSetTransactionCompleteCallback(long ptr, long frameNumber,
41             TransactionCompleteCallback callback);
nativeGetLastAcquiredFrameNum(long ptr)42     private static native long nativeGetLastAcquiredFrameNum(long ptr);
43 
44     /**
45      * Callback sent to {@link #setTransactionCompleteCallback(long, TransactionCompleteCallback)}
46      */
47     public interface TransactionCompleteCallback {
48         /**
49          * Invoked when the transaction has completed.
50          * @param frameNumber The frame number of the buffer that was in that transaction
51          */
onTransactionComplete(long frameNumber)52         void onTransactionComplete(long frameNumber);
53     }
54 
55     /** Create a new connection with the surface flinger. */
BLASTBufferQueue(String name, SurfaceControl sc, int width, int height, @PixelFormat.Format int format)56     public BLASTBufferQueue(String name, SurfaceControl sc, int width, int height,
57             @PixelFormat.Format int format) {
58         mNativeObject = nativeCreateAndUpdate(name, sc.mNativeObject, width, height, format);
59     }
60 
BLASTBufferQueue(String name)61     public BLASTBufferQueue(String name) {
62         mNativeObject = nativeCreate(name);
63     }
64 
destroy()65     public void destroy() {
66         nativeDestroy(mNativeObject);
67         mNativeObject = 0;
68     }
69 
70     /**
71      * @return a new Surface instance from the IGraphicsBufferProducer of the adapter.
72      */
createSurface()73     public Surface createSurface() {
74         return nativeGetSurface(mNativeObject, false /* includeSurfaceControlHandle */);
75     }
76 
77     /**
78      * @return a new Surface instance from the IGraphicsBufferProducer of the adapter and
79      * the SurfaceControl handle.
80      */
createSurfaceWithHandle()81     public Surface createSurfaceWithHandle() {
82         return nativeGetSurface(mNativeObject, true /* includeSurfaceControlHandle */);
83     }
84 
85     /**
86      * Send the transaction to BBQ so the next frame can be added and not applied immediately.
87      * This gives the caller a chance to apply the transaction when it's ready.
88      * @param t The transaction to add the frame to. This can be null to clear the transaction.
89      */
setNextTransaction(@ullable SurfaceControl.Transaction t)90     public void setNextTransaction(@Nullable SurfaceControl.Transaction t) {
91         nativeSetNextTransaction(mNativeObject, t == null ? 0 : t.mNativeObject);
92     }
93 
94     /**
95      * Updates {@link SurfaceControl}, size, and format for a particular BLASTBufferQueue
96      * @param sc The new SurfaceControl that this BLASTBufferQueue will update
97      * @param width The new width for the buffer.
98      * @param height The new height for the buffer.
99      * @param format The new format for the buffer.
100      * @param t Adds destination frame changes to the passed in transaction.
101      */
update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format, SurfaceControl.Transaction t)102     public void update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format,
103             SurfaceControl.Transaction t) {
104         nativeUpdate(mNativeObject, sc.mNativeObject, width, height, format, t.mNativeObject);
105     }
106 
update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format)107     public void update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format) {
108         nativeUpdate(mNativeObject, sc.mNativeObject, width, height, format, 0);
109     }
110 
111     /**
112      * Set a callback when the transaction with the frame number has been completed.
113      * @param frameNumber The frame number to get the transaction complete callback for
114      * @param completeCallback The callback that should be invoked.
115      */
setTransactionCompleteCallback(long frameNumber, @Nullable TransactionCompleteCallback completeCallback)116     public void setTransactionCompleteCallback(long frameNumber,
117             @Nullable TransactionCompleteCallback completeCallback) {
118         nativeSetTransactionCompleteCallback(mNativeObject, frameNumber, completeCallback);
119     }
120 
121     @Override
finalize()122     protected void finalize() throws Throwable {
123         try {
124             if (mNativeObject != 0) {
125                 nativeDestroy(mNativeObject);
126             }
127         } finally {
128             super.finalize();
129         }
130     }
131 
132     /**
133      * Merge the transaction passed in to the next transaction in BlastBufferQueue. The next
134      * transaction will be applied or merged when the next frame with specified frame number
135      * is available.
136      */
mergeWithNextTransaction(SurfaceControl.Transaction t, long frameNumber)137     public void mergeWithNextTransaction(SurfaceControl.Transaction t, long frameNumber) {
138         nativeMergeWithNextTransaction(mNativeObject, t.mNativeObject, frameNumber);
139     }
140 
141     /**
142      * Merge the transaction passed in to the next transaction in BlastBufferQueue.
143      * @param nativeTransaction native handle passed from native c/c++ code.
144      */
mergeWithNextTransaction(long nativeTransaction, long frameNumber)145     public void mergeWithNextTransaction(long nativeTransaction, long frameNumber) {
146         nativeMergeWithNextTransaction(mNativeObject, nativeTransaction, frameNumber);
147     }
148 
getLastAcquiredFrameNum()149     public long getLastAcquiredFrameNum() {
150         return nativeGetLastAcquiredFrameNum(mNativeObject);
151     }
152 }
153