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> | |
xhwang
2014/07/10 16:42:39
This is for std::swap.
| |
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::TakeBufferDev() { | |
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 // If still owning the |buffer_|, release it in the |allocator_|. | |
74 if (buffer_id_) { | |
75 PP_DCHECK(!buffer_.is_null()); | |
ddorwin
2014/07/10 21:56:14
nit: Move up and make sure !id == !is_null()
xhwang
2014/07/10 22:37:33
Done.
| |
76 allocator_->Release(buffer_id_); | |
xhwang
2014/07/10 16:42:39
Sorry for moving things around. The changes in thi
| |
77 } | |
78 } | |
79 | |
23 cdm::Buffer* PpbBufferAllocator::Allocate(uint32_t capacity) { | 80 cdm::Buffer* PpbBufferAllocator::Allocate(uint32_t capacity) { |
24 PP_DCHECK(pp::Module::Get()->core()->IsMainThread()); | 81 PP_DCHECK(pp::Module::Get()->core()->IsMainThread()); |
25 | 82 |
26 if (!capacity) | 83 if (!capacity) |
27 return NULL; | 84 return NULL; |
28 | 85 |
29 pp::Buffer_Dev buffer; | 86 pp::Buffer_Dev buffer; |
30 uint32_t buffer_id = 0; | 87 uint32_t buffer_id = 0; |
31 | 88 |
32 // Reuse a buffer in the free list if there is one that fits |capacity|. | 89 // Reuse a buffer in the free list if there is one that fits |capacity|. |
33 // Otherwise, create a new one. | 90 // Otherwise, create a new one. |
34 FreeBufferMap::iterator found = free_buffers_.lower_bound(capacity); | 91 FreeBufferMap::iterator found = free_buffers_.lower_bound(capacity); |
35 if (found == free_buffers_.end()) { | 92 if (found == free_buffers_.end()) { |
36 // TODO(xhwang): Report statistics about how many new buffers are allocated. | 93 // TODO(xhwang): Report statistics about how many new buffers are allocated. |
37 buffer = AllocateNewBuffer(capacity); | 94 buffer = AllocateNewBuffer(capacity); |
38 if (buffer.is_null()) | 95 if (buffer.is_null()) |
39 return NULL; | 96 return NULL; |
40 buffer_id = next_buffer_id_++; | 97 buffer_id = next_buffer_id_++; |
41 } else { | 98 } else { |
42 buffer = found->second.second; | 99 buffer = found->second.second; |
43 buffer_id = found->second.first; | 100 buffer_id = found->second.first; |
44 free_buffers_.erase(found); | 101 free_buffers_.erase(found); |
45 } | 102 } |
46 | 103 |
47 allocated_buffers_.insert(std::make_pair(buffer_id, buffer)); | 104 allocated_buffers_.insert(std::make_pair(buffer_id, buffer)); |
48 | 105 |
49 return PpbBuffer::Create(buffer, buffer_id); | 106 return PpbBuffer::Create(buffer, buffer_id, this); |
50 } | 107 } |
51 | 108 |
52 void PpbBufferAllocator::Release(uint32_t buffer_id) { | 109 void PpbBufferAllocator::Release(uint32_t buffer_id) { |
53 if (!buffer_id) | 110 if (!buffer_id) |
54 return; | 111 return; |
55 | 112 |
56 AllocatedBufferMap::iterator found = allocated_buffers_.find(buffer_id); | 113 AllocatedBufferMap::iterator found = allocated_buffers_.find(buffer_id); |
57 if (found == allocated_buffers_.end()) | 114 if (found == allocated_buffers_.end()) |
58 return; | 115 return; |
59 | 116 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 strides_[i] = 0; | 150 strides_[i] = 0; |
94 } | 151 } |
95 } | 152 } |
96 | 153 |
97 VideoFrameImpl::~VideoFrameImpl() { | 154 VideoFrameImpl::~VideoFrameImpl() { |
98 if (frame_buffer_) | 155 if (frame_buffer_) |
99 frame_buffer_->Destroy(); | 156 frame_buffer_->Destroy(); |
100 } | 157 } |
101 | 158 |
102 } // namespace media | 159 } // namespace media |
OLD | NEW |