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/pepper/common.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "ppapi/c/dev/ppb_buffer_dev.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
),
28 PPB_Buffer_Impl::~PPB_Buffer_Impl() {
32 PP_Resource
PPB_Buffer_Impl::Create(PP_Instance instance
, uint32_t size
) {
33 scoped_refptr
<PPB_Buffer_Impl
> new_resource(CreateResource(instance
, size
));
34 if (new_resource
.get())
35 return new_resource
->GetReference();
40 scoped_refptr
<PPB_Buffer_Impl
> PPB_Buffer_Impl::CreateResource(
43 scoped_refptr
<PPB_Buffer_Impl
> buffer(new PPB_Buffer_Impl(instance
));
44 if (!buffer
->Init(size
))
45 return scoped_refptr
<PPB_Buffer_Impl
>();
49 PPB_Buffer_Impl
* PPB_Buffer_Impl::AsPPB_Buffer_Impl() {
53 PPB_Buffer_API
* PPB_Buffer_Impl::AsPPB_Buffer_API() {
57 bool PPB_Buffer_Impl::Init(uint32_t size
) {
62 RenderThread::Get()->HostAllocateSharedMemoryBuffer(size
).release());
63 return shared_memory_
.get() != NULL
;
66 PP_Bool
PPB_Buffer_Impl::Describe(uint32_t* size_in_bytes
) {
67 *size_in_bytes
= size_
;
71 PP_Bool
PPB_Buffer_Impl::IsMapped() {
72 return PP_FromBool(!!shared_memory_
->memory());
75 void* PPB_Buffer_Impl::Map() {
77 DCHECK(shared_memory_
.get());
78 if (map_count_
++ == 0)
79 shared_memory_
->Map(size_
);
80 return shared_memory_
->memory();
83 void PPB_Buffer_Impl::Unmap() {
84 if (--map_count_
== 0)
85 shared_memory_
->Unmap();
88 int32_t PPB_Buffer_Impl::GetSharedMemory(int* shm_handle
) {
90 *shm_handle
= shared_memory_
->handle().fd
;
92 *shm_handle
= reinterpret_cast<int>(
93 shared_memory_
->handle());
95 #error "Platform not supported."
100 BufferAutoMapper::BufferAutoMapper(PPB_Buffer_API
* api
) : api_(api
) {
101 needs_unmap_
= !PP_ToBool(api
->IsMapped());
103 api
->Describe(&size_
);
106 BufferAutoMapper::~BufferAutoMapper() {
111 } // namespace content