OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cdm/ppapi/cdm_helpers.h" | 5 #include "media/cdm/ppapi/cdm_helpers.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
12 #include "media/cdm/ppapi/api/content_decryption_module.h" | 13 #include "media/cdm/ppapi/api/content_decryption_module.h" |
13 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
14 #include "ppapi/c/pp_stdint.h" | 15 #include "ppapi/c/pp_stdint.h" |
15 #include "ppapi/cpp/core.h" | 16 #include "ppapi/cpp/core.h" |
16 #include "ppapi/cpp/dev/buffer_dev.h" | 17 #include "ppapi/cpp/dev/buffer_dev.h" |
17 #include "ppapi/cpp/instance.h" | 18 #include "ppapi/cpp/instance.h" |
18 #include "ppapi/cpp/logging.h" | 19 #include "ppapi/cpp/logging.h" |
19 #include "ppapi/cpp/module.h" | 20 #include "ppapi/cpp/module.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 | 23 |
| 24 // static |
| 25 PpbBuffer* PpbBuffer::Create(const pp::Buffer_Dev& buffer, |
| 26 uint32_t buffer_id, |
| 27 PpbBufferAllocator* allocator) { |
| 28 PP_DCHECK(buffer.data()); |
| 29 PP_DCHECK(buffer.size()); |
| 30 PP_DCHECK(buffer_id); |
| 31 PP_DCHECK(allocator); |
| 32 return new PpbBuffer(buffer, buffer_id, allocator); |
| 33 } |
| 34 |
| 35 void PpbBuffer::Destroy() { |
| 36 delete this; |
| 37 } |
| 38 |
| 39 uint32_t PpbBuffer::Capacity() const { |
| 40 return buffer_.size(); |
| 41 } |
| 42 |
| 43 uint8_t* PpbBuffer::Data() { |
| 44 return static_cast<uint8_t*>(buffer_.data()); |
| 45 } |
| 46 |
| 47 void PpbBuffer::SetSize(uint32_t size) { |
| 48 PP_DCHECK(size <= Capacity()); |
| 49 if (size > Capacity()) { |
| 50 size_ = 0; |
| 51 return; |
| 52 } |
| 53 |
| 54 size_ = size; |
| 55 } |
| 56 |
| 57 pp::Buffer_Dev PpbBuffer::TakeBuffer() { |
| 58 PP_DCHECK(!buffer_.is_null()); |
| 59 pp::Buffer_Dev buffer; |
| 60 std::swap(buffer, buffer_); |
| 61 buffer_id_ = 0; |
| 62 size_ = 0; |
| 63 return buffer; |
| 64 } |
| 65 |
| 66 PpbBuffer::PpbBuffer(pp::Buffer_Dev buffer, |
| 67 uint32_t buffer_id, |
| 68 PpbBufferAllocator* allocator) |
| 69 : buffer_(buffer), buffer_id_(buffer_id), size_(0), allocator_(allocator) { |
| 70 } |
| 71 |
| 72 PpbBuffer::~PpbBuffer() { |
| 73 PP_DCHECK(!buffer_id_ == buffer_.is_null()); |
| 74 // If still owning the |buffer_|, release it in the |allocator_|. |
| 75 if (buffer_id_) |
| 76 allocator_->Release(buffer_id_); |
| 77 } |
| 78 |
23 cdm::Buffer* PpbBufferAllocator::Allocate(uint32_t capacity) { | 79 cdm::Buffer* PpbBufferAllocator::Allocate(uint32_t capacity) { |
24 PP_DCHECK(pp::Module::Get()->core()->IsMainThread()); | 80 PP_DCHECK(pp::Module::Get()->core()->IsMainThread()); |
25 | 81 |
26 if (!capacity) | 82 if (!capacity) |
27 return NULL; | 83 return NULL; |
28 | 84 |
29 pp::Buffer_Dev buffer; | 85 pp::Buffer_Dev buffer; |
30 uint32_t buffer_id = 0; | 86 uint32_t buffer_id = 0; |
31 | 87 |
32 // Reuse a buffer in the free list if there is one that fits |capacity|. | 88 // Reuse a buffer in the free list if there is one that fits |capacity|. |
33 // Otherwise, create a new one. | 89 // Otherwise, create a new one. |
34 FreeBufferMap::iterator found = free_buffers_.lower_bound(capacity); | 90 FreeBufferMap::iterator found = free_buffers_.lower_bound(capacity); |
35 if (found == free_buffers_.end()) { | 91 if (found == free_buffers_.end()) { |
36 // TODO(xhwang): Report statistics about how many new buffers are allocated. | 92 // TODO(xhwang): Report statistics about how many new buffers are allocated. |
37 buffer = AllocateNewBuffer(capacity); | 93 buffer = AllocateNewBuffer(capacity); |
38 if (buffer.is_null()) | 94 if (buffer.is_null()) |
39 return NULL; | 95 return NULL; |
40 buffer_id = next_buffer_id_++; | 96 buffer_id = next_buffer_id_++; |
41 } else { | 97 } else { |
42 buffer = found->second.second; | 98 buffer = found->second.second; |
43 buffer_id = found->second.first; | 99 buffer_id = found->second.first; |
44 free_buffers_.erase(found); | 100 free_buffers_.erase(found); |
45 } | 101 } |
46 | 102 |
47 allocated_buffers_.insert(std::make_pair(buffer_id, buffer)); | 103 allocated_buffers_.insert(std::make_pair(buffer_id, buffer)); |
48 | 104 |
49 return PpbBuffer::Create(buffer, buffer_id); | 105 return PpbBuffer::Create(buffer, buffer_id, this); |
50 } | 106 } |
51 | 107 |
52 void PpbBufferAllocator::Release(uint32_t buffer_id) { | 108 void PpbBufferAllocator::Release(uint32_t buffer_id) { |
53 if (!buffer_id) | 109 if (!buffer_id) |
54 return; | 110 return; |
55 | 111 |
56 AllocatedBufferMap::iterator found = allocated_buffers_.find(buffer_id); | 112 AllocatedBufferMap::iterator found = allocated_buffers_.find(buffer_id); |
57 if (found == allocated_buffers_.end()) | 113 if (found == allocated_buffers_.end()) |
58 return; | 114 return; |
59 | 115 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 strides_[i] = 0; | 149 strides_[i] = 0; |
94 } | 150 } |
95 } | 151 } |
96 | 152 |
97 VideoFrameImpl::~VideoFrameImpl() { | 153 VideoFrameImpl::~VideoFrameImpl() { |
98 if (frame_buffer_) | 154 if (frame_buffer_) |
99 frame_buffer_->Destroy(); | 155 frame_buffer_->Destroy(); |
100 } | 156 } |
101 | 157 |
102 } // namespace media | 158 } // namespace media |
OLD | NEW |