1 /* 2 * Copyright (C) 2008 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_CAMERA_HARDWARE_INTERFACE_H 18 #define ANDROID_HARDWARE_CAMERA_HARDWARE_INTERFACE_H 19 20 #include <binder/IMemory.h> 21 #include <binder/MemoryBase.h> 22 #include <binder/MemoryHeapBase.h> 23 #include <utils/RefBase.h> 24 #include <ui/GraphicBuffer.h> 25 #include <camera/Camera.h> 26 #include <camera/CameraParameters.h> 27 #include <system/window.h> 28 #include <hardware/camera.h> 29 30 namespace android { 31 32 typedef void (*notify_callback)(int32_t msgType, 33 int32_t ext1, 34 int32_t ext2, 35 void* user); 36 37 typedef void (*data_callback)(int32_t msgType, 38 const sp<IMemory> &dataPtr, 39 camera_frame_metadata_t *metadata, 40 void* user); 41 42 typedef void (*data_callback_timestamp)(nsecs_t timestamp, 43 int32_t msgType, 44 const sp<IMemory> &dataPtr, 45 void *user); 46 47 /** 48 * CameraHardwareInterface.h defines the interface to the 49 * camera hardware abstraction layer, used for setting and getting 50 * parameters, live previewing, and taking pictures. It is used for 51 * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only. 52 * 53 * It is a referenced counted interface with RefBase as its base class. 54 * CameraService calls openCameraHardware() to retrieve a strong pointer to the 55 * instance of this interface and may be called multiple times. The 56 * following steps describe a typical sequence: 57 * 58 * -# After CameraService calls openCameraHardware(), getParameters() and 59 * setParameters() are used to initialize the camera instance. 60 * -# startPreview() is called. 61 * 62 * Prior to taking a picture, CameraService often calls autofocus(). When auto 63 * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification, 64 * which informs the application whether focusing was successful. The camera instance 65 * only sends this message once and it is up to the application to call autoFocus() 66 * again if refocusing is desired. 67 * 68 * CameraService calls takePicture() to request the camera instance take a 69 * picture. At this point, if a shutter, postview, raw, and/or compressed 70 * callback is desired, the corresponding message must be enabled. Any memory 71 * provided in a data callback must be copied if it's needed after returning. 72 */ 73 74 class CameraHardwareInterface : public virtual RefBase { 75 public: CameraHardwareInterface(const char * name)76 CameraHardwareInterface(const char *name): 77 mDevice(nullptr), 78 mName(name), 79 mPreviewScalingMode(NOT_SET), 80 mPreviewTransform(NOT_SET), 81 mPreviewWidth(NOT_SET), 82 mPreviewHeight(NOT_SET), 83 mPreviewFormat(NOT_SET), 84 mPreviewUsage(0), 85 mPreviewSwapInterval(NOT_SET), 86 mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET} 87 { 88 } 89 ~CameraHardwareInterface()90 ~CameraHardwareInterface() 91 { 92 ALOGI("Destroying camera %s", mName.string()); 93 if(mDevice) { 94 int rc = mDevice->common.close(&mDevice->common); 95 if (rc != OK) 96 ALOGE("Could not close camera %s: %d", mName.string(), rc); 97 } 98 } 99 initialize(CameraModule * module)100 status_t initialize(CameraModule *module) 101 { 102 ALOGI("Opening camera %s", mName.string()); 103 camera_info info; 104 status_t res = module->getCameraInfo(atoi(mName.string()), &info); 105 if (res != OK) { 106 return res; 107 } 108 109 int rc = OK; 110 if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 && 111 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) { 112 // Open higher version camera device as HAL1.0 device. 113 rc = module->openLegacy(mName.string(), 114 CAMERA_DEVICE_API_VERSION_1_0, 115 (hw_device_t **)&mDevice); 116 } else { 117 rc = module->open(mName.string(), (hw_device_t **)&mDevice); 118 } 119 if (rc != OK) { 120 ALOGE("Could not open camera %s: %d", mName.string(), rc); 121 return rc; 122 } 123 initHalPreviewWindow(); 124 return rc; 125 } 126 127 /** Set the ANativeWindow to which preview frames are sent */ setPreviewWindow(const sp<ANativeWindow> & buf)128 status_t setPreviewWindow(const sp<ANativeWindow>& buf) 129 { 130 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get()); 131 if (mDevice->ops->set_preview_window) { 132 mPreviewWindow = buf; 133 if (buf != nullptr) { 134 if (mPreviewScalingMode != NOT_SET) { 135 setPreviewScalingMode(mPreviewScalingMode); 136 } 137 if (mPreviewTransform != NOT_SET) { 138 setPreviewTransform(mPreviewTransform); 139 } 140 } 141 mHalPreviewWindow.user = this; 142 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__, 143 &mHalPreviewWindow, mHalPreviewWindow.user); 144 return mDevice->ops->set_preview_window(mDevice, 145 buf.get() ? &mHalPreviewWindow.nw : 0); 146 } 147 return INVALID_OPERATION; 148 } 149 setPreviewScalingMode(int scalingMode)150 status_t setPreviewScalingMode(int scalingMode) 151 { 152 int rc = OK; 153 mPreviewScalingMode = scalingMode; 154 if (mPreviewWindow != nullptr) { 155 rc = native_window_set_scaling_mode(mPreviewWindow.get(), 156 scalingMode); 157 } 158 return rc; 159 } 160 setPreviewTransform(int transform)161 status_t setPreviewTransform(int transform) { 162 int rc = OK; 163 mPreviewTransform = transform; 164 if (mPreviewWindow != nullptr) { 165 rc = native_window_set_buffers_transform(mPreviewWindow.get(), 166 mPreviewTransform); 167 } 168 return rc; 169 } 170 171 /** Set the notification and data callbacks */ setCallbacks(notify_callback notify_cb,data_callback data_cb,data_callback_timestamp data_cb_timestamp,void * user)172 void setCallbacks(notify_callback notify_cb, 173 data_callback data_cb, 174 data_callback_timestamp data_cb_timestamp, 175 void* user) 176 { 177 mNotifyCb = notify_cb; 178 mDataCb = data_cb; 179 mDataCbTimestamp = data_cb_timestamp; 180 mCbUser = user; 181 182 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 183 184 if (mDevice->ops->set_callbacks) { 185 mDevice->ops->set_callbacks(mDevice, 186 __notify_cb, 187 __data_cb, 188 __data_cb_timestamp, 189 __get_memory, 190 this); 191 } 192 } 193 194 /** 195 * The following three functions all take a msgtype, 196 * which is a bitmask of the messages defined in 197 * include/ui/Camera.h 198 */ 199 200 /** 201 * Enable a message, or set of messages. 202 */ enableMsgType(int32_t msgType)203 void enableMsgType(int32_t msgType) 204 { 205 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 206 if (mDevice->ops->enable_msg_type) 207 mDevice->ops->enable_msg_type(mDevice, msgType); 208 } 209 210 /** 211 * Disable a message, or a set of messages. 212 * 213 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal 214 * should not rely on its client to call releaseRecordingFrame() to release 215 * video recording frames sent out by the cameral hal before and after the 216 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not 217 * modify/access any video recording frame after calling 218 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). 219 */ disableMsgType(int32_t msgType)220 void disableMsgType(int32_t msgType) 221 { 222 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 223 if (mDevice->ops->disable_msg_type) 224 mDevice->ops->disable_msg_type(mDevice, msgType); 225 } 226 227 /** 228 * Query whether a message, or a set of messages, is enabled. 229 * Note that this is operates as an AND, if any of the messages 230 * queried are off, this will return false. 231 */ msgTypeEnabled(int32_t msgType)232 int msgTypeEnabled(int32_t msgType) 233 { 234 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 235 if (mDevice->ops->msg_type_enabled) 236 return mDevice->ops->msg_type_enabled(mDevice, msgType); 237 return false; 238 } 239 240 /** 241 * Start preview mode. 242 */ startPreview()243 status_t startPreview() 244 { 245 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 246 if (mDevice->ops->start_preview) 247 return mDevice->ops->start_preview(mDevice); 248 return INVALID_OPERATION; 249 } 250 251 /** 252 * Stop a previously started preview. 253 */ stopPreview()254 void stopPreview() 255 { 256 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 257 if (mDevice->ops->stop_preview) 258 mDevice->ops->stop_preview(mDevice); 259 } 260 261 /** 262 * Returns true if preview is enabled. 263 */ previewEnabled()264 int previewEnabled() 265 { 266 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 267 if (mDevice->ops->preview_enabled) 268 return mDevice->ops->preview_enabled(mDevice); 269 return false; 270 } 271 272 /** 273 * Request the camera hal to store meta data or real YUV data in 274 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a 275 * recording session. If it is not called, the default camera 276 * hal behavior is to store real YUV data in the video buffers. 277 * 278 * This method should be called before startRecording() in order 279 * to be effective. 280 * 281 * If meta data is stored in the video buffers, it is up to the 282 * receiver of the video buffers to interpret the contents and 283 * to find the actual frame data with the help of the meta data 284 * in the buffer. How this is done is outside of the scope of 285 * this method. 286 * 287 * Some camera hal may not support storing meta data in the video 288 * buffers, but all camera hal should support storing real YUV data 289 * in the video buffers. If the camera hal does not support storing 290 * the meta data in the video buffers when it is requested to do 291 * do, INVALID_OPERATION must be returned. It is very useful for 292 * the camera hal to pass meta data rather than the actual frame 293 * data directly to the video encoder, since the amount of the 294 * uncompressed frame data can be very large if video size is large. 295 * 296 * @param enable if true to instruct the camera hal to store 297 * meta data in the video buffers; false to instruct 298 * the camera hal to store real YUV data in the video 299 * buffers. 300 * 301 * @return OK on success. 302 */ 303 storeMetaDataInBuffers(int enable)304 status_t storeMetaDataInBuffers(int enable) 305 { 306 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 307 if (mDevice->ops->store_meta_data_in_buffers) 308 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable); 309 return enable ? INVALID_OPERATION: OK; 310 } 311 312 /** 313 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME 314 * message is sent with the corresponding frame. Every record frame must be released 315 * by a cameral hal client via releaseRecordingFrame() before the client calls 316 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls 317 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility 318 * to manage the life-cycle of the video recording frames, and the client must 319 * not modify/access any video recording frames. 320 */ startRecording()321 status_t startRecording() 322 { 323 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 324 if (mDevice->ops->start_recording) 325 return mDevice->ops->start_recording(mDevice); 326 return INVALID_OPERATION; 327 } 328 329 /** 330 * Stop a previously started recording. 331 */ stopRecording()332 void stopRecording() 333 { 334 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 335 if (mDevice->ops->stop_recording) 336 mDevice->ops->stop_recording(mDevice); 337 } 338 339 /** 340 * Returns true if recording is enabled. 341 */ recordingEnabled()342 int recordingEnabled() 343 { 344 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 345 if (mDevice->ops->recording_enabled) 346 return mDevice->ops->recording_enabled(mDevice); 347 return false; 348 } 349 350 /** 351 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME. 352 * 353 * It is camera hal client's responsibility to release video recording 354 * frames sent out by the camera hal before the camera hal receives 355 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives 356 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's 357 * responsibility of managing the life-cycle of the video recording 358 * frames. 359 */ releaseRecordingFrame(const sp<IMemory> & mem)360 void releaseRecordingFrame(const sp<IMemory>& mem) 361 { 362 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 363 if (mDevice->ops->release_recording_frame) { 364 ssize_t offset; 365 size_t size; 366 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); 367 void *data = ((uint8_t *)heap->base()) + offset; 368 return mDevice->ops->release_recording_frame(mDevice, data); 369 } 370 } 371 372 /** 373 * Start auto focus, the notification callback routine is called 374 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus() 375 * will be called again if another auto focus is needed. 376 */ autoFocus()377 status_t autoFocus() 378 { 379 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 380 if (mDevice->ops->auto_focus) 381 return mDevice->ops->auto_focus(mDevice); 382 return INVALID_OPERATION; 383 } 384 385 /** 386 * Cancels auto-focus function. If the auto-focus is still in progress, 387 * this function will cancel it. Whether the auto-focus is in progress 388 * or not, this function will return the focus position to the default. 389 * If the camera does not support auto-focus, this is a no-op. 390 */ cancelAutoFocus()391 status_t cancelAutoFocus() 392 { 393 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 394 if (mDevice->ops->cancel_auto_focus) 395 return mDevice->ops->cancel_auto_focus(mDevice); 396 return INVALID_OPERATION; 397 } 398 399 /** 400 * Take a picture. 401 */ takePicture()402 status_t takePicture() 403 { 404 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 405 if (mDevice->ops->take_picture) 406 return mDevice->ops->take_picture(mDevice); 407 return INVALID_OPERATION; 408 } 409 410 /** 411 * Cancel a picture that was started with takePicture. Calling this 412 * method when no picture is being taken is a no-op. 413 */ cancelPicture()414 status_t cancelPicture() 415 { 416 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 417 if (mDevice->ops->cancel_picture) 418 return mDevice->ops->cancel_picture(mDevice); 419 return INVALID_OPERATION; 420 } 421 422 /** 423 * Set the camera parameters. This returns BAD_VALUE if any parameter is 424 * invalid or not supported. */ setParameters(const CameraParameters & params)425 status_t setParameters(const CameraParameters ¶ms) 426 { 427 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 428 if (mDevice->ops->set_parameters) 429 return mDevice->ops->set_parameters(mDevice, 430 params.flatten().string()); 431 return INVALID_OPERATION; 432 } 433 434 /** Return the camera parameters. */ getParameters()435 CameraParameters getParameters() const 436 { 437 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 438 CameraParameters parms; 439 if (mDevice->ops->get_parameters) { 440 char *temp = mDevice->ops->get_parameters(mDevice); 441 String8 str_parms(temp); 442 if (mDevice->ops->put_parameters) 443 mDevice->ops->put_parameters(mDevice, temp); 444 else 445 free(temp); 446 parms.unflatten(str_parms); 447 } 448 return parms; 449 } 450 451 /** 452 * Send command to camera driver. 453 */ sendCommand(int32_t cmd,int32_t arg1,int32_t arg2)454 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) 455 { 456 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 457 if (mDevice->ops->send_command) 458 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2); 459 return INVALID_OPERATION; 460 } 461 462 /** 463 * Release the hardware resources owned by this object. Note that this is 464 * *not* done in the destructor. 465 */ release()466 void release() { 467 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 468 if (mDevice->ops->release) 469 mDevice->ops->release(mDevice); 470 } 471 472 /** 473 * Dump state of the camera hardware 474 */ dump(int fd,const Vector<String16> &)475 status_t dump(int fd, const Vector<String16>& /*args*/) const 476 { 477 ALOGV("%s(%s)", __FUNCTION__, mName.string()); 478 if (mDevice->ops->dump) 479 return mDevice->ops->dump(mDevice, fd); 480 return OK; // It's fine if the HAL doesn't implement dump() 481 } 482 483 private: 484 camera_device_t *mDevice; 485 String8 mName; 486 __notify_cb(int32_t msg_type,int32_t ext1,int32_t ext2,void * user)487 static void __notify_cb(int32_t msg_type, int32_t ext1, 488 int32_t ext2, void *user) 489 { 490 ALOGV("%s", __FUNCTION__); 491 CameraHardwareInterface *__this = 492 static_cast<CameraHardwareInterface *>(user); 493 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser); 494 } 495 __data_cb(int32_t msg_type,const camera_memory_t * data,unsigned int index,camera_frame_metadata_t * metadata,void * user)496 static void __data_cb(int32_t msg_type, 497 const camera_memory_t *data, unsigned int index, 498 camera_frame_metadata_t *metadata, 499 void *user) 500 { 501 ALOGV("%s", __FUNCTION__); 502 CameraHardwareInterface *__this = 503 static_cast<CameraHardwareInterface *>(user); 504 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle)); 505 if (index >= mem->mNumBufs) { 506 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, 507 index, mem->mNumBufs); 508 return; 509 } 510 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser); 511 } 512 __data_cb_timestamp(nsecs_t timestamp,int32_t msg_type,const camera_memory_t * data,unsigned index,void * user)513 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type, 514 const camera_memory_t *data, unsigned index, 515 void *user) 516 { 517 ALOGV("%s", __FUNCTION__); 518 CameraHardwareInterface *__this = 519 static_cast<CameraHardwareInterface *>(user); 520 // Start refcounting the heap object from here on. When the clients 521 // drop all references, it will be destroyed (as well as the enclosed 522 // MemoryHeapBase. 523 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle)); 524 if (index >= mem->mNumBufs) { 525 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, 526 index, mem->mNumBufs); 527 return; 528 } 529 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser); 530 } 531 532 // This is a utility class that combines a MemoryHeapBase and a MemoryBase 533 // in one. Since we tend to use them in a one-to-one relationship, this is 534 // handy. 535 536 class CameraHeapMemory : public RefBase { 537 public: 538 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) : mBufSize(buf_size)539 mBufSize(buf_size), 540 mNumBufs(num_buffers) 541 { 542 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers); 543 commonInitialization(); 544 } 545 546 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) : mBufSize(buf_size)547 mBufSize(buf_size), 548 mNumBufs(num_buffers) 549 { 550 mHeap = new MemoryHeapBase(buf_size * num_buffers); 551 commonInitialization(); 552 } 553 commonInitialization()554 void commonInitialization() 555 { 556 handle.data = mHeap->base(); 557 handle.size = mBufSize * mNumBufs; 558 handle.handle = this; 559 560 mBuffers = new sp<MemoryBase>[mNumBufs]; 561 for (uint_t i = 0; i < mNumBufs; i++) 562 mBuffers[i] = new MemoryBase(mHeap, 563 i * mBufSize, 564 mBufSize); 565 566 handle.release = __put_memory; 567 } 568 ~CameraHeapMemory()569 virtual ~CameraHeapMemory() 570 { 571 delete [] mBuffers; 572 } 573 574 size_t mBufSize; 575 uint_t mNumBufs; 576 sp<MemoryHeapBase> mHeap; 577 sp<MemoryBase> *mBuffers; 578 579 camera_memory_t handle; 580 }; 581 __get_memory(int fd,size_t buf_size,uint_t num_bufs,void * user)582 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs, 583 void *user __attribute__((unused))) 584 { 585 CameraHeapMemory *mem; 586 if (fd < 0) 587 mem = new CameraHeapMemory(buf_size, num_bufs); 588 else 589 mem = new CameraHeapMemory(fd, buf_size, num_bufs); 590 mem->incStrong(mem); 591 return &mem->handle; 592 } 593 __put_memory(camera_memory_t * data)594 static void __put_memory(camera_memory_t *data) 595 { 596 if (!data) 597 return; 598 599 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle); 600 mem->decStrong(mem); 601 } 602 __to_anw(void * user)603 static ANativeWindow *__to_anw(void *user) 604 { 605 CameraHardwareInterface *__this = 606 reinterpret_cast<CameraHardwareInterface *>(user); 607 return __this->mPreviewWindow.get(); 608 } 609 #define anw(n) __to_anw(((struct camera_preview_window *)n)->user) 610 #define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\ 611 ((struct camera_preview_window *)n)->user) 612 __dequeue_buffer(struct preview_stream_ops * w,buffer_handle_t ** buffer,int * stride)613 static int __dequeue_buffer(struct preview_stream_ops* w, 614 buffer_handle_t** buffer, int *stride) 615 { 616 int rc; 617 ANativeWindow *a = anw(w); 618 ANativeWindowBuffer* anb; 619 rc = native_window_dequeue_buffer_and_wait(a, &anb); 620 if (!rc) { 621 *buffer = &anb->handle; 622 *stride = anb->stride; 623 } 624 return rc; 625 } 626 627 #ifndef container_of 628 #define container_of(ptr, type, member) ({ \ 629 const __typeof__(((type *) 0)->member) *__mptr = (ptr); \ 630 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); }) 631 #endif 632 __lock_buffer(struct preview_stream_ops * w,buffer_handle_t *)633 static int __lock_buffer(struct preview_stream_ops* w, 634 buffer_handle_t* /*buffer*/) 635 { 636 ANativeWindow *a = anw(w); 637 (void)a; 638 return 0; 639 } 640 __enqueue_buffer(struct preview_stream_ops * w,buffer_handle_t * buffer)641 static int __enqueue_buffer(struct preview_stream_ops* w, 642 buffer_handle_t* buffer) 643 { 644 ANativeWindow *a = anw(w); 645 return a->queueBuffer(a, 646 container_of(buffer, ANativeWindowBuffer, handle), -1); 647 } 648 __cancel_buffer(struct preview_stream_ops * w,buffer_handle_t * buffer)649 static int __cancel_buffer(struct preview_stream_ops* w, 650 buffer_handle_t* buffer) 651 { 652 ANativeWindow *a = anw(w); 653 return a->cancelBuffer(a, 654 container_of(buffer, ANativeWindowBuffer, handle), -1); 655 } 656 __set_buffer_count(struct preview_stream_ops * w,int count)657 static int __set_buffer_count(struct preview_stream_ops* w, int count) 658 { 659 ANativeWindow *a = anw(w); 660 661 if (a != nullptr) { 662 // Workaround for b/27039775 663 // Previously, setting the buffer count would reset the buffer 664 // queue's flag that allows for all buffers to be dequeued on the 665 // producer side, instead of just the producer's declared max count, 666 // if no filled buffers have yet been queued by the producer. This 667 // reset no longer happens, but some HALs depend on this behavior, 668 // so it needs to be maintained for HAL backwards compatibility. 669 // Simulate the prior behavior by disconnecting/reconnecting to the 670 // window and setting the values again. This has the drawback of 671 // actually causing memory reallocation, which may not have happened 672 // in the past. 673 CameraHardwareInterface *hw = hwi(w); 674 native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA); 675 native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA); 676 if (hw->mPreviewScalingMode != NOT_SET) { 677 native_window_set_scaling_mode(a, hw->mPreviewScalingMode); 678 } 679 if (hw->mPreviewTransform != NOT_SET) { 680 native_window_set_buffers_transform(a, hw->mPreviewTransform); 681 } 682 if (hw->mPreviewWidth != NOT_SET) { 683 native_window_set_buffers_dimensions(a, 684 hw->mPreviewWidth, hw->mPreviewHeight); 685 native_window_set_buffers_format(a, hw->mPreviewFormat); 686 } 687 if (hw->mPreviewUsage != 0) { 688 native_window_set_usage(a, hw->mPreviewUsage); 689 } 690 if (hw->mPreviewSwapInterval != NOT_SET) { 691 a->setSwapInterval(a, hw->mPreviewSwapInterval); 692 } 693 if (hw->mPreviewCrop.left != NOT_SET) { 694 native_window_set_crop(a, &(hw->mPreviewCrop)); 695 } 696 } 697 698 return native_window_set_buffer_count(a, count); 699 } 700 __set_buffers_geometry(struct preview_stream_ops * w,int width,int height,int format)701 static int __set_buffers_geometry(struct preview_stream_ops* w, 702 int width, int height, int format) 703 { 704 int rc; 705 ANativeWindow *a = anw(w); 706 CameraHardwareInterface *hw = hwi(w); 707 hw->mPreviewWidth = width; 708 hw->mPreviewHeight = height; 709 hw->mPreviewFormat = format; 710 rc = native_window_set_buffers_dimensions(a, width, height); 711 if (!rc) { 712 rc = native_window_set_buffers_format(a, format); 713 } 714 return rc; 715 } 716 __set_crop(struct preview_stream_ops * w,int left,int top,int right,int bottom)717 static int __set_crop(struct preview_stream_ops *w, 718 int left, int top, int right, int bottom) 719 { 720 ANativeWindow *a = anw(w); 721 CameraHardwareInterface *hw = hwi(w); 722 hw->mPreviewCrop.left = left; 723 hw->mPreviewCrop.top = top; 724 hw->mPreviewCrop.right = right; 725 hw->mPreviewCrop.bottom = bottom; 726 return native_window_set_crop(a, &(hw->mPreviewCrop)); 727 } 728 __set_timestamp(struct preview_stream_ops * w,int64_t timestamp)729 static int __set_timestamp(struct preview_stream_ops *w, 730 int64_t timestamp) { 731 ANativeWindow *a = anw(w); 732 return native_window_set_buffers_timestamp(a, timestamp); 733 } 734 __set_usage(struct preview_stream_ops * w,int usage)735 static int __set_usage(struct preview_stream_ops* w, int usage) 736 { 737 ANativeWindow *a = anw(w); 738 CameraHardwareInterface *hw = hwi(w); 739 hw->mPreviewUsage = usage; 740 return native_window_set_usage(a, usage); 741 } 742 __set_swap_interval(struct preview_stream_ops * w,int interval)743 static int __set_swap_interval(struct preview_stream_ops *w, int interval) 744 { 745 ANativeWindow *a = anw(w); 746 CameraHardwareInterface *hw = hwi(w); 747 hw->mPreviewSwapInterval = interval; 748 return a->setSwapInterval(a, interval); 749 } 750 __get_min_undequeued_buffer_count(const struct preview_stream_ops * w,int * count)751 static int __get_min_undequeued_buffer_count( 752 const struct preview_stream_ops *w, 753 int *count) 754 { 755 ANativeWindow *a = anw(w); 756 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count); 757 } 758 initHalPreviewWindow()759 void initHalPreviewWindow() 760 { 761 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer; 762 mHalPreviewWindow.nw.lock_buffer = __lock_buffer; 763 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer; 764 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer; 765 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count; 766 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry; 767 mHalPreviewWindow.nw.set_crop = __set_crop; 768 mHalPreviewWindow.nw.set_timestamp = __set_timestamp; 769 mHalPreviewWindow.nw.set_usage = __set_usage; 770 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval; 771 772 mHalPreviewWindow.nw.get_min_undequeued_buffer_count = 773 __get_min_undequeued_buffer_count; 774 } 775 776 sp<ANativeWindow> mPreviewWindow; 777 778 struct camera_preview_window { 779 struct preview_stream_ops nw; 780 void *user; 781 }; 782 783 struct camera_preview_window mHalPreviewWindow; 784 785 notify_callback mNotifyCb; 786 data_callback mDataCb; 787 data_callback_timestamp mDataCbTimestamp; 788 void *mCbUser; 789 790 // Cached values for preview stream parameters 791 static const int NOT_SET = -1; 792 int mPreviewScalingMode; 793 int mPreviewTransform; 794 int mPreviewWidth; 795 int mPreviewHeight; 796 int mPreviewFormat; 797 int mPreviewUsage; 798 int mPreviewSwapInterval; 799 android_native_rect_t mPreviewCrop; 800 }; 801 802 }; // namespace android 803 804 #endif 805