CameraFlashlight.cpp revision ae21e335e392125168601dba4731c85b5c25f33f
1/*
2 * Copyright (C) 2015 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#define LOG_TAG "CameraFlashlight"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19// #define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23#include <cutils/properties.h>
24
25#include "camera/CameraMetadata.h"
26#include "CameraFlashlight.h"
27#include "gui/IGraphicBufferConsumer.h"
28#include "gui/BufferQueue.h"
29#include "camera/camera2/CaptureRequest.h"
30#include "CameraDeviceFactory.h"
31
32
33namespace android {
34
35/////////////////////////////////////////////////////////////////////
36// CameraFlashlight implementation begins
37// used by camera service to control flashflight.
38/////////////////////////////////////////////////////////////////////
39CameraFlashlight::CameraFlashlight(CameraModule& cameraModule,
40        const camera_module_callbacks_t& callbacks) :
41        mCameraModule(&cameraModule),
42        mCallbacks(&callbacks),
43        mFlashlightMapInitialized(false) {
44}
45
46CameraFlashlight::~CameraFlashlight() {
47}
48
49status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
50    ALOGV("%s: creating a flash light control for camera %s", __FUNCTION__,
51            cameraId.string());
52    if (mFlashControl != NULL) {
53        return INVALID_OPERATION;
54    }
55
56    status_t res = OK;
57
58    if (mCameraModule->getRawModule()->module_api_version >=
59            CAMERA_MODULE_API_VERSION_2_4) {
60        mFlashControl = new ModuleFlashControl(*mCameraModule, *mCallbacks);
61        if (mFlashControl == NULL) {
62            ALOGV("%s: cannot create flash control for module api v2.4+",
63                     __FUNCTION__);
64            return NO_MEMORY;
65        }
66    } else {
67        uint32_t deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
68
69        if (mCameraModule->getRawModule()->module_api_version >=
70                CAMERA_MODULE_API_VERSION_2_0) {
71            camera_info info;
72            res = mCameraModule->getCameraInfo(
73                    atoi(String8(cameraId).string()), &info);
74            if (res) {
75                ALOGE("%s: failed to get camera info for camera %s",
76                        __FUNCTION__, cameraId.string());
77                return res;
78            }
79            deviceVersion = info.device_version;
80        }
81
82        if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
83            CameraDeviceClientFlashControl *flashControl =
84                    new CameraDeviceClientFlashControl(*mCameraModule,
85                                                       *mCallbacks);
86            if (!flashControl) {
87                return NO_MEMORY;
88            }
89
90            mFlashControl = flashControl;
91        } else {
92            mFlashControl =
93                    new CameraHardwareInterfaceFlashControl(*mCameraModule,
94                                                            *mCallbacks);
95        }
96    }
97
98    return OK;
99}
100
101status_t CameraFlashlight::setTorchMode(const String8& cameraId, bool enabled) {
102    if (!mFlashlightMapInitialized) {
103        ALOGE("%s: findFlashUnits() must be called before this method.");
104        return NO_INIT;
105    }
106
107    ALOGV("%s: set torch mode of camera %s to %d", __FUNCTION__,
108            cameraId.string(), enabled);
109
110    status_t res = OK;
111    Mutex::Autolock l(mLock);
112
113    if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) {
114        // This case is needed to avoid state corruption during the following call sequence:
115        // CameraService::setTorchMode for camera ID 0 begins, does torch status checks
116        // CameraService::connect for camera ID 0 begins, calls prepareDeviceOpen, ends
117        // CameraService::setTorchMode for camera ID 0 continues, calls
118        //        CameraFlashlight::setTorchMode
119
120        // TODO: Move torch status checks and state updates behind this CameraFlashlight lock
121        // to avoid other similar race conditions.
122        ALOGE("%s: Camera device %s is in use, cannot set torch mode.",
123                __FUNCTION__, cameraId.string());
124        return -EBUSY;
125    }
126
127    if (mFlashControl == NULL) {
128        if (enabled == false) {
129            return OK;
130        }
131
132        res = createFlashlightControl(cameraId);
133        if (res) {
134            return res;
135        }
136        res =  mFlashControl->setTorchMode(cameraId, enabled);
137        return res;
138    }
139
140    // if flash control already exists, turning on torch mode may fail if it's
141    // tied to another camera device for module v2.3 and below.
142    res = mFlashControl->setTorchMode(cameraId, enabled);
143    if (res == BAD_INDEX) {
144        // flash control is tied to another camera device, need to close it and
145        // try again.
146        mFlashControl.clear();
147        res = createFlashlightControl(cameraId);
148        if (res) {
149            return res;
150        }
151        res = mFlashControl->setTorchMode(cameraId, enabled);
152    }
153
154    return res;
155}
156
157status_t CameraFlashlight::findFlashUnits() {
158    Mutex::Autolock l(mLock);
159    status_t res;
160    int32_t numCameras = mCameraModule->getNumberOfCameras();
161
162    mHasFlashlightMap.clear();
163    mFlashlightMapInitialized = false;
164
165    for (int32_t i = 0; i < numCameras; i++) {
166        bool hasFlash = false;
167        String8 id = String8::format("%d", i);
168
169        res = createFlashlightControl(id);
170        if (res) {
171            ALOGE("%s: failed to create flash control for %s", __FUNCTION__,
172                    id.string());
173        } else {
174            res = mFlashControl->hasFlashUnit(id, &hasFlash);
175            if (res == -EUSERS || res == -EBUSY) {
176                ALOGE("%s: failed to check if camera %s has a flash unit. Some "
177                        "camera devices may be opened", __FUNCTION__,
178                        id.string());
179                return res;
180            } else if (res) {
181                ALOGE("%s: failed to check if camera %s has a flash unit. %s"
182                        " (%d)", __FUNCTION__, id.string(), strerror(-res),
183                        res);
184            }
185
186            mFlashControl.clear();
187        }
188        mHasFlashlightMap.add(id, hasFlash);
189    }
190
191    mFlashlightMapInitialized = true;
192    return OK;
193}
194
195bool CameraFlashlight::hasFlashUnit(const String8& cameraId) {
196    status_t res;
197
198    Mutex::Autolock l(mLock);
199    return hasFlashUnitLocked(cameraId);
200}
201
202bool CameraFlashlight::hasFlashUnitLocked(const String8& cameraId) {
203    if (!mFlashlightMapInitialized) {
204        ALOGE("%s: findFlashUnits() must be called before this method.");
205        return false;
206    }
207
208    ssize_t index = mHasFlashlightMap.indexOfKey(cameraId);
209    if (index == NAME_NOT_FOUND) {
210        ALOGE("%s: camera %s not present when findFlashUnits() was called",
211                __FUNCTION__, cameraId.string());
212        return false;
213    }
214
215    return mHasFlashlightMap.valueAt(index);
216}
217
218status_t CameraFlashlight::prepareDeviceOpen(const String8& cameraId) {
219    ALOGV("%s: prepare for device open", __FUNCTION__);
220
221    Mutex::Autolock l(mLock);
222    if (!mFlashlightMapInitialized) {
223        ALOGE("%s: findFlashUnits() must be called before this method.");
224        return NO_INIT;
225    }
226
227    if (mCameraModule->getRawModule()->module_api_version <
228            CAMERA_MODULE_API_VERSION_2_4) {
229        // framework is going to open a camera device, all flash light control
230        // should be closed for backward compatible support.
231        mFlashControl.clear();
232
233        if (mOpenedCameraIds.size() == 0) {
234            // notify torch unavailable for all cameras with a flash
235            int numCameras = mCameraModule->getNumberOfCameras();
236            for (int i = 0; i < numCameras; i++) {
237                if (hasFlashUnitLocked(String8::format("%d", i))) {
238                    mCallbacks->torch_mode_status_change(mCallbacks,
239                            String8::format("%d", i).string(),
240                            TORCH_MODE_STATUS_NOT_AVAILABLE);
241                }
242            }
243        }
244
245        // close flash control that may be opened by calling hasFlashUnitLocked.
246        mFlashControl.clear();
247    }
248
249    if (mOpenedCameraIds.indexOf(cameraId) == NAME_NOT_FOUND) {
250        mOpenedCameraIds.add(cameraId);
251    }
252
253    return OK;
254}
255
256status_t CameraFlashlight::deviceClosed(const String8& cameraId) {
257    ALOGV("%s: device %s is closed", __FUNCTION__, cameraId.string());
258
259    Mutex::Autolock l(mLock);
260    if (!mFlashlightMapInitialized) {
261        ALOGE("%s: findFlashUnits() must be called before this method.");
262        return NO_INIT;
263    }
264
265    ssize_t index = mOpenedCameraIds.indexOf(cameraId);
266    if (index == NAME_NOT_FOUND) {
267        ALOGE("%s: couldn't find camera %s in the opened list", __FUNCTION__,
268                cameraId.string());
269    } else {
270        mOpenedCameraIds.removeAt(index);
271    }
272
273    // Cannot do anything until all cameras are closed.
274    if (mOpenedCameraIds.size() != 0)
275        return OK;
276
277    if (mCameraModule->getRawModule()->module_api_version <
278            CAMERA_MODULE_API_VERSION_2_4) {
279        // notify torch available for all cameras with a flash
280        int numCameras = mCameraModule->getNumberOfCameras();
281        for (int i = 0; i < numCameras; i++) {
282            if (hasFlashUnitLocked(String8::format("%d", i))) {
283                mCallbacks->torch_mode_status_change(mCallbacks,
284                        String8::format("%d", i).string(),
285                        TORCH_MODE_STATUS_AVAILABLE_OFF);
286            }
287        }
288    }
289
290    return OK;
291}
292// CameraFlashlight implementation ends
293
294
295FlashControlBase::~FlashControlBase() {
296}
297
298/////////////////////////////////////////////////////////////////////
299// ModuleFlashControl implementation begins
300// Flash control for camera module v2.4 and above.
301/////////////////////////////////////////////////////////////////////
302ModuleFlashControl::ModuleFlashControl(CameraModule& cameraModule,
303        const camera_module_callbacks_t& callbacks) :
304    mCameraModule(&cameraModule) {
305}
306
307ModuleFlashControl::~ModuleFlashControl() {
308}
309
310status_t ModuleFlashControl::hasFlashUnit(const String8& cameraId, bool *hasFlash) {
311    if (!hasFlash) {
312        return BAD_VALUE;
313    }
314
315    *hasFlash = false;
316    Mutex::Autolock l(mLock);
317
318    camera_info info;
319    status_t res = mCameraModule->getCameraInfo(atoi(cameraId.string()),
320            &info);
321    if (res != 0) {
322        return res;
323    }
324
325    CameraMetadata metadata;
326    metadata = info.static_camera_characteristics;
327    camera_metadata_entry flashAvailable =
328            metadata.find(ANDROID_FLASH_INFO_AVAILABLE);
329    if (flashAvailable.count == 1 && flashAvailable.data.u8[0] == 1) {
330        *hasFlash = true;
331    }
332
333    return OK;
334}
335
336status_t ModuleFlashControl::setTorchMode(const String8& cameraId, bool enabled) {
337    ALOGV("%s: set camera %s torch mode to %d", __FUNCTION__,
338            cameraId.string(), enabled);
339
340    Mutex::Autolock l(mLock);
341    return mCameraModule->setTorchMode(cameraId.string(), enabled);
342}
343// ModuleFlashControl implementation ends
344
345/////////////////////////////////////////////////////////////////////
346// CameraDeviceClientFlashControl implementation begins
347// Flash control for camera module <= v2.3 and camera HAL v2-v3
348/////////////////////////////////////////////////////////////////////
349CameraDeviceClientFlashControl::CameraDeviceClientFlashControl(
350        CameraModule& cameraModule,
351        const camera_module_callbacks_t& callbacks) :
352        mCameraModule(&cameraModule),
353        mCallbacks(&callbacks),
354        mTorchEnabled(false),
355        mMetadata(NULL),
356        mStreaming(false) {
357}
358
359CameraDeviceClientFlashControl::~CameraDeviceClientFlashControl() {
360    disconnectCameraDevice();
361    if (mMetadata) {
362        delete mMetadata;
363    }
364
365    mAnw.clear();
366    mSurfaceTexture.clear();
367    mProducer.clear();
368    mConsumer.clear();
369
370    if (mTorchEnabled) {
371        if (mCallbacks) {
372            ALOGV("%s: notify the framework that torch was turned off",
373                    __FUNCTION__);
374            mCallbacks->torch_mode_status_change(mCallbacks,
375                    mCameraId.string(), TORCH_MODE_STATUS_AVAILABLE_OFF);
376        }
377    }
378}
379
380status_t CameraDeviceClientFlashControl::initializeSurface(
381        sp<CameraDeviceBase> &device, int32_t width, int32_t height) {
382    status_t res;
383    BufferQueue::createBufferQueue(&mProducer, &mConsumer);
384
385    mSurfaceTexture = new GLConsumer(mConsumer, 0, GLConsumer::TEXTURE_EXTERNAL,
386            true, true);
387    if (mSurfaceTexture == NULL) {
388        return NO_MEMORY;
389    }
390
391    int32_t format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
392    res = mSurfaceTexture->setDefaultBufferSize(width, height);
393    if (res) {
394        return res;
395    }
396    res = mSurfaceTexture->setDefaultBufferFormat(format);
397    if (res) {
398        return res;
399    }
400
401    mAnw = new Surface(mProducer, /*useAsync*/ true);
402    if (mAnw == NULL) {
403        return NO_MEMORY;
404    }
405    res = device->createStream(mAnw, width, height, format,
406            HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0, &mStreamId);
407    if (res) {
408        return res;
409    }
410
411    res = device->configureStreams();
412    if (res) {
413        return res;
414    }
415
416    return res;
417}
418
419status_t CameraDeviceClientFlashControl::getSmallestSurfaceSize(
420        const camera_info& info, int32_t *width, int32_t *height) {
421    if (!width || !height) {
422        return BAD_VALUE;
423    }
424
425    int32_t w = INT32_MAX;
426    int32_t h = 1;
427
428    CameraMetadata metadata;
429    metadata = info.static_camera_characteristics;
430    camera_metadata_entry streamConfigs =
431            metadata.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
432    for (size_t i = 0; i < streamConfigs.count; i += 4) {
433        int32_t fmt = streamConfigs.data.i32[i];
434        if (fmt == ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED) {
435            int32_t ww = streamConfigs.data.i32[i + 1];
436            int32_t hh = streamConfigs.data.i32[i + 2];
437
438            if (w * h > ww * hh) {
439                w = ww;
440                h = hh;
441            }
442        }
443    }
444
445    // if stream configuration is not found, try available processed sizes.
446    if (streamConfigs.count == 0) {
447        camera_metadata_entry availableProcessedSizes =
448            metadata.find(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES);
449        for (size_t i = 0; i < availableProcessedSizes.count; i += 2) {
450            int32_t ww = availableProcessedSizes.data.i32[i];
451            int32_t hh = availableProcessedSizes.data.i32[i + 1];
452            if (w * h > ww * hh) {
453                w = ww;
454                h = hh;
455            }
456        }
457    }
458
459    if (w == INT32_MAX) {
460        return NAME_NOT_FOUND;
461    }
462
463    *width = w;
464    *height = h;
465
466    return OK;
467}
468
469status_t CameraDeviceClientFlashControl::connectCameraDevice(
470        const String8& cameraId) {
471    camera_info info;
472    status_t res = mCameraModule->getCameraInfo(atoi(cameraId.string()), &info);
473    if (res != 0) {
474        ALOGE("%s: failed to get camera info for camera %s", __FUNCTION__,
475                cameraId.string());
476        return res;
477    }
478
479    sp<CameraDeviceBase> device =
480            CameraDeviceFactory::createDevice(atoi(cameraId.string()));
481    if (device == NULL) {
482        return NO_MEMORY;
483    }
484
485    res = device->initialize(mCameraModule);
486    if (res) {
487        return res;
488    }
489
490    int32_t width, height;
491    res = getSmallestSurfaceSize(info, &width, &height);
492    if (res) {
493        return res;
494    }
495    res = initializeSurface(device, width, height);
496    if (res) {
497        return res;
498    }
499
500    mCameraId = cameraId;
501    mStreaming = (info.device_version <= CAMERA_DEVICE_API_VERSION_3_1);
502    mDevice = device;
503
504    return OK;
505}
506
507status_t CameraDeviceClientFlashControl::disconnectCameraDevice() {
508    if (mDevice != NULL) {
509        mDevice->disconnect();
510        mDevice.clear();
511    }
512
513    return OK;
514}
515
516
517
518status_t CameraDeviceClientFlashControl::hasFlashUnit(const String8& cameraId,
519        bool *hasFlash) {
520    ALOGV("%s: checking if camera %s has a flash unit", __FUNCTION__,
521            cameraId.string());
522
523    Mutex::Autolock l(mLock);
524    return hasFlashUnitLocked(cameraId, hasFlash);
525
526}
527
528status_t CameraDeviceClientFlashControl::hasFlashUnitLocked(
529        const String8& cameraId, bool *hasFlash) {
530    if (!hasFlash) {
531        return BAD_VALUE;
532    }
533
534    camera_info info;
535    status_t res = mCameraModule->getCameraInfo(
536            atoi(cameraId.string()), &info);
537    if (res != 0) {
538        ALOGE("%s: failed to get camera info for camera %s", __FUNCTION__,
539                cameraId.string());
540        return res;
541    }
542
543    CameraMetadata metadata;
544    metadata = info.static_camera_characteristics;
545    camera_metadata_entry flashAvailable =
546            metadata.find(ANDROID_FLASH_INFO_AVAILABLE);
547    if (flashAvailable.count == 1 && flashAvailable.data.u8[0] == 1) {
548        *hasFlash = true;
549    }
550
551    return OK;
552}
553
554status_t CameraDeviceClientFlashControl::submitTorchEnabledRequest() {
555    status_t res;
556
557    if (mMetadata == NULL) {
558        mMetadata = new CameraMetadata();
559        if (mMetadata == NULL) {
560            return NO_MEMORY;
561        }
562        res = mDevice->createDefaultRequest(
563                CAMERA3_TEMPLATE_PREVIEW, mMetadata);
564        if (res) {
565            return res;
566        }
567    }
568
569    uint8_t torchOn = ANDROID_FLASH_MODE_TORCH;
570    mMetadata->update(ANDROID_FLASH_MODE, &torchOn, 1);
571    mMetadata->update(ANDROID_REQUEST_OUTPUT_STREAMS, &mStreamId, 1);
572
573    uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON;
574    mMetadata->update(ANDROID_CONTROL_AE_MODE, &aeMode, 1);
575
576    int32_t requestId = 0;
577    mMetadata->update(ANDROID_REQUEST_ID, &requestId, 1);
578
579    if (mStreaming) {
580        res = mDevice->setStreamingRequest(*mMetadata);
581    } else {
582        res = mDevice->capture(*mMetadata);
583    }
584    return res;
585}
586
587
588
589
590status_t CameraDeviceClientFlashControl::setTorchMode(
591        const String8& cameraId, bool enabled) {
592    bool hasFlash = false;
593
594    Mutex::Autolock l(mLock);
595    status_t res = hasFlashUnitLocked(cameraId, &hasFlash);
596
597    // pre-check
598    if (enabled) {
599        // invalid camera?
600        if (res) {
601            return -EINVAL;
602        }
603        // no flash unit?
604        if (!hasFlash) {
605            return -ENOSYS;
606        }
607        // already opened for a different device?
608        if (mDevice != NULL && cameraId != mCameraId) {
609            return BAD_INDEX;
610        }
611    } else if (mDevice == NULL || cameraId != mCameraId) {
612        // disabling the torch mode of an un-opened or different device.
613        return OK;
614    } else {
615        // disabling the torch mode of currently opened device
616        disconnectCameraDevice();
617        mTorchEnabled = false;
618        mCallbacks->torch_mode_status_change(mCallbacks,
619            cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_OFF);
620        return OK;
621    }
622
623    if (mDevice == NULL) {
624        res = connectCameraDevice(cameraId);
625        if (res) {
626            return res;
627        }
628    }
629
630    res = submitTorchEnabledRequest();
631    if (res) {
632        return res;
633    }
634
635    mTorchEnabled = true;
636    mCallbacks->torch_mode_status_change(mCallbacks,
637            cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_ON);
638    return OK;
639}
640// CameraDeviceClientFlashControl implementation ends
641
642
643/////////////////////////////////////////////////////////////////////
644// CameraHardwareInterfaceFlashControl implementation begins
645// Flash control for camera module <= v2.3 and camera HAL v1
646/////////////////////////////////////////////////////////////////////
647CameraHardwareInterfaceFlashControl::CameraHardwareInterfaceFlashControl(
648        CameraModule& cameraModule,
649        const camera_module_callbacks_t& callbacks) :
650        mCameraModule(&cameraModule),
651        mCallbacks(&callbacks),
652        mTorchEnabled(false) {
653
654}
655
656CameraHardwareInterfaceFlashControl::~CameraHardwareInterfaceFlashControl() {
657    disconnectCameraDevice();
658
659    mAnw.clear();
660    mSurfaceTexture.clear();
661    mProducer.clear();
662    mConsumer.clear();
663
664    if (mTorchEnabled) {
665        if (mCallbacks) {
666            ALOGV("%s: notify the framework that torch was turned off",
667                    __FUNCTION__);
668            mCallbacks->torch_mode_status_change(mCallbacks,
669                    mCameraId.string(), TORCH_MODE_STATUS_AVAILABLE_OFF);
670        }
671    }
672}
673
674status_t CameraHardwareInterfaceFlashControl::setTorchMode(
675        const String8& cameraId, bool enabled) {
676    Mutex::Autolock l(mLock);
677
678    // pre-check
679    status_t res;
680    if (enabled) {
681        bool hasFlash = false;
682        res = hasFlashUnitLocked(cameraId, &hasFlash);
683        // invalid camera?
684        if (res) {
685            // hasFlashUnitLocked() returns BAD_INDEX if mDevice is connected to
686            // another camera device.
687            return res == BAD_INDEX ? BAD_INDEX : -EINVAL;
688        }
689        // no flash unit?
690        if (!hasFlash) {
691            return -ENOSYS;
692        }
693    } else if (mDevice == NULL || cameraId != mCameraId) {
694        // disabling the torch mode of an un-opened or different device.
695        return OK;
696    } else {
697        // disabling the torch mode of currently opened device
698        disconnectCameraDevice();
699        mTorchEnabled = false;
700        mCallbacks->torch_mode_status_change(mCallbacks,
701            cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_OFF);
702        return OK;
703    }
704
705    res = startPreviewAndTorch();
706    if (res) {
707        return res;
708    }
709
710    mTorchEnabled = true;
711    mCallbacks->torch_mode_status_change(mCallbacks,
712            cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_ON);
713    return OK;
714}
715
716status_t CameraHardwareInterfaceFlashControl::hasFlashUnit(
717        const String8& cameraId, bool *hasFlash) {
718    Mutex::Autolock l(mLock);
719    return hasFlashUnitLocked(cameraId, hasFlash);
720}
721
722status_t CameraHardwareInterfaceFlashControl::hasFlashUnitLocked(
723        const String8& cameraId, bool *hasFlash) {
724    if (!hasFlash) {
725        return BAD_VALUE;
726    }
727
728    status_t res;
729    if (mDevice == NULL) {
730        res = connectCameraDevice(cameraId);
731        if (res) {
732            return res;
733        }
734    }
735
736    if (cameraId != mCameraId) {
737        return BAD_INDEX;
738    }
739
740    const char *flashMode =
741            mParameters.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES);
742    if (flashMode && strstr(flashMode, CameraParameters::FLASH_MODE_TORCH)) {
743        *hasFlash = true;
744    } else {
745        *hasFlash = false;
746    }
747
748    return OK;
749}
750
751status_t CameraHardwareInterfaceFlashControl::startPreviewAndTorch() {
752    status_t res = OK;
753    res = mDevice->startPreview();
754    if (res) {
755        ALOGE("%s: start preview failed. %s (%d)", __FUNCTION__,
756                strerror(-res), res);
757        return res;
758    }
759
760    mParameters.set(CameraParameters::KEY_FLASH_MODE,
761            CameraParameters::FLASH_MODE_TORCH);
762
763    return mDevice->setParameters(mParameters);
764}
765
766status_t CameraHardwareInterfaceFlashControl::getSmallestSurfaceSize(
767        int32_t *width, int32_t *height) {
768    if (!width || !height) {
769        return BAD_VALUE;
770    }
771
772    int32_t w = INT32_MAX;
773    int32_t h = 1;
774    Vector<Size> sizes;
775
776    mParameters.getSupportedPreviewSizes(sizes);
777    for (size_t i = 0; i < sizes.size(); i++) {
778        Size s = sizes[i];
779        if (w * h > s.width * s.height) {
780            w = s.width;
781            h = s.height;
782        }
783    }
784
785    if (w == INT32_MAX) {
786        return NAME_NOT_FOUND;
787    }
788
789    *width = w;
790    *height = h;
791
792    return OK;
793}
794
795status_t CameraHardwareInterfaceFlashControl::initializePreviewWindow(
796        sp<CameraHardwareInterface> device, int32_t width, int32_t height) {
797    status_t res;
798    BufferQueue::createBufferQueue(&mProducer, &mConsumer);
799
800    mSurfaceTexture = new GLConsumer(mConsumer, 0, GLConsumer::TEXTURE_EXTERNAL,
801            true, true);
802    if (mSurfaceTexture == NULL) {
803        return NO_MEMORY;
804    }
805
806    int32_t format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
807    res = mSurfaceTexture->setDefaultBufferSize(width, height);
808    if (res) {
809        return res;
810    }
811    res = mSurfaceTexture->setDefaultBufferFormat(format);
812    if (res) {
813        return res;
814    }
815
816    mAnw = new Surface(mProducer, /*useAsync*/ true);
817    if (mAnw == NULL) {
818        return NO_MEMORY;
819    }
820
821    res = native_window_api_connect(mAnw.get(), NATIVE_WINDOW_API_CAMERA);
822    if (res) {
823        ALOGE("%s: Unable to connect to native window", __FUNCTION__);
824        return res;
825    }
826
827    return device->setPreviewWindow(mAnw);
828}
829
830status_t CameraHardwareInterfaceFlashControl::connectCameraDevice(
831        const String8& cameraId) {
832    sp<CameraHardwareInterface> device =
833            new CameraHardwareInterface(cameraId.string());
834
835    status_t res = device->initialize(mCameraModule);
836    if (res) {
837        ALOGE("%s: initializing camera %s failed", __FUNCTION__,
838                cameraId.string());
839        return res;
840    }
841
842    // need to set __get_memory in set_callbacks().
843    device->setCallbacks(NULL, NULL, NULL, NULL);
844
845    mParameters = device->getParameters();
846
847    int32_t width, height;
848    res = getSmallestSurfaceSize(&width, &height);
849    if (res) {
850        ALOGE("%s: failed to get smallest surface size for camera %s",
851                __FUNCTION__, cameraId.string());
852        return res;
853    }
854
855    res = initializePreviewWindow(device, width, height);
856    if (res) {
857        ALOGE("%s: failed to initialize preview window for camera %s",
858                __FUNCTION__, cameraId.string());
859        return res;
860    }
861
862    mCameraId = cameraId;
863    mDevice = device;
864    return OK;
865}
866
867status_t CameraHardwareInterfaceFlashControl::disconnectCameraDevice() {
868    if (mDevice == NULL) {
869        return OK;
870    }
871
872    mParameters.set(CameraParameters::KEY_FLASH_MODE,
873            CameraParameters::FLASH_MODE_OFF);
874    mDevice->setParameters(mParameters);
875    mDevice->stopPreview();
876    status_t res = native_window_api_disconnect(mAnw.get(),
877            NATIVE_WINDOW_API_CAMERA);
878    if (res) {
879        ALOGW("%s: native_window_api_disconnect failed: %s (%d)",
880                __FUNCTION__, strerror(-res), res);
881    }
882    mDevice->setPreviewWindow(NULL);
883    mDevice->release();
884
885    return OK;
886}
887// CameraHardwareInterfaceFlashControl implementation ends
888
889}
890