mercredi 22 février 2017

Air native extension: pass bitmapdata from thread (winapi) to as3

In my native extension for air I want to create a screenshot. I managed to do this in synchronous way. Now I want to do it asynchronous in a thread (using WINAPI CreateThread).

In the thread I create new bitmapdata and set the color for each pixel. But now my question is:

How do I pass the bitmapdata created in the thread back to actionscript?

Here I create the thread:

FREObject getScreenShotAsynchronous(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
    FREGetObjectAsInt32(argv[1], &_iSsWidth);
    FREGetObjectAsInt32(argv[2], &_iSsHeight);
    FREGetObjectAsInt32(argv[3], &_iSsOffsetX);
    FREGetObjectAsInt32(argv[4], &_iSsOffsetY);

    //Context ref for thread:
    oCtxThreadScreenshot = ctx;

    //Create thread:
    bRunThreadScreenshot = true;
    hThreadScreenshot = CreateThread(NULL, 0, threadScreenshot, 0, 0, &dwThreadScreenshotID);

    return NULL;

And here is the thread itself:

DWORD WINAPI threadScreenshot(LPVOID data)
    int32_t iWidth, iHeight, iOffsetX, iOffsetY;
    iWidth = _iSsWidth;
    iHeight = _iSsHeight;
    iOffsetX = _iSsOffsetX;
    iOffsetY = _iSsOffsetY;

    FREObject freWidth;
    FRENewObjectFromUint32(uint32_t(iWidth), &freWidth);

    FREObject freHeight;
    FRENewObjectFromUint32(uint32_t(iHeight), &freHeight);

    FREObject freTransparent;
    FRENewObjectFromBool(uint32_t(0), &freTransparent);

    FREObject freFillColor;
    FRENewObjectFromUint32(uint32_t(0xFF0000), &freFillColor);

    // first get those values from imaage
    FREObject freArguments[4] = { freWidth, freHeight, freTransparent, freFillColor };

    // create an instance of BitmapData
    FREObject freoBitmapData;
    FRENewObject((uint8_t *)"flash.display.BitmapData", 4, freArguments, &freoBitmapData, NULL);

    // now acquire the bitmap data in order to manipulate it
    FREBitmapData freBitmapData;
    FREAcquireBitmapData(freoBitmapData, &freBitmapData);

    //Grab screenshot data and fill bitmapdata...

    // Tell Flash which region of the bitmapData changes (all of it here)
    FREInvalidateBitmapDataRect(freoBitmapData, 0, 0, freBitmapData.width, freBitmapData.height);
    // Release our control over the bitmapData

    //Dispatch event:
    FREDispatchStatusEventAsync(oCtxThreadScreenshot, (const uint8_t *)"onMsg", (const uint8_t *)"screenshot_done");

    return 0;

Thanks for help!

Aucun commentaire:

Enregistrer un commentaire