blob: ff6eb39091f992b2e84660d235717ca31c0adcd4 [file] [log] [blame]
Scott Randolph83422792017-03-01 20:32:59 -08001/*
2 * Copyright (C) 2016 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 ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_H
18#define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_H
19
20#include <android/hardware/automotive/evs/1.0/types.h>
21#include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
22#include <ui/GraphicBuffer.h>
23
24#include <thread>
25
26
27namespace android {
28namespace hardware {
29namespace automotive {
30namespace evs {
31namespace V1_0 {
32namespace implementation {
33
34
Scott Randolphde9880e2017-03-30 14:04:12 -070035// From EvsEnumerator.h
36class EvsEnumerator;
37
38
Scott Randolph83422792017-03-01 20:32:59 -080039class EvsCamera : public IEvsCamera {
40public:
41 // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
Scott Randolphde9880e2017-03-30 14:04:12 -070042 Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override;
Scott Randolph83422792017-03-01 20:32:59 -080043 Return <EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override;
Scott Randolph83422792017-03-01 20:32:59 -080044 Return <EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream>& stream) override;
Scott Randolph83422792017-03-01 20:32:59 -080045 Return<void> doneWithFrame(const BufferDesc& buffer) override;
Scott Randolph83422792017-03-01 20:32:59 -080046 Return<void> stopVideoStream() override;
Scott Randolph83422792017-03-01 20:32:59 -080047 Return <int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override;
Scott Randolph83422792017-03-01 20:32:59 -080048 Return <EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override;
49
50 // Implementation details
Scott Randolphde9880e2017-03-30 14:04:12 -070051 EvsCamera(const char *id);
Scott Randolph83422792017-03-01 20:32:59 -080052 virtual ~EvsCamera() override;
Scott Randolphde9880e2017-03-30 14:04:12 -070053 void forceShutdown(); // This gets called if another caller "steals" ownership of the camera
Scott Randolph83422792017-03-01 20:32:59 -080054
55 const CameraDesc& getDesc() { return mDescription; };
56
57 static const char kCameraName_Backup[];
Scott Randolph83422792017-03-01 20:32:59 -080058
59private:
60 // These three functions are expected to be called while mAccessLock is held
61 bool setAvailableFrames_Locked(unsigned bufferCount);
Scott Randolph83422792017-03-01 20:32:59 -080062 unsigned increaseAvailableFrames_Locked(unsigned numToAdd);
Scott Randolph83422792017-03-01 20:32:59 -080063 unsigned decreaseAvailableFrames_Locked(unsigned numToRemove);
64
65 void generateFrames();
Scott Randolph83422792017-03-01 20:32:59 -080066 void fillTestFrame(const BufferDesc& buff);
67
Scott Randolphde9880e2017-03-30 14:04:12 -070068 sp<EvsEnumerator> mEnumerator; // The enumerator object that created this camera
69
70 CameraDesc mDescription = {}; // The properties of this camera
Scott Randolph83422792017-03-01 20:32:59 -080071
72 std::thread mCaptureThread; // The thread we'll use to synthesize frames
73
Scott Randolphde9880e2017-03-30 14:04:12 -070074 uint32_t mWidth = 0; // Horizontal pixel count in the buffers
75 uint32_t mHeight = 0; // Vertical pixel count in the buffers
76 uint32_t mFormat = 0; // Values from android_pixel_format_t [TODO: YUV? Leave opaque?]
77 uint32_t mUsage = 0; // Values from from Gralloc.h
78 uint32_t mStride = 0; // Bytes per line in the buffers
Scott Randolph83422792017-03-01 20:32:59 -080079
80 sp <IEvsCameraStream> mStream = nullptr; // The callback used to deliver each frame
81
82 struct BufferRecord {
83 buffer_handle_t handle;
84 bool inUse;
85
86 explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {};
87 };
88
89 std::vector <BufferRecord> mBuffers; // Graphics buffers to transfer images
90 unsigned mFramesAllowed; // How many buffers are we currently using
91 unsigned mFramesInUse; // How many buffers are currently outstanding
92
93 enum StreamStateValues {
94 STOPPED,
95 RUNNING,
96 STOPPING,
Scott Randolphde9880e2017-03-30 14:04:12 -070097 DEAD,
Scott Randolph83422792017-03-01 20:32:59 -080098 };
99 StreamStateValues mStreamState;
100
Scott Randolphde9880e2017-03-30 14:04:12 -0700101 // Synchronization necessary to deconflict mCaptureThread from the main service thread
Scott Randolph83422792017-03-01 20:32:59 -0800102 std::mutex mAccessLock;
103};
104
105} // namespace implementation
106} // namespace V1_0
107} // namespace evs
108} // namespace automotive
109} // namespace hardware
110} // namespace android
111
112#endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_H