Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 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 | // 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' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 33 | #include <pdx/default_transport/client_channel.h> |
| 34 | #include <pdx/default_transport/client_channel_factory.h> |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 35 | #include <pdx/file_handle.h> |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 36 | #include <private/dvr/bufferhub_rpc.h> |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 37 | #pragma clang diagnostic pop |
| 38 | |
| 39 | #include <ui/BufferHubBuffer.h> |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 40 | |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 41 | #include <poll.h> |
| 42 | |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 43 | using android::dvr::BufferTraits; |
| 44 | using android::dvr::DetachedBufferRPC; |
| 45 | using android::dvr::NativeHandleWrapper; |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 46 | |
| 47 | // TODO(b/112338294): Remove PDX dependencies from libui. |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 48 | using android::pdx::LocalChannelHandle; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 49 | using android::pdx::LocalHandle; |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 50 | using android::pdx::Status; |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 51 | using android::pdx::default_transport::ClientChannel; |
| 52 | using android::pdx::default_transport::ClientChannelFactory; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 53 | |
| 54 | namespace android { |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 55 | |
Jiwen 'Steve' Cai | 9004b8c | 2018-10-03 18:52:23 -0700 | [diff] [blame] | 56 | namespace { |
| 57 | |
| 58 | // TODO(b/112338294): Remove this string literal after refactoring BufferHub |
| 59 | // to use Binder. |
| 60 | static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; |
| 61 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 62 | } // namespace |
Jiwen 'Steve' Cai | 9004b8c | 2018-10-03 18:52:23 -0700 | [diff] [blame] | 63 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 64 | BufferHubClient::BufferHubClient() : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {} |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 65 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 66 | BufferHubClient::BufferHubClient(LocalChannelHandle mChannelHandle) |
| 67 | : Client(ClientChannel::Create(std::move(mChannelHandle))) {} |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 68 | |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 69 | BufferHubClient::~BufferHubClient() {} |
| 70 | |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 71 | bool BufferHubClient::IsValid() const { |
| 72 | return IsConnected() && GetChannelHandle().valid(); |
| 73 | } |
| 74 | |
| 75 | LocalChannelHandle BufferHubClient::TakeChannelHandle() { |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 76 | if (IsConnected()) { |
| 77 | return std::move(GetChannelHandle()); |
| 78 | } else { |
| 79 | return {}; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, |
| 84 | uint32_t format, uint64_t usage, size_t mUserMetadataSize) { |
| 85 | ATRACE_CALL(); |
| 86 | ALOGD("BufferHubBuffer::BufferHubBuffer: width=%u height=%u layerCount=%u, format=%u " |
| 87 | "usage=%" PRIx64 " mUserMetadataSize=%zu", |
| 88 | width, height, layerCount, format, usage, mUserMetadataSize); |
| 89 | |
| 90 | auto status = |
| 91 | mClient.InvokeRemoteMethod<DetachedBufferRPC::Create>(width, height, layerCount, format, |
| 92 | usage, mUserMetadataSize); |
| 93 | if (!status) { |
| 94 | ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to create detached buffer: %s", |
| 95 | status.GetErrorMessage().c_str()); |
| 96 | mClient.Close(-status.error()); |
| 97 | } |
| 98 | |
| 99 | const int ret = ImportGraphicBuffer(); |
| 100 | if (ret < 0) { |
| 101 | ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret)); |
| 102 | mClient.Close(ret); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | BufferHubBuffer::BufferHubBuffer(LocalChannelHandle mChannelHandle) |
| 107 | : mClient(std::move(mChannelHandle)) { |
| 108 | const int ret = ImportGraphicBuffer(); |
| 109 | if (ret < 0) { |
| 110 | ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret)); |
| 111 | mClient.Close(ret); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | int BufferHubBuffer::ImportGraphicBuffer() { |
| 116 | ATRACE_CALL(); |
| 117 | |
| 118 | auto status = mClient.InvokeRemoteMethod<DetachedBufferRPC::Import>(); |
| 119 | if (!status) { |
| 120 | ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import GraphicBuffer: %s", |
| 121 | status.GetErrorMessage().c_str()); |
| 122 | return -status.error(); |
| 123 | } |
| 124 | |
| 125 | BufferTraits<LocalHandle> bufferTraits = status.take(); |
| 126 | if (bufferTraits.id() < 0) { |
| 127 | ALOGE("BufferHubBuffer::BufferHubBuffer: Received an invalid id!"); |
| 128 | return -EIO; |
| 129 | } |
| 130 | |
| 131 | // Stash the buffer id to replace the value in mId. |
| 132 | const int bufferId = bufferTraits.id(); |
| 133 | |
| 134 | // Import the metadata. |
| 135 | mMetadata = BufferHubMetadata::Import(bufferTraits.take_metadata_handle()); |
| 136 | |
| 137 | if (!mMetadata.IsValid()) { |
| 138 | ALOGE("BufferHubBuffer::ImportGraphicBuffer: invalid metadata."); |
| 139 | return -ENOMEM; |
| 140 | } |
| 141 | |
| 142 | if (mMetadata.metadata_size() != bufferTraits.metadata_size()) { |
| 143 | ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata buffer too small: " |
| 144 | "%zu, expected: %" PRIu64 ".", |
| 145 | mMetadata.metadata_size(), bufferTraits.metadata_size()); |
| 146 | return -ENOMEM; |
| 147 | } |
| 148 | |
| 149 | size_t metadataSize = static_cast<size_t>(bufferTraits.metadata_size()); |
| 150 | if (metadataSize < dvr::BufferHubDefs::kMetadataHeaderSize) { |
| 151 | ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | // Import the buffer: We only need to hold on the native_handle_t here so that |
| 156 | // GraphicBuffer instance can be created in future. |
| 157 | mBufferHandle = bufferTraits.take_buffer_handle(); |
| 158 | |
| 159 | // If all imports succeed, replace the previous buffer and id. |
| 160 | mId = bufferId; |
Tianyu Jiang | 7e204b7 | 2018-10-26 15:39:18 -0700 | [diff] [blame] | 161 | mClientStateMask = bufferTraits.client_state_mask(); |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 162 | |
| 163 | // TODO(b/112012161) Set up shared fences. |
| 164 | ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), |
| 165 | mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire)); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int BufferHubBuffer::Poll(int timeoutMs) { |
| 170 | ATRACE_CALL(); |
| 171 | |
| 172 | pollfd p = {mClient.event_fd(), POLLIN, 0}; |
| 173 | return poll(&p, 1, timeoutMs); |
| 174 | } |
| 175 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 176 | Status<LocalChannelHandle> BufferHubBuffer::Duplicate() { |
| 177 | ATRACE_CALL(); |
| 178 | ALOGD("BufferHubBuffer::Duplicate: id=%d.", mId); |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 179 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 180 | auto statusOrHandle = mClient.InvokeRemoteMethod<DetachedBufferRPC::Duplicate>(); |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 181 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 182 | if (!statusOrHandle.ok()) { |
| 183 | ALOGE("BufferHubBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", mId, |
| 184 | statusOrHandle.GetErrorMessage().c_str()); |
| 185 | } |
| 186 | return statusOrHandle; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 189 | } // namespace android |