1 /* 2 * Copyright (C) 2016 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 #ifndef STAGEFRIGHT_CODEC2_FENCE_FACTORY_H_ 18 #define STAGEFRIGHT_CODEC2_FENCE_FACTORY_H_ 19 20 21 #include <C2Buffer.h> 22 23 #include <android-base/unique_fd.h> 24 25 /* 26 * Extract a list of sync fence fds from a potentially multi-sync C2Fence. 27 * This will return dupped file descriptors of the fences used to creating the 28 * sync fence. Specifically, for an unordered mult-sync fence, the merged 29 * singular fence will not be returned even though it is created aspart of 30 * constructing the C2Fence object. On the other hand, for a single fd sync 31 * fence, the returned list will contain the sole file descriptor. 32 * 33 * \param fence C2Fence object from which associated 34 * file descriptors need to be extracted 35 * \return a vector of sync fence fds. This will be a vector of size 0 if C2Fence 36 * is not a sync fence. The caller is responsible for closing the 37 * fds in the returned vector. 38 */ 39 std::vector<int> ExtractFdsFromCodec2SyncFence(const C2Fence& fence); 40 41 class C2SurfaceSyncMemory; 42 43 /** 44 * C2Fence implementation factory 45 */ 46 struct _C2FenceFactory { 47 48 class SurfaceFenceImpl; 49 class SyncFenceImpl; 50 class PipeFenceImpl; 51 52 /* 53 * Create C2Fence for BufferQueueBased blockpool. 54 * 55 * \param syncMem Shared memory object for synchronization between processes. 56 * \param waitId wait id for tracking status change for C2Fence. 57 */ 58 static C2Fence CreateSurfaceFence( 59 std::shared_ptr<C2SurfaceSyncMemory> syncMem, 60 uint32_t waitId); 61 62 /* 63 * Create C2Fence from a sync fence fd. 64 * 65 * \param fenceFd Sync fence file descriptor. 66 * It will be owned and closed by the returned fence object. 67 * \param validate If true, the fence fd will be validated to ensure 68 * it is a valid pending sync fence fd. 69 */ 70 static C2Fence CreateSyncFence(int fenceFd, bool validate = true); 71 72 /* 73 * Create C2Fence from list of sync fence fds, while also merging them to 74 * create a singular fence, which can be used as a backward compatible sync 75 * fence. 76 * 77 * \param fenceFds Vector of sync fence file descriptors. 78 * All file descriptors will be owned (and closed) by 79 * the returned fence object. 80 */ 81 [[deprecated("Use CreateUnorderedMultiSyncFence instead.")]] CreateMultipleFdSyncFence_C2FenceFactory82 static C2Fence CreateMultipleFdSyncFence(const std::vector<int>& fenceFds) { 83 return CreateUnorderedMultiSyncFence(fenceFds); 84 } 85 86 /* 87 * Create C2Fence from a list of unordered sync fence fds, while also merging 88 * them to create a singular fence, which can be used as a backward compatible 89 * sync fence. 90 * 91 * \param fenceFds Vector of sync fence file descriptors. 92 * All file descriptors will be owned (and closed) by 93 * the returned fence object. 94 * \param status Optional pointer to a status field. If not null, it will be 95 * updated with the status of the operation. Possible values 96 * are: 97 * - C2_OK: The operation succeeded. 98 * - C2_NO_MEMORY: The operation failed because of lack of 99 * memory. 100 * - C2_CORRUPTED: The operation failed because the sync 101 * fence fds could bot be merged. 102 * \return A C2Fence object representing the sync fence fds, or 103 * an empty C2Fence if the no C2Fence could be created. 104 * It is possible for the operation to fail but still return 105 * a possibly viable C2Fence object, e.g. if the merge 106 * operation failed only partially. Similarly, it is possible 107 * for the operation to succeed but still return an empty 108 * C2Fence object, e.g. if all fence fds were invalid. 109 */ 110 static C2Fence CreateUnorderedMultiSyncFence( 111 const std::vector<int>& fenceFds, c2_status_t *status = nullptr /* nullable */); 112 113 /* 114 * Create C2Fence from a list of sync fence fds. Waiting for the last fence 115 * must guarantee that all other fences are also signaled. 116 * 117 * \param fenceFds Vector of sync fence file descriptors. 118 * All file descriptors will be owned (and closed) by 119 * the returned fence object. 120 * \param status Optional pointer to a status field. If not null, it will be 121 * updated with the status of the operation. Possible values 122 * are: 123 * - C2_OK: The operation succeeded. 124 * - C2_NO_MEMORY: The operation failed because of lack of 125 * memory. 126 * \return A C2Fence object representing the sync fence fds, or 127 * an empty C2Fence if the operation failed. It is possible 128 * for the operation to succeed but still return an empty 129 * C2Fence object, e.g. if all fence fds were invalid. 130 */ 131 static C2Fence CreateMultiSyncFence( 132 const std::vector<int>& fenceFds, c2_status_t *status = nullptr /* nullable */); 133 134 /* 135 * Create C2Fence from an fd created by pipe()/pipe2() syscall. 136 * The ownership of \p fd is transterred to the returned C2Fence. 137 * 138 * \param fd An fd representing the write end from a pair of 139 * file descriptors which are created by 140 * pipe()/pipe2() syscall. 141 */ 142 static C2Fence CreatePipeFence(int fd); 143 144 /* 145 * Create C2Fence from a unique_fd created by pipe()/pipe2() syscall. 146 * 147 * \param ufd A unique_fd representing the write end from a pair 148 * of file descriptors which are created by 149 * pipe()/pipe2() syscall. 150 */ 151 static C2Fence CreatePipeFence(::android::base::unique_fd &&ufd); 152 153 /** 154 * Create a native handle from fence for marshalling 155 * 156 * \return a non-null pointer if the fence can be marshalled, otherwise return nullptr 157 */ 158 static native_handle_t* CreateNativeHandle(const C2Fence& fence); 159 160 /* 161 * Create C2Fence from a native handle. 162 * 163 * \param handle A native handle representing a fence 164 * \param takeOwnership If true, the native handle and the file descriptors 165 * within will be owned by the returned fence object. 166 * If false (default), the caller will still own the 167 * handle and its file descriptors and will have to 168 * close it. 169 * In either case the caller is responsible for 170 * deleting the native handle. 171 */ 172 static C2Fence CreateFromNativeHandle( 173 const native_handle_t* handle, bool takeOwnership = false); 174 }; 175 176 #endif // STAGEFRIGHT_CODEC2_FENCE_FACTORY_H_ 177