Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -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 | #ifndef ANDROID_BUFFER_HUB_BUFFER_H_ |
| 18 | #define ANDROID_BUFFER_HUB_BUFFER_H_ |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 19 | |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 20 | // 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' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 36 | #include <pdx/client.h> |
| 37 | #include <private/dvr/buffer_hub_defs.h> |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 38 | #include <private/dvr/native_handle_wrapper.h> |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 39 | #pragma clang diagnostic pop |
| 40 | |
| 41 | #include <ui/BufferHubMetadata.h> |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 42 | |
| 43 | namespace android { |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 44 | |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 45 | class BufferHubClient : public pdx::Client { |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 46 | public: |
| 47 | BufferHubClient(); |
| 48 | virtual ~BufferHubClient(); |
| 49 | explicit BufferHubClient(pdx::LocalChannelHandle mChannelHandle); |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 50 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 51 | bool IsValid() const; |
| 52 | pdx::LocalChannelHandle TakeChannelHandle(); |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 53 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 54 | using pdx::Client::Close; |
| 55 | using pdx::Client::event_fd; |
| 56 | using pdx::Client::GetChannel; |
| 57 | using pdx::Client::InvokeRemoteMethod; |
Jiwen 'Steve' Cai | 088b3b6 | 2018-10-03 18:49:07 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 60 | class BufferHubBuffer { |
| 61 | public: |
| 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' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 68 | } |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 69 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 70 | // 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' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 74 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 75 | BufferHubBuffer(const BufferHubBuffer&) = delete; |
| 76 | void operator=(const BufferHubBuffer&) = delete; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 77 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 78 | // 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' Cai | 2e06c1c | 2018-07-30 21:35:32 -0700 | [diff] [blame] | 81 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 82 | const native_handle_t* DuplicateHandle() { return mBufferHandle.DuplicateHandle(); } |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 83 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 84 | // 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' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 88 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 89 | // A state mask which is unique to a buffer hub client among all its siblings sharing the same |
| 90 | // concrete graphic buffer. |
Tianyu Jiang | 7e204b7 | 2018-10-26 15:39:18 -0700 | [diff] [blame^] | 91 | uint64_t client_state_mask() const { return mClientStateMask; } |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 92 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 93 | size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } |
Jiwen 'Steve' Cai | f326fce | 2018-08-06 17:36:50 -0700 | [diff] [blame] | 94 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 95 | // Returns true if the buffer holds an open PDX channels towards bufferhubd. |
| 96 | bool IsConnected() const { return mClient.IsValid(); } |
Jiwen 'Steve' Cai | f326fce | 2018-08-06 17:36:50 -0700 | [diff] [blame] | 97 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 98 | // 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' Cai | f326fce | 2018-08-06 17:36:50 -0700 | [diff] [blame] | 101 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 102 | // 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' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 115 | // 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 | |
| 119 | private: |
| 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' Cai | 1850da2 | 2018-10-15 21:29:14 -0700 | [diff] [blame] | 128 | int mId = -1; |
Tianyu Jiang | 7e204b7 | 2018-10-26 15:39:18 -0700 | [diff] [blame^] | 129 | uint64_t mClientStateMask = 0; |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 130 | |
| 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' Cai | 1850da2 | 2018-10-15 21:29:14 -0700 | [diff] [blame] | 136 | BufferHubMetadata mMetadata; |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 137 | |
| 138 | // PDX backend. |
| 139 | BufferHubClient mClient; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 142 | } // namespace android |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 143 | |
Jiwen 'Steve' Cai | ff675b7 | 2018-10-09 18:08:29 -0700 | [diff] [blame] | 144 | #endif // ANDROID_BUFFER_HUB_BUFFER_H_ |