Yeah, not possible to avoid a copy here I think. The canvas's typed array cannot alias the emscripten memory typed array. But, hopefully those copies don't make things impractical, in my experience the speed of compiled code is worth the copy in/out of the compiled code's memory space.
Note that if you alias the heap and do a transfer of that array, you must copy it before - or it will transfer the entire underlying buffer, not just the view. This is an annoying footgun in typed arrays sadly. - Alon On Wed, May 28, 2014 at 9:19 AM, Chad Austin <[email protected]> wrote: > If you allocate data within the Emscripten heap via Module._malloc, you > can then create typed arrays that alias said region of the Emscripten heap > which allows zero-copy data transfer between C++ and JavaScript. > > It's not currently possible C++ to efficiently write into a Canvas's > TypedArray, but if your C++ decoded into the Emscripten heap, I think you > could use one of the ArrayBufferView.set calls to efficiently copy data > between the Emscripten heap and the canvas. > > Embind provides a memory_view type which, on the C++ side, describes a > range of memory (pointer + length). When passed to JavaScript, it comes > out as a typed array that aliases the Emscripten heap. Of course, you can > do this manually by passing pointer values between C++ and JS too. > > > https://github.com/kripken/emscripten/blob/master/system/include/emscripten/wire.h#L332 > > https://github.com/kripken/emscripten/blob/master/tests/embind/embind_test.cpp#L1170 > https://github.com/kripken/emscripten/blob/master/src/embind/embind.js#L541 > > > > On Wed, May 28, 2014 at 9:11 AM, ben layet <[email protected]> wrote: > >> One costly part of our app is image decoding. We use a custom codec not >> supported by browser - and which has a C++ implementation. We want a high >> frame rate, so I'm investigating options to improve performance. I thought >> we could pass the encoded image to a C++ function, decode and return the >> decoded result to the JS. >> >> Can I pass TypedArrays into the C++? (One as input, one to hold the >> output.) The wiki page "Interacting with Compiled >> Code<https://github.com/kripken/emscripten/wiki/Interacting-with-code>" >> only says that JS native types Number and String can be used. I could pass >> the encoded input as a String I suppose. But what about the output? In >> order to avoid copying, I would like to decode directly into the TypedArray >> from a Canvas 2d context ImageData object. >> >> As for using the C++ heap, I can't see how to do that without copying >> back and forth - as per the "Accessing Memory" example in the wiki page. >> (Well, maybe I could wrap a TypedArray around a part of Emscripten's heap >> ArrayBuffer that I reserved with malloc, but that would only useful with JS >> APIs that only consume TypedArrays - and it doesn't allow me to efficiently >> get the data from a TypedArray into the C++.) >> >> Maybe I've tried an awkward split between the C++ and the JS. >> >> Any comments welcome, thanks >> ben >> >> -- >> You received this message because you are subscribed to the Google Groups >> "emscripten-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Chad Austin > Technical Director, IMVU > http://engineering.imvu.com <http://www.imvu.com/members/Chad/> > http://chadaustin.me > > > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
