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