Camera service: Add skeleton support for camera2 devices.

- Add Camera2Client, which maps the current android.hardware.Camera
  API to the new camera2 semantics
- Add Camera2Device, a C++ wrapper for the HAL camera 2 device

No functionality besides create/destroy; all client methods return
error or null.

Bug: 6243944
Change-Id: I6bd5136fa1efd8279b8cd2d9847d9b0bb2862294
diff --git a/services/camera/libcameraservice/Android.mk b/services/camera/libcameraservice/Android.mk
index 0022f2b..a2a7e57 100644
--- a/services/camera/libcameraservice/Android.mk
+++ b/services/camera/libcameraservice/Android.mk
@@ -8,7 +8,9 @@
 
 LOCAL_SRC_FILES:=               \
     CameraService.cpp \
-    CameraClient.cpp
+    CameraClient.cpp \
+    Camera2Client.cpp \
+    Camera2Device.cpp
 
 LOCAL_SHARED_LIBRARIES:= \
     libui \
@@ -21,6 +23,9 @@
     libgui \
     libhardware
 
+LOCAL_C_INCLUDES += \
+    system/media/camera/include
+
 LOCAL_MODULE:= libcameraservice
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/services/camera/libcameraservice/Camera2Client.cpp b/services/camera/libcameraservice/Camera2Client.cpp
new file mode 100644
index 0000000..f0725ed
--- /dev/null
+++ b/services/camera/libcameraservice/Camera2Client.cpp
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "Camera2Client"
+//#define LOG_NDEBUG 0
+
+#include <cutils/properties.h>
+#include <gui/SurfaceTextureClient.h>
+#include <gui/Surface.h>
+
+#include "Camera2Client.h"
+
+namespace android {
+
+#define ALOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
+#define ALOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
+
+#define ALOG1_ENTRY {       \
+    int callingPid = getCallingPid(); \
+    ALOG1("%s: E (pid %d, id %d) ", __FUNCTION__, \
+            callingPid, mCameraId);      \
+}
+
+static int getCallingPid() {
+    return IPCThreadState::self()->getCallingPid();
+}
+
+static int getCallingUid() {
+    return IPCThreadState::self()->getCallingUid();
+}
+
+// Interface used by CameraService
+
+Camera2Client::Camera2Client(const sp<CameraService>& cameraService,
+        const sp<ICameraClient>& cameraClient,
+        const sp<Camera2Device>& device,
+        int cameraId,
+        int cameraFacing,
+        int clientPid):
+        Client(cameraService, cameraClient,
+                cameraId, cameraFacing, clientPid) {
+    int callingPid = getCallingPid();
+    ALOG1("%s: E (pid %d, id %d)", __FUNCTION__, callingPid, cameraId);
+    ALOG1_ENTRY;
+
+}
+
+Camera2Client::~Camera2Client() {
+}
+
+status_t Camera2Client::dump(int fd, const Vector<String16>& args) {
+    return BAD_VALUE;
+}
+
+// ICamera interface
+
+void Camera2Client::disconnect() {
+    CameraService::Client::disconnect();
+}
+
+status_t Camera2Client::connect(const sp<ICameraClient>& client) {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::lock() {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::unlock() {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::setPreviewDisplay(const sp<Surface>& surface) {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture) {
+    return BAD_VALUE;
+}
+
+void Camera2Client::setPreviewCallbackFlag(int flag) {
+
+}
+
+status_t Camera2Client::startPreview() {
+    return BAD_VALUE;
+}
+
+void Camera2Client::stopPreview() {
+
+}
+
+bool Camera2Client::previewEnabled() {
+    return false;
+}
+
+status_t Camera2Client::storeMetaDataInBuffers(bool enabled) {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::startRecording() {
+    return BAD_VALUE;
+}
+
+void Camera2Client::stopRecording() {
+}
+
+bool Camera2Client::recordingEnabled() {
+    return BAD_VALUE;
+}
+
+void Camera2Client::releaseRecordingFrame(const sp<IMemory>& mem) {
+
+}
+
+status_t Camera2Client::autoFocus() {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::cancelAutoFocus() {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::takePicture(int msgType) {
+    return BAD_VALUE;
+}
+
+status_t Camera2Client::setParameters(const String8& params) {
+    return BAD_VALUE;
+}
+String8 Camera2Client::getParameters() const {
+    return String8();
+}
+
+status_t Camera2Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
+    return BAD_VALUE;
+}
+
+
+} // namespace android
diff --git a/services/camera/libcameraservice/Camera2Client.h b/services/camera/libcameraservice/Camera2Client.h
new file mode 100644
index 0000000..709de0b
--- /dev/null
+++ b/services/camera/libcameraservice/Camera2Client.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
+#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
+
+#include "Camera2Device.h"
+#include "CameraService.h"
+
+namespace android {
+
+/**
+ * Implements the android.hardware.camera API on top of
+ * camera device HAL version 2.
+ */
+class Camera2Client : public CameraService::Client
+{
+public:
+    // ICamera interface (see ICamera for details)
+    virtual void            disconnect();
+    virtual status_t        connect(const sp<ICameraClient>& client);
+    virtual status_t        lock();
+    virtual status_t        unlock();
+    virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
+    virtual status_t        setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
+    virtual void            setPreviewCallbackFlag(int flag);
+    virtual status_t        startPreview();
+    virtual void            stopPreview();
+    virtual bool            previewEnabled();
+    virtual status_t        storeMetaDataInBuffers(bool enabled);
+    virtual status_t        startRecording();
+    virtual void            stopRecording();
+    virtual bool            recordingEnabled();
+    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
+    virtual status_t        autoFocus();
+    virtual status_t        cancelAutoFocus();
+    virtual status_t        takePicture(int msgType);
+    virtual status_t        setParameters(const String8& params);
+    virtual String8         getParameters() const;
+    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
+
+    // Interface used by CameraService
+    Camera2Client(const sp<CameraService>& cameraService,
+            const sp<ICameraClient>& cameraClient,
+            const sp<Camera2Device>& device,
+            int cameraId,
+            int cameraFacing,
+            int clientPid);
+    ~Camera2Client();
+
+    virtual status_t dump(int fd, const Vector<String16>& args);
+
+private:
+
+
+};
+
+}; // namespace android
+
+#endif
diff --git a/services/camera/libcameraservice/Camera2Device.cpp b/services/camera/libcameraservice/Camera2Device.cpp
new file mode 100644
index 0000000..ff5b057
--- /dev/null
+++ b/services/camera/libcameraservice/Camera2Device.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "Camera2Device"
+//#define LOG_NDEBUG 0
+
+#include <utils/Log.h>
+#include "Camera2Device.h"
+
+namespace android {
+
+Camera2Device::Camera2Device(const char *name):
+        mName(name),
+        mDevice(NULL)
+{
+
+}
+
+Camera2Device::~Camera2Device()
+{
+    if (mDevice) {
+        status_t res;
+        res = mDevice->common.close(&mDevice->common);
+        if (res != OK) {
+            ALOGE("Could not close camera2 %s: %s (%d)",
+                    mName, strerror(-res), res);
+        }
+    }
+}
+
+status_t Camera2Device::initialize(hw_module_t *module)
+{
+    status_t res;
+    res = module->methods->open(module, mName,
+            reinterpret_cast<hw_device_t**>(&mDevice));
+
+    if (res != OK) {
+        ALOGE("Could not open camera %s: %s (%d)", mName, strerror(-res), res);
+        return res;
+    }
+
+    if (mDevice->common.version != CAMERA_DEVICE_API_VERSION_2_0) {
+        ALOGE("Could not open camera %s: "
+                "Camera device is not version 2.0, reports %x instead",
+                mName, mDevice->common.version);
+        return BAD_VALUE;
+    }
+
+    return OK;
+}
+
+
+}; // namespace android
diff --git a/services/camera/libcameraservice/Camera2Device.h b/services/camera/libcameraservice/Camera2Device.h
new file mode 100644
index 0000000..0ce5421
--- /dev/null
+++ b/services/camera/libcameraservice/Camera2Device.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
+#define ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
+
+#include <utils/RefBase.h>
+#include <utils/Errors.h>
+#include "hardware/camera2.h"
+
+namespace android {
+
+class Camera2Device : public virtual RefBase {
+  public:
+    Camera2Device(const char *name);
+
+    ~Camera2Device();
+
+    status_t initialize(hw_module_t *module);
+  private:
+
+    const char *mName;
+    camera2_device_t *mDevice;
+
+};
+
+}; // namespace android
+
+#endif
diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp
index 03830c4..b273f6f 100644
--- a/services/camera/libcameraservice/CameraService.cpp
+++ b/services/camera/libcameraservice/CameraService.cpp
@@ -40,6 +40,8 @@
 #include "CameraService.h"
 #include "CameraClient.h"
 #include "CameraHardwareInterface.h"
+#include "Camera2Client.h"
+#include "Camera2Device.h"
 
 namespace android {
 
@@ -134,7 +136,6 @@
 sp<ICamera> CameraService::connect(
         const sp<ICameraClient>& cameraClient, int cameraId) {
     int callingPid = getCallingPid();
-    sp<CameraHardwareInterface> hardware = NULL;
 
     LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
 
@@ -190,13 +191,41 @@
     char camera_device_name[10];
     snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
 
-    hardware = new CameraHardwareInterface(camera_device_name);
-    if (hardware->initialize(&mModule->common) != OK) {
-        hardware.clear();
+    int deviceVersion;
+    if (mModule->common.module_api_version == CAMERA_MODULE_API_VERSION_2_0) {
+        deviceVersion = info.device_version;
+    } else {
+        deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
+    }
+
+    switch(deviceVersion) {
+    case CAMERA_DEVICE_API_VERSION_1_0: {
+        sp<CameraHardwareInterface> hardware =
+            new CameraHardwareInterface(camera_device_name);
+        if (hardware->initialize(&mModule->common) != OK) {
+            return NULL;
+        }
+
+        client = new CameraClient(this, cameraClient, hardware, cameraId,
+                info.facing, callingPid);
+        break;
+    }
+    case CAMERA_DEVICE_API_VERSION_2_0: {
+        sp<Camera2Device> hardware =
+            new Camera2Device(camera_device_name);
+        if (hardware->initialize(&mModule->common) != OK) {
+            return NULL;
+        }
+
+        client = new Camera2Client(this, cameraClient, hardware, cameraId,
+                info.facing, callingPid);
+        break;
+    }
+    default:
+        ALOGE("Unknown camera device HAL version: %d", deviceVersion);
         return NULL;
     }
 
-    client = new CameraClient(this, cameraClient, hardware, cameraId, info.facing, callingPid);
     mClient[cameraId] = client;
     LOG1("CameraService::connect X (id %d)", cameraId);
     return client;
diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h
index bf9a6bc..7f8ef6c 100644
--- a/services/camera/libcameraservice/CameraService.h
+++ b/services/camera/libcameraservice/CameraService.h
@@ -31,7 +31,6 @@
 
 class MemoryHeapBase;
 class MediaPlayer;
-class CameraHardwareInterface;
 
 class CameraService :
     public BinderService<CameraService>,