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