【问题标题】:how to send array buffer from and to in Native client and javascript如何在本机客户端和 javascript 中相互发送数组缓冲区
【发布时间】:2015-09-18 17:48:18
【问题描述】:

我想将 Javascript 中的数组缓冲区发送到 Native Client 模块,然后我想将数组缓冲区转换为整数指针。我在 nacl-sdk 目录中看到了 earth 示例。他们正在传递图像数据并将其转换为:

    //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 值处理。

【问题讨论】:

    标签: javascript arrays google-chrome-app google-nativeclient chrome-native-messaging


    【解决方案1】:

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

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

    pp::Var 变量会自动进行引用计数,因此您无需显式调用AddRef

    【讨论】:

    • 如果对您有用,请将答案标记为正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2023-03-14
    • 2013-10-27
    • 2019-12-24
    • 2017-07-26
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多