1 /* 2 * Copyright (C) 2011 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 HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA_DEVICE_H 18 #define HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA_DEVICE_H 19 20 /* 21 * Contains declaration of a class EmulatedQemuCameraDevice that encapsulates 22 * an emulated camera device connected to the host. 23 */ 24 25 #include "EmulatedCameraDevice.h" 26 #include "QemuClient.h" 27 28 namespace android { 29 30 class EmulatedQemuCamera; 31 32 /* Encapsulates an emulated camera device connected to the host. 33 */ 34 class EmulatedQemuCameraDevice : public EmulatedCameraDevice { 35 public: 36 /* Constructs EmulatedQemuCameraDevice instance. */ 37 explicit EmulatedQemuCameraDevice(EmulatedQemuCamera* camera_hal); 38 39 /* Destructs EmulatedQemuCameraDevice instance. */ 40 ~EmulatedQemuCameraDevice(); 41 42 /*************************************************************************** 43 * Public API 44 **************************************************************************/ 45 46 public: 47 /* Initializes EmulatedQemuCameraDevice instance. 48 * Param: 49 * device_name - Name of the camera device connected to the host. The name 50 * that is used here must have been reported by the 'factory' camera 51 * service when it listed camera devices connected to the host. 52 * Return: 53 * NO_ERROR on success, or an appropriate error status. 54 */ 55 status_t Initialize(const char* device_name); 56 57 /*************************************************************************** 58 * Emulated camera device abstract interface implementation. 59 * See declarations of these methods in EmulatedCameraDevice class for 60 * information on each of these methods. 61 **************************************************************************/ 62 63 public: 64 /* Connects to the camera device. */ 65 status_t connectDevice(); 66 67 /* Disconnects from the camera device. */ 68 status_t disconnectDevice(); 69 70 /* Starts capturing frames from the camera device. */ 71 status_t startDevice(int width, int height, uint32_t pix_fmt); 72 73 /* Stops capturing frames from the camera device. */ 74 status_t stopDevice(); 75 76 /*************************************************************************** 77 * EmulatedCameraDevice virtual overrides 78 * See declarations of these methods in EmulatedCameraDevice class for 79 * information on each of these methods. 80 **************************************************************************/ 81 82 public: 83 84 /* Copy the current frame to |buffer| */ 85 status_t getCurrentFrame(void* buffer, uint32_t pixelFormat, 86 int64_t* timestamp) override; 87 88 /* Copy the current preview frame to |buffer| */ 89 status_t getCurrentPreviewFrame(void* buffer, 90 int64_t* timestamp) override; 91 92 /* Get a pointer to the current frame, lock it first using FrameLock in 93 * EmulatedCameraDevice class */ 94 const void* getCurrentFrame() override; 95 96 /*************************************************************************** 97 * Worker thread management overrides. 98 * See declarations of these methods in EmulatedCameraDevice class for 99 * information on each of these methods. 100 **************************************************************************/ 101 102 protected: 103 /* Implementation of the frame production routine. */ 104 bool produceFrame(void* buffer, int64_t* timestamp) override; 105 106 void* getPrimaryBuffer() override; 107 void* getSecondaryBuffer() override; 108 109 /*************************************************************************** 110 * Qemu camera device data members 111 **************************************************************************/ 112 113 private: 114 /* Qemu client that is used to communicate with the 'emulated camera' 115 * service, created for this instance in the emulator. */ 116 CameraQemuClient mQemuClient; 117 118 /* Name of the camera device connected to the host. */ 119 String8 mDeviceName; 120 121 /* Current preview framebuffer. */ 122 std::vector<uint32_t> mPreviewFrames[2]; 123 124 /* Since the Qemu camera needs to keep track of two buffers per frame we 125 * use a pair here. One frame is the camera frame and the other is the 126 * preview frame. These are in different formats and instead of converting 127 * them in the guest it's more efficient to have the host provide the same 128 * frame in two different formats. The first buffer in the pair is the raw 129 * frame and the second buffer is the RGB encoded frame. The downside of 130 * this is that we need to override the getCurrentFrame and 131 * getCurrentPreviewFrame methods to extract the correct buffer from this 132 * pair. */ 133 using FrameBufferPair = std::pair<uint8_t*, uint32_t*>; 134 FrameBufferPair mFrameBufferPairs[2]; 135 using EmulatedCameraDevice::Initialize; 136 }; 137 138 }; /* namespace android */ 139 140 #endif /* HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA_DEVICE_H */ 141