1 /*
2 * Copyright (C) 2023 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 // #define LOG_NDEBUG 0
18 #define LOG_TAG "VirtualCameraStream"
19 #include "VirtualCameraStream.h"
20
21 #include <cstdint>
22 #include <memory>
23 #include <mutex>
24 #include <tuple>
25 #include <utility>
26
27 #include "EGL/egl.h"
28 #include "aidl/android/hardware/camera/device/Stream.h"
29 #include "aidlcommonsupport/NativeHandle.h"
30 #include "android/hardware_buffer.h"
31 #include "cutils/native_handle.h"
32 #include "ui/GraphicBuffer.h"
33 #include "ui/GraphicBufferMapper.h"
34
35 namespace android {
36 namespace companion {
37 namespace virtualcamera {
38
39 using ::aidl::android::hardware::camera::device::Stream;
40 using ::aidl::android::hardware::common::NativeHandle;
41
42 namespace {
43
createGraphicBuffer(GraphicBufferMapper & mapper,const buffer_handle_t bufferHandle)44 sp<GraphicBuffer> createGraphicBuffer(GraphicBufferMapper& mapper,
45 const buffer_handle_t bufferHandle) {
46 uint64_t width;
47 uint64_t height;
48 uint64_t usage;
49 uint64_t layerCount;
50 ui::PixelFormat pixelFormat;
51 if (mapper.getWidth(bufferHandle, &width) != NO_ERROR ||
52 mapper.getHeight(bufferHandle, &height) != NO_ERROR ||
53 mapper.getUsage(bufferHandle, &usage) != NO_ERROR ||
54 mapper.getLayerCount(bufferHandle, &layerCount) != NO_ERROR ||
55 mapper.getPixelFormatRequested(bufferHandle, &pixelFormat) != NO_ERROR) {
56 ALOGE("Error fetching metadata for the imported YCbCr420 buffer handle.");
57 return nullptr;
58 }
59
60 return sp<GraphicBuffer>::make(
61 bufferHandle, GraphicBuffer::HandleWrapMethod::TAKE_HANDLE, width, height,
62 static_cast<int>(pixelFormat), layerCount, usage, width);
63 }
64
importBufferInternal(const NativeHandle & aidlHandle)65 std::shared_ptr<AHardwareBuffer> importBufferInternal(
66 const NativeHandle& aidlHandle) {
67 if (aidlHandle.fds.empty()) {
68 ALOGE("Empty handle - nothing to import");
69 return nullptr;
70 }
71 std::unique_ptr<native_handle_t, int (*)(native_handle_t*)> nativeHandle(
72 ::android::makeFromAidl(aidlHandle), native_handle_delete);
73
74 GraphicBufferMapper& mapper = GraphicBufferMapper::get();
75
76 buffer_handle_t bufferHandle;
77 // Use importBufferNoValidate to rely on ground-truth metadata passed along
78 // the buffer.
79 int ret = mapper.importBufferNoValidate(nativeHandle.get(), &bufferHandle);
80 if (ret != NO_ERROR) {
81 ALOGE("Failed to import buffer handle: %d", ret);
82 return nullptr;
83 }
84
85 sp<GraphicBuffer> buf = createGraphicBuffer(mapper, bufferHandle);
86
87 if (buf == nullptr || buf->initCheck() != NO_ERROR) {
88 ALOGE("Imported graphic buffer is not correcly initialized.");
89 return nullptr;
90 }
91
92 AHardwareBuffer* rawPtr = buf->toAHardwareBuffer();
93 AHardwareBuffer_acquire(rawPtr);
94
95 return std::shared_ptr<AHardwareBuffer>(buf->toAHardwareBuffer(),
96 AHardwareBuffer_release);
97 }
98
99 } // namespace
100
VirtualCameraStream(const Stream & stream)101 VirtualCameraStream::VirtualCameraStream(const Stream& stream)
102 : mStreamConfig(stream) {
103 }
104
importBuffer(const::aidl::android::hardware::camera::device::StreamBuffer & buffer)105 std::shared_ptr<AHardwareBuffer> VirtualCameraStream::importBuffer(
106 const ::aidl::android::hardware::camera::device::StreamBuffer& buffer) {
107 auto hwBufferPtr = importBufferInternal(buffer.buffer);
108 if (hwBufferPtr != nullptr) {
109 std::lock_guard<std::mutex> lock(mLock);
110 mBuffers.emplace(std::piecewise_construct,
111 std::forward_as_tuple(buffer.bufferId),
112 std::forward_as_tuple(hwBufferPtr));
113 }
114 return hwBufferPtr;
115 }
116
getHardwareBuffer(const int bufferId)117 std::shared_ptr<AHardwareBuffer> VirtualCameraStream::getHardwareBuffer(
118 const int bufferId) {
119 std::lock_guard<std::mutex> lock(mLock);
120 return getHardwareBufferLocked(bufferId);
121 }
122
getEglFrameBuffer(const EGLDisplay eglDisplay,const int bufferId)123 std::shared_ptr<EglFrameBuffer> VirtualCameraStream::getEglFrameBuffer(
124 const EGLDisplay eglDisplay, const int bufferId) {
125 const FramebufferMapKey key(bufferId, eglDisplay);
126
127 std::lock_guard<std::mutex> lock(mLock);
128
129 auto it = mEglFramebuffers.find(key);
130 if (it != mEglFramebuffers.end()) {
131 return it->second;
132 }
133
134 std::shared_ptr<AHardwareBuffer> hwBufferPtr =
135 getHardwareBufferLocked(bufferId);
136 if (hwBufferPtr == nullptr) {
137 return nullptr;
138 }
139 std::shared_ptr<EglFrameBuffer> framebufferPtr =
140 std::make_shared<EglFrameBuffer>(eglDisplay, hwBufferPtr);
141 mEglFramebuffers.emplace(std::piecewise_construct, std::forward_as_tuple(key),
142 std::forward_as_tuple(framebufferPtr));
143
144 return framebufferPtr;
145 }
146
getHardwareBufferLocked(const int bufferId)147 std::shared_ptr<AHardwareBuffer> VirtualCameraStream::getHardwareBufferLocked(
148 const int bufferId) {
149 auto it = mBuffers.find(bufferId);
150 return it != mBuffers.end() ? it->second : nullptr;
151 }
152
removeBuffer(int bufferId)153 bool VirtualCameraStream::removeBuffer(int bufferId) {
154 std::lock_guard<std::mutex> lock(mLock);
155
156 return mBuffers.erase(bufferId) == 1;
157 }
158
getStreamConfig() const159 Stream VirtualCameraStream::getStreamConfig() const {
160 return mStreamConfig;
161 }
162
163 } // namespace virtualcamera
164 } // namespace companion
165 } // namespace android
166