OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/plugins/ppapi/ppb_buffer_impl.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "ppapi/c/dev/ppb_buffer_dev.h" | |
12 #include "ppapi/c/pp_instance.h" | |
13 #include "ppapi/c/pp_resource.h" | |
14 #include "webkit/plugins/ppapi/common.h" | |
15 #include "webkit/plugins/ppapi/plugin_delegate.h" | |
16 #include "webkit/plugins/ppapi/resource_helper.h" | |
17 | |
18 using ::ppapi::thunk::PPB_Buffer_API; | |
19 | |
20 namespace webkit { | |
21 namespace ppapi { | |
22 | |
23 PPB_Buffer_Impl::PPB_Buffer_Impl(PP_Instance instance) | |
24 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | |
25 size_(0), | |
26 map_count_(0) { | |
27 } | |
28 | |
29 PPB_Buffer_Impl::~PPB_Buffer_Impl() { | |
30 } | |
31 | |
32 // static | |
33 PP_Resource PPB_Buffer_Impl::Create(PP_Instance instance, uint32_t size) { | |
34 scoped_refptr<PPB_Buffer_Impl> new_resource(CreateResource(instance, size)); | |
35 if (new_resource.get()) | |
36 return new_resource->GetReference(); | |
37 return 0; | |
38 } | |
39 | |
40 // static | |
41 scoped_refptr<PPB_Buffer_Impl> PPB_Buffer_Impl::CreateResource( | |
42 PP_Instance instance, | |
43 uint32_t size) { | |
44 scoped_refptr<PPB_Buffer_Impl> buffer(new PPB_Buffer_Impl(instance)); | |
45 if (!buffer->Init(size)) | |
46 return scoped_refptr<PPB_Buffer_Impl>(); | |
47 return buffer; | |
48 } | |
49 | |
50 PPB_Buffer_Impl* PPB_Buffer_Impl::AsPPB_Buffer_Impl() { | |
51 return this; | |
52 } | |
53 | |
54 PPB_Buffer_API* PPB_Buffer_Impl::AsPPB_Buffer_API() { | |
55 return this; | |
56 } | |
57 | |
58 bool PPB_Buffer_Impl::Init(uint32_t size) { | |
59 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
60 if (size == 0 || !plugin_delegate) | |
61 return false; | |
62 size_ = size; | |
63 shared_memory_.reset(plugin_delegate->CreateAnonymousSharedMemory(size)); | |
64 return shared_memory_.get() != NULL; | |
65 } | |
66 | |
67 PP_Bool PPB_Buffer_Impl::Describe(uint32_t* size_in_bytes) { | |
68 *size_in_bytes = size_; | |
69 return PP_TRUE; | |
70 } | |
71 | |
72 PP_Bool PPB_Buffer_Impl::IsMapped() { | |
73 return PP_FromBool(!!shared_memory_->memory()); | |
74 } | |
75 | |
76 void* PPB_Buffer_Impl::Map() { | |
77 DCHECK(size_); | |
78 DCHECK(shared_memory_.get()); | |
79 if (map_count_++ == 0) | |
80 shared_memory_->Map(size_); | |
81 return shared_memory_->memory(); | |
82 } | |
83 | |
84 void PPB_Buffer_Impl::Unmap() { | |
85 if (--map_count_ == 0) | |
86 shared_memory_->Unmap(); | |
87 } | |
88 | |
89 int32_t PPB_Buffer_Impl::GetSharedMemory(int* shm_handle) { | |
90 #if defined(OS_POSIX) | |
91 *shm_handle = shared_memory_->handle().fd; | |
92 #elif defined(OS_WIN) | |
93 *shm_handle = reinterpret_cast<int>( | |
94 shared_memory_->handle()); | |
95 #else | |
96 #error "Platform not supported." | |
97 #endif | |
98 return PP_OK; | |
99 } | |
100 | |
101 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API* api) : api_(api) { | |
102 needs_unmap_ = !PP_ToBool(api->IsMapped()); | |
103 data_ = api->Map(); | |
104 api->Describe(&size_); | |
105 } | |
106 | |
107 BufferAutoMapper::~BufferAutoMapper() { | |
108 if (needs_unmap_) | |
109 api_->Unmap(); | |
110 } | |
111 | |
112 } // namespace ppapi | |
113 } // namespace webkit | |
OLD | NEW |