OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_ppb_graphics_2d.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 #include <string.h> |
| 9 #include "native_client/src/include/portability.h" |
| 10 #include "native_client/src/shared/ppapi_proxy/plugin_callback.h" |
| 11 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 12 #include "native_client/src/shared/ppapi_proxy/plugin_upcall.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/pp_completion_callback.h" |
| 16 #include "ppapi/c/pp_errors.h" |
| 17 #include "ppapi/c/pp_rect.h" |
| 18 #include "ppapi/c/pp_size.h" |
| 19 #include "ppapi/c/ppb_graphics_2d.h" |
| 20 #include "srpcgen/ppb_rpc.h" |
| 21 #include "srpcgen/upcall.h" |
| 22 |
| 23 namespace ppapi_proxy { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const nacl_abi_size_t kPPSizeBytes = |
| 28 static_cast<nacl_abi_size_t>(sizeof(struct PP_Size)); |
| 29 const nacl_abi_size_t kPPPointBytes = |
| 30 static_cast<nacl_abi_size_t>(sizeof(struct PP_Point)); |
| 31 const nacl_abi_size_t kPPRectBytes = |
| 32 static_cast<nacl_abi_size_t>(sizeof(struct PP_Rect)); |
| 33 |
| 34 PP_Resource Create(PP_Instance instance, |
| 35 const struct PP_Size* size, |
| 36 PP_Bool is_always_opaque) { |
| 37 DebugPrintf("PPB_Graphics2D::Create: instance=%"NACL_PRIu32"\n", instance); |
| 38 char* size_as_char_ptr = |
| 39 reinterpret_cast<char*>(const_cast<struct PP_Size*>(size)); |
| 40 int32_t is_always_opaque_as_int = static_cast<int32_t>(is_always_opaque); |
| 41 PP_Resource resource = kInvalidResourceId; |
| 42 NaClSrpcError srpc_result = |
| 43 PpbGraphics2DRpcClient::PPB_Graphics2D_Create( |
| 44 GetMainSrpcChannel(), |
| 45 instance, |
| 46 kPPSizeBytes, |
| 47 size_as_char_ptr, |
| 48 is_always_opaque_as_int, |
| 49 &resource); |
| 50 DebugPrintf("PPB_Graphics2D::Create: %s\n", NaClSrpcErrorString(srpc_result)); |
| 51 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 52 scoped_refptr<PluginGraphics2D> graphics_2d = |
| 53 PluginResource::AdoptAs<PluginGraphics2D>(resource); |
| 54 if (graphics_2d.get()) { |
| 55 return resource; |
| 56 } |
| 57 } |
| 58 return kInvalidResourceId; |
| 59 } |
| 60 |
| 61 PP_Bool IsGraphics2D(PP_Resource resource) { |
| 62 DebugPrintf("PPB_Graphics2D::IsGraphics2D: resource=%"NACL_PRIu32"\n", |
| 63 resource); |
| 64 return PluginResource::GetAs<PluginGraphics2D>(resource).get() |
| 65 ? PP_TRUE : PP_FALSE; |
| 66 } |
| 67 |
| 68 PP_Bool Describe(PP_Resource graphics_2d, |
| 69 struct PP_Size* size, |
| 70 PP_Bool* is_always_opaque) { |
| 71 DebugPrintf("PPB_Graphics2D::Describe: graphics_2d=%"NACL_PRIu32"\n", |
| 72 graphics_2d); |
| 73 if (size == NULL || is_always_opaque == NULL) { |
| 74 return PP_FALSE; |
| 75 } |
| 76 int32_t is_always_opaque_as_int; |
| 77 nacl_abi_size_t size_ret = kPPSizeBytes; |
| 78 int32_t result; |
| 79 NaClSrpcError srpc_result = |
| 80 PpbGraphics2DRpcClient::PPB_Graphics2D_Describe( |
| 81 GetMainSrpcChannel(), |
| 82 graphics_2d, |
| 83 &size_ret, |
| 84 reinterpret_cast<char*>(size), |
| 85 &is_always_opaque_as_int, |
| 86 &result); |
| 87 DebugPrintf("PPB_Graphics2D::Describe: %s\n", |
| 88 NaClSrpcErrorString(srpc_result)); |
| 89 if (srpc_result == NACL_SRPC_RESULT_OK || size_ret != kPPSizeBytes) { |
| 90 *is_always_opaque = is_always_opaque_as_int ? PP_TRUE : PP_FALSE; |
| 91 return result ? PP_TRUE : PP_FALSE; |
| 92 } else { |
| 93 return PP_FALSE; |
| 94 } |
| 95 } |
| 96 |
| 97 void PaintImageData(PP_Resource graphics_2d, |
| 98 PP_Resource image, |
| 99 const struct PP_Point* top_left, |
| 100 const struct PP_Rect* src_rect) { |
| 101 DebugPrintf("PPB_Graphics2D::PaintImageData: graphics_2d=%"NACL_PRIu32"\n", |
| 102 graphics_2d); |
| 103 nacl_abi_size_t rect_size = kPPRectBytes; |
| 104 if (src_rect == NULL) { // Corresponds to the entire image. |
| 105 rect_size = 0; |
| 106 } |
| 107 NaClSrpcError srpc_result = |
| 108 PpbGraphics2DRpcClient::PPB_Graphics2D_PaintImageData( |
| 109 GetMainSrpcChannel(), |
| 110 graphics_2d, |
| 111 image, |
| 112 kPPPointBytes, |
| 113 reinterpret_cast<char*>(const_cast<struct PP_Point*>(top_left)), |
| 114 rect_size, |
| 115 reinterpret_cast<char*>(const_cast<struct PP_Rect*>(src_rect))); |
| 116 DebugPrintf("PPB_Graphics2D::PaintImageData: %s\n", |
| 117 NaClSrpcErrorString(srpc_result)); |
| 118 } |
| 119 |
| 120 void Scroll(PP_Resource graphics_2d, |
| 121 const struct PP_Rect* clip_rect, |
| 122 const struct PP_Point* amount) { |
| 123 DebugPrintf("PPB_Graphics2D::Scroll: graphics_2d=%"NACL_PRIu32"\n", |
| 124 graphics_2d); |
| 125 nacl_abi_size_t rect_size = kPPRectBytes; |
| 126 if (clip_rect == NULL) { // Corresponds to the entire graphics region. |
| 127 rect_size = 0; |
| 128 } |
| 129 NaClSrpcError srpc_result = PpbGraphics2DRpcClient::PPB_Graphics2D_Scroll( |
| 130 GetMainSrpcChannel(), |
| 131 graphics_2d, |
| 132 rect_size, |
| 133 reinterpret_cast<char*>(const_cast<struct PP_Rect*>(clip_rect)), |
| 134 kPPPointBytes, |
| 135 reinterpret_cast<char*>(const_cast<struct PP_Point*>(amount))); |
| 136 DebugPrintf("PPB_Graphics2D::Scroll: %s\n", NaClSrpcErrorString(srpc_result)); |
| 137 } |
| 138 |
| 139 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image) { |
| 140 DebugPrintf("PPB_Graphics2D::ReplaceContents: graphics_2d=%"NACL_PRIu32"\n", |
| 141 graphics_2d); |
| 142 NaClSrpcError srpc_result = |
| 143 PpbGraphics2DRpcClient::PPB_Graphics2D_ReplaceContents( |
| 144 GetMainSrpcChannel(), graphics_2d, image); |
| 145 DebugPrintf("PPB_Graphics2D::ReplaceContents: %s\n", |
| 146 NaClSrpcErrorString(srpc_result)); |
| 147 } |
| 148 |
| 149 int32_t Flush(PP_Resource graphics_2d, |
| 150 struct PP_CompletionCallback callback) { |
| 151 DebugPrintf("PPB_Graphics2D::Flush: graphics_2d=%"NACL_PRIu32"\n", |
| 152 graphics_2d); |
| 153 int32_t callback_id = |
| 154 CompletionCallbackTable::Get()->AddCallback(callback); |
| 155 if (callback_id == 0) // Just like Chrome, for now disallow blocking calls. |
| 156 return PP_ERROR_BADARGUMENT; |
| 157 |
| 158 int32_t pp_error; |
| 159 NaClSrpcError srpc_result = |
| 160 PpbGraphics2DRpcClient::PPB_Graphics2D_Flush( |
| 161 GetMainSrpcChannel(), graphics_2d, callback_id, &pp_error); |
| 162 DebugPrintf("PPB_Graphics2D::Flush: %s\n", NaClSrpcErrorString(srpc_result)); |
| 163 if (srpc_result != NACL_SRPC_RESULT_OK) |
| 164 pp_error = PP_ERROR_FAILED; |
| 165 return MayForceCallback(callback, pp_error); |
| 166 } |
| 167 |
| 168 } // namespace |
| 169 |
| 170 const PPB_Graphics2D* PluginGraphics2D::GetInterface() { |
| 171 static const PPB_Graphics2D graphics_2d_interface = { |
| 172 Create, |
| 173 IsGraphics2D, |
| 174 Describe, |
| 175 PaintImageData, |
| 176 Scroll, |
| 177 ReplaceContents, |
| 178 Flush, |
| 179 }; |
| 180 return &graphics_2d_interface; |
| 181 } |
| 182 |
| 183 } // namespace ppapi_proxy |
OLD | NEW |