blob: 9669135ad67905e6b2bf52e7bb520f4db750d771 [file] [log] [blame]
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -07001/*
2 * Copyright (C) 2018 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
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070017#include <poll.h>
18
Fan Xu069e8382018-11-16 16:28:08 -080019#include <android-base/unique_fd.h>
Fan Xu021776e2018-12-05 13:34:48 -080020#include <android/frameworks/bufferhub/1.0/IBufferHub.h>
21#include <log/log.h>
Fan Xu069e8382018-11-16 16:28:08 -080022#include <ui/BufferHubBuffer.h>
Fan Xucfbe0742018-11-21 15:03:32 -080023#include <ui/BufferHubDefs.h>
Fan Xu021776e2018-12-05 13:34:48 -080024#include <utils/Trace.h>
Fan Xu069e8382018-11-16 16:28:08 -080025
Fan Xu021776e2018-12-05 13:34:48 -080026using ::android::base::unique_fd;
27using ::android::BufferHubDefs::AnyClientAcquired;
28using ::android::BufferHubDefs::AnyClientGained;
29using ::android::BufferHubDefs::IsClientAcquired;
30using ::android::BufferHubDefs::IsClientGained;
31using ::android::BufferHubDefs::IsClientPosted;
32using ::android::BufferHubDefs::IsClientReleased;
33using ::android::frameworks::bufferhub::V1_0::BufferHubStatus;
34using ::android::frameworks::bufferhub::V1_0::BufferTraits;
35using ::android::frameworks::bufferhub::V1_0::IBufferClient;
36using ::android::frameworks::bufferhub::V1_0::IBufferHub;
37using ::android::hardware::hidl_handle;
38using ::android::hardware::graphics::common::V1_2::HardwareBufferDescription;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070039
40namespace android {
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070041
Fan Xu021776e2018-12-05 13:34:48 -080042std::unique_ptr<BufferHubBuffer> BufferHubBuffer::Create(uint32_t width, uint32_t height,
43 uint32_t layerCount, uint32_t format,
44 uint64_t usage, size_t userMetadataSize) {
45 auto buffer = std::unique_ptr<BufferHubBuffer>(
46 new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize));
47 return buffer->IsValid() ? std::move(buffer) : nullptr;
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070048}
49
Fan Xu021776e2018-12-05 13:34:48 -080050std::unique_ptr<BufferHubBuffer> BufferHubBuffer::Import(const native_handle_t* token) {
51 if (token == nullptr) {
52 ALOGE("%s: token cannot be nullptr!", __FUNCTION__);
53 return nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070054 }
Fan Xu021776e2018-12-05 13:34:48 -080055
56 auto buffer = std::unique_ptr<BufferHubBuffer>(new BufferHubBuffer(token));
57 return buffer->IsValid() ? std::move(buffer) : nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070058}
59
60BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount,
Fan Xu021776e2018-12-05 13:34:48 -080061 uint32_t format, uint64_t usage, size_t userMetadataSize) {
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070062 ATRACE_CALL();
Fan Xu021776e2018-12-05 13:34:48 -080063 ALOGD("%s: width=%u height=%u layerCount=%u, format=%u "
64 "usage=%" PRIx64 " mUserMetadataSize=%zu",
65 __FUNCTION__, width, height, layerCount, format, usage, userMetadataSize);
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070066
Fan Xu021776e2018-12-05 13:34:48 -080067 sp<IBufferHub> bufferhub = IBufferHub::getService();
68 if (bufferhub.get() == nullptr) {
69 ALOGE("%s: BufferHub service not found!", __FUNCTION__);
70 return;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070071 }
72
Fan Xu021776e2018-12-05 13:34:48 -080073 AHardwareBuffer_Desc aDesc = {width, height, layerCount, format,
74 usage, /*stride=*/0UL, /*rfu0=*/0UL, /*rfu1=*/0ULL};
75 HardwareBufferDescription desc;
76 memcpy(&desc, &aDesc, sizeof(HardwareBufferDescription));
77
78 BufferHubStatus ret;
79 sp<IBufferClient> client;
80 BufferTraits bufferTraits;
81 IBufferHub::allocateBuffer_cb alloc_cb = [&](const auto& status, const auto& outClient,
82 const auto& traits) {
83 ret = status;
84 client = std::move(outClient);
85 bufferTraits = std::move(traits);
86 };
87
88 if (!bufferhub->allocateBuffer(desc, static_cast<uint32_t>(userMetadataSize), alloc_cb)
89 .isOk()) {
90 ALOGE("%s: allocateBuffer transaction failed!", __FUNCTION__);
91 return;
92 } else if (ret != BufferHubStatus::NO_ERROR) {
93 ALOGE("%s: allocateBuffer failed with error %u.", __FUNCTION__, ret);
94 return;
95 } else if (client == nullptr) {
96 ALOGE("%s: allocateBuffer got null BufferClient.", __FUNCTION__);
97 return;
98 }
99
100 const int importRet = initWithBufferTraits(bufferTraits);
101 if (importRet < 0) {
102 ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet));
103 client->close();
104 }
105 mBufferClient = std::move(client);
106}
107
108BufferHubBuffer::BufferHubBuffer(const native_handle_t* token) {
109 sp<IBufferHub> bufferhub = IBufferHub::getService();
110 if (bufferhub.get() == nullptr) {
111 ALOGE("%s: BufferHub service not found!", __FUNCTION__);
112 return;
113 }
114
115 BufferHubStatus ret;
116 sp<IBufferClient> client;
117 BufferTraits bufferTraits;
118 IBufferHub::importBuffer_cb import_cb = [&](const auto& status, const auto& outClient,
119 const auto& traits) {
120 ret = status;
121 client = std::move(outClient);
122 bufferTraits = std::move(traits);
123 };
124
125 // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership
126 // transfer.
127 if (!bufferhub->importBuffer(hidl_handle(token), import_cb).isOk()) {
128 ALOGE("%s: importBuffer transaction failed!", __FUNCTION__);
129 return;
130 } else if (ret != BufferHubStatus::NO_ERROR) {
131 ALOGE("%s: importBuffer failed with error %u.", __FUNCTION__, ret);
132 return;
133 } else if (client == nullptr) {
134 ALOGE("%s: importBuffer got null BufferClient.", __FUNCTION__);
135 return;
136 }
137
138 const int importRet = initWithBufferTraits(bufferTraits);
139 if (importRet < 0) {
140 ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet));
141 client->close();
142 }
143 mBufferClient = std::move(client);
144}
145
146BufferHubBuffer::~BufferHubBuffer() {
147 // Close buffer client to avoid possible race condition: user could first duplicate and hold
148 // token with the original buffer gone, and then try to import the token. The close function
149 // will explicitly invalidate the token to avoid this.
150 if (mBufferClient != nullptr) {
151 if (!mBufferClient->close().isOk()) {
152 ALOGE("%s: close BufferClient transaction failed!", __FUNCTION__);
153 }
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700154 }
155}
156
Fan Xu021776e2018-12-05 13:34:48 -0800157int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) {
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700158 ATRACE_CALL();
159
Fan Xu021776e2018-12-05 13:34:48 -0800160 if (bufferTraits.bufferInfo.getNativeHandle() == nullptr) {
161 ALOGE("%s: missing buffer info handle.", __FUNCTION__);
162 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700163 }
164
Fan Xu021776e2018-12-05 13:34:48 -0800165 if (bufferTraits.bufferHandle.getNativeHandle() == nullptr) {
166 ALOGE("%s: missing gralloc handle.", __FUNCTION__);
167 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700168 }
169
Fan Xu5cf47bc2019-01-15 15:02:15 -0800170 int bufferId = bufferTraits.bufferInfo->data[2];
Fan Xu021776e2018-12-05 13:34:48 -0800171 if (bufferId < 0) {
172 ALOGE("%s: Received an invalid (negative) id!", __FUNCTION__);
173 return -EINVAL;
174 }
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700175
Fan Xu021776e2018-12-05 13:34:48 -0800176 uint32_t clientBitMask;
Fan Xu5cf47bc2019-01-15 15:02:15 -0800177 memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[3], sizeof(clientBitMask));
Fan Xu021776e2018-12-05 13:34:48 -0800178 if (clientBitMask == 0U) {
179 ALOGE("%s: Received a invalid client state mask!", __FUNCTION__);
180 return -EINVAL;
181 }
182
Fan Xu5cf47bc2019-01-15 15:02:15 -0800183 const int eventFd = bufferTraits.bufferInfo->data[1];
184 if (eventFd < 0) {
185 ALOGE("%s: Received a invalid event fd!", __FUNCTION__);
186 return -EINVAL;
187 }
188 mEventFd = BufferHubEventFd(eventFd);
189
Fan Xu021776e2018-12-05 13:34:48 -0800190 // Import the metadata. Dup since hidl_handle owns the fd
Nick Kralevich876a0492019-01-14 11:51:50 -0800191 unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0));
Fan Xu021776e2018-12-05 13:34:48 -0800192 mMetadata = BufferHubMetadata::Import(std::move(ashmemFd));
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700193
194 if (!mMetadata.IsValid()) {
Tianyu Jiangdf9d91d2018-12-17 10:48:54 -0800195 ALOGE("%s: invalid metadata.", __FUNCTION__);
Fan Xu021776e2018-12-05 13:34:48 -0800196 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700197 }
198
Fan Xu021776e2018-12-05 13:34:48 -0800199 uint32_t userMetadataSize;
Fan Xu5cf47bc2019-01-15 15:02:15 -0800200 memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[4], sizeof(userMetadataSize));
Fan Xu021776e2018-12-05 13:34:48 -0800201 if (mMetadata.user_metadata_size() != userMetadataSize) {
202 ALOGE("%s: user metadata size not match: expected %u, actual %zu.", __FUNCTION__,
203 userMetadataSize, mMetadata.user_metadata_size());
204 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700205 }
206
Fan Xu021776e2018-12-05 13:34:48 -0800207 size_t metadataSize = static_cast<size_t>(mMetadata.metadata_size());
Fan Xucfbe0742018-11-21 15:03:32 -0800208 if (metadataSize < BufferHubDefs::kMetadataHeaderSize) {
Tianyu Jiangdf9d91d2018-12-17 10:48:54 -0800209 ALOGE("%s: metadata too small: %zu", __FUNCTION__, metadataSize);
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700210 return -EINVAL;
211 }
212
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800213 // Populate shortcuts to the atomics in metadata.
214 auto metadata_header = mMetadata.metadata_header();
215 buffer_state_ = &metadata_header->buffer_state;
216 fence_state_ = &metadata_header->fence_state;
217 active_clients_bit_mask_ = &metadata_header->active_clients_bit_mask;
Tianyu Jiang2ceb3202018-12-17 12:58:34 -0800218 // The C++ standard recommends (but does not require) that lock-free atomic operations are
219 // also address-free, that is, suitable for communication between processes using shared
220 // memory.
221 LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) ||
222 !std::atomic_is_lock_free(fence_state_) ||
223 !std::atomic_is_lock_free(active_clients_bit_mask_),
224 "Atomic variables in ashmen are not lock free.");
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800225
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700226 // Import the buffer: We only need to hold on the native_handle_t here so that
227 // GraphicBuffer instance can be created in future.
Fan Xu021776e2018-12-05 13:34:48 -0800228 mBufferHandle = std::move(bufferTraits.bufferHandle);
229 memcpy(&mBufferDesc, &bufferTraits.bufferDesc, sizeof(AHardwareBuffer_Desc));
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700230
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700231 mId = bufferId;
Fan Xu021776e2018-12-05 13:34:48 -0800232 mClientStateMask = clientBitMask;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700233
234 // TODO(b/112012161) Set up shared fences.
Fan Xu021776e2018-12-05 13:34:48 -0800235 ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, mId,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800236 buffer_state_->load(std::memory_order_acquire));
237 return 0;
238}
239
240int BufferHubBuffer::Gain() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800241 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800242 if (IsClientGained(current_buffer_state, mClientStateMask)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800243 ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800244 mClientStateMask);
245 return 0;
246 }
247 do {
248 if (AnyClientGained(current_buffer_state & (~mClientStateMask)) ||
249 AnyClientAcquired(current_buffer_state)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800250 ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800251 __FUNCTION__, mId, mClientStateMask, current_buffer_state);
252 return -EBUSY;
253 }
254 // Change the buffer state to gained state, whose value happens to be the same as
255 // mClientStateMask.
256 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, mClientStateMask,
257 std::memory_order_acq_rel,
258 std::memory_order_acquire));
259 // TODO(b/119837586): Update fence state and return GPU fence.
260 return 0;
261}
262
263int BufferHubBuffer::Post() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800264 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Fan Xu021776e2018-12-05 13:34:48 -0800265 uint32_t updated_buffer_state = (~mClientStateMask) & BufferHubDefs::kHighBitsMask;
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800266 do {
267 if (!IsClientGained(current_buffer_state, mClientStateMask)) {
268 ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d "
Tianyu Jianga99f9112018-12-13 18:23:07 -0800269 "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800270 __FUNCTION__, mId, mClientStateMask, current_buffer_state);
271 return -EBUSY;
272 }
273 // Set the producer client buffer state to released, other clients' buffer state to posted.
Tianyu Jiangec97b762019-01-07 17:14:02 -0800274 // Post to all existing and non-existing clients.
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800275 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
276 std::memory_order_acq_rel,
277 std::memory_order_acquire));
278 // TODO(b/119837586): Update fence state and return GPU fence if needed.
279 return 0;
280}
281
282int BufferHubBuffer::Acquire() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800283 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800284 if (IsClientAcquired(current_buffer_state, mClientStateMask)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800285 ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800286 mClientStateMask);
287 return 0;
288 }
Tianyu Jianga99f9112018-12-13 18:23:07 -0800289 uint32_t updated_buffer_state = 0U;
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800290 do {
291 if (!IsClientPosted(current_buffer_state, mClientStateMask)) {
292 ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d "
Tianyu Jianga99f9112018-12-13 18:23:07 -0800293 "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800294 __FUNCTION__, mId, mClientStateMask, current_buffer_state);
295 return -EBUSY;
296 }
297 // Change the buffer state for this consumer from posted to acquired.
298 updated_buffer_state = current_buffer_state ^ mClientStateMask;
299 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
300 std::memory_order_acq_rel,
301 std::memory_order_acquire));
302 // TODO(b/119837586): Update fence state and return GPU fence.
303 return 0;
304}
305
306int BufferHubBuffer::Release() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800307 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800308 if (IsClientReleased(current_buffer_state, mClientStateMask)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800309 ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800310 mClientStateMask);
311 return 0;
312 }
Tianyu Jianga99f9112018-12-13 18:23:07 -0800313 uint32_t updated_buffer_state = 0U;
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800314 do {
315 updated_buffer_state = current_buffer_state & (~mClientStateMask);
316 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
317 std::memory_order_acq_rel,
318 std::memory_order_acquire));
319 // TODO(b/119837586): Update fence state and return GPU fence if needed.
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700320 return 0;
321}
322
Fan Xu021776e2018-12-05 13:34:48 -0800323bool BufferHubBuffer::IsValid() const {
Fan Xu021776e2018-12-05 13:34:48 -0800324 return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U &&
Fan Xu5cf47bc2019-01-15 15:02:15 -0800325 mEventFd.get() >= 0 && mMetadata.IsValid() && mBufferClient != nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700326}
327
Fan Xu021776e2018-12-05 13:34:48 -0800328native_handle_t* BufferHubBuffer::Duplicate() {
329 if (mBufferClient == nullptr) {
330 ALOGE("%s: missing BufferClient!", __FUNCTION__);
331 return nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700332 }
Fan Xu021776e2018-12-05 13:34:48 -0800333
334 hidl_handle token;
335 BufferHubStatus ret;
336 IBufferClient::duplicate_cb dup_cb = [&](const auto& outToken, const auto& status) {
337 token = std::move(outToken);
338 ret = status;
339 };
340
341 if (!mBufferClient->duplicate(dup_cb).isOk()) {
342 ALOGE("%s: duplicate transaction failed!", __FUNCTION__);
343 return nullptr;
344 } else if (ret != BufferHubStatus::NO_ERROR) {
345 ALOGE("%s: duplicate failed with error %u.", __FUNCTION__, ret);
346 return nullptr;
347 } else if (token.getNativeHandle() == nullptr) {
348 ALOGE("%s: duplicate got null token.", __FUNCTION__);
349 return nullptr;
350 }
351
352 return native_handle_clone(token.getNativeHandle());
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700353}
354
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700355} // namespace android