1 /* 2 * Copyright 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 ANDROID_HARDWARE_GRAPHICS_BUFFERQUEUE_V1_0_WGRAPHICBUFFERPRODUCER_H_ 18 #define ANDROID_HARDWARE_GRAPHICS_BUFFERQUEUE_V1_0_WGRAPHICBUFFERPRODUCER_H_ 19 20 #include <hidl/MQDescriptor.h> 21 #include <hidl/Status.h> 22 23 #include <binder/Binder.h> 24 #include <gui/IGraphicBufferProducer.h> 25 #include <gui/IProducerListener.h> 26 #include <gui/bufferqueue/1.0/Conversion.h> 27 #include <gui/bufferqueue/1.0/WProducerListener.h> 28 #include <system/window.h> 29 30 #include <android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer.h> 31 32 namespace android { 33 34 using ::android::hardware::media::V1_0::AnwBuffer; 35 using ::android::hidl::base::V1_0::IBase; 36 using ::android::hardware::hidl_array; 37 using ::android::hardware::hidl_handle; 38 using ::android::hardware::hidl_memory; 39 using ::android::hardware::hidl_string; 40 using ::android::hardware::hidl_vec; 41 using ::android::hardware::Return; 42 using ::android::hardware::Void; 43 using ::android::sp; 44 45 typedef ::android::hardware::graphics::bufferqueue::V1_0:: 46 IGraphicBufferProducer HGraphicBufferProducer; 47 typedef ::android::hardware::graphics::bufferqueue::V1_0:: 48 IProducerListener HProducerListener; 49 50 typedef ::android::IGraphicBufferProducer BGraphicBufferProducer; 51 typedef ::android::IProducerListener BProducerListener; 52 using ::android::BnGraphicBufferProducer; 53 54 #ifndef LOG 55 struct LOG_dummy { 56 template <typename T> 57 LOG_dummy& operator<< (const T&) { return *this; } 58 }; 59 60 #define LOG(x) LOG_dummy() 61 #endif 62 63 // Instantiate only if HGraphicBufferProducer is base of BASE. 64 template <typename BASE, 65 typename = typename std::enable_if<std::is_base_of<HGraphicBufferProducer, BASE>::value>::type> 66 struct TWGraphicBufferProducer : public BASE { TWGraphicBufferProducerTWGraphicBufferProducer67 TWGraphicBufferProducer(sp<BGraphicBufferProducer> const& base) : mBase(base) {} requestBufferTWGraphicBufferProducer68 Return<void> requestBuffer(int32_t slot, HGraphicBufferProducer::requestBuffer_cb _hidl_cb) override { 69 sp<GraphicBuffer> buf; 70 status_t status = mBase->requestBuffer(slot, &buf); 71 AnwBuffer anwBuffer{}; 72 if (buf != nullptr) { 73 ::android::conversion::wrapAs(&anwBuffer, *buf); 74 } 75 _hidl_cb(static_cast<int32_t>(status), anwBuffer); 76 return Void(); 77 } 78 setMaxDequeuedBufferCountTWGraphicBufferProducer79 Return<int32_t> setMaxDequeuedBufferCount(int32_t maxDequeuedBuffers) override { 80 return static_cast<int32_t>(mBase->setMaxDequeuedBufferCount( 81 static_cast<int>(maxDequeuedBuffers))); 82 } 83 setAsyncModeTWGraphicBufferProducer84 Return<int32_t> setAsyncMode(bool async) override { 85 return static_cast<int32_t>(mBase->setAsyncMode(async)); 86 } 87 dequeueBufferTWGraphicBufferProducer88 Return<void> dequeueBuffer( 89 uint32_t width, uint32_t height, 90 ::android::hardware::graphics::common::V1_0::PixelFormat format, uint32_t usage, 91 bool getFrameTimestamps, HGraphicBufferProducer::dequeueBuffer_cb _hidl_cb) override { 92 int slot{}; 93 sp<Fence> fence; 94 ::android::FrameEventHistoryDelta outTimestamps; 95 status_t status = mBase->dequeueBuffer( 96 &slot, &fence, width, height, 97 static_cast<::android::PixelFormat>(format), usage, nullptr, 98 getFrameTimestamps ? &outTimestamps : nullptr); 99 hidl_handle tFence{}; 100 HGraphicBufferProducer::FrameEventHistoryDelta tOutTimestamps{}; 101 102 native_handle_t* nh = nullptr; 103 if ((fence == nullptr) || !::android::conversion::wrapAs(&tFence, &nh, *fence)) { 104 LOG(ERROR) << "TWGraphicBufferProducer::dequeueBuffer - " 105 "Invalid output fence"; 106 _hidl_cb(static_cast<int32_t>(status), 107 static_cast<int32_t>(slot), 108 tFence, 109 tOutTimestamps); 110 return Void(); 111 } 112 std::vector<std::vector<native_handle_t*> > nhAA; 113 if (getFrameTimestamps && !::android::conversion::wrapAs(&tOutTimestamps, &nhAA, outTimestamps)) { 114 LOG(ERROR) << "TWGraphicBufferProducer::dequeueBuffer - " 115 "Invalid output timestamps"; 116 _hidl_cb(static_cast<int32_t>(status), 117 static_cast<int32_t>(slot), 118 tFence, 119 tOutTimestamps); 120 native_handle_delete(nh); 121 return Void(); 122 } 123 124 _hidl_cb(static_cast<int32_t>(status), 125 static_cast<int32_t>(slot), 126 tFence, 127 tOutTimestamps); 128 native_handle_delete(nh); 129 if (getFrameTimestamps) { 130 for (auto& nhA : nhAA) { 131 for (auto& handle : nhA) { 132 native_handle_delete(handle); 133 } 134 } 135 } 136 return Void(); 137 } 138 detachBufferTWGraphicBufferProducer139 Return<int32_t> detachBuffer(int32_t slot) override { 140 return static_cast<int32_t>(mBase->detachBuffer(slot)); 141 } 142 detachNextBufferTWGraphicBufferProducer143 Return<void> detachNextBuffer(HGraphicBufferProducer::detachNextBuffer_cb _hidl_cb) override { 144 sp<GraphicBuffer> outBuffer; 145 sp<Fence> outFence; 146 status_t status = mBase->detachNextBuffer(&outBuffer, &outFence); 147 AnwBuffer tBuffer{}; 148 hidl_handle tFence{}; 149 150 if (outBuffer == nullptr) { 151 LOG(ERROR) << "TWGraphicBufferProducer::detachNextBuffer - " 152 "Invalid output buffer"; 153 _hidl_cb(static_cast<int32_t>(status), tBuffer, tFence); 154 return Void(); 155 } 156 ::android::conversion::wrapAs(&tBuffer, *outBuffer); 157 native_handle_t* nh = nullptr; 158 if ((outFence != nullptr) && !::android::conversion::wrapAs(&tFence, &nh, *outFence)) { 159 LOG(ERROR) << "TWGraphicBufferProducer::detachNextBuffer - " 160 "Invalid output fence"; 161 _hidl_cb(static_cast<int32_t>(status), tBuffer, tFence); 162 return Void(); 163 } 164 165 _hidl_cb(static_cast<int32_t>(status), tBuffer, tFence); 166 native_handle_delete(nh); 167 return Void(); 168 } 169 attachBufferTWGraphicBufferProducer170 Return<void> attachBuffer(const AnwBuffer& buffer, HGraphicBufferProducer::attachBuffer_cb _hidl_cb) override { 171 int outSlot; 172 sp<GraphicBuffer> lBuffer = new GraphicBuffer(); 173 if (!::android::conversion::convertTo(lBuffer.get(), buffer)) { 174 LOG(ERROR) << "TWGraphicBufferProducer::attachBuffer - " 175 "Invalid input native window buffer"; 176 _hidl_cb(static_cast<int32_t>(BAD_VALUE), -1); 177 return Void(); 178 } 179 status_t status = mBase->attachBuffer(&outSlot, lBuffer); 180 181 _hidl_cb(static_cast<int32_t>(status), static_cast<int32_t>(outSlot)); 182 return Void(); 183 } 184 queueBufferTWGraphicBufferProducer185 Return<void> queueBuffer( 186 int32_t slot, const HGraphicBufferProducer::QueueBufferInput& input, 187 HGraphicBufferProducer::queueBuffer_cb _hidl_cb) override { 188 HGraphicBufferProducer::QueueBufferOutput tOutput{}; 189 BGraphicBufferProducer::QueueBufferInput lInput( 190 0, false, HAL_DATASPACE_UNKNOWN, 191 ::android::Rect(0, 0, 1, 1), 192 NATIVE_WINDOW_SCALING_MODE_FREEZE, 193 0, ::android::Fence::NO_FENCE); 194 if (!::android::conversion::convertTo(&lInput, input)) { 195 LOG(ERROR) << "TWGraphicBufferProducer::queueBuffer - " 196 "Invalid input"; 197 _hidl_cb(static_cast<int32_t>(BAD_VALUE), tOutput); 198 return Void(); 199 } 200 BGraphicBufferProducer::QueueBufferOutput lOutput; 201 status_t status = mBase->queueBuffer( 202 static_cast<int>(slot), lInput, &lOutput); 203 204 std::vector<std::vector<native_handle_t*> > nhAA; 205 if (!::android::conversion::wrapAs(&tOutput, &nhAA, lOutput)) { 206 LOG(ERROR) << "TWGraphicBufferProducer::queueBuffer - " 207 "Invalid output"; 208 _hidl_cb(static_cast<int32_t>(BAD_VALUE), tOutput); 209 return Void(); 210 } 211 212 _hidl_cb(static_cast<int32_t>(status), tOutput); 213 for (auto& nhA : nhAA) { 214 for (auto& nh : nhA) { 215 native_handle_delete(nh); 216 } 217 } 218 return Void(); 219 } 220 cancelBufferTWGraphicBufferProducer221 Return<int32_t> cancelBuffer(int32_t slot, const hidl_handle& fence) override { 222 sp<Fence> lFence = new Fence(); 223 if (!::android::conversion::convertTo(lFence.get(), fence)) { 224 LOG(ERROR) << "TWGraphicBufferProducer::cancelBuffer - " 225 "Invalid input fence"; 226 return static_cast<int32_t>(BAD_VALUE); 227 } 228 return static_cast<int32_t>(mBase->cancelBuffer(static_cast<int>(slot), lFence)); 229 } 230 queryTWGraphicBufferProducer231 Return<void> query(int32_t what, HGraphicBufferProducer::query_cb _hidl_cb) override { 232 int lValue; 233 int lReturn = mBase->query(static_cast<int>(what), &lValue); 234 _hidl_cb(static_cast<int32_t>(lReturn), static_cast<int32_t>(lValue)); 235 return Void(); 236 } 237 connectTWGraphicBufferProducer238 Return<void> connect(const sp<HProducerListener>& listener, 239 int32_t api, bool producerControlledByApp, 240 HGraphicBufferProducer::connect_cb _hidl_cb) override { 241 sp<BProducerListener> lListener = listener == nullptr ? 242 nullptr : new LWProducerListener(listener); 243 BGraphicBufferProducer::QueueBufferOutput lOutput; 244 status_t status = mBase->connect(lListener, 245 static_cast<int>(api), 246 producerControlledByApp, 247 &lOutput); 248 249 HGraphicBufferProducer::QueueBufferOutput tOutput{}; 250 std::vector<std::vector<native_handle_t*> > nhAA; 251 if (!::android::conversion::wrapAs(&tOutput, &nhAA, lOutput)) { 252 LOG(ERROR) << "TWGraphicBufferProducer::connect - " 253 "Invalid output"; 254 _hidl_cb(static_cast<int32_t>(status), tOutput); 255 return Void(); 256 } 257 258 _hidl_cb(static_cast<int32_t>(status), tOutput); 259 for (auto& nhA : nhAA) { 260 for (auto& nh : nhA) { 261 native_handle_delete(nh); 262 } 263 } 264 return Void(); 265 } 266 disconnectTWGraphicBufferProducer267 Return<int32_t> disconnect( 268 int32_t api, 269 HGraphicBufferProducer::DisconnectMode mode) override { 270 return static_cast<int32_t>(mBase->disconnect( 271 static_cast<int>(api), 272 ::android::conversion::toGuiDisconnectMode(mode))); 273 } 274 setSidebandStreamTWGraphicBufferProducer275 Return<int32_t> setSidebandStream(const hidl_handle& stream) override { 276 return static_cast<int32_t>(mBase->setSidebandStream(NativeHandle::create( 277 stream ? native_handle_clone(stream) : NULL, true))); 278 } 279 allocateBuffersTWGraphicBufferProducer280 Return<void> allocateBuffers( 281 uint32_t width, uint32_t height, 282 ::android::hardware::graphics::common::V1_0::PixelFormat format, 283 uint32_t usage) override { 284 mBase->allocateBuffers( 285 width, height, 286 static_cast<::android::PixelFormat>(format), 287 usage); 288 return Void(); 289 } 290 allowAllocationTWGraphicBufferProducer291 Return<int32_t> allowAllocation(bool allow) override { 292 return static_cast<int32_t>(mBase->allowAllocation(allow)); 293 } 294 setGenerationNumberTWGraphicBufferProducer295 Return<int32_t> setGenerationNumber(uint32_t generationNumber) override { 296 return static_cast<int32_t>(mBase->setGenerationNumber(generationNumber)); 297 } 298 getConsumerNameTWGraphicBufferProducer299 Return<void> getConsumerName(HGraphicBufferProducer::getConsumerName_cb _hidl_cb) override { 300 _hidl_cb(mBase->getConsumerName().string()); 301 return Void(); 302 } 303 setSharedBufferModeTWGraphicBufferProducer304 Return<int32_t> setSharedBufferMode(bool sharedBufferMode) override { 305 return static_cast<int32_t>(mBase->setSharedBufferMode(sharedBufferMode)); 306 } 307 setAutoRefreshTWGraphicBufferProducer308 Return<int32_t> setAutoRefresh(bool autoRefresh) override { 309 return static_cast<int32_t>(mBase->setAutoRefresh(autoRefresh)); 310 } 311 setDequeueTimeoutTWGraphicBufferProducer312 Return<int32_t> setDequeueTimeout(int64_t timeoutNs) override { 313 return static_cast<int32_t>(mBase->setDequeueTimeout(timeoutNs)); 314 } 315 getLastQueuedBufferTWGraphicBufferProducer316 Return<void> getLastQueuedBuffer(HGraphicBufferProducer::getLastQueuedBuffer_cb _hidl_cb) override { 317 sp<GraphicBuffer> lOutBuffer = new GraphicBuffer(); 318 sp<Fence> lOutFence = new Fence(); 319 float lOutTransformMatrix[16]; 320 status_t status = mBase->getLastQueuedBuffer( 321 &lOutBuffer, &lOutFence, lOutTransformMatrix); 322 323 AnwBuffer tOutBuffer{}; 324 if (lOutBuffer != nullptr) { 325 ::android::conversion::wrapAs(&tOutBuffer, *lOutBuffer); 326 } 327 hidl_handle tOutFence{}; 328 native_handle_t* nh = nullptr; 329 if ((lOutFence == nullptr) || !::android::conversion::wrapAs(&tOutFence, &nh, *lOutFence)) { 330 LOG(ERROR) << "TWGraphicBufferProducer::getLastQueuedBuffer - " 331 "Invalid output fence"; 332 _hidl_cb(static_cast<int32_t>(status), 333 tOutBuffer, 334 tOutFence, 335 hidl_array<float, 16>()); 336 return Void(); 337 } 338 hidl_array<float, 16> tOutTransformMatrix(lOutTransformMatrix); 339 340 _hidl_cb(static_cast<int32_t>(status), tOutBuffer, tOutFence, tOutTransformMatrix); 341 native_handle_delete(nh); 342 return Void(); 343 } 344 getFrameTimestampsTWGraphicBufferProducer345 Return<void> getFrameTimestamps(HGraphicBufferProducer::getFrameTimestamps_cb _hidl_cb) override { 346 ::android::FrameEventHistoryDelta lDelta; 347 mBase->getFrameTimestamps(&lDelta); 348 349 HGraphicBufferProducer::FrameEventHistoryDelta tDelta{}; 350 std::vector<std::vector<native_handle_t*> > nhAA; 351 if (!::android::conversion::wrapAs(&tDelta, &nhAA, lDelta)) { 352 LOG(ERROR) << "TWGraphicBufferProducer::getFrameTimestamps - " 353 "Invalid output frame timestamps"; 354 _hidl_cb(tDelta); 355 return Void(); 356 } 357 358 _hidl_cb(tDelta); 359 for (auto& nhA : nhAA) { 360 for (auto& nh : nhA) { 361 native_handle_delete(nh); 362 } 363 } 364 return Void(); 365 } 366 getUniqueIdTWGraphicBufferProducer367 Return<void> getUniqueId(HGraphicBufferProducer::getUniqueId_cb _hidl_cb) override { 368 uint64_t outId{}; 369 status_t status = mBase->getUniqueId(&outId); 370 _hidl_cb(static_cast<int32_t>(status), outId); 371 return Void(); 372 } 373 374 private: 375 sp<BGraphicBufferProducer> mBase; 376 }; 377 378 } // namespace android 379 380 #endif // ANDROID_HARDWARE_GRAPHICS_BUFFERQUEUE_V1_0_WGRAPHICBUFFERPRODUCER_H_ 381