17 assert(emscripten_is_main_browser_thread());
20 pthread_attr_init(&attr);
21 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
25 [](
void* context) ->
void* {
26 static_cast<Surface*
>(context)->_runWorker();
39 assert(emscripten_is_main_browser_thread());
40 emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VI,
41 reinterpret_cast<void*
>(fDispose),
nullptr,
47 assert(emscripten_is_main_browser_thread());
48 uint32_t callbackId = ++_currentCallbackId;
49 std::unique_ptr<sk_sp<SkPicture>[]> picturePointers =
50 std::make_unique<sk_sp<SkPicture>[]>(
count);
64 assert(emscripten_is_main_browser_thread());
65 uint32_t callbackId = ++_currentCallbackId;
68 emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VIIII,
69 reinterpret_cast<void*
>(fRasterizeImage),
76 return std::unique_ptr<TextureSourceWrapper>(
82 assert(emscripten_is_main_browser_thread());
83 _callbackHandler = callbackHandler;
87void Surface::_runWorker() {
89 emscripten_exit_with_live_runtime();
93void Surface::_init() {
98 printf(
"Failed to create context!\n");
103 emscripten_webgl_enable_extension(_glContext,
"WEBGL_debug_renderer_info");
109 emscripten_glBindFramebuffer(GL_FRAMEBUFFER, 0);
110 emscripten_glClearColor(0, 0, 0, 0);
111 emscripten_glClearStencil(0);
112 emscripten_glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
119 _fbInfo.
fFormat = GL_RGBA8_OES;
121 emscripten_glGetIntegerv(GL_SAMPLES, &_sampleCount);
122 emscripten_glGetIntegerv(GL_STENCIL_BITS, &_stencil);
126void Surface::_dispose() {
131void Surface::_resizeCanvasToFit(
int width,
int height) {
132 if (!_surface ||
width > _canvasWidth ||
height > _canvasHeight) {
140void Surface::_recreateSurface() {
144 _sampleCount, _stencil, _fbInfo);
154 double rasterStart) {
157 SkwasmObject imagePromiseArray = __builtin_wasm_ref_null_extern();
158 for (
int i = 0;
i < pictureCount;
i++) {
162 pictureRect.
roundOut(&roundedOutRect);
163 _resizeCanvasToFit(roundedOutRect.
width(), roundedOutRect.
height());
173 roundedOutRect.
height(), imagePromiseArray);
180 uint32_t callbackId) {
192 size_t byteSize =
info.computeByteSize(bytesPerRow);
194 uint8_t* pixels =
reinterpret_cast<uint8_t*
>(
data->writable_data());
198 printf(
"Failed to read pixels from image!\n");
201 emscripten_async_run_in_main_runtime_thread(
202 EM_FUNC_SIG_VIII, fOnRasterizeComplete,
this,
data.release(), callbackId);
205void Surface::_onRasterizeComplete(
SkData*
data, uint32_t callbackId) {
206 _callbackHandler(callbackId,
data, __builtin_wasm_ref_null_extern());
211 assert(emscripten_is_main_browser_thread());
212 _callbackHandler(callbackId,
nullptr, imageBitmap);
221 uint32_t callbackId) {
222 surface->_onRasterizeComplete(imageData, callbackId);
228 uint32_t callbackId) {
244 surface->setCallbackHandler(callbackHandler);
261 double rasterStart) {
263 std::unique_ptr<sk_sp<SkPicture>[]> picturesPointer =
264 std::unique_ptr<sk_sp<SkPicture>[]>(pictures);
265 surface->renderPicturesOnWorker(pictures, pictureCount, callbackId,
280 return surface->onRenderComplete(callbackId, imageBitmap);
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
@ kBottomLeft_GrSurfaceOrigin
@ kRenderTarget_GrGLBackendState
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
constexpr SkColor SK_ColorTRANSPARENT
sk_sp< T > sk_ref_sp(T *obj)
void resetContext(uint32_t state=kAll_GrBackendState)
GrSemaphoresSubmitted flush(const GrFlushInfo &info)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkData > MakeUninitialized(size_t length)
const SkImageInfo & imageInfo() const
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
static SkMatrix Translate(SkScalar dx, SkScalar dy)
virtual SkRect cullRect() const =0
std::unique_ptr< TextureSourceWrapper > createTextureSourceWrapper(SkwasmObject textureSource)
void setCallbackHandler(CallbackHandler *callbackHandler)
void(uint32_t, void *, SkwasmObject) CallbackHandler
void onRenderComplete(uint32_t callbackId, SkwasmObject imageBitmap)
uint32_t rasterizeImage(SkImage *image, ImageByteFormat format)
void renderPicturesOnWorker(sk_sp< SkPicture > *picture, int pictureCount, uint32_t callbackId, double rasterStart)
uint32_t renderPictures(SkPicture **picture, int count)
uint32_t uint32_t * format
static float max(float r, float g, float b)
SKWASM_EXPORT void surface_destroy(Surface *surface)
SKWASM_EXPORT uint32_t surface_renderPictures(Surface *surface, SkPicture **pictures, int count)
SKWASM_EXPORT void surface_renderPicturesOnWorker(Surface *surface, sk_sp< SkPicture > *pictures, int pictureCount, uint32_t callbackId, double rasterStart)
SKWASM_EXPORT void surface_onRenderComplete(Surface *surface, uint32_t callbackId, SkwasmObject imageBitmap)
SKWASM_EXPORT void surface_setCallbackHandler(Surface *surface, Surface::CallbackHandler *callbackHandler)
SKWASM_EXPORT unsigned long surface_getThreadId(Surface *surface)
SKWASM_EXPORT Surface * surface_create()
SKWASM_EXPORT uint32_t surface_rasterizeImage(Surface *surface, SkImage *image, ImageByteFormat format)
SK_API GrBackendRenderTarget MakeGL(int width, int height, int sampleCnt, int stencilBits, const GrGLFramebufferInfo &glInfo)
SK_API sk_sp< GrDirectContext > MakeGL(sk_sp< const GrGLInterface >, const GrContextOptions &)
SK_API sk_sp< const GrGLInterface > MakeWebGL()
unsigned useCenter Optional< SkMatrix > matrix
sk_sp< const SkImage > image
sk_sp< const SkPicture > picture
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
SK_API sk_sp< SkSurface > WrapBackendRenderTarget(GrRecordingContext *context, const GrBackendRenderTarget &backendRenderTarget, GrSurfaceOrigin origin, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, RenderTargetReleaseProc releaseProc=nullptr, ReleaseContext releaseContext=nullptr)
void makeCurrent(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE handle)
void skwasm_dispatchRenderPictures(unsigned long threadId, Skwasm::Surface *surface, sk_sp< SkPicture > *pictures, int count, uint32_t callbackId)
SkwasmObject skwasm_captureImageBitmap(uint32_t contextHandle, int width, int height, SkwasmObject imagePromises)
uint32_t skwasm_createOffscreenCanvas(int width, int height)
void skwasm_resizeCanvas(uint32_t contextHandle, int width, int height)
void skwasm_syncTimeOriginForThread(pthread_t threadId)
void skwasm_resolveAndPostImages(Skwasm::Surface *surface, SkwasmObject imagePromises, double rasterStart, uint32_t callbackId)
__externref_t SkwasmObject
void skwasm_registerMessageListener(pthread_t threadId)
constexpr int32_t height() const
int32_t fTop
smaller y-axis bounds
constexpr int32_t width() const
int32_t fLeft
smaller x-axis bounds
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
void roundOut(SkIRect *dst) const
std::shared_ptr< const fml::Mapping > data