blob: cefe5b3f3fab6c9077c1ec98201e062012318c45 [file] [log] [blame]
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -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
17#ifndef ANDROID_BUFFER_HUB_BUFFER_H_
18#define ANDROID_BUFFER_HUB_BUFFER_H_
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070019
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -070020// We would eliminate the clang warnings introduced by libdpx.
21// TODO(b/112338294): Remove those once BufferHub moved to use Binder
22#pragma clang diagnostic push
23#pragma clang diagnostic ignored "-Wconversion"
24#pragma clang diagnostic ignored "-Wdouble-promotion"
25#pragma clang diagnostic ignored "-Wgnu-case-range"
26#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
27#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
28#pragma clang diagnostic ignored "-Wnested-anon-types"
29#pragma clang diagnostic ignored "-Wpacked"
30#pragma clang diagnostic ignored "-Wshadow"
31#pragma clang diagnostic ignored "-Wsign-conversion"
32#pragma clang diagnostic ignored "-Wswitch-enum"
33#pragma clang diagnostic ignored "-Wundefined-func-template"
34#pragma clang diagnostic ignored "-Wunused-template"
35#pragma clang diagnostic ignored "-Wweak-vtables"
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070036#include <pdx/client.h>
37#include <private/dvr/buffer_hub_defs.h>
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -070038#include <private/dvr/native_handle_wrapper.h>
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -070039#pragma clang diagnostic pop
40
41#include <ui/BufferHubMetadata.h>
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070042
43namespace android {
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070044
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070045class BufferHubClient : public pdx::Client {
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070046public:
47 BufferHubClient();
48 virtual ~BufferHubClient();
49 explicit BufferHubClient(pdx::LocalChannelHandle mChannelHandle);
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070050
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070051 bool IsValid() const;
52 pdx::LocalChannelHandle TakeChannelHandle();
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070053
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070054 using pdx::Client::Close;
55 using pdx::Client::event_fd;
56 using pdx::Client::GetChannel;
57 using pdx::Client::InvokeRemoteMethod;
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070058};
59
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070060class BufferHubBuffer {
61public:
62 // Allocates a standalone BufferHubBuffer not associated with any producer consumer set.
63 static std::unique_ptr<BufferHubBuffer> Create(uint32_t width, uint32_t height,
64 uint32_t layerCount, uint32_t format,
65 uint64_t usage, size_t mUserMetadataSize) {
66 return std::unique_ptr<BufferHubBuffer>(
67 new BufferHubBuffer(width, height, layerCount, format, usage, mUserMetadataSize));
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070068 }
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070069
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070070 // Imports the given channel handle to a BufferHubBuffer, taking ownership.
71 static std::unique_ptr<BufferHubBuffer> Import(pdx::LocalChannelHandle mChannelHandle) {
72 return std::unique_ptr<BufferHubBuffer>(new BufferHubBuffer(std::move(mChannelHandle)));
73 }
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070074
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070075 BufferHubBuffer(const BufferHubBuffer&) = delete;
76 void operator=(const BufferHubBuffer&) = delete;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070077
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070078 // Gets ID of the buffer client. All BufferHubBuffer clients derived from the same buffer in
79 // bufferhubd share the same buffer id.
80 int id() const { return mId; }
Jiwen 'Steve' Cai2e06c1c2018-07-30 21:35:32 -070081
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070082 const native_handle_t* DuplicateHandle() { return mBufferHandle.DuplicateHandle(); }
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070083
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070084 // Returns the current value of MetadataHeader::buffer_state.
85 uint64_t buffer_state() {
86 return mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire);
87 }
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070088
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070089 // A state mask which is unique to a buffer hub client among all its siblings sharing the same
90 // concrete graphic buffer.
Jiwen 'Steve' Cai1850da22018-10-15 21:29:14 -070091 uint64_t buffer_state_bit() const { return mBufferStateBit; }
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070092
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070093 size_t user_metadata_size() const { return mMetadata.user_metadata_size(); }
Jiwen 'Steve' Caif326fce2018-08-06 17:36:50 -070094
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070095 // Returns true if the buffer holds an open PDX channels towards bufferhubd.
96 bool IsConnected() const { return mClient.IsValid(); }
Jiwen 'Steve' Caif326fce2018-08-06 17:36:50 -070097
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070098 // Returns true if the buffer holds an valid native buffer handle that's availble for the client
99 // to read from and/or write into.
100 bool IsValid() const { return mBufferHandle.IsValid(); }
Jiwen 'Steve' Caif326fce2018-08-06 17:36:50 -0700101
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700102 // Returns the event mask for all the events that are pending on this buffer (see sys/poll.h for
103 // all possible bits).
104 pdx::Status<int> GetEventMask(int events) {
105 if (auto* channel = mClient.GetChannel()) {
106 return channel->GetEventMask(events);
107 } else {
108 return pdx::ErrorStatus(EINVAL);
109 }
110 }
111
112 // Polls the fd for |timeoutMs| milliseconds (-1 for infinity).
113 int Poll(int timeoutMs);
114
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700115 // Creates a BufferHubBuffer client from an existing one. The new client will
116 // share the same underlying gralloc buffer and ashmem region for metadata.
117 pdx::Status<pdx::LocalChannelHandle> Duplicate();
118
119private:
120 BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format,
121 uint64_t usage, size_t mUserMetadataSize);
122
123 BufferHubBuffer(pdx::LocalChannelHandle mChannelHandle);
124
125 int ImportGraphicBuffer();
126
127 // Global id for the buffer that is consistent across processes.
Jiwen 'Steve' Cai1850da22018-10-15 21:29:14 -0700128 int mId = -1;
129 uint64_t mBufferStateBit = 0;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700130
131 // Wrapps the gralloc buffer handle of this buffer.
132 dvr::NativeHandleWrapper<pdx::LocalHandle> mBufferHandle;
133
134 // An ashmem-based metadata object. The same shared memory are mapped to the
135 // bufferhubd daemon and all buffer clients.
Jiwen 'Steve' Cai1850da22018-10-15 21:29:14 -0700136 BufferHubMetadata mMetadata;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700137
138 // PDX backend.
139 BufferHubClient mClient;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700140};
141
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700142} // namespace android
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700143
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700144#endif // ANDROID_BUFFER_HUB_BUFFER_H_