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