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 "ppapi/proxy/ppb_image_data_proxy.h" |
| 6 |
| 7 #include <string.h> // For memcpy |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "build/build_config.h" |
| 13 #include "ppapi/c/pp_completion_callback.h" |
| 14 #include "ppapi/c/pp_resource.h" |
| 15 #include "ppapi/c/ppb_image_data.h" |
| 16 #include "ppapi/c/trusted/ppb_image_data_trusted.h" |
| 17 #include "ppapi/proxy/plugin_dispatcher.h" |
| 18 #include "ppapi/proxy/plugin_resource.h" |
| 19 #include "ppapi/proxy/ppapi_messages.h" |
| 20 |
| 21 #if defined(OS_LINUX) |
| 22 #include <sys/shm.h> |
| 23 #endif |
| 24 |
| 25 namespace pp { |
| 26 namespace proxy { |
| 27 |
| 28 class ImageData : public PluginResource { |
| 29 public: |
| 30 ImageData(const PP_ImageDataDesc& desc, uint64_t memory_handle); |
| 31 virtual ~ImageData(); |
| 32 |
| 33 // Resource overrides. |
| 34 virtual ImageData* AsImageData() { return this; } |
| 35 |
| 36 void* Map(); |
| 37 void Unmap(); |
| 38 |
| 39 const PP_ImageDataDesc& desc() const { return desc_; } |
| 40 |
| 41 private: |
| 42 PP_ImageDataDesc desc_; |
| 43 uint64_t memory_handle_; |
| 44 |
| 45 void* mapped_data_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(ImageData); |
| 48 }; |
| 49 |
| 50 ImageData::ImageData(const PP_ImageDataDesc& desc, |
| 51 uint64_t memory_handle) |
| 52 : desc_(desc), |
| 53 memory_handle_(memory_handle), |
| 54 mapped_data_(NULL) { |
| 55 } |
| 56 |
| 57 ImageData::~ImageData() { |
| 58 Unmap(); |
| 59 } |
| 60 |
| 61 void* ImageData::Map() { |
| 62 #if defined(OS_LINUX) |
| 63 // On linux, the memory handle is a SysV shared memory segment. |
| 64 int shmkey = static_cast<int>(memory_handle_); |
| 65 void* address = shmat(shmkey, NULL, 0); |
| 66 // Mark for deletion in case we crash so the kernel will clean it up. |
| 67 shmctl(shmkey, IPC_RMID, 0); |
| 68 if (address == (void*)-1) |
| 69 return NULL; |
| 70 mapped_data_ = address; |
| 71 return address; |
| 72 #else |
| 73 #error write this |
| 74 #endif |
| 75 } |
| 76 |
| 77 void ImageData::Unmap() { |
| 78 #if defined(OS_LINUX) |
| 79 if (mapped_data_) |
| 80 shmdt(mapped_data_); |
| 81 #else |
| 82 #error write this |
| 83 #endif |
| 84 mapped_data_ = NULL; |
| 85 } |
| 86 |
| 87 namespace { |
| 88 |
| 89 PP_ImageDataFormat GetNativeImageDataFormat() { |
| 90 int32 format = 0; |
| 91 PluginDispatcher::Get()->Send( |
| 92 new PpapiHostMsg_PPBImageData_GetNativeImageDataFormat( |
| 93 INTERFACE_ID_PPB_IMAGE_DATA, &format)); |
| 94 return static_cast<PP_ImageDataFormat>(format); |
| 95 } |
| 96 |
| 97 bool IsImageDataFormatSupported(PP_ImageDataFormat format) { |
| 98 bool supported = false; |
| 99 PluginDispatcher::Get()->Send( |
| 100 new PpapiHostMsg_PPBImageData_IsImageDataFormatSupported( |
| 101 INTERFACE_ID_PPB_IMAGE_DATA, static_cast<int32_t>(format), |
| 102 &supported)); |
| 103 return supported; |
| 104 } |
| 105 |
| 106 PP_Resource Create(PP_Module module_id, |
| 107 PP_ImageDataFormat format, |
| 108 const PP_Size* size, |
| 109 bool init_to_zero) { |
| 110 PP_Resource result = 0; |
| 111 std::string image_data_desc; |
| 112 uint64_t shm_handle = -1; |
| 113 PluginDispatcher::Get()->Send( |
| 114 new PpapiHostMsg_PPBImageData_Create( |
| 115 INTERFACE_ID_PPB_IMAGE_DATA, module_id, format, *size, init_to_zero, |
| 116 &result, &image_data_desc, &shm_handle)); |
| 117 |
| 118 if (result && image_data_desc.size() == sizeof(PP_ImageDataDesc)) { |
| 119 // We serialize the PP_ImageDataDesc just by copying to a string. |
| 120 PP_ImageDataDesc desc; |
| 121 memcpy(&desc, image_data_desc.data(), sizeof(PP_ImageDataDesc)); |
| 122 |
| 123 linked_ptr<ImageData> object( |
| 124 new ImageData(desc, shm_handle)); |
| 125 PluginDispatcher::Get()->plugin_resource_tracker()->AddResource( |
| 126 result, object); |
| 127 } |
| 128 return result; |
| 129 } |
| 130 |
| 131 bool IsImageData(PP_Resource resource) { |
| 132 ImageData* object = PluginResource::GetAs<ImageData>(resource); |
| 133 return !!object; |
| 134 } |
| 135 |
| 136 bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { |
| 137 ImageData* object = PluginResource::GetAs<ImageData>(resource); |
| 138 if (!object) |
| 139 return false; |
| 140 memcpy(desc, &object->desc(), sizeof(PP_ImageDataDesc)); |
| 141 return true; |
| 142 } |
| 143 |
| 144 void* Map(PP_Resource resource) { |
| 145 ImageData* object = PluginResource::GetAs<ImageData>(resource); |
| 146 if (!object) |
| 147 return NULL; |
| 148 return object->Map(); |
| 149 } |
| 150 |
| 151 void Unmap(PP_Resource resource) { |
| 152 ImageData* object = PluginResource::GetAs<ImageData>(resource); |
| 153 if (object) |
| 154 object->Unmap(); |
| 155 } |
| 156 |
| 157 const PPB_ImageData ppb_imagedata = { |
| 158 &GetNativeImageDataFormat, |
| 159 &IsImageDataFormatSupported, |
| 160 &Create, |
| 161 &IsImageData, |
| 162 &Describe, |
| 163 &Map, |
| 164 &Unmap, |
| 165 }; |
| 166 |
| 167 } // namespace |
| 168 |
| 169 PPB_ImageData_Proxy::PPB_ImageData_Proxy(Dispatcher* dispatcher, |
| 170 const void* target_interface) |
| 171 : InterfaceProxy(dispatcher, target_interface) { |
| 172 } |
| 173 |
| 174 PPB_ImageData_Proxy::~PPB_ImageData_Proxy() { |
| 175 } |
| 176 |
| 177 const void* PPB_ImageData_Proxy::GetSourceInterface() const { |
| 178 return &ppb_imagedata; |
| 179 } |
| 180 |
| 181 InterfaceID PPB_ImageData_Proxy::GetInterfaceId() const { |
| 182 return INTERFACE_ID_PPB_IMAGE_DATA; |
| 183 } |
| 184 |
| 185 void PPB_ImageData_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 186 IPC_BEGIN_MESSAGE_MAP(PPB_ImageData_Proxy, msg) |
| 187 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_GetNativeImageDataFormat, |
| 188 OnMsgGetNativeImageDataFormat) |
| 189 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_IsImageDataFormatSupported, |
| 190 OnMsgIsImageDataFormatSupported) |
| 191 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_Create, OnMsgCreate) |
| 192 IPC_END_MESSAGE_MAP() |
| 193 // FIXME(brettw) handle bad messages! |
| 194 } |
| 195 |
| 196 void PPB_ImageData_Proxy::OnMsgGetNativeImageDataFormat(int32* result) { |
| 197 *result = ppb_image_data_target()->GetNativeImageDataFormat(); |
| 198 } |
| 199 |
| 200 void PPB_ImageData_Proxy::OnMsgIsImageDataFormatSupported(int32 format, |
| 201 bool* result) { |
| 202 *result = ppb_image_data_target()->IsImageDataFormatSupported( |
| 203 static_cast<PP_ImageDataFormat>(format)); |
| 204 } |
| 205 |
| 206 void PPB_ImageData_Proxy::OnMsgCreate(PP_Module module, |
| 207 int32_t format, |
| 208 const PP_Size& size, |
| 209 bool init_to_zero, |
| 210 PP_Resource* result, |
| 211 std::string* image_data_desc, |
| 212 uint64_t* result_shm_handle) { |
| 213 *result = ppb_image_data_target()->Create( |
| 214 module, static_cast<PP_ImageDataFormat>(format), &size, init_to_zero); |
| 215 *result_shm_handle = 0; |
| 216 if (*result) { |
| 217 // The ImageDesc is just serialized as a string. |
| 218 PP_ImageDataDesc desc; |
| 219 if (ppb_image_data_target()->Describe(*result, &desc)) { |
| 220 image_data_desc->resize(sizeof(PP_ImageDataDesc)); |
| 221 memcpy(&(*image_data_desc)[0], &desc, sizeof(PP_ImageDataDesc)); |
| 222 } |
| 223 |
| 224 // Get the shared memory handle. |
| 225 const PPB_ImageDataTrusted* trusted = |
| 226 reinterpret_cast<const PPB_ImageDataTrusted*>( |
| 227 dispatcher()->GetLocalInterface(PPB_IMAGEDATA_TRUSTED_INTERFACE)); |
| 228 if (trusted) |
| 229 *result_shm_handle = trusted->GetNativeMemoryHandle(*result); |
| 230 } |
| 231 } |
| 232 |
| 233 } // namespace proxy |
| 234 } // namespace pp |
OLD | NEW |