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/renderer/render_thread_impl.h"
12 #include "ppapi/c/dev/ppb_buffer_dev.h"
13 #include "ppapi/c/pp_bool.h"
14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/c/pp_instance.h"
16 #include "ppapi/c/pp_resource.h"
18 using ppapi::thunk::PPB_Buffer_API
;
22 PPB_Buffer_Impl::PPB_Buffer_Impl(PP_Instance instance
)
23 : Resource(ppapi::OBJECT_IS_IMPL
, instance
), size_(0), map_count_(0) {}
25 PPB_Buffer_Impl::~PPB_Buffer_Impl() {}
28 PP_Resource
PPB_Buffer_Impl::Create(PP_Instance instance
, uint32_t size
) {
29 scoped_refptr
<PPB_Buffer_Impl
> new_resource(CreateResource(instance
, size
));
30 if (new_resource
.get())
31 return new_resource
->GetReference();
36 scoped_refptr
<PPB_Buffer_Impl
> PPB_Buffer_Impl::CreateResource(
39 scoped_refptr
<PPB_Buffer_Impl
> buffer(new PPB_Buffer_Impl(instance
));
40 if (!buffer
->Init(size
))
41 return scoped_refptr
<PPB_Buffer_Impl
>();
45 PPB_Buffer_Impl
* PPB_Buffer_Impl::AsPPB_Buffer_Impl() { return this; }
47 PPB_Buffer_API
* PPB_Buffer_Impl::AsPPB_Buffer_API() { return this; }
49 bool PPB_Buffer_Impl::Init(uint32_t size
) {
54 RenderThread::Get()->HostAllocateSharedMemoryBuffer(size
).release());
55 return shared_memory_
.get() != NULL
;
58 PP_Bool
PPB_Buffer_Impl::Describe(uint32_t* size_in_bytes
) {
59 *size_in_bytes
= size_
;
63 PP_Bool
PPB_Buffer_Impl::IsMapped() {
64 return PP_FromBool(!!shared_memory_
->memory());
67 void* PPB_Buffer_Impl::Map() {
69 DCHECK(shared_memory_
.get());
70 if (map_count_
++ == 0)
71 shared_memory_
->Map(size_
);
72 return shared_memory_
->memory();
75 void PPB_Buffer_Impl::Unmap() {
76 if (--map_count_
== 0)
77 shared_memory_
->Unmap();
80 int32_t PPB_Buffer_Impl::GetSharedMemory(int* shm_handle
) {
82 *shm_handle
= shared_memory_
->handle().fd
;
84 *shm_handle
= reinterpret_cast<int>(shared_memory_
->handle());
86 #error "Platform not supported."
91 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API
* api
) : api_(api
) {
92 needs_unmap_
= !PP_ToBool(api
->IsMapped());
94 api
->Describe(&size_
);
97 BufferAutoMapper::~BufferAutoMapper() {
102 } // namespace content