summaryrefslogtreecommitdiff
path: root/libs/ui/BufferHubBuffer.cpp
blob: 1dfc1e9e88b08cd23710f9cb0cb77456d6948afa (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright (C) 2018 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.
 */

#define LOG_TAG "BufferHubBuffer"
#include <poll.h>

#include <android-base/unique_fd.h>
#include <android/frameworks/bufferhub/1.0/IBufferHub.h>
#include <log/log.h>
#include <ui/BufferHubBuffer.h>
#include <ui/BufferHubDefs.h>
#include <utils/Trace.h>

using ::android::base::unique_fd;
using ::android::BufferHubDefs::isAnyClientAcquired;
using ::android::BufferHubDefs::isAnyClientGained;
using ::android::BufferHubDefs::isClientAcquired;
using ::android::BufferHubDefs::isClientGained;
using ::android::BufferHubDefs::isClientPosted;
using ::android::BufferHubDefs::isClientReleased;
using ::android::frameworks::bufferhub::V1_0::BufferHubStatus;
using ::android::frameworks::bufferhub::V1_0::BufferTraits;
using ::android::frameworks::bufferhub::V1_0::IBufferClient;
using ::android::frameworks::bufferhub::V1_0::IBufferHub;
using ::android::hardware::hidl_handle;
using ::android::hardware::graphics::common::V1_2::HardwareBufferDescription;

namespace android {

std::unique_ptr<BufferHubBuffer> BufferHubBuffer::create(uint32_t width, uint32_t height,
                                                         uint32_t layerCount, uint32_t format,
                                                         uint64_t usage, size_t userMetadataSize) {
    auto buffer = std::unique_ptr<BufferHubBuffer>(
            new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize));
    return buffer->isValid() ? std::move(buffer) : nullptr;
}

std::unique_ptr<BufferHubBuffer> BufferHubBuffer::import(const sp<NativeHandle>& token) {
    if (token == nullptr || token.get() == nullptr) {
        ALOGE("%s: token cannot be nullptr!", __FUNCTION__);
        return nullptr;
    }

    auto buffer = std::unique_ptr<BufferHubBuffer>(new BufferHubBuffer(token));
    return buffer->isValid() ? std::move(buffer) : nullptr;
}

BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount,
                                 uint32_t format, uint64_t usage, size_t userMetadataSize) {
    ATRACE_CALL();
    ALOGD("%s: width=%u height=%u layerCount=%u, format=%u "
          "usage=%" PRIx64 " mUserMetadataSize=%zu",
          __FUNCTION__, width, height, layerCount, format, usage, userMetadataSize);

    sp<IBufferHub> bufferhub = IBufferHub::getService();
    if (bufferhub.get() == nullptr) {
        ALOGE("%s: BufferHub service not found!", __FUNCTION__);
        return;
    }

    AHardwareBuffer_Desc aDesc = {width, height,         layerCount,   format,
                                  usage, /*stride=*/0UL, /*rfu0=*/0UL, /*rfu1=*/0ULL};
    HardwareBufferDescription desc;
    memcpy(&desc, &aDesc, sizeof(HardwareBufferDescription));

    BufferHubStatus ret;
    sp<IBufferClient> client;
    BufferTraits bufferTraits;
    IBufferHub::allocateBuffer_cb allocCb = [&](const auto& status, const auto& outClient,
                                                const auto& outTraits) {
        ret = status;
        client = std::move(outClient);
        bufferTraits = std::move(outTraits);
    };

    if (!bufferhub->allocateBuffer(desc, static_cast<uint32_t>(userMetadataSize), allocCb).isOk()) {
        ALOGE("%s: allocateBuffer transaction failed!", __FUNCTION__);
        return;
    } else if (ret != BufferHubStatus::NO_ERROR) {
        ALOGE("%s: allocateBuffer failed with error %u.", __FUNCTION__, ret);
        return;
    } else if (client == nullptr) {
        ALOGE("%s: allocateBuffer got null BufferClient.", __FUNCTION__);
        return;
    }

    const int importRet = initWithBufferTraits(bufferTraits);
    if (importRet < 0) {
        ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet));
        client->close();
    }
    mBufferClient = std::move(client);
}

BufferHubBuffer::BufferHubBuffer(const sp<NativeHandle>& token) {
    sp<IBufferHub> bufferhub = IBufferHub::getService();
    if (bufferhub.get() == nullptr) {
        ALOGE("%s: BufferHub service not found!", __FUNCTION__);
        return;
    }

    BufferHubStatus ret;
    sp<IBufferClient> client;
    BufferTraits bufferTraits;
    IBufferHub::importBuffer_cb importCb = [&](const auto& status, const auto& outClient,
                                               const auto& outTraits) {
        ret = status;
        client = std::move(outClient);
        bufferTraits = std::move(outTraits);
    };

    // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership
    // transfer.
    if (!bufferhub->importBuffer(hidl_handle(token.get()->handle()), importCb).isOk()) {
        ALOGE("%s: importBuffer transaction failed!", __FUNCTION__);
        return;
    } else if (ret != BufferHubStatus::NO_ERROR) {
        ALOGE("%s: importBuffer failed with error %u.", __FUNCTION__, ret);
        return;
    } else if (client == nullptr) {
        ALOGE("%s: importBuffer got null BufferClient.", __FUNCTION__);
        return;
    }

    const int importRet = initWithBufferTraits(bufferTraits);
    if (importRet < 0) {
        ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet));
        client->close();
    }
    mBufferClient = std::move(client);
}

BufferHubBuffer::~BufferHubBuffer() {
    // Close buffer client to avoid possible race condition: user could first duplicate and hold
    // token with the original buffer gone, and then try to import the token. The close function
    // will explicitly invalidate the token to avoid this.
    if (mBufferClient != nullptr) {
        if (!mBufferClient->close().isOk()) {
            ALOGE("%s: close BufferClient transaction failed!", __FUNCTION__);
        }
    }
}

int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) {
    ATRACE_CALL();

    if (bufferTraits.bufferInfo.getNativeHandle() == nullptr) {
        ALOGE("%s: missing buffer info handle.", __FUNCTION__);
        return -EINVAL;
    }

    if (bufferTraits.bufferHandle.getNativeHandle() == nullptr) {
        ALOGE("%s: missing gralloc handle.", __FUNCTION__);
        return -EINVAL;
    }

    // Import fds. Dup fds because hidl_handle owns the fds.
    unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0));
    mMetadata = BufferHubMetadata::import(std::move(ashmemFd));
    if (!mMetadata.isValid()) {
        ALOGE("%s: Received an invalid metadata.", __FUNCTION__);
        return -EINVAL;
    }

    mEventFd = BufferHubEventFd(fcntl(bufferTraits.bufferInfo->data[1], F_DUPFD_CLOEXEC, 0));
    if (!mEventFd.isValid()) {
        ALOGE("%s: Received ad invalid event fd.", __FUNCTION__);
        return -EINVAL;
    }

    int bufferId = bufferTraits.bufferInfo->data[2];
    if (bufferId < 0) {
        ALOGE("%s: Received an invalid (negative) id.", __FUNCTION__);
        return -EINVAL;
    }

    uint32_t clientBitMask;
    memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[3], sizeof(clientBitMask));
    if (clientBitMask == 0U) {
        ALOGE("%s: Received an invalid client state mask.", __FUNCTION__);
        return -EINVAL;
    }

    uint32_t userMetadataSize;
    memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[4], sizeof(userMetadataSize));
    if (mMetadata.userMetadataSize() != userMetadataSize) {
        ALOGE("%s: user metadata size not match: expected %u, actual %zu.", __FUNCTION__,
              userMetadataSize, mMetadata.userMetadataSize());
        return -EINVAL;
    }

    size_t metadataSize = static_cast<size_t>(mMetadata.metadataSize());
    if (metadataSize < BufferHubDefs::kMetadataHeaderSize) {
        ALOGE("%s: metadata too small: %zu", __FUNCTION__, metadataSize);
        return -EINVAL;
    }

    // Populate shortcuts to the atomics in metadata.
    auto metadataHeader = mMetadata.metadataHeader();
    mBufferState = &metadataHeader->bufferState;
    mFenceState = &metadataHeader->fenceState;
    mActiveClientsBitMask = &metadataHeader->activeClientsBitMask;
    // The C++ standard recommends (but does not require) that lock-free atomic operations are
    // also address-free, that is, suitable for communication between processes using shared
    // memory.
    LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(mBufferState) ||
                                !std::atomic_is_lock_free(mFenceState) ||
                                !std::atomic_is_lock_free(mActiveClientsBitMask),
                        "Atomic variables in ashmen are not lock free.");

    // Import the buffer: We only need to hold on the native_handle_t here so that
    // GraphicBuffer instance can be created in future.
    mBufferHandle = std::move(bufferTraits.bufferHandle);
    memcpy(&mBufferDesc, &bufferTraits.bufferDesc, sizeof(AHardwareBuffer_Desc));

    mId = bufferId;
    mClientStateMask = clientBitMask;

    // TODO(b/112012161) Set up shared fences.
    ALOGD("%s: id=%d, mBufferState=%" PRIx32 ".", __FUNCTION__, mId,
          mBufferState->load(std::memory_order_acquire));
    return 0;
}

int BufferHubBuffer::gain() {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    if (isClientGained(currentBufferState, mClientStateMask)) {
        ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__,
              mClientStateMask);
        return 0;
    }
    do {
        if (isAnyClientGained(currentBufferState & (~mClientStateMask)) ||
            isAnyClientAcquired(currentBufferState)) {
            ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
                  __FUNCTION__, mId, mClientStateMask, currentBufferState);
            return -EBUSY;
        }
        // Change the buffer state to gained state, whose value happens to be the same as
        // mClientStateMask.
    } while (!mBufferState->compare_exchange_weak(currentBufferState, mClientStateMask,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence.
    return 0;
}

int BufferHubBuffer::post() {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    uint32_t updatedBufferState = (~mClientStateMask) & BufferHubDefs::kHighBitsMask;
    do {
        if (!isClientGained(currentBufferState, mClientStateMask)) {
            ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d "
                  "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
                  __FUNCTION__, mId, mClientStateMask, currentBufferState);
            return -EBUSY;
        }
        // Set the producer client buffer state to released, other clients' buffer state to posted.
        // Post to all existing and non-existing clients.
    } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence if needed.
    return 0;
}

int BufferHubBuffer::acquire() {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    if (isClientAcquired(currentBufferState, mClientStateMask)) {
        ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__,
              mClientStateMask);
        return 0;
    }
    uint32_t updatedBufferState = 0U;
    do {
        if (!isClientPosted(currentBufferState, mClientStateMask)) {
            ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d "
                  "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
                  __FUNCTION__, mId, mClientStateMask, currentBufferState);
            return -EBUSY;
        }
        // Change the buffer state for this consumer from posted to acquired.
        updatedBufferState = currentBufferState ^ mClientStateMask;
    } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence.
    return 0;
}

int BufferHubBuffer::release() {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    if (isClientReleased(currentBufferState, mClientStateMask)) {
        ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__,
              mClientStateMask);
        return 0;
    }
    uint32_t updatedBufferState = 0U;
    do {
        updatedBufferState = currentBufferState & (~mClientStateMask);
    } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence if needed.
    return 0;
}

bool BufferHubBuffer::isReleased() const {
    return (mBufferState->load(std::memory_order_acquire) &
            mActiveClientsBitMask->load(std::memory_order_acquire)) == 0;
}

bool BufferHubBuffer::isValid() const {
    return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U &&
            mEventFd.get() >= 0 && mMetadata.isValid() && mBufferClient != nullptr;
}

sp<NativeHandle> BufferHubBuffer::duplicate() {
    if (mBufferClient == nullptr) {
        ALOGE("%s: missing BufferClient!", __FUNCTION__);
        return nullptr;
    }

    hidl_handle token;
    BufferHubStatus ret;
    IBufferClient::duplicate_cb dupCb = [&](const auto& outToken, const auto& status) {
        token = std::move(outToken);
        ret = status;
    };

    if (!mBufferClient->duplicate(dupCb).isOk()) {
        ALOGE("%s: duplicate transaction failed!", __FUNCTION__);
        return nullptr;
    } else if (ret != BufferHubStatus::NO_ERROR) {
        ALOGE("%s: duplicate failed with error %u.", __FUNCTION__, ret);
        return nullptr;
    } else if (token.getNativeHandle() == nullptr) {
        ALOGE("%s: duplicate got null token.", __FUNCTION__);
        return nullptr;
    }

    return NativeHandle::create(native_handle_clone(token.getNativeHandle()), /*ownsHandle=*/true);
}

} // namespace android