Imported Upstream version 0.1.0+git20131207+e452e83
[deb_libhybris.git] / compat / camera / camera_compatibility_layer.cpp
1 /*
2 * Copyright (C) 2013 Canonical Ltd
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 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 * Ricardo Salveti de Araujo <ricardo.salveti@canonical.com>
18 */
19
20 #include <hybris/internal/camera_control.h>
21 #include <hybris/camera/camera_compatibility_layer.h>
22 #include <hybris/camera/camera_compatibility_layer_capabilities.h>
23 #include <hybris/camera/camera_compatibility_layer_configuration_translator.h>
24
25 #include <hybris/internal/surface_flinger_compatibility_layer_internal.h>
26
27 #include <binder/ProcessState.h>
28 #include <camera/Camera.h>
29 #include <camera/CameraParameters.h>
30 #include <gui/SurfaceTexture.h>
31 #include <ui/GraphicBuffer.h>
32
33 #undef LOG_TAG
34 #define LOG_TAG "CameraCompatibilityLayer"
35 #include <utils/KeyedVector.h>
36 #include <utils/Log.h>
37
38 #define REPORT_FUNCTION() ALOGV("%s \n", __PRETTY_FUNCTION__)
39
40 // From android::SurfaceTexture::FrameAvailableListener
41 void CameraControl::onFrameAvailable()
42 {
43 REPORT_FUNCTION();
44 if (listener)
45 listener->on_preview_texture_needs_update_cb(listener->context);
46 }
47
48 // From android::CameraListener
49 void CameraControl::notify(int32_t msg_type, int32_t ext1, int32_t ext2)
50 {
51 REPORT_FUNCTION();
52 printf("\text1: %d, ext2: %d \n", ext1, ext2);
53
54 if (!listener)
55 return;
56
57 switch (msg_type) {
58 case CAMERA_MSG_ERROR:
59 if (listener->on_msg_error_cb)
60 listener->on_msg_error_cb(listener->context);
61 break;
62 case CAMERA_MSG_SHUTTER:
63 if (listener->on_msg_shutter_cb)
64 listener->on_msg_shutter_cb(listener->context);
65 break;
66 case CAMERA_MSG_ZOOM:
67 if (listener->on_msg_zoom_cb)
68 listener->on_msg_zoom_cb(listener->context, ext1);
69 break;
70 case CAMERA_MSG_FOCUS:
71 if (listener->on_msg_focus_cb)
72 listener->on_msg_focus_cb(listener->context);
73 break;
74 default:
75 break;
76 }
77 }
78
79 void CameraControl::postData(
80 int32_t msg_type,
81 const android::sp<android::IMemory>& data,
82 camera_frame_metadata_t* metadata)
83 {
84 REPORT_FUNCTION();
85
86 if (!listener)
87 return;
88
89 switch (msg_type) {
90 case CAMERA_MSG_RAW_IMAGE:
91 if (listener->on_data_raw_image_cb)
92 listener->on_data_raw_image_cb(data->pointer(), data->size(), listener->context);
93 break;
94 case CAMERA_MSG_COMPRESSED_IMAGE:
95 if (listener->on_data_compressed_image_cb)
96 listener->on_data_compressed_image_cb(data->pointer(), data->size(), listener->context);
97 break;
98 default:
99 break;
100 }
101 }
102
103 void CameraControl::postDataTimestamp(
104 nsecs_t timestamp,
105 int32_t msg_type,
106 const android::sp<android::IMemory>& data)
107 {
108 REPORT_FUNCTION();
109 (void) timestamp;
110 (void) msg_type;
111 (void) data;
112 }
113
114 namespace android
115 {
116 NativeBufferAlloc::NativeBufferAlloc() {
117 }
118
119 NativeBufferAlloc::~NativeBufferAlloc() {
120 }
121
122 sp<GraphicBuffer> NativeBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
123 PixelFormat format, uint32_t usage, status_t* error) {
124 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
125 status_t err = graphicBuffer->initCheck();
126 *error = err;
127 if (err != 0 || graphicBuffer->handle == 0) {
128 if (err == NO_MEMORY) {
129 GraphicBuffer::dumpAllocationsToSystemLog();
130 }
131 ALOGI("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
132 "failed (%s), handle=%p",
133 w, h, strerror(-err), graphicBuffer->handle);
134 return 0;
135 }
136 return graphicBuffer;
137 }
138 }
139
140 namespace
141 {
142
143 android::sp<CameraControl> camera_control_instance;
144
145 }
146
147 int android_camera_get_number_of_devices()
148 {
149 REPORT_FUNCTION();
150 return android::Camera::getNumberOfCameras();
151 }
152
153 CameraControl* android_camera_connect_to(CameraType camera_type, CameraControlListener* listener)
154 {
155 REPORT_FUNCTION();
156
157 int32_t camera_id;
158 int32_t camera_count = camera_id = android::Camera::getNumberOfCameras();
159
160 for (camera_id = 0; camera_id < camera_count; camera_id++) {
161 android::CameraInfo ci;
162 android::Camera::getCameraInfo(camera_id, &ci);
163
164 if (ci.facing == camera_type)
165 break;
166 }
167
168 if (camera_id == camera_count)
169 return NULL;
170
171 CameraControl* cc = new CameraControl();
172 cc->listener = listener;
173 cc->camera = android::Camera::connect(camera_id);
174
175 if (cc->camera == NULL)
176 return NULL;
177
178 cc->camera_parameters = android::CameraParameters(cc->camera->getParameters());
179
180 camera_control_instance = cc;
181 cc->camera->setListener(camera_control_instance);
182 cc->camera->lock();
183
184 // TODO: Move this to a more generic component
185 android::ProcessState::self()->startThreadPool();
186
187 return cc;
188 }
189
190 void android_camera_disconnect(CameraControl* control)
191 {
192 REPORT_FUNCTION();
193 assert(control);
194
195 android::Mutex::Autolock al(control->guard);
196
197 if (control->preview_texture != NULL)
198 control->preview_texture->abandon();
199
200 control->camera->disconnect();
201 control->camera->unlock();
202 }
203
204 int android_camera_lock(CameraControl* control)
205 {
206 android::Mutex::Autolock al(control->guard);
207 return control->camera->lock();
208 }
209
210 int android_camera_unlock(CameraControl* control)
211 {
212 android::Mutex::Autolock al(control->guard);
213 return control->camera->unlock();
214 }
215
216 void android_camera_delete(CameraControl* control)
217 {
218 delete control;
219 }
220
221 void android_camera_dump_parameters(CameraControl* control)
222 {
223 REPORT_FUNCTION();
224 assert(control);
225
226 printf("%s \n", control->camera->getParameters().string());
227 }
228
229 void android_camera_set_flash_mode(CameraControl* control, FlashMode mode)
230 {
231 REPORT_FUNCTION();
232 assert(control);
233
234 android::Mutex::Autolock al(control->guard);
235 control->camera_parameters.set(
236 android::CameraParameters::KEY_FLASH_MODE,
237 flash_modes[mode]);
238 control->camera->setParameters(control->camera_parameters.flatten());
239 }
240
241 void android_camera_get_flash_mode(CameraControl* control, FlashMode* mode)
242 {
243 REPORT_FUNCTION();
244 assert(control);
245
246 android::Mutex::Autolock al(control->guard);
247 static const char* flash_mode = control->camera_parameters.get(
248 android::CameraParameters::KEY_FLASH_MODE);
249 if (flash_mode)
250 *mode = flash_modes_lut.valueFor(android::String8(flash_mode));
251 else
252 *mode = FLASH_MODE_OFF;
253 }
254
255 void android_camera_set_white_balance_mode(CameraControl* control, WhiteBalanceMode mode)
256 {
257 REPORT_FUNCTION();
258 assert(control);
259
260 android::Mutex::Autolock al(control->guard);
261
262 control->camera_parameters.set(
263 android::CameraParameters::KEY_WHITE_BALANCE,
264 white_balance_modes[mode]);
265 control->camera->setParameters(control->camera_parameters.flatten());
266 }
267
268 void android_camera_get_white_balance_mode(CameraControl* control, WhiteBalanceMode* mode)
269 {
270 REPORT_FUNCTION();
271 assert(control);
272
273 android::Mutex::Autolock al(control->guard);
274
275 *mode = white_balance_modes_lut.valueFor(
276 android::String8(
277 control->camera_parameters.get(
278 android::CameraParameters::KEY_WHITE_BALANCE)));
279 }
280
281 void android_camera_set_scene_mode(CameraControl* control, SceneMode mode)
282 {
283 REPORT_FUNCTION();
284 assert(control);
285
286 android::Mutex::Autolock al(control->guard);
287
288 control->camera_parameters.set(
289 android::CameraParameters::KEY_SCENE_MODE,
290 scene_modes[mode]);
291 control->camera->setParameters(control->camera_parameters.flatten());
292 }
293
294 void android_camera_get_scene_mode(CameraControl* control, SceneMode* mode)
295 {
296 REPORT_FUNCTION();
297 assert(control);
298
299 android::Mutex::Autolock al(control->guard);
300
301 *mode = scene_modes_lut.valueFor(
302 android::String8(
303 control->camera_parameters.get(
304 android::CameraParameters::KEY_SCENE_MODE)));
305 }
306
307 void android_camera_set_auto_focus_mode(CameraControl* control, AutoFocusMode mode)
308 {
309 REPORT_FUNCTION();
310 assert(control);
311
312 android::Mutex::Autolock al(control->guard);
313
314 control->camera_parameters.set(
315 android::CameraParameters::KEY_FOCUS_MODE,
316 auto_focus_modes[mode]);
317 control->camera->setParameters(control->camera_parameters.flatten());
318 }
319
320 void android_camera_get_auto_focus_mode(CameraControl* control, AutoFocusMode* mode)
321 {
322 REPORT_FUNCTION();
323 assert(control);
324
325 android::Mutex::Autolock al(control->guard);
326
327 *mode = auto_focus_modes_lut.valueFor(
328 android::String8(
329 control->camera_parameters.get(
330 android::CameraParameters::KEY_FOCUS_MODE)));
331 }
332
333
334 void android_camera_set_effect_mode(CameraControl* control, EffectMode mode)
335 {
336 REPORT_FUNCTION();
337 assert(control);
338
339 android::Mutex::Autolock al(control->guard);
340
341 control->camera_parameters.set(
342 android::CameraParameters::KEY_EFFECT,
343 effect_modes[mode]);
344 control->camera->setParameters(control->camera_parameters.flatten());
345 }
346
347 void android_camera_get_effect_mode(CameraControl* control, EffectMode* mode)
348 {
349 REPORT_FUNCTION();
350 assert(control);
351
352 android::Mutex::Autolock al(control->guard);
353
354 *mode = effect_modes_lut.valueFor(
355 android::String8(
356 control->camera_parameters.get(
357 android::CameraParameters::KEY_EFFECT)));
358 }
359
360 void android_camera_get_preview_fps_range(CameraControl* control, int* min, int* max)
361 {
362 REPORT_FUNCTION();
363 assert(control);
364
365 android::Mutex::Autolock al(control->guard);
366
367 control->camera_parameters.getPreviewFpsRange(min, max);
368 }
369
370 void android_camera_set_preview_fps(CameraControl* control, int fps)
371 {
372 REPORT_FUNCTION();
373 assert(control);
374
375 android::Mutex::Autolock al(control->guard);
376 control->camera_parameters.setPreviewFrameRate(fps);
377 control->camera->setParameters(control->camera_parameters.flatten());
378 }
379
380 void android_camera_get_preview_fps(CameraControl* control, int* fps)
381 {
382 REPORT_FUNCTION();
383 assert(control);
384
385 android::Mutex::Autolock al(control->guard);
386 *fps = control->camera_parameters.getPreviewFrameRate();
387 }
388
389 void android_camera_enumerate_supported_preview_sizes(CameraControl* control, size_callback cb, void* ctx)
390 {
391 REPORT_FUNCTION();
392 assert(control);
393
394 android::Mutex::Autolock al(control->guard);
395 android::Vector<android::Size> sizes;
396 control->camera_parameters.getSupportedPreviewSizes(sizes);
397
398 for (unsigned int i = 0; i < sizes.size(); i++) {
399 cb(ctx, sizes[i].width, sizes[i].height);
400 }
401 }
402
403 void android_camera_enumerate_supported_picture_sizes(CameraControl* control, size_callback cb, void* ctx)
404 {
405 REPORT_FUNCTION();
406 assert(control);
407
408 android::Mutex::Autolock al(control->guard);
409 android::Vector<android::Size> sizes;
410 control->camera_parameters.getSupportedPictureSizes(sizes);
411
412 for (unsigned int i = 0; i < sizes.size(); i++) {
413 cb(ctx, sizes[i].width, sizes[i].height);
414 }
415 }
416
417 void android_camera_get_preview_size(CameraControl* control, int* width, int* height)
418 {
419 REPORT_FUNCTION();
420 assert(control);
421
422 android::Mutex::Autolock al(control->guard);
423
424 control->camera_parameters.getPreviewSize(width, height);
425 }
426
427 void android_camera_set_preview_size(CameraControl* control, int width, int height)
428 {
429 REPORT_FUNCTION();
430 assert(control);
431
432 android::Mutex::Autolock al(control->guard);
433
434 control->camera_parameters.setPreviewSize(width, height);
435 control->camera->setParameters(control->camera_parameters.flatten());
436 }
437
438 void android_camera_get_picture_size(CameraControl* control, int* width, int* height)
439 {
440 REPORT_FUNCTION();
441 assert(control);
442
443 android::Mutex::Autolock al(control->guard);
444
445 control->camera_parameters.getPictureSize(width, height);
446 }
447
448 void android_camera_set_picture_size(CameraControl* control, int width, int height)
449 {
450 REPORT_FUNCTION();
451 assert(control);
452
453 android::Mutex::Autolock al(control->guard);
454
455 control->camera_parameters.setPictureSize(width, height);
456 control->camera->setParameters(control->camera_parameters.flatten());
457 }
458
459 void android_camera_get_current_zoom(CameraControl* control, int* zoom)
460 {
461 REPORT_FUNCTION();
462 assert(control);
463
464 android::Mutex::Autolock al(control->guard);
465
466 *zoom = control->camera_parameters.getInt(android::CameraParameters::KEY_ZOOM);
467 }
468
469 void android_camera_get_max_zoom(CameraControl* control, int* zoom)
470 {
471 REPORT_FUNCTION();
472 assert(control);
473
474 android::Mutex::Autolock al(control->guard);
475
476 *zoom = control->camera_parameters.getInt(android::CameraParameters::KEY_MAX_ZOOM);
477 }
478
479 void android_camera_set_display_orientation(CameraControl* control, int32_t clockwise_rotation_degree)
480 {
481 REPORT_FUNCTION();
482 assert(control);
483
484 android::Mutex::Autolock al(control->guard);
485 static const int32_t ignored_parameter = 0;
486 control->camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, clockwise_rotation_degree, ignored_parameter);
487 }
488
489 void android_camera_get_preview_texture_transformation(CameraControl* control, float m[16])
490 {
491 REPORT_FUNCTION();
492 assert(control);
493
494 if (control->preview_texture == NULL)
495 return;
496
497 control->preview_texture->getTransformMatrix(m);
498 }
499
500 void android_camera_update_preview_texture(CameraControl* control)
501 {
502 REPORT_FUNCTION();
503 assert(control);
504
505 control->preview_texture->updateTexImage();
506 }
507
508 void android_camera_set_preview_texture(CameraControl* control, int texture_id)
509 {
510 REPORT_FUNCTION();
511 assert(control);
512
513 static const bool allow_synchronous_mode = false;
514
515 android::sp<android::NativeBufferAlloc> native_alloc(
516 new android::NativeBufferAlloc()
517 );
518
519 android::sp<android::BufferQueue> buffer_queue(
520 new android::BufferQueue(false, NULL, native_alloc)
521 );
522
523 if (control->preview_texture == NULL) {
524 control->preview_texture = android::sp<android::SurfaceTexture>(
525 new android::SurfaceTexture(
526 texture_id,
527 allow_synchronous_mode,
528 GL_TEXTURE_EXTERNAL_OES,
529 true,
530 buffer_queue));
531 }
532
533 control->preview_texture->setFrameAvailableListener(
534 android::sp<android::SurfaceTexture::FrameAvailableListener>(control));
535 control->camera->setPreviewTexture(control->preview_texture->getBufferQueue());
536 }
537
538 void android_camera_set_preview_surface(CameraControl* control, SfSurface* surface)
539 {
540 REPORT_FUNCTION();
541 assert(control);
542 assert(surface);
543
544 android::Mutex::Autolock al(control->guard);
545 control->camera->setPreviewDisplay(surface->surface);
546 }
547
548 void android_camera_start_preview(CameraControl* control)
549 {
550 REPORT_FUNCTION();
551 assert(control);
552
553 android::Mutex::Autolock al(control->guard);
554 control->camera->startPreview();
555 }
556
557 void android_camera_stop_preview(CameraControl* control)
558 {
559 REPORT_FUNCTION();
560 assert(control);
561
562 android::Mutex::Autolock al(control->guard);
563 control->camera->stopPreview();
564 }
565
566 void android_camera_start_autofocus(CameraControl* control)
567 {
568 REPORT_FUNCTION();
569 assert(control);
570
571 android::Mutex::Autolock al(control->guard);
572 control->camera->autoFocus();
573 }
574
575 void android_camera_stop_autofocus(CameraControl* control)
576 {
577 REPORT_FUNCTION();
578 assert(control);
579
580 android::Mutex::Autolock al(control->guard);
581 control->camera->cancelAutoFocus();
582 }
583
584 void android_camera_start_zoom(CameraControl* control, int32_t zoom)
585 {
586 REPORT_FUNCTION();
587 assert(control);
588
589 static const int ignored_argument = 0;
590
591 android::Mutex::Autolock al(control->guard);
592 control->camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM,
593 zoom,
594 ignored_argument);
595 }
596
597 // Adjust the zoom level immediately as opposed to smoothly zoomin gin.
598 void android_camera_set_zoom(CameraControl* control, int32_t zoom)
599 {
600 REPORT_FUNCTION();
601 assert(control);
602
603 android::Mutex::Autolock al(control->guard);
604
605 control->camera_parameters.set(
606 android::CameraParameters::KEY_ZOOM,
607 zoom);
608
609 control->camera->setParameters(control->camera_parameters.flatten());
610 }
611
612 void android_camera_stop_zoom(CameraControl* control)
613 {
614 REPORT_FUNCTION();
615 assert(control);
616
617 static const int ignored_argument = 0;
618
619 android::Mutex::Autolock al(control->guard);
620 control->camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM,
621 ignored_argument,
622 ignored_argument);
623 }
624
625 void android_camera_take_snapshot(CameraControl* control)
626 {
627 REPORT_FUNCTION();
628 assert(control);
629 android::Mutex::Autolock al(control->guard);
630 control->camera->takePicture(CAMERA_MSG_SHUTTER | CAMERA_MSG_COMPRESSED_IMAGE);
631 }
632
633 void android_camera_set_preview_format(CameraControl* control, CameraPixelFormat pf)
634 {
635 REPORT_FUNCTION();
636 assert(control);
637
638 android::Mutex::Autolock al(control->guard);
639
640 control->camera_parameters.set(
641 android::CameraParameters::KEY_PREVIEW_FORMAT,
642 camera_pixel_formats[pf]);
643
644 control->camera->setParameters(control->camera_parameters.flatten());
645 }
646
647 void android_camera_get_preview_format(CameraControl* control, CameraPixelFormat* pf)
648 {
649 REPORT_FUNCTION();
650 assert(control);
651
652 android::Mutex::Autolock al(control->guard);
653
654 *pf = pixel_formats_lut.valueFor(
655 android::String8(
656 control->camera_parameters.get(
657 android::CameraParameters::KEY_PREVIEW_FORMAT)));
658 }
659
660 void android_camera_set_focus_region(
661 CameraControl* control,
662 FocusRegion* region)
663 {
664 REPORT_FUNCTION();
665 assert(control);
666
667 android::Mutex::Autolock al(control->guard);
668 static const char* focus_region_pattern = "(%d,%d,%d,%d,%d)";
669 static char focus_region[256];
670 snprintf(focus_region,
671 sizeof(focus_region),
672 focus_region_pattern,
673 region->left,
674 region->top,
675 region->right,
676 region->bottom,
677 region->weight);
678
679 control->camera_parameters.set(
680 android::CameraParameters::KEY_FOCUS_AREAS,
681 focus_region);
682
683 control->camera->setParameters(control->camera_parameters.flatten());
684 }
685
686 void android_camera_reset_focus_region(CameraControl* control)
687 {
688 static FocusRegion region = { 0, 0, 0, 0, 0 };
689
690 android_camera_set_focus_region(control, &region);
691 }
692
693 void android_camera_set_rotation(CameraControl* control, int rotation)
694 {
695 REPORT_FUNCTION();
696 assert(control);
697
698 android::Mutex::Autolock al(control->guard);
699 control->camera_parameters.set(
700 android::CameraParameters::KEY_ROTATION,
701 rotation);
702 control->camera->setParameters(control->camera_parameters.flatten());
703 }
704
705 void android_camera_enumerate_supported_video_sizes(CameraControl* control, size_callback cb, void* ctx)
706 {
707 REPORT_FUNCTION();
708 assert(control);
709 assert(cb);
710
711 android::Mutex::Autolock al(control->guard);
712 android::Vector<android::Size> sizes;
713 control->camera_parameters.getSupportedVideoSizes(sizes);
714
715 for (unsigned int i = 0; i < sizes.size(); i++) {
716 cb(ctx, sizes[i].width, sizes[i].height);
717 }
718 }
719
720 void android_camera_get_video_size(CameraControl* control, int* width, int* height)
721 {
722 REPORT_FUNCTION();
723 assert(control);
724
725 android::Mutex::Autolock al(control->guard);
726
727 control->camera_parameters.getVideoSize(width, height);
728 }
729
730 void android_camera_set_video_size(CameraControl* control, int width, int height)
731 {
732 REPORT_FUNCTION();
733 assert(control);
734
735 android::Mutex::Autolock al(control->guard);
736
737 control->camera_parameters.setVideoSize(width, height);
738 control->camera->setParameters(control->camera_parameters.flatten());
739 }