1/*
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
17#ifndef ANDROID_DETACHED_BUFFER_HUB_HANDLE_H
18#define ANDROID_DETACHED_BUFFER_HUB_HANDLE_H
19
20#include <pdx/channel_handle.h>
21
22#include <memory>
23
24namespace android {
25
26// A wrapper that holds a pdx::LocalChannelHandle object. From the handle, a BufferHub buffer can be
27// created. Current implementation assumes that the underlying transport is using libpdx (thus
28// holding a pdx::LocalChannelHandle object), but future implementation can change it to a Binder
29// backend if ever needed.
30class DetachedBufferHandle {
31public:
32    static std::unique_ptr<DetachedBufferHandle> Create(pdx::LocalChannelHandle handle) {
33        return std::unique_ptr<DetachedBufferHandle>(new DetachedBufferHandle(std::move(handle)));
34    }
35
36    // Accessors to get or take the internal pdx::LocalChannelHandle.
37    pdx::LocalChannelHandle& handle() { return mHandle; }
38    const pdx::LocalChannelHandle& handle() const { return mHandle; }
39
40    // Returns whether the DetachedBufferHandle holds a BufferHub channel.
41    bool isValid() const { return mHandle.valid(); }
42
43private:
44    // Constructs a DetachedBufferHandle from a pdx::LocalChannelHandle.
45    explicit DetachedBufferHandle(pdx::LocalChannelHandle handle) : mHandle(std::move(handle)) {}
46
47    pdx::LocalChannelHandle mHandle;
48};
49
50} // namespace android
51
52#endif // ANDROID_DETACHED_BUFFER_HUB_HANDLE_H
53