OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/pepper/ppb_buffer_impl.h" | 5 #include "content/renderer/pepper/ppb_buffer_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "content/renderer/pepper/common.h" | 11 #include "content/renderer/pepper/common.h" |
12 #include "content/renderer/render_thread_impl.h" | 12 #include "content/renderer/render_thread_impl.h" |
13 #include "ppapi/c/dev/ppb_buffer_dev.h" | 13 #include "ppapi/c/dev/ppb_buffer_dev.h" |
14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/c/pp_instance.h" | 15 #include "ppapi/c/pp_instance.h" |
16 #include "ppapi/c/pp_resource.h" | 16 #include "ppapi/c/pp_resource.h" |
17 | 17 |
18 using ppapi::thunk::PPB_Buffer_API; | 18 using ppapi::thunk::PPB_Buffer_API; |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 PPB_Buffer_Impl::PPB_Buffer_Impl(PP_Instance instance) | 22 PPB_Buffer_Impl::PPB_Buffer_Impl(PP_Instance instance) |
23 : Resource(ppapi::OBJECT_IS_IMPL, instance), | 23 : Resource(ppapi::OBJECT_IS_IMPL, instance), size_(0), map_count_(0) {} |
24 size_(0), | |
25 map_count_(0) { | |
26 } | |
27 | 24 |
28 PPB_Buffer_Impl::~PPB_Buffer_Impl() { | 25 PPB_Buffer_Impl::~PPB_Buffer_Impl() {} |
29 } | |
30 | 26 |
31 // static | 27 // static |
32 PP_Resource PPB_Buffer_Impl::Create(PP_Instance instance, uint32_t size) { | 28 PP_Resource PPB_Buffer_Impl::Create(PP_Instance instance, uint32_t size) { |
33 scoped_refptr<PPB_Buffer_Impl> new_resource(CreateResource(instance, size)); | 29 scoped_refptr<PPB_Buffer_Impl> new_resource(CreateResource(instance, size)); |
34 if (new_resource.get()) | 30 if (new_resource.get()) |
35 return new_resource->GetReference(); | 31 return new_resource->GetReference(); |
36 return 0; | 32 return 0; |
37 } | 33 } |
38 | 34 |
39 // static | 35 // static |
40 scoped_refptr<PPB_Buffer_Impl> PPB_Buffer_Impl::CreateResource( | 36 scoped_refptr<PPB_Buffer_Impl> PPB_Buffer_Impl::CreateResource( |
41 PP_Instance instance, | 37 PP_Instance instance, |
42 uint32_t size) { | 38 uint32_t size) { |
43 scoped_refptr<PPB_Buffer_Impl> buffer(new PPB_Buffer_Impl(instance)); | 39 scoped_refptr<PPB_Buffer_Impl> buffer(new PPB_Buffer_Impl(instance)); |
44 if (!buffer->Init(size)) | 40 if (!buffer->Init(size)) |
45 return scoped_refptr<PPB_Buffer_Impl>(); | 41 return scoped_refptr<PPB_Buffer_Impl>(); |
46 return buffer; | 42 return buffer; |
47 } | 43 } |
48 | 44 |
49 PPB_Buffer_Impl* PPB_Buffer_Impl::AsPPB_Buffer_Impl() { | 45 PPB_Buffer_Impl* PPB_Buffer_Impl::AsPPB_Buffer_Impl() { return this; } |
50 return this; | |
51 } | |
52 | 46 |
53 PPB_Buffer_API* PPB_Buffer_Impl::AsPPB_Buffer_API() { | 47 PPB_Buffer_API* PPB_Buffer_Impl::AsPPB_Buffer_API() { return this; } |
54 return this; | |
55 } | |
56 | 48 |
57 bool PPB_Buffer_Impl::Init(uint32_t size) { | 49 bool PPB_Buffer_Impl::Init(uint32_t size) { |
58 if (size == 0) | 50 if (size == 0) |
59 return false; | 51 return false; |
60 size_ = size; | 52 size_ = size; |
61 shared_memory_.reset( | 53 shared_memory_.reset( |
62 RenderThread::Get()->HostAllocateSharedMemoryBuffer(size).release()); | 54 RenderThread::Get()->HostAllocateSharedMemoryBuffer(size).release()); |
63 return shared_memory_.get() != NULL; | 55 return shared_memory_.get() != NULL; |
64 } | 56 } |
65 | 57 |
(...skipping 16 matching lines...) Expand all Loading... |
82 | 74 |
83 void PPB_Buffer_Impl::Unmap() { | 75 void PPB_Buffer_Impl::Unmap() { |
84 if (--map_count_ == 0) | 76 if (--map_count_ == 0) |
85 shared_memory_->Unmap(); | 77 shared_memory_->Unmap(); |
86 } | 78 } |
87 | 79 |
88 int32_t PPB_Buffer_Impl::GetSharedMemory(int* shm_handle) { | 80 int32_t PPB_Buffer_Impl::GetSharedMemory(int* shm_handle) { |
89 #if defined(OS_POSIX) | 81 #if defined(OS_POSIX) |
90 *shm_handle = shared_memory_->handle().fd; | 82 *shm_handle = shared_memory_->handle().fd; |
91 #elif defined(OS_WIN) | 83 #elif defined(OS_WIN) |
92 *shm_handle = reinterpret_cast<int>( | 84 *shm_handle = reinterpret_cast<int>(shared_memory_->handle()); |
93 shared_memory_->handle()); | |
94 #else | 85 #else |
95 #error "Platform not supported." | 86 #error "Platform not supported." |
96 #endif | 87 #endif |
97 return PP_OK; | 88 return PP_OK; |
98 } | 89 } |
99 | 90 |
100 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API* api) : api_(api) { | 91 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API* api) : api_(api) { |
101 needs_unmap_ = !PP_ToBool(api->IsMapped()); | 92 needs_unmap_ = !PP_ToBool(api->IsMapped()); |
102 data_ = api->Map(); | 93 data_ = api->Map(); |
103 api->Describe(&size_); | 94 api->Describe(&size_); |
104 } | 95 } |
105 | 96 |
106 BufferAutoMapper::~BufferAutoMapper() { | 97 BufferAutoMapper::~BufferAutoMapper() { |
107 if (needs_unmap_) | 98 if (needs_unmap_) |
108 api_->Unmap(); | 99 api_->Unmap(); |
109 } | 100 } |
110 | 101 |
111 } // namespace content | 102 } // namespace content |
OLD | NEW |