blob: a6e6d73819345016deefc38bb3edb2916ff51d50 [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
17// We would eliminate the clang warnings introduced by libdpx.
18// TODO(b/112338294): Remove those once BufferHub moved to use Binder
19#pragma clang diagnostic push
20#pragma clang diagnostic ignored "-Wconversion"
21#pragma clang diagnostic ignored "-Wdouble-promotion"
22#pragma clang diagnostic ignored "-Wgnu-case-range"
23#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
24#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
25#pragma clang diagnostic ignored "-Wnested-anon-types"
26#pragma clang diagnostic ignored "-Wpacked"
27#pragma clang diagnostic ignored "-Wshadow"
28#pragma clang diagnostic ignored "-Wsign-conversion"
29#pragma clang diagnostic ignored "-Wswitch-enum"
30#pragma clang diagnostic ignored "-Wundefined-func-template"
31#pragma clang diagnostic ignored "-Wunused-template"
32#pragma clang diagnostic ignored "-Wweak-vtables"
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070033#include <pdx/default_transport/client_channel.h>
34#include <pdx/default_transport/client_channel_factory.h>
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070035#include <pdx/file_handle.h>
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070036#include <private/dvr/bufferhub_rpc.h>
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -070037#pragma clang diagnostic pop
38
39#include <ui/BufferHubBuffer.h>
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070040#include <ui/DetachedBufferHandle.h>
41
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070042#include <poll.h>
43
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -070044using android::dvr::BufferTraits;
45using android::dvr::DetachedBufferRPC;
46using android::dvr::NativeHandleWrapper;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070047
48// TODO(b/112338294): Remove PDX dependencies from libui.
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070049using android::pdx::LocalChannelHandle;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070050using android::pdx::LocalHandle;
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070051using android::pdx::Status;
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070052using android::pdx::default_transport::ClientChannel;
53using android::pdx::default_transport::ClientChannelFactory;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070054
55namespace android {
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070056
Jiwen 'Steve' Cai9004b8c2018-10-03 18:52:23 -070057namespace {
58
59// TODO(b/112338294): Remove this string literal after refactoring BufferHub
60// to use Binder.
61static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client";
62
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070063} // namespace
Jiwen 'Steve' Cai9004b8c2018-10-03 18:52:23 -070064
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070065BufferHubClient::BufferHubClient() : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {}
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070066
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070067BufferHubClient::BufferHubClient(LocalChannelHandle mChannelHandle)
68 : Client(ClientChannel::Create(std::move(mChannelHandle))) {}
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070069
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -070070BufferHubClient::~BufferHubClient() {}
71
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070072bool BufferHubClient::IsValid() const {
73 return IsConnected() && GetChannelHandle().valid();
74}
75
76LocalChannelHandle BufferHubClient::TakeChannelHandle() {
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070077 if (IsConnected()) {
78 return std::move(GetChannelHandle());
79 } else {
80 return {};
81 }
82}
83
84BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount,
85 uint32_t format, uint64_t usage, size_t mUserMetadataSize) {
86 ATRACE_CALL();
87 ALOGD("BufferHubBuffer::BufferHubBuffer: width=%u height=%u layerCount=%u, format=%u "
88 "usage=%" PRIx64 " mUserMetadataSize=%zu",
89 width, height, layerCount, format, usage, mUserMetadataSize);
90
91 auto status =
92 mClient.InvokeRemoteMethod<DetachedBufferRPC::Create>(width, height, layerCount, format,
93 usage, mUserMetadataSize);
94 if (!status) {
95 ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to create detached buffer: %s",
96 status.GetErrorMessage().c_str());
97 mClient.Close(-status.error());
98 }
99
100 const int ret = ImportGraphicBuffer();
101 if (ret < 0) {
102 ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret));
103 mClient.Close(ret);
104 }
105}
106
107BufferHubBuffer::BufferHubBuffer(LocalChannelHandle mChannelHandle)
108 : mClient(std::move(mChannelHandle)) {
109 const int ret = ImportGraphicBuffer();
110 if (ret < 0) {
111 ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret));
112 mClient.Close(ret);
113 }
114}
115
116int BufferHubBuffer::ImportGraphicBuffer() {
117 ATRACE_CALL();
118
119 auto status = mClient.InvokeRemoteMethod<DetachedBufferRPC::Import>();
120 if (!status) {
121 ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import GraphicBuffer: %s",
122 status.GetErrorMessage().c_str());
123 return -status.error();
124 }
125
126 BufferTraits<LocalHandle> bufferTraits = status.take();
127 if (bufferTraits.id() < 0) {
128 ALOGE("BufferHubBuffer::BufferHubBuffer: Received an invalid id!");
129 return -EIO;
130 }
131
132 // Stash the buffer id to replace the value in mId.
133 const int bufferId = bufferTraits.id();
134
135 // Import the metadata.
136 mMetadata = BufferHubMetadata::Import(bufferTraits.take_metadata_handle());
137
138 if (!mMetadata.IsValid()) {
139 ALOGE("BufferHubBuffer::ImportGraphicBuffer: invalid metadata.");
140 return -ENOMEM;
141 }
142
143 if (mMetadata.metadata_size() != bufferTraits.metadata_size()) {
144 ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata buffer too small: "
145 "%zu, expected: %" PRIu64 ".",
146 mMetadata.metadata_size(), bufferTraits.metadata_size());
147 return -ENOMEM;
148 }
149
150 size_t metadataSize = static_cast<size_t>(bufferTraits.metadata_size());
151 if (metadataSize < dvr::BufferHubDefs::kMetadataHeaderSize) {
152 ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize);
153 return -EINVAL;
154 }
155
156 // Import the buffer: We only need to hold on the native_handle_t here so that
157 // GraphicBuffer instance can be created in future.
158 mBufferHandle = bufferTraits.take_buffer_handle();
159
160 // If all imports succeed, replace the previous buffer and id.
161 mId = bufferId;
Tianyu Jiang7e204b72018-10-26 15:39:18 -0700162 mClientStateMask = bufferTraits.client_state_mask();
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700163
164 // TODO(b/112012161) Set up shared fences.
165 ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(),
166 mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire));
167 return 0;
168}
169
170int BufferHubBuffer::Poll(int timeoutMs) {
171 ATRACE_CALL();
172
173 pollfd p = {mClient.event_fd(), POLLIN, 0};
174 return poll(&p, 1, timeoutMs);
175}
176
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700177Status<LocalChannelHandle> BufferHubBuffer::Duplicate() {
178 ATRACE_CALL();
179 ALOGD("BufferHubBuffer::Duplicate: id=%d.", mId);
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700180
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700181 auto statusOrHandle = mClient.InvokeRemoteMethod<DetachedBufferRPC::Duplicate>();
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700182
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700183 if (!statusOrHandle.ok()) {
184 ALOGE("BufferHubBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", mId,
185 statusOrHandle.GetErrorMessage().c_str());
186 }
187 return statusOrHandle;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700188}
189
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700190} // namespace android