OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_MAPPED_SHARED_BUFFER_H_ |
| 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_MAPPED_SHARED_BUFFER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "mojo/public/cpp/system/buffer.h" |
| 11 #include "mojo/services/media/common/cpp/fifo_allocator.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace media { |
| 15 |
| 16 // MappedSharedBuffer simplifies the use of shared buffers by taking care of |
| 17 // mapping/unmapping and by providing offset/pointer translation. It can be |
| 18 // used when the caller wants to allocate its own buffer (InitNew) and when |
| 19 // the caller needs to use a buffer supplied by another party (InitFromHandle). |
| 20 // It can be used by itself when regions of the buffer are allocated by another |
| 21 // party. If the caller needs to allocate regions, SharedMediaBufferAllocator, |
| 22 // which is derived from MappedSharedBuffer, provides allocation semantics |
| 23 // using FifoAllocator. |
| 24 class MappedSharedBuffer { |
| 25 public: |
| 26 MappedSharedBuffer(); |
| 27 |
| 28 virtual ~MappedSharedBuffer(); |
| 29 |
| 30 // Initializes by creating a new shared buffer of the indicated size. |
| 31 void InitNew(uint64_t size); |
| 32 |
| 33 // Initializes from a handle to an existing shared buffer. |
| 34 void InitFromHandle(ScopedSharedBufferHandle handle, uint64_t size); |
| 35 |
| 36 // Indicates whether the buffer is initialized. |
| 37 bool initialized() const; |
| 38 |
| 39 // Gets the size of the buffer. |
| 40 uint64_t size() const; |
| 41 |
| 42 // Gets a duplicate handle for the shared buffer. |
| 43 ScopedSharedBufferHandle GetDuplicateHandle() const; |
| 44 |
| 45 // Translates an offset into a pointer. |
| 46 void* PtrFromOffset(uint64_t offset) const; |
| 47 |
| 48 // Translates a pointer into an offset. |
| 49 uint64_t OffsetFromPtr(void *payload_ptr) const; |
| 50 |
| 51 protected: |
| 52 void InitInternal(ScopedSharedBufferHandle& handle, uint64_t size); |
| 53 |
| 54 // Does nothing. Called when initialization is complete. Subclasses may |
| 55 // override. |
| 56 virtual void OnInit(); |
| 57 |
| 58 private: |
| 59 struct MappedBufferDeleter { |
| 60 inline void operator()(uint8_t* ptr) const { |
| 61 UnmapBuffer(ptr); |
| 62 } |
| 63 }; |
| 64 |
| 65 // Size of the shared buffer. |
| 66 uint64_t size_; |
| 67 |
| 68 // Shared buffer when initialized with InitNew. |
| 69 std::unique_ptr<SharedBuffer> buffer_; |
| 70 |
| 71 // Handle to shared buffer when initialized with InitFromHandle. |
| 72 ScopedSharedBufferHandle handle_; |
| 73 |
| 74 // Pointer to the mapped buffer. |
| 75 std::unique_ptr<uint8_t, MappedBufferDeleter> buffer_ptr_; |
| 76 }; |
| 77 |
| 78 } // namespace media |
| 79 } // namespace mojo |
| 80 |
| 81 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_MAPPED_SHARED_BUFFER_H_ |
OLD | NEW |