How to send array buffer from and to native client and javascript

I want to send an array buffer from Javascript to a native client module, and then I want to convert the array buffer to an integer pointer. I saw an example of land in the nacl-sdk directory. They pass in image data and transform it like this:

    //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();

      

I am using eclipse debug and I don't know how to check if the array buffer was received correctly and if I can pass the pixels as a parameter to some function or do I need to create them with pixels = new uint32_t[size]

before passing. More importantly, I need to know how to convert uint32_t*

pixels to VarArrayBuffer

and send it to Javascript using a dictionary and post a message and how to get it in Javascript and process the message as a value ArrayBuffer

.

+3


source to share


1 answer


The simplest example of this is the ArrayBuffer example in the SDK (examples / api / var_array_buffer).

The memory for the ArrayBuffer belongs to pp :: VarArrayBuffer, so as long as you refer to this (and you haven't called pp :: VarArrayBuffer :: Unmap ) you don't need to make a memory copy.



pp :: var variables are automatically counted, so you don't need to explicitly call AddRef .

+2


source







All Articles