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