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