From ddd61db54184ca4491521f8ce8f4e50a4ca5025f Mon Sep 17 00:00:00 2001 From: "dmichael@chromium.org" Date: Wed, 7 Dec 2011 06:49:00 +0000 Subject: Draft of a PPAPI interface for ArrayBuffer. See the TypedArray spec for reference: http://www.khronos.org/registry/typedarray/specs/latest/ Things in the spec that I'm omiting: - slice (Having a view of the ArrayBuffer that has a different offset/length) TODO in future CLs: -Implementation for in-process/trusted + tests (almost ready) -NaCl proxy -OOP proxy Later still: - Support for ArrayBufferView and TypedArray based on that. BUG=103435 TEST=N/A Review URL: http://codereview.chromium.org/8502030 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113355 0039d316-1c4b-4281-b951-d872f2087c98 --- ppapi/api/dev/ppb_var_array_buffer_dev.idl | 47 +++++++++++++++ ppapi/api/pp_var.idl | 11 +++- ppapi/c/dev/ppb_var_array_buffer_dev.h | 67 ++++++++++++++++++++++ ppapi/c/pp_var.h | 12 +++- ppapi/cpp/dev/var_array_buffer_dev.cc | 66 +++++++++++++++++++++ ppapi/cpp/dev/var_array_buffer_dev.h | 57 ++++++++++++++++++ ppapi/cpp/var.h | 3 + .../src/shared/ppapi_proxy/object_serialize.cc | 4 ++ .../src/shared/ppapi_proxy/plugin_ppb_var.cc | 2 + ppapi/ppapi_sources.gypi | 5 +- ppapi/proxy/serialized_var.cc | 1 + 11 files changed, 271 insertions(+), 4 deletions(-) create mode 100644 ppapi/api/dev/ppb_var_array_buffer_dev.idl create mode 100644 ppapi/c/dev/ppb_var_array_buffer_dev.h create mode 100644 ppapi/cpp/dev/var_array_buffer_dev.cc create mode 100644 ppapi/cpp/dev/var_array_buffer_dev.h (limited to 'ppapi') diff --git a/ppapi/api/dev/ppb_var_array_buffer_dev.idl b/ppapi/api/dev/ppb_var_array_buffer_dev.idl new file mode 100644 index 0000000..9105b11 --- /dev/null +++ b/ppapi/api/dev/ppb_var_array_buffer_dev.idl @@ -0,0 +1,47 @@ +/* Copyright (c) 2011 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * This file defines the PPB_VarArrayBuffer_Dev struct. + */ + +label Chrome { + M17 = 0.1 +}; + +/** + * PPB_VarArrayBuffer_Dev API. This provides a way to interact with JavaScript + * ArrayBuffers, which represent a contiguous sequence of bytes. To manage the + * reference count for a VarArrayBuffer, please see PPB_Var. Note that + * these Vars are not part of the embedding page's DOM, and can only be shared + * with JavaScript via pp::Instance's PostMessage and HandleMessage functions. + */ +[macro="PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE"] +interface PPB_VarArrayBuffer_Dev { + /** + * Create a zero-initialized VarArrayBuffer. + * + * @param[in] size_in_bytes The size of the array buffer that will be created. + * + * @return A PP_Var which represents an VarArrayBuffer of the requested size + * with a reference count of 1. + */ + PP_Var Create([in] uint32_t size_in_bytes); + /** + * Returns the length of the VarArrayBuffer in bytes. + * + * @return The length of the VarArrayBuffer in bytes. + */ + uint32_t ByteLength([in] PP_Var array); + /** + * Returns a pointer to the beginning of the buffer for the given array. + * + * @param[in] array The array whose buffer should be returned. + * + * @return A pointer to the buffer for this array. + */ + mem_t Map([in] PP_Var array); +}; + diff --git a/ppapi/api/pp_var.idl b/ppapi/api/pp_var.idl index 1a6ad7e..8a1b05a 100644 --- a/ppapi/api/pp_var.idl +++ b/ppapi/api/pp_var.idl @@ -61,7 +61,16 @@ enum PP_VarType { * module will continue to work with future versions of the API. */ PP_VARTYPE_ARRAY = 7, - PP_VARTYPE_DICTIONARY = 8 + PP_VARTYPE_DICTIONARY = 8, + + /** + * ArrayBuffer represents a JavaScript ArrayBuffer. This is the type which + * represents Typed Arrays in JavaScript. Unlike JavaScript 'Array', it is + * only meant to contain basic numeric types, and is always stored + * contiguously. See PPB_VarArrayBuffer_Dev for functions special to + * ArrayBuffer vars. + */ + PP_VARTYPE_ARRAY_BUFFER = 9 }; diff --git a/ppapi/c/dev/ppb_var_array_buffer_dev.h b/ppapi/c/dev/ppb_var_array_buffer_dev.h new file mode 100644 index 0000000..49d9e6a --- /dev/null +++ b/ppapi/c/dev/ppb_var_array_buffer_dev.h @@ -0,0 +1,67 @@ +/* Copyright (c) 2011 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* From dev/ppb_var_array_buffer_dev.idl modified Fri Dec 2 16:17:08 2011. */ + +#ifndef PPAPI_C_DEV_PPB_VAR_ARRAY_BUFFER_DEV_H_ +#define PPAPI_C_DEV_PPB_VAR_ARRAY_BUFFER_DEV_H_ + +#include "ppapi/c/pp_bool.h" +#include "ppapi/c/pp_macros.h" +#include "ppapi/c/pp_stdint.h" +#include "ppapi/c/pp_var.h" + +#define PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE_0_1 "PPB_VarArrayBuffer(Dev);0.1" +#define PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE \ + PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE_0_1 + +/** + * @file + * This file defines the PPB_VarArrayBuffer_Dev struct. + */ + + +/** + * @addtogroup Interfaces + * @{ + */ +/** + * PPB_VarArrayBuffer_Dev API. This provides a way to interact with JavaScript + * ArrayBuffers, which represent a contiguous sequence of bytes. To manage the + * reference count for a VarArrayBuffer, please see PPB_Var. Note that + * these Vars are not part of the embedding page's DOM, and can only be shared + * with JavaScript via pp::Instance's PostMessage and HandleMessage functions. + */ +struct PPB_VarArrayBuffer_Dev { + /** + * Create a zero-initialized VarArrayBuffer. + * + * @param[in] size_in_bytes The size of the array buffer that will be created. + * + * @return A PP_Var which represents an VarArrayBuffer of the requested size + * with a reference count of 1. + */ + struct PP_Var (*Create)(uint32_t size_in_bytes); + /** + * Returns the length of the VarArrayBuffer in bytes. + * + * @return The length of the VarArrayBuffer in bytes. + */ + uint32_t (*ByteLength)(struct PP_Var array); + /** + * Returns a pointer to the beginning of the buffer for the given array. + * + * @param[in] array The array whose buffer should be returned. + * + * @return A pointer to the buffer for this array. + */ + void* (*Map)(struct PP_Var array); +}; +/** + * @} + */ + +#endif /* PPAPI_C_DEV_PPB_VAR_ARRAY_BUFFER_DEV_H_ */ + diff --git a/ppapi/c/pp_var.h b/ppapi/c/pp_var.h index fb174a8..2b388f6 100644 --- a/ppapi/c/pp_var.h +++ b/ppapi/c/pp_var.h @@ -3,7 +3,7 @@ * found in the LICENSE file. */ -/* From pp_var.idl modified Fri Nov 11 19:57:17 2011. */ +/* From pp_var.idl modified Fri Dec 2 16:45:08 2011. */ #ifndef PPAPI_C_PP_VAR_H_ #define PPAPI_C_PP_VAR_H_ @@ -68,7 +68,15 @@ typedef enum { * module will continue to work with future versions of the API. */ PP_VARTYPE_ARRAY = 7, - PP_VARTYPE_DICTIONARY = 8 + PP_VARTYPE_DICTIONARY = 8, + /** + * ArrayBuffer represents a JavaScript ArrayBuffer. This is the type which + * represents Typed Arrays in JavaScript. Unlike JavaScript 'Array', it is + * only meant to contain basic numeric types, and is always stored + * contiguously. See PPB_VarArrayBuffer_Dev for functions special to + * ArrayBuffer vars. + */ + PP_VARTYPE_ARRAY_BUFFER = 9 } PP_VarType; PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_VarType, 4); /** diff --git a/ppapi/cpp/dev/var_array_buffer_dev.cc b/ppapi/cpp/dev/var_array_buffer_dev.cc new file mode 100644 index 0000000..4ce6215 --- /dev/null +++ b/ppapi/cpp/dev/var_array_buffer_dev.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/cpp/dev/var_array_buffer_dev.h" + +#include "ppapi/c/dev/ppb_var_array_buffer_dev.h" +#include "ppapi/cpp/logging.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +namespace { + +template <> const char* interface_name() { + return PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE; +} + +} // namespace + +VarArrayBuffer_Dev::VarArrayBuffer_Dev(const Var& var) + : Var(var), buffer_(NULL) { + if (var.is_array_buffer()) { + buffer_ = Map(); + } else { + PP_NOTREACHED(); + var_ = PP_MakeNull(); + } +} + +VarArrayBuffer_Dev::VarArrayBuffer_Dev(uint32_t size_in_bytes) + : buffer_(NULL) { + if (has_interface()) { + var_ = get_interface()->Create(size_in_bytes); + buffer_ = Map(); + } else { + PP_NOTREACHED(); + var_ = PP_MakeNull(); + } +} + +uint32_t VarArrayBuffer_Dev::ByteLength() const { + if (has_interface()) { + return get_interface()->ByteLength(var_); + } + PP_NOTREACHED(); + return 0; +} + +const void* VarArrayBuffer_Dev::Map() const { + return const_cast(this)->Map(); +} + +void* VarArrayBuffer_Dev::Map() { + // TODO(dmichael): This is not thread-safe. + if (buffer_) + return buffer_; + if (has_interface()) { + buffer_ = get_interface()->Map(var_); + return buffer_; + } + PP_NOTREACHED(); + return 0; +} + +} // namespace pp diff --git a/ppapi/cpp/dev/var_array_buffer_dev.h b/ppapi/cpp/dev/var_array_buffer_dev.h new file mode 100644 index 0000000..6f8c959 --- /dev/null +++ b/ppapi/cpp/dev/var_array_buffer_dev.h @@ -0,0 +1,57 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_CPP_DEV_VAR_ARRAY_BUFFER_DEV_H_ +#define PPAPI_CPP_DEV_VAR_ARRAY_BUFFER_DEV_H_ + +#include "ppapi/cpp/var.h" + +/// @file +/// This file defines the API for interacting with an ArrayBuffer. + +namespace pp { + +/// VarArrayBuffer_Dev provides a way to interact with JavaScript ArrayBuffers, +/// which represent a contiguous sequence of bytes. Note that +/// VarArrayBuffer_Devs are not part of the embedding page's DOM, and can only +/// be shared with JavaScript via pp::Instance's PostMessage and HandleMessage +/// functions. +class VarArrayBuffer_Dev : public Var { + public: + /// Contruct a VarArrayBuffer_Dev given a var for which is_array_buffer() is + /// true. This will refer to the same buffer as var, but allows you to access + /// methods specific to VarArrayBuffer_Dev. + /// + /// @param[in] var An array buffer Var. + explicit VarArrayBuffer_Dev(const Var& var); + + VarArrayBuffer_Dev(const VarArrayBuffer_Dev& buffer) : Var(buffer) {} + + /// Construct a new VarArrayBuffer_Dev which is size_in_bytes bytes long and + /// initialized to zero. + /// + /// @param[in] size_in_bytes The size of the constructed array in bytes. + VarArrayBuffer_Dev(uint32_t size_in_bytes); + + /// Return the length of the VarArrayBuffer_Dev in bytes. + /// + /// @return The length of the VarArrayBuffer_Dev in bytes. + uint32_t ByteLength() const; + + /// Return a pointer to the buffer associated with this VarArrayBuffer_Dev. + /// + /// @return A pointer to the buffer associated with this VarArrayBuffer_Dev. + void* Map(); + const void* Map() const; + + virtual ~VarArrayBuffer_Dev() {} + + private: + // We cache the buffer so that repeated calls to Map() are quick. + void* buffer_; +}; + +} // namespace pp + +#endif // PPAPI_CPP_DEV_VAR_ARRAY_BUFFER_DEV_H_ diff --git a/ppapi/cpp/var.h b/ppapi/cpp/var.h index 0cd8218..fc65d38 100644 --- a/ppapi/cpp/var.h +++ b/ppapi/cpp/var.h @@ -158,6 +158,9 @@ class Var { var_.type == PP_VARTYPE_DOUBLE; } + /// This function determines if this Var is an ArrayBuffer. + bool is_array_buffer() const { return var_.type == PP_VARTYPE_ARRAY_BUFFER; } + /// AsBool() converts this Var to a bool. Assumes the /// internal representation is_bool(). If it's not, it will assert in debug /// mode, and return false. diff --git a/ppapi/native_client/src/shared/ppapi_proxy/object_serialize.cc b/ppapi/native_client/src/shared/ppapi_proxy/object_serialize.cc index 7fa0e02..16323af 100644 --- a/ppapi/native_client/src/shared/ppapi_proxy/object_serialize.cc +++ b/ppapi/native_client/src/shared/ppapi_proxy/object_serialize.cc @@ -131,6 +131,7 @@ uint32_t PpVarSize(const PP_Var& var) { case PP_VARTYPE_OBJECT: case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: NACL_NOTREACHED(); break; } @@ -215,6 +216,7 @@ bool SerializePpVar(const PP_Var* vars, case PP_VARTYPE_OBJECT: case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: NACL_NOTREACHED(); default: return false; @@ -306,6 +308,7 @@ uint32_t DeserializePpVarSize(char* p, case PP_VARTYPE_OBJECT: case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: NACL_NOTREACHED(); break; } @@ -377,6 +380,7 @@ bool DeserializePpVar(NaClSrpcChannel* channel, case PP_VARTYPE_OBJECT: case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: NACL_NOTREACHED(); default: return false; diff --git a/ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_var.cc b/ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_var.cc index 93ef384..558272e 100644 --- a/ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_var.cc +++ b/ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_var.cc @@ -129,6 +129,7 @@ std::string PluginVar::DebugString(const PP_Var& var) { } case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: NACL_NOTREACHED(); break; } @@ -191,6 +192,7 @@ void PluginVar::Print(const PP_Var& var) { break; case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: NACL_NOTREACHED(); break; } diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index 2bd9583..94f10fb 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -65,6 +65,7 @@ 'c/dev/ppb_scrollbar_dev.h', 'c/dev/ppb_testing_dev.h', 'c/dev/ppb_url_util_dev.h', + 'c/dev/ppb_var_array_buffer_dev.h', 'c/dev/ppb_video_decoder_dev.h', 'c/dev/ppb_websocket_dev.h', 'c/dev/ppb_widget_dev.h', @@ -200,6 +201,8 @@ 'cpp/dev/text_input_dev.h', 'cpp/dev/url_util_dev.cc', 'cpp/dev/url_util_dev.h', + 'cpp/dev/var_array_buffer_dev.cc', + 'cpp/dev/var_array_buffer_dev.h', 'cpp/dev/video_capture_client_dev.cc', 'cpp/dev/video_capture_client_dev.h', 'cpp/dev/video_capture_dev.cc', @@ -380,4 +383,4 @@ }, }], ], -} \ No newline at end of file +} diff --git a/ppapi/proxy/serialized_var.cc b/ppapi/proxy/serialized_var.cc index afd493f..7a9eb6f 100644 --- a/ppapi/proxy/serialized_var.cc +++ b/ppapi/proxy/serialized_var.cc @@ -157,6 +157,7 @@ void SerializedVar::Inner::WriteToMessage(IPC::Message* m) const { break; case PP_VARTYPE_ARRAY: case PP_VARTYPE_DICTIONARY: + case PP_VARTYPE_ARRAY_BUFFER: // TODO(brettw) when these are supported, implement this. NOTIMPLEMENTED(); break; -- cgit v1.1