1 /*
2  * Copyright (C) 2017 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 /*
18  * This class is an abstraction to treat a capture device (e.g., a webcam)
19  * connected to the host computer as an image sensor.  The capture device must
20  * support both 360x240 and 640x480 resolutions.
21  *
22  * The characteristics of this sensor don't correspond to any actual sensor,
23  * but are not far off typical sensors.
24  */
25 
26 #ifndef HW_EMULATOR_CAMERA2_QEMU_SENSOR_H
27 #define HW_EMULATOR_CAMERA2_QEMU_SENSOR_H
28 
29 #include "fake-pipeline2/Base.h"
30 #include "QemuClient.h"
31 
32 #include <ui/GraphicBufferAllocator.h>
33 #include <ui/GraphicBufferMapper.h>
34 #include <utils/Mutex.h>
35 #include <utils/Thread.h>
36 #include <utils/Timers.h>
37 
38 namespace android {
39 
40 class EmulatedFakeCamera2;
41 
42 class QemuSensor: private Thread, public virtual RefBase {
43   public:
44    /*
45     * Args:
46     *     deviceName: File path where the capture device can be found (e.g.,
47     *                 "/dev/video0").
48     *     width: Width of pixel array.
49     *     height: Height of pixel array.
50     */
51     QemuSensor(const char *deviceName, uint32_t width, uint32_t height,
52                GraphicBufferMapper* gbm);
53     ~QemuSensor();
54 
55     /*
56      * Power Control
57      */
58 
59     status_t startUp();
60     status_t shutDown();
61 
62     /*
63      * Controls that can be updated every frame.
64      */
65 
66     void setFrameDuration(uint64_t ns);
67 
68     /*
69      * Each Buffer in "buffers" must be at least stride*height*2 bytes in size.
70      */
71     void setDestinationBuffers(Buffers *buffers);
72     /*
73      * To simplify tracking the sensor's current frame.
74      */
75     void setFrameNumber(uint32_t frameNumber);
76 
77     /*
78      * Synchronizing with sensor operation (vertical sync).
79      */
80 
81     /*
82      * Wait until the sensor outputs its next vertical sync signal, meaning it
83      * is starting readout of its latest frame of data.
84      *
85      * Returns:
86      *     true if vertical sync is signaled; false if the wait timed out.
87      */
88     bool waitForVSync(nsecs_t reltime);
89 
90     /*
91      * Wait until a new frame has been read out, and then return the time
92      * capture started. May return immediately if a new frame has been pushed
93      * since the last wait for a new frame.
94      *
95      * Returns:
96      *     true if new frame is returned; false if timed out.
97      */
98     bool waitForNewFrame(nsecs_t reltime, nsecs_t *captureTime);
99 
100     /*
101      * Interrupt event servicing from the sensor. Only triggers for sensor
102      * cycles that have valid buffers to write to.
103      */
104     struct QemuSensorListener {
105         enum Event {
106             EXPOSURE_START,
107         };
108 
109         virtual void onQemuSensorEvent(uint32_t frameNumber, Event e,
110                 nsecs_t timestamp) = 0;
111         virtual ~QemuSensorListener();
112     };
113 
114     void setQemuSensorListener(QemuSensorListener *listener);
115 
116     /*
117      * Static Sensor Characteristics
118      */
119     const uint32_t mWidth, mHeight;
120     const uint32_t mActiveArray[4];
121 
122     static const nsecs_t kExposureTimeRange[2];
123     static const nsecs_t kFrameDurationRange[2];
124     static const nsecs_t kMinVerticalBlank;
125 
126     static const int32_t kSensitivityRange[2];
127     static const uint32_t kDefaultSensitivity;
128 
129     static const char kHostCameraVerString[];
130 
131   private:
132     int32_t mLastRequestWidth, mLastRequestHeight;
133 
134     /*
135      * Defines possible states of the emulated camera device object.
136      */
137     enum EmulatedCameraDeviceState {
138         // Object has been constructed.
139         ECDS_CONSTRUCTED,
140         // Object has been initialized.
141         ECDS_INITIALIZED,
142         // Object has been connected to the physical device.
143         ECDS_CONNECTED,
144         // Camera device has been started.
145         ECDS_STARTED,
146     };
147     // Object state.
148     EmulatedCameraDeviceState mState;
149 
150     CameraQemuClient mCameraQemuClient;
151     const char *mDeviceName;
152     GraphicBufferAllocator* mGBA;
153     GraphicBufferMapper*    mGBM;
154 
155     // Always lock before accessing control parameters.
156     Mutex mControlMutex;
157     /*
158      * Control Parameters
159      */
160     Condition mVSync;
161     bool mGotVSync;
162     uint64_t mFrameDuration;
163     Buffers *mNextBuffers;
164     uint32_t mFrameNumber;
165 
166     // Always lock before accessing readout variables.
167     Mutex mReadoutMutex;
168     /*
169      * Readout Variables
170      */
171     Condition mReadoutAvailable;
172     Condition mReadoutComplete;
173     Buffers *mCapturedBuffers;
174     nsecs_t mCaptureTime;
175     QemuSensorListener *mListener;
176 
177     // Time of sensor startup (used for simulation zero-time point).
178     nsecs_t mStartupTime;
179     int32_t mHostCameraVer;
180 
181   private:
182     /*
183      * Inherited Thread Virtual Overrides
184      */
185     virtual status_t readyToRun();
186     /*
187      * QemuSensor capture operation main loop.
188      */
189     virtual bool threadLoop();
190 
191     /*
192      * Members only used by the processing thread.
193      */
194     nsecs_t mNextCaptureTime;
195     Buffers *mNextCapturedBuffers;
196 
197     void captureRGBA(uint32_t width, uint32_t height, uint32_t stride,
198                      int64_t *timestamp, buffer_handle_t* handle);
199     void captureYU12(uint32_t width, uint32_t height, uint32_t stride,
200                      int64_t *timestamp, buffer_handle_t* handle);
201     void captureRGBA(uint8_t *img, uint32_t width, uint32_t height,
202                      uint32_t stride, int64_t *timestamp);
203     void captureYU12(uint8_t *img, uint32_t width, uint32_t height,
204                      uint32_t stride, int64_t *timestamp);
205     void captureRGB(uint8_t *img, uint32_t width, uint32_t height,
206                     uint32_t stride, int64_t *timestamp);
207 };
208 
209 }; // end of namespace android
210 
211 #endif // HW_EMULATOR_CAMERA2_QEMU_SENSOR_H
212