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.
5 #include "content/renderer/pepper/ppb_buffer_impl.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/common/pepper_file_util.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "ppapi/c/dev/ppb_buffer_dev.h"
14 #include "ppapi/c/pp_bool.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/pp_instance.h"
17 #include "ppapi/c/pp_resource.h"
19 using ppapi::thunk::PPB_Buffer_API
;
23 PPB_Buffer_Impl::PPB_Buffer_Impl(PP_Instance instance
)
24 : Resource(ppapi::OBJECT_IS_IMPL
, instance
), size_(0), map_count_(0) {}
26 PPB_Buffer_Impl::~PPB_Buffer_Impl() {}
29 PP_Resource
PPB_Buffer_Impl::Create(PP_Instance instance
, uint32_t size
) {
30 scoped_refptr
<PPB_Buffer_Impl
> new_resource(CreateResource(instance
, size
));
31 if (new_resource
.get())
32 return new_resource
->GetReference();
37 scoped_refptr
<PPB_Buffer_Impl
> PPB_Buffer_Impl::CreateResource(
40 scoped_refptr
<PPB_Buffer_Impl
> buffer(new PPB_Buffer_Impl(instance
));
41 if (!buffer
->Init(size
))
42 return scoped_refptr
<PPB_Buffer_Impl
>();
46 PPB_Buffer_Impl
* PPB_Buffer_Impl::AsPPB_Buffer_Impl() { return this; }
48 PPB_Buffer_API
* PPB_Buffer_Impl::AsPPB_Buffer_API() { return this; }
50 bool PPB_Buffer_Impl::Init(uint32_t size
) {
55 RenderThread::Get()->HostAllocateSharedMemoryBuffer(size
).release());
56 return shared_memory_
.get() != NULL
;
59 PP_Bool
PPB_Buffer_Impl::Describe(uint32_t* size_in_bytes
) {
60 *size_in_bytes
= size_
;
64 PP_Bool
PPB_Buffer_Impl::IsMapped() {
65 return PP_FromBool(!!shared_memory_
->memory());
68 void* PPB_Buffer_Impl::Map() {
70 DCHECK(shared_memory_
.get());
71 if (map_count_
++ == 0)
72 shared_memory_
->Map(size_
);
73 return shared_memory_
->memory();
76 void PPB_Buffer_Impl::Unmap() {
77 if (--map_count_
== 0)
78 shared_memory_
->Unmap();
81 int32_t PPB_Buffer_Impl::GetSharedMemory(base::SharedMemory
** shm
) {
82 *shm
= shared_memory_
.get();
86 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API
* api
) : api_(api
) {
87 needs_unmap_
= !PP_ToBool(api
->IsMapped());
89 api
->Describe(&size_
);
92 BufferAutoMapper::~BufferAutoMapper() {
97 } // namespace content