| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/glue/plugins/pepper_buffer.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/scoped_ptr.h" | |
| 11 #include "ppapi/c/dev/ppb_buffer_dev.h" | |
| 12 #include "ppapi/c/pp_instance.h" | |
| 13 #include "ppapi/c/pp_module.h" | |
| 14 #include "ppapi/c/pp_resource.h" | |
| 15 #include "webkit/glue/plugins/pepper_common.h" | |
| 16 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
| 17 #include "webkit/glue/plugins/pepper_plugin_module.h" | |
| 18 | |
| 19 namespace pepper { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 PP_Resource Create(PP_Module module_id, uint32_t size) { | |
| 24 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | |
| 25 if (!module) | |
| 26 return 0; | |
| 27 | |
| 28 scoped_refptr<Buffer> buffer(new Buffer(module)); | |
| 29 if (!buffer->Init(size)) | |
| 30 return 0; | |
| 31 | |
| 32 return buffer->GetReference(); | |
| 33 } | |
| 34 | |
| 35 PP_Bool IsBuffer(PP_Resource resource) { | |
| 36 return BoolToPPBool(!!Resource::GetAs<Buffer>(resource)); | |
| 37 } | |
| 38 | |
| 39 PP_Bool Describe(PP_Resource resource, uint32_t* size_in_bytes) { | |
| 40 scoped_refptr<Buffer> buffer(Resource::GetAs<Buffer>(resource)); | |
| 41 if (!buffer) | |
| 42 return PP_FALSE; | |
| 43 buffer->Describe(size_in_bytes); | |
| 44 return PP_TRUE; | |
| 45 } | |
| 46 | |
| 47 void* Map(PP_Resource resource) { | |
| 48 scoped_refptr<Buffer> buffer(Resource::GetAs<Buffer>(resource)); | |
| 49 if (!buffer) | |
| 50 return NULL; | |
| 51 return buffer->Map(); | |
| 52 } | |
| 53 | |
| 54 void Unmap(PP_Resource resource) { | |
| 55 scoped_refptr<Buffer> buffer(Resource::GetAs<Buffer>(resource)); | |
| 56 if (!buffer) | |
| 57 return; | |
| 58 return buffer->Unmap(); | |
| 59 } | |
| 60 | |
| 61 const PPB_Buffer_Dev ppb_buffer = { | |
| 62 &Create, | |
| 63 &IsBuffer, | |
| 64 &Describe, | |
| 65 &Map, | |
| 66 &Unmap, | |
| 67 }; | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 Buffer::Buffer(PluginModule* module) | |
| 72 : Resource(module), | |
| 73 size_(0) { | |
| 74 } | |
| 75 | |
| 76 Buffer::~Buffer() { | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 const PPB_Buffer_Dev* Buffer::GetInterface() { | |
| 81 return &ppb_buffer; | |
| 82 } | |
| 83 | |
| 84 Buffer* Buffer::AsBuffer() { return this; } | |
| 85 | |
| 86 bool Buffer::Init(uint32_t size) { | |
| 87 if (size == 0) | |
| 88 return false; | |
| 89 Unmap(); | |
| 90 size_ = size; | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 void Buffer::Describe(uint32_t* size_in_bytes) const { | |
| 95 *size_in_bytes = size_; | |
| 96 } | |
| 97 | |
| 98 void* Buffer::Map() { | |
| 99 if (size_ == 0) | |
| 100 return NULL; | |
| 101 | |
| 102 if (!is_mapped()) { | |
| 103 mem_buffer_.reset(new unsigned char[size_]); | |
| 104 memset(mem_buffer_.get(), 0, size_); | |
| 105 } | |
| 106 return mem_buffer_.get(); | |
| 107 } | |
| 108 | |
| 109 void Buffer::Unmap() { | |
| 110 mem_buffer_.reset(); | |
| 111 } | |
| 112 | |
| 113 void Buffer::Swap(Buffer* other) { | |
| 114 swap(other->mem_buffer_, mem_buffer_); | |
| 115 std::swap(other->size_, size_); | |
| 116 } | |
| 117 | |
| 118 } // namespace pepper | |
| 119 | |
| OLD | NEW |