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 nativeCreate(String name, long surfaceControl, long width, long height, int format)30 private static native long nativeCreate(String name, long surfaceControl, long width, 31 long height, int format); nativeDestroy(long ptr)32 private static native void nativeDestroy(long ptr); nativeGetSurface(long ptr, boolean includeSurfaceControlHandle)33 private static native Surface nativeGetSurface(long ptr, boolean includeSurfaceControlHandle); nativeSetNextTransaction(long ptr, long transactionPtr)34 private static native void nativeSetNextTransaction(long ptr, long transactionPtr); nativeUpdate(long ptr, long surfaceControl, long width, long height, int format, long transactionPtr)35 private static native void nativeUpdate(long ptr, long surfaceControl, long width, long height, 36 int format, long transactionPtr); nativeFlushShadowQueue(long ptr)37 private static native void nativeFlushShadowQueue(long ptr); 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); 42 43 /** 44 * Callback sent to {@link #setTransactionCompleteCallback(long, TransactionCompleteCallback)} 45 */ 46 public interface TransactionCompleteCallback { 47 /** 48 * Invoked when the transaction has completed. 49 * @param frameNumber The frame number of the buffer that was in that transaction 50 */ onTransactionComplete(long frameNumber)51 void onTransactionComplete(long frameNumber); 52 } 53 54 /** Create a new connection with the surface flinger. */ BLASTBufferQueue(String name, SurfaceControl sc, int width, int height, @PixelFormat.Format int format)55 public BLASTBufferQueue(String name, SurfaceControl sc, int width, int height, 56 @PixelFormat.Format int format) { 57 mNativeObject = nativeCreate(name, sc.mNativeObject, width, height, format); 58 } 59 destroy()60 public void destroy() { 61 nativeDestroy(mNativeObject); 62 mNativeObject = 0; 63 } 64 65 /** 66 * @return a new Surface instance from the IGraphicsBufferProducer of the adapter. 67 */ createSurface()68 public Surface createSurface() { 69 return nativeGetSurface(mNativeObject, false /* includeSurfaceControlHandle */); 70 } 71 72 /** 73 * @return a new Surface instance from the IGraphicsBufferProducer of the adapter and 74 * the SurfaceControl handle. 75 */ createSurfaceWithHandle()76 public Surface createSurfaceWithHandle() { 77 return nativeGetSurface(mNativeObject, true /* includeSurfaceControlHandle */); 78 } 79 80 /** 81 * Send the transaction to BBQ so the next frame can be added and not applied immediately. 82 * This gives the caller a chance to apply the transaction when it's ready. 83 * @param t The transaction to add the frame to. This can be null to clear the transaction. 84 */ setNextTransaction(@ullable SurfaceControl.Transaction t)85 public void setNextTransaction(@Nullable SurfaceControl.Transaction t) { 86 nativeSetNextTransaction(mNativeObject, t == null ? 0 : t.mNativeObject); 87 } 88 89 /** 90 * Updates {@link SurfaceControl}, size, and format for a particular BLASTBufferQueue 91 * @param sc The new SurfaceControl that this BLASTBufferQueue will update 92 * @param width The new width for the buffer. 93 * @param height The new height for the buffer. 94 * @param format The new format for the buffer. 95 * @param t Adds destination frame changes to the passed in transaction. 96 */ update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format, SurfaceControl.Transaction t)97 public void update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format, 98 SurfaceControl.Transaction t) { 99 nativeUpdate(mNativeObject, sc.mNativeObject, width, height, format, t.mNativeObject); 100 } 101 update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format)102 public void update(SurfaceControl sc, int width, int height, @PixelFormat.Format int format) { 103 nativeUpdate(mNativeObject, sc.mNativeObject, width, height, format, 0); 104 } 105 106 /** 107 * Set a callback when the transaction with the frame number has been completed. 108 * @param frameNumber The frame number to get the transaction complete callback for 109 * @param completeCallback The callback that should be invoked. 110 */ setTransactionCompleteCallback(long frameNumber, @Nullable TransactionCompleteCallback completeCallback)111 public void setTransactionCompleteCallback(long frameNumber, 112 @Nullable TransactionCompleteCallback completeCallback) { 113 nativeSetTransactionCompleteCallback(mNativeObject, frameNumber, completeCallback); 114 } 115 116 @Override finalize()117 protected void finalize() throws Throwable { 118 try { 119 if (mNativeObject != 0) { 120 nativeDestroy(mNativeObject); 121 } 122 } finally { 123 super.finalize(); 124 } 125 } 126 flushShadowQueue()127 public void flushShadowQueue() { 128 nativeFlushShadowQueue(mNativeObject); 129 } 130 131 /** 132 * Merge the transaction passed in to the next transaction in BlastBufferQueue. The next 133 * transaction will be applied or merged when the next frame with specified frame number 134 * is available. 135 */ mergeWithNextTransaction(SurfaceControl.Transaction t, long frameNumber)136 public void mergeWithNextTransaction(SurfaceControl.Transaction t, long frameNumber) { 137 nativeMergeWithNextTransaction(mNativeObject, t.mNativeObject, frameNumber); 138 } 139 140 /** 141 * Merge the transaction passed in to the next transaction in BlastBufferQueue. 142 * @param nativeTransaction native handle passed from native c/c++ code. 143 */ mergeWithNextTransaction(long nativeTransaction, long frameNumber)144 public void mergeWithNextTransaction(long nativeTransaction, long frameNumber) { 145 nativeMergeWithNextTransaction(mNativeObject, nativeTransaction, frameNumber); 146 } 147 148 } 149