OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 The Native Client Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can |
| 3 // be found in the LICENSE file. |
| 4 |
| 5 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_image_data.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 #include <string.h> |
| 9 #include <sys/mman.h> |
| 10 #include "srpcgen/ppb_rpc.h" |
| 11 #include "native_client/src/include/portability.h" |
| 12 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 14 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 15 #include "ppapi/c/ppb_image_data.h" |
| 16 |
| 17 namespace ppapi_proxy { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // round size up to next 64k |
| 22 size_t ceil64k(size_t n) { |
| 23 return (n + 0xFFFF) & (~0xFFFF); |
| 24 } |
| 25 |
| 26 PP_ImageDataFormat GetNativeImageDataFormat() { |
| 27 DebugPrintf("PPB_ImageData::GetNativeImageDataFormat\n"); |
| 28 int32_t format; |
| 29 NaClSrpcError srpc_result = |
| 30 PpbImageDataRpcClient::PPB_ImageData_GetNativeImageDataFormat( |
| 31 GetMainSrpcChannel(), |
| 32 &format); |
| 33 DebugPrintf("PPB_ImageData::GetNativeImageDataFormat: %s\n", |
| 34 NaClSrpcErrorString(srpc_result)); |
| 35 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 36 return static_cast<PP_ImageDataFormat>(format); |
| 37 } else { |
| 38 return PP_IMAGEDATAFORMAT_BGRA_PREMUL; |
| 39 } |
| 40 } |
| 41 |
| 42 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) { |
| 43 DebugPrintf("PPB_ImageData::IsImageDataFormatSupported: format=%" |
| 44 NACL_PRId32"\n", static_cast<int32_t>(format)); |
| 45 int32_t result; |
| 46 NaClSrpcError srpc_result = |
| 47 PpbImageDataRpcClient::PPB_ImageData_IsImageDataFormatSupported( |
| 48 GetMainSrpcChannel(), |
| 49 static_cast<int32_t>(format), |
| 50 &result); |
| 51 DebugPrintf("PPB_ImageData::IsImageDataFormatSupported: %s\n", |
| 52 NaClSrpcErrorString(srpc_result)); |
| 53 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 54 return (result ? PP_TRUE : PP_FALSE); |
| 55 } else { |
| 56 return PP_FALSE; |
| 57 } |
| 58 } |
| 59 |
| 60 PP_Resource Create(PP_Instance instance, |
| 61 PP_ImageDataFormat format, |
| 62 const struct PP_Size* size, |
| 63 PP_Bool init_to_zero) { |
| 64 DebugPrintf("PPB_ImageData::Create: instance=%"NACL_PRIu32"\n", instance); |
| 65 PP_Resource resource; |
| 66 NaClSrpcError srpc_result = |
| 67 PpbImageDataRpcClient::PPB_ImageData_Create( |
| 68 GetMainSrpcChannel(), |
| 69 instance, |
| 70 static_cast<int32_t>(format), |
| 71 static_cast<nacl_abi_size_t>(sizeof(struct PP_Size)), |
| 72 reinterpret_cast<char*>(const_cast<struct PP_Size*>(size)), |
| 73 (init_to_zero == PP_TRUE), |
| 74 &resource); |
| 75 DebugPrintf("PPB_ImageData::Create: %s\n", NaClSrpcErrorString(srpc_result)); |
| 76 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 77 scoped_refptr<PluginImageData> image_data = |
| 78 PluginResource::AdoptAs<PluginImageData>(resource); |
| 79 if (image_data.get()) { |
| 80 return resource; |
| 81 } |
| 82 } |
| 83 return kInvalidResourceId; |
| 84 } |
| 85 |
| 86 PP_Bool IsImageData(PP_Resource resource) { |
| 87 DebugPrintf("PPB_ImageData::IsImageData: resource=%"NACL_PRIu32"\n", |
| 88 resource); |
| 89 return PluginResource::GetAs<PluginImageData>(resource).get() |
| 90 ? PP_TRUE : PP_FALSE; |
| 91 } |
| 92 |
| 93 PP_Bool Describe(PP_Resource resource, |
| 94 struct PP_ImageDataDesc* desc) { |
| 95 DebugPrintf("PPB_ImageData::Describe: resource=%"NACL_PRIu32"\n", |
| 96 resource); |
| 97 scoped_refptr<PluginImageData> imagedata = |
| 98 PluginResource::GetAs<PluginImageData>(resource); |
| 99 if (!imagedata.get()) { |
| 100 return PP_FALSE; |
| 101 } |
| 102 |
| 103 *desc = imagedata->desc(); |
| 104 return PP_TRUE; |
| 105 } |
| 106 |
| 107 void* DoMap(PP_Resource resource) { |
| 108 DebugPrintf("PPB_ImageData::DoMap: resource=%"NACL_PRIu32"\n", resource); |
| 109 scoped_refptr<PluginImageData> imagedata = |
| 110 PluginResource::GetAs<PluginImageData>(resource); |
| 111 |
| 112 return imagedata.get() ? imagedata->Map() : NULL; |
| 113 } |
| 114 |
| 115 void DoUnmap(PP_Resource resource) { |
| 116 DebugPrintf("PPB_ImageData::DoUnmap: resource=%"NACL_PRIu32"\n", resource); |
| 117 scoped_refptr<PluginImageData> imagedata = |
| 118 PluginResource::GetAs<PluginImageData>(resource); |
| 119 if (imagedata.get()) |
| 120 imagedata->Unmap(); |
| 121 } |
| 122 |
| 123 } // namespace |
| 124 |
| 125 const PPB_ImageData* PluginImageData::GetInterface() { |
| 126 static const PPB_ImageData image_data_interface = { |
| 127 &GetNativeImageDataFormat, |
| 128 &IsImageDataFormatSupported, |
| 129 &Create, |
| 130 &IsImageData, |
| 131 &Describe, |
| 132 &DoMap, |
| 133 &DoUnmap, |
| 134 }; |
| 135 return &image_data_interface; |
| 136 } |
| 137 |
| 138 PluginImageData::PluginImageData() |
| 139 : shm_size_(0), |
| 140 addr_(NULL) { |
| 141 } |
| 142 |
| 143 bool PluginImageData::InitFromBrowserResource(PP_Resource resource) { |
| 144 nacl_abi_size_t desc_size = static_cast<nacl_abi_size_t>(sizeof(desc_)); |
| 145 int32_t success = PP_FALSE; |
| 146 |
| 147 NaClSrpcError result = |
| 148 PpbImageDataRpcClient::PPB_ImageData_Describe( |
| 149 GetMainSrpcChannel(), |
| 150 resource, |
| 151 &desc_size, |
| 152 reinterpret_cast<char*>(&desc_), |
| 153 &shm_fd_, |
| 154 &shm_size_, |
| 155 &success); |
| 156 return (NACL_SRPC_RESULT_OK == result) && (PP_TRUE == success); |
| 157 } |
| 158 |
| 159 void* PluginImageData::Map() { |
| 160 if (NULL != addr_) { |
| 161 return addr_; |
| 162 } |
| 163 if (!shm_size_) { |
| 164 return NULL; |
| 165 } |
| 166 |
| 167 addr_ = mmap(0, ceil64k(shm_size_), PROT_READ | PROT_WRITE, |
| 168 MAP_SHARED, shm_fd_, 0); |
| 169 return addr_; |
| 170 } |
| 171 |
| 172 void PluginImageData::Unmap() { |
| 173 if (addr_) { |
| 174 munmap(addr_, ceil64k(shm_size_)); |
| 175 addr_ = NULL; |
| 176 } |
| 177 } |
| 178 |
| 179 } // namespace ppapi_proxy |
OLD | NEW |