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) override;
86 
87     /* Copy the current preview frame to |buffer| */
88     status_t getCurrentPreviewFrame(void* buffer) override;
89 
90     /* Get a pointer to the current frame, lock it first using FrameLock in
91      * EmulatedCameraDevice class */
92     const void* getCurrentFrame() override;
93 
94     /***************************************************************************
95      * Worker thread management overrides.
96      * See declarations of these methods in EmulatedCameraDevice class for
97      * information on each of these methods.
98      **************************************************************************/
99 
100 protected:
101     /* Implementation of the frame production routine. */
102     bool produceFrame(void* buffer) override;
103 
104     void* getPrimaryBuffer() override;
105     void* getSecondaryBuffer() override;
106 
107     /***************************************************************************
108      * Qemu camera device data members
109      **************************************************************************/
110 
111 private:
112     /* Qemu client that is used to communicate with the 'emulated camera'
113      * service, created for this instance in the emulator. */
114     CameraQemuClient    mQemuClient;
115 
116     /* Name of the camera device connected to the host. */
117     String8             mDeviceName;
118 
119     /* Current preview framebuffer. */
120     std::vector<uint32_t> mPreviewFrames[2];
121 
122     /* Since the Qemu camera needs to keep track of two buffers per frame we
123      * use a pair here. One frame is the camera frame and the other is the
124      * preview frame. These are in different formats and instead of converting
125      * them in the guest it's more efficient to have the host provide the same
126      * frame in two different formats. The first buffer in the pair is the raw
127      * frame and the second buffer is the RGB encoded frame. The downside of
128      * this is that we need to override the getCurrentFrame and
129      * getCurrentPreviewFrame methods to extract the correct buffer from this
130      * pair. */
131     using FrameBufferPair = std::pair<uint8_t*, uint32_t*>;
132     FrameBufferPair     mFrameBufferPairs[2];
133 
134 };
135 
136 }; /* namespace android */
137 
138 #endif  /* HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA_DEVICE_H */
139