如何在Native客户端和javascript中发送数组缓冲区

how to send array buffer from and to in Native client and javascript

本文关键字:数组 缓冲区 javascript Native 客户端      更新时间:2023-09-26

我想从Javascript向Native Client模块发送一个数组缓冲区,然后将数组缓冲区转换为整数指针。我在nacl-sdk目录中看到了地球的例子。他们正在传递图像数据,并像这样转换:

    //javascript
    var imageData = context.getImageData(0, 0, img.width, img.height);
// Send NaCl module the raw image data obtained from canvas.
common.naclModule.postMessage({'message' : 'texture',
                               'name' : name,
                               'width' : img.width,
                               'height' : img.height,
                               'data' : imageData.data.buffer});
    //nativeclient
    std::string name = dictionary.Get("name").AsString();
    int width = dictionary.Get("width").AsInt();
    int height = dictionary.Get("height").AsInt();
    pp::VarArrayBuffer array_buffer(dictionary.Get("data"));
    if (!name.empty() && !array_buffer.is_null()) {
      if (width > 0 && height > 0) {
        uint32_t* pixels = static_cast<uint32_t*>(array_buffer.Map());
        SetTexture(name, width, height, pixels);
        array_buffer.Unmap();

我正在使用eclipse调试,我不知道如何检查数组缓冲区是否正确接收,以及是否可以将像素作为参数传递给某个函数,或者在传递之前必须使用pixels = new uint32_t[size]创建它们。更重要的是,我需要知道如何将uint32_t*像素转换为VarArrayBuffer,并使用字典将其发送到Javascript并发布消息,以及如何在Javascript中接收并将消息作为ArrayBuffer值处理。

最简单的例子是SDK中的ArrayBuffer示例(examples/api/var_array_buffer)。

ArrayBuffer的内存归pp::VarArrayBuff所有,所以只要您有对它的引用(并且您还没有调用pp::VarArrayBuffer::Unmap),就不必复制内存。

pp::Var变量是自动引用计数的,所以您不需要显式调用AddRef。