Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017, 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 CODEC2_BUFFER_H_ |
| 18 | |
| 19 | #define CODEC2_BUFFER_H_ |
| 20 | |
| 21 | #include <C2Buffer.h> |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 22 | #include <C2Config.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 23 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 24 | #include <binder/IMemory.h> |
| 25 | #include <media/hardware/VideoAPI.h> |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 26 | #include <media/stagefright/foundation/ABuffer.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 27 | #include <media/MediaCodecBuffer.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 31 | namespace hardware { |
| 32 | class HidlMemory; |
| 33 | namespace cas { |
| 34 | namespace native { |
| 35 | namespace V1_0 { |
| 36 | struct SharedBuffer; |
| 37 | } // namespace V1_0 |
| 38 | } // namespace native |
| 39 | } // namespace cas |
| 40 | namespace drm { |
| 41 | namespace V1_0 { |
| 42 | struct SharedBuffer; |
| 43 | } // namespace V1_0 |
| 44 | } // namespace drm |
| 45 | } // namespace hardware |
| 46 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 47 | class Codec2Buffer : public MediaCodecBuffer { |
| 48 | public: |
| 49 | using MediaCodecBuffer::MediaCodecBuffer; |
| 50 | ~Codec2Buffer() override = default; |
| 51 | |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 52 | sp<ABuffer> getImageData() const { return mImageData; } |
| 53 | |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 54 | virtual void clearC2BufferRefs() {} |
| 55 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 56 | protected: |
| 57 | /** |
| 58 | * canCopy() implementation for linear buffers. |
| 59 | */ |
| 60 | bool canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const; |
| 61 | |
| 62 | /** |
| 63 | * copy() implementation for linear buffers. |
| 64 | */ |
| 65 | bool copyLinear(const std::shared_ptr<C2Buffer> &buffer); |
| 66 | |
| 67 | /** |
| 68 | * sets MediaImage data for flexible graphic buffers |
| 69 | */ |
| 70 | void setImageData(const sp<ABuffer> &imageData); |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 71 | |
| 72 | sp<ABuffer> mImageData; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * MediaCodecBuffer implementation on top of local linear buffer. This cannot |
| 77 | * cross process boundary so asC2Buffer() returns only nullptr. |
| 78 | */ |
| 79 | class LocalLinearBuffer : public Codec2Buffer { |
| 80 | public: |
| 81 | using Codec2Buffer::Codec2Buffer; |
| 82 | |
| 83 | std::shared_ptr<C2Buffer> asC2Buffer() override { return nullptr; } |
| 84 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 85 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * MediaCodecBuffer implementation to be used only as a dummy wrapper around a |
| 90 | * C2Buffer object. |
| 91 | */ |
| 92 | class DummyContainerBuffer : public Codec2Buffer { |
| 93 | public: |
| 94 | DummyContainerBuffer( |
| 95 | const sp<AMessage> &format, |
| 96 | const std::shared_ptr<C2Buffer> &buffer = nullptr); |
| 97 | |
| 98 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 99 | void clearC2BufferRefs() override; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 100 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 101 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 102 | |
| 103 | private: |
| 104 | std::shared_ptr<C2Buffer> mBufferRef; |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * MediaCodecBuffer implementation wraps around C2LinearBlock. |
| 109 | */ |
| 110 | class LinearBlockBuffer : public Codec2Buffer { |
| 111 | public: |
| 112 | /** |
| 113 | * Allocate a new LinearBufferBlock wrapping around C2LinearBlock object. |
| 114 | * |
| 115 | * \param format mandatory buffer format for MediaCodecBuffer |
| 116 | * \param block C2LinearBlock object to wrap around. |
| 117 | * \return LinearBlockBuffer object with writable mapping. |
| 118 | * nullptr if unsuccessful. |
| 119 | */ |
| 120 | static sp<LinearBlockBuffer> Allocate( |
| 121 | const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block); |
| 122 | |
| 123 | virtual ~LinearBlockBuffer() = default; |
| 124 | |
| 125 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 126 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 127 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 128 | |
| 129 | private: |
| 130 | LinearBlockBuffer( |
| 131 | const sp<AMessage> &format, |
| 132 | C2WriteView &&writeView, |
| 133 | const std::shared_ptr<C2LinearBlock> &block); |
| 134 | LinearBlockBuffer() = delete; |
| 135 | |
| 136 | C2WriteView mWriteView; |
| 137 | std::shared_ptr<C2LinearBlock> mBlock; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * MediaCodecBuffer implementation wraps around C2ConstLinearBlock. |
| 142 | */ |
| 143 | class ConstLinearBlockBuffer : public Codec2Buffer { |
| 144 | public: |
| 145 | /** |
| 146 | * Allocate a new ConstLinearBlockBuffer wrapping around C2Buffer object. |
| 147 | * |
| 148 | * \param format mandatory buffer format for MediaCodecBuffer |
| 149 | * \param buffer linear C2Buffer object to wrap around. |
| 150 | * \return ConstLinearBlockBuffer object with readable mapping. |
| 151 | * nullptr if unsuccessful. |
| 152 | */ |
| 153 | static sp<ConstLinearBlockBuffer> Allocate( |
| 154 | const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer); |
| 155 | |
| 156 | virtual ~ConstLinearBlockBuffer() = default; |
| 157 | |
| 158 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 159 | void clearC2BufferRefs() override; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | ConstLinearBlockBuffer( |
| 163 | const sp<AMessage> &format, |
| 164 | C2ReadView &&readView, |
| 165 | const std::shared_ptr<C2Buffer> &buffer); |
| 166 | ConstLinearBlockBuffer() = delete; |
| 167 | |
| 168 | C2ReadView mReadView; |
| 169 | std::shared_ptr<C2Buffer> mBufferRef; |
| 170 | }; |
| 171 | |
| 172 | /** |
| 173 | * MediaCodecBuffer implementation wraps around C2GraphicBlock. |
| 174 | * |
| 175 | * This object exposes the underlying bits via accessor APIs and "image-data" |
| 176 | * metadata, created automatically at allocation time. |
| 177 | */ |
| 178 | class GraphicBlockBuffer : public Codec2Buffer { |
| 179 | public: |
| 180 | /** |
| 181 | * Allocate a new GraphicBlockBuffer wrapping around C2GraphicBlock object. |
| 182 | * If |block| is not in good color formats, it allocates YV12 local buffer |
| 183 | * and copies the content over at asC2Buffer(). |
| 184 | * |
| 185 | * \param format mandatory buffer format for MediaCodecBuffer |
| 186 | * \param block C2GraphicBlock object to wrap around. |
| 187 | * \param alloc a function to allocate backing ABuffer if needed. |
| 188 | * \return GraphicBlockBuffer object with writable mapping. |
| 189 | * nullptr if unsuccessful. |
| 190 | */ |
| 191 | static sp<GraphicBlockBuffer> Allocate( |
| 192 | const sp<AMessage> &format, |
| 193 | const std::shared_ptr<C2GraphicBlock> &block, |
| 194 | std::function<sp<ABuffer>(size_t)> alloc); |
| 195 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 196 | virtual ~GraphicBlockBuffer() = default; |
| 197 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 198 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 199 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 200 | private: |
| 201 | GraphicBlockBuffer( |
| 202 | const sp<AMessage> &format, |
| 203 | const sp<ABuffer> &buffer, |
| 204 | C2GraphicView &&view, |
| 205 | const std::shared_ptr<C2GraphicBlock> &block, |
| 206 | const sp<ABuffer> &imageData, |
| 207 | bool wrapped); |
| 208 | GraphicBlockBuffer() = delete; |
| 209 | |
| 210 | inline MediaImage2 *imageData() { return (MediaImage2 *)mImageData->data(); } |
| 211 | |
| 212 | C2GraphicView mView; |
| 213 | std::shared_ptr<C2GraphicBlock> mBlock; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 214 | const bool mWrapped; |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * MediaCodecBuffer implementation wraps around VideoNativeMetadata. |
| 219 | */ |
| 220 | class GraphicMetadataBuffer : public Codec2Buffer { |
| 221 | public: |
| 222 | /** |
| 223 | * Construct a new GraphicMetadataBuffer with local linear buffer for |
| 224 | * VideoNativeMetadata. |
| 225 | * |
| 226 | * \param format mandatory buffer format for MediaCodecBuffer |
| 227 | */ |
| 228 | GraphicMetadataBuffer( |
| 229 | const sp<AMessage> &format, const std::shared_ptr<C2Allocator> &alloc); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 230 | virtual ~GraphicMetadataBuffer() = default; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 231 | |
| 232 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 233 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 234 | private: |
| 235 | GraphicMetadataBuffer() = delete; |
| 236 | |
| 237 | std::shared_ptr<C2Allocator> mAlloc; |
| 238 | }; |
| 239 | |
| 240 | /** |
| 241 | * MediaCodecBuffer implementation wraps around graphic C2Buffer object. |
| 242 | * |
| 243 | * This object exposes the underlying bits via accessor APIs and "image-data" |
| 244 | * metadata, created automatically at allocation time. |
| 245 | */ |
| 246 | class ConstGraphicBlockBuffer : public Codec2Buffer { |
| 247 | public: |
| 248 | /** |
| 249 | * Allocate a new ConstGraphicBlockBuffer wrapping around C2Buffer object. |
| 250 | * If |buffer| is not in good color formats, it allocates YV12 local buffer |
| 251 | * and copies the content of |buffer| over to expose. |
| 252 | * |
| 253 | * \param format mandatory buffer format for MediaCodecBuffer |
| 254 | * \param buffer graphic C2Buffer object to wrap around. |
| 255 | * \param alloc a function to allocate backing ABuffer if needed. |
| 256 | * \return ConstGraphicBlockBuffer object with readable mapping. |
| 257 | * nullptr if unsuccessful. |
| 258 | */ |
| 259 | static sp<ConstGraphicBlockBuffer> Allocate( |
| 260 | const sp<AMessage> &format, |
| 261 | const std::shared_ptr<C2Buffer> &buffer, |
| 262 | std::function<sp<ABuffer>(size_t)> alloc); |
| 263 | |
| 264 | /** |
| 265 | * Allocate a new ConstGraphicBlockBuffer which allocates YV12 local buffer |
| 266 | * and copies the content of |buffer| over to expose. |
| 267 | * |
| 268 | * \param format mandatory buffer format for MediaCodecBuffer |
| 269 | * \param alloc a function to allocate backing ABuffer if needed. |
| 270 | * \return ConstGraphicBlockBuffer object with no wrapping buffer. |
| 271 | */ |
| 272 | static sp<ConstGraphicBlockBuffer> AllocateEmpty( |
| 273 | const sp<AMessage> &format, |
| 274 | std::function<sp<ABuffer>(size_t)> alloc); |
| 275 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 276 | virtual ~ConstGraphicBlockBuffer() = default; |
| 277 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 278 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 279 | void clearC2BufferRefs() override; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 280 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 281 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 282 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 283 | private: |
| 284 | ConstGraphicBlockBuffer( |
| 285 | const sp<AMessage> &format, |
| 286 | const sp<ABuffer> &aBuffer, |
| 287 | std::unique_ptr<const C2GraphicView> &&view, |
| 288 | const std::shared_ptr<C2Buffer> &buffer, |
| 289 | const sp<ABuffer> &imageData, |
| 290 | bool wrapped); |
| 291 | ConstGraphicBlockBuffer() = delete; |
| 292 | |
| 293 | sp<ABuffer> mImageData; |
| 294 | std::unique_ptr<const C2GraphicView> mView; |
| 295 | std::shared_ptr<C2Buffer> mBufferRef; |
| 296 | const bool mWrapped; |
| 297 | }; |
| 298 | |
| 299 | /** |
| 300 | * MediaCodecBuffer implementation wraps around C2LinearBlock for component |
| 301 | * and IMemory for client. Underlying C2LinearBlock won't be mapped for secure |
| 302 | * usecases.. |
| 303 | */ |
| 304 | class EncryptedLinearBlockBuffer : public Codec2Buffer { |
| 305 | public: |
| 306 | /** |
| 307 | * Construct a new EncryptedLinearBufferBlock wrapping around C2LinearBlock |
| 308 | * object and writable IMemory region. |
| 309 | * |
| 310 | * \param format mandatory buffer format for MediaCodecBuffer |
| 311 | * \param block C2LinearBlock object to wrap around. |
| 312 | * \param memory IMemory object to store encrypted content. |
| 313 | * \param heapSeqNum Heap sequence number from ICrypto; -1 if N/A |
| 314 | */ |
| 315 | EncryptedLinearBlockBuffer( |
| 316 | const sp<AMessage> &format, |
| 317 | const std::shared_ptr<C2LinearBlock> &block, |
| 318 | const sp<IMemory> &memory, |
| 319 | int32_t heapSeqNum = -1); |
| 320 | EncryptedLinearBlockBuffer() = delete; |
| 321 | |
| 322 | virtual ~EncryptedLinearBlockBuffer() = default; |
| 323 | |
| 324 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 325 | |
| 326 | /** |
| 327 | * Fill the source buffer structure with appropriate value based on |
| 328 | * internal IMemory object. |
| 329 | * |
| 330 | * \param source source buffer structure to fill. |
| 331 | */ |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 332 | void fillSourceBuffer( |
| 333 | hardware::drm::V1_0::SharedBuffer *source); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 334 | void fillSourceBuffer( |
| 335 | hardware::cas::native::V1_0::SharedBuffer *source); |
| 336 | |
| 337 | /** |
| 338 | * Copy the content of |decrypted| into C2LinearBlock inside. This shall |
| 339 | * only be called in non-secure usecases. |
| 340 | * |
| 341 | * \param decrypted decrypted content to copy from. |
| 342 | * \param length length of the content |
| 343 | * \return true if successful |
| 344 | * false otherwise. |
| 345 | */ |
| 346 | bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length); |
| 347 | |
| 348 | /** |
| 349 | * Copy the content of internal IMemory object into C2LinearBlock inside. |
| 350 | * This shall only be called in non-secure usecases. |
| 351 | * |
| 352 | * \param length length of the content |
| 353 | * \return true if successful |
| 354 | * false otherwise. |
| 355 | */ |
| 356 | bool copyDecryptedContentFromMemory(size_t length); |
| 357 | |
| 358 | /** |
| 359 | * Return native handle of secure buffer understood by ICrypto. |
| 360 | * |
| 361 | * \return secure buffer handle |
| 362 | */ |
| 363 | native_handle_t *handle() const; |
| 364 | |
Arun Johnson | 564f3a9 | 2024-02-01 19:09:45 +0000 | [diff] [blame] | 365 | class MappedBlock { |
| 366 | public: |
| 367 | explicit MappedBlock(const std::shared_ptr<C2LinearBlock> &block); |
| 368 | virtual ~MappedBlock(); |
| 369 | bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length); |
| 370 | private: |
| 371 | C2WriteView mView; |
| 372 | }; |
| 373 | |
| 374 | void getMappedBlock(std::unique_ptr<MappedBlock> * const mappedBlock) const; |
| 375 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 376 | private: |
| 377 | |
| 378 | std::shared_ptr<C2LinearBlock> mBlock; |
| 379 | sp<IMemory> mMemory; |
| 380 | sp<hardware::HidlMemory> mHidlMemory; |
| 381 | int32_t mHeapSeqNum; |
| 382 | }; |
| 383 | |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 384 | /** |
| 385 | * Get HDR metadata from Gralloc4 handle. |
| 386 | * |
| 387 | * \param[in] handle handle of the allocation |
| 388 | * \param[out] staticInfo HDR static info to be filled. Ignored if null; |
| 389 | * if |handle| is invalid or does not contain the metadata, |
| 390 | * the shared_ptr is reset. |
| 391 | * \param[out] dynamicInfo HDR dynamic info to be filled. Ignored if null; |
| 392 | * if |handle| is invalid or does not contain the metadata, |
| 393 | * the shared_ptr is reset. |
| 394 | * \return C2_OK if successful |
| 395 | */ |
| 396 | c2_status_t GetHdrMetadataFromGralloc4Handle( |
| 397 | const C2Handle *const handle, |
| 398 | std::shared_ptr<C2StreamHdrStaticMetadataInfo::input> *staticInfo, |
| 399 | std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> *dynamicInfo); |
| 400 | |
| 401 | /** |
Hongguang | fc1478a | 2022-07-20 22:56:06 -0700 | [diff] [blame] | 402 | * Set metadata to Gralloc4 handle. |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 403 | * |
Hongguang | fc1478a | 2022-07-20 22:56:06 -0700 | [diff] [blame] | 404 | * \param[in] dataSpace Dataspace to set. |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 405 | * \param[in] staticInfo HDR static info to set. Ignored if null or invalid. |
| 406 | * \param[in] dynamicInfo HDR dynamic info to set. Ignored if null or invalid. |
| 407 | * \param[out] handle handle of the allocation. |
| 408 | * \return C2_OK if successful |
| 409 | */ |
Hongguang | fc1478a | 2022-07-20 22:56:06 -0700 | [diff] [blame] | 410 | c2_status_t SetMetadataToGralloc4Handle( |
| 411 | const android_dataspace_t dataSpace, |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 412 | const std::shared_ptr<const C2StreamHdrStaticMetadataInfo::output> &staticInfo, |
| 413 | const std::shared_ptr<const C2StreamHdrDynamicMetadataInfo::output> &dynamicInfo, |
| 414 | const C2Handle *const handle); |
| 415 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 416 | } // namespace android |
| 417 | |
| 418 | #endif // CODEC2_BUFFER_H_ |