Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
surface.cpp
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "surface.h"
6#include <algorithm>
7
12
13using namespace Skwasm;
14
15Surface::Surface() {
16 assert(emscripten_is_main_browser_thread());
17
18 pthread_attr_t attr;
19 pthread_attr_init(&attr);
20 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
21
22 pthread_create(
23 &_thread, &attr,
24 [](void* context) -> void* {
25 static_cast<Surface*>(context)->_runWorker();
26 return nullptr;
27 },
28 this);
29 // Listen to messages from the worker
31
32 // Synchronize the time origin for the worker thread
34}
35
36// Main thread only
38 assert(emscripten_is_main_browser_thread());
39 emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VI,
40 reinterpret_cast<void*>(fDispose), nullptr,
41 this);
42}
43
44// Main thread only
45uint32_t Surface::renderPictures(SkPicture** pictures, int count) {
46 assert(emscripten_is_main_browser_thread());
47 uint32_t callbackId = ++_currentCallbackId;
48 std::unique_ptr<sk_sp<SkPicture>[]> picturePointers =
49 std::make_unique<sk_sp<SkPicture>[]>(count);
50 for (int i = 0; i < count; i++) {
51 picturePointers[i] = sk_ref_sp(pictures[i]);
52 }
53
54 // Releasing picturePointers here and will recreate the unique_ptr on the
55 // other thread See surface_renderPicturesOnWorker
56 skwasm_dispatchRenderPictures(_thread, this, picturePointers.release(), count,
57 callbackId);
58 return callbackId;
59}
60
61// Main thread only
63 assert(emscripten_is_main_browser_thread());
64 uint32_t callbackId = ++_currentCallbackId;
65 image->ref();
66
67 emscripten_dispatch_to_thread(_thread, EM_FUNC_SIG_VIIII,
68 reinterpret_cast<void*>(fRasterizeImage),
69 nullptr, this, image, format, callbackId);
70 return callbackId;
71}
72
73std::unique_ptr<TextureSourceWrapper> Surface::createTextureSourceWrapper(
74 SkwasmObject textureSource) {
75 return std::unique_ptr<TextureSourceWrapper>(
76 new TextureSourceWrapper(_thread, textureSource));
77}
78
79// Main thread only
81 assert(emscripten_is_main_browser_thread());
82 _callbackHandler = callbackHandler;
83}
84
85// Worker thread only
86void Surface::_runWorker() {
87 _init();
88 emscripten_exit_with_live_runtime();
89}
90
91// Worker thread only
92void Surface::_init() {
93 // Listen to messages from the main thread
95 _glContext = skwasm_createOffscreenCanvas(256, 256);
96 if (!_glContext) {
97 printf("Failed to create context!\n");
98 return;
99 }
100
101 makeCurrent(_glContext);
102 emscripten_webgl_enable_extension(_glContext, "WEBGL_debug_renderer_info");
103
105
106 // WebGL should already be clearing the color and stencil buffers, but do it
107 // again here to ensure Skia receives them in the expected state.
108 emscripten_glBindFramebuffer(GL_FRAMEBUFFER, 0);
109 emscripten_glClearColor(0, 0, 0, 0);
110 emscripten_glClearStencil(0);
111 emscripten_glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
114
115 // The on-screen canvas is FBO 0. Wrap it in a Skia render target so Skia
116 // can render to it.
117 _fbInfo.fFBOID = 0;
118 _fbInfo.fFormat = GL_RGBA8_OES;
119
120 emscripten_glGetIntegerv(GL_SAMPLES, &_sampleCount);
121 emscripten_glGetIntegerv(GL_STENCIL_BITS, &_stencil);
122}
123
124// Worker thread only
125void Surface::_dispose() {
126 delete this;
127}
128
129// Worker thread only
130void Surface::_resizeCanvasToFit(int width, int height) {
131 if (!_surface || width > _canvasWidth || height > _canvasHeight) {
132 _canvasWidth = std::max(width, _canvasWidth);
133 _canvasHeight = std::max(height, _canvasHeight);
134 _recreateSurface();
135 }
136}
137
138// Worker thread only
139void Surface::_recreateSurface() {
140 makeCurrent(_glContext);
141 skwasm_resizeCanvas(_glContext, _canvasWidth, _canvasHeight);
142 auto target = GrBackendRenderTargets::MakeGL(_canvasWidth, _canvasHeight,
143 _sampleCount, _stencil, _fbInfo);
147}
148
149// Worker thread only
151 int pictureCount,
152 uint32_t callbackId,
153 double rasterStart) {
154 // This is populated by the `captureImageBitmap` call the first time it is
155 // passed in.
156 SkwasmObject imagePromiseArray = __builtin_wasm_ref_null_extern();
157 for (int i = 0; i < pictureCount; i++) {
158 sk_sp<SkPicture> picture = pictures[i];
159 SkRect pictureRect = picture->cullRect();
160 SkIRect roundedOutRect;
161 pictureRect.roundOut(&roundedOutRect);
162 _resizeCanvasToFit(roundedOutRect.width(), roundedOutRect.height());
163 SkMatrix matrix =
164 SkMatrix::Translate(-roundedOutRect.fLeft, -roundedOutRect.fTop);
165 makeCurrent(_glContext);
166 auto canvas = _surface->getCanvas();
168 canvas->drawPicture(picture, &matrix, nullptr);
169 _grContext->flush(_surface.get());
170 imagePromiseArray =
171 skwasm_captureImageBitmap(_glContext, roundedOutRect.width(),
172 roundedOutRect.height(), imagePromiseArray);
173 }
174 skwasm_resolveAndPostImages(this, imagePromiseArray, rasterStart, callbackId);
175}
176
177void Surface::_rasterizeImage(SkImage* image,
179 uint32_t callbackId) {
180 // We handle PNG encoding with browser APIs so that we can omit libpng from
181 // skia to save binary size.
182 assert(format != ImageByteFormat::png);
183 sk_sp<SkData> data;
189 alphaType, SkColorSpace::MakeSRGB());
190 size_t bytesPerRow = 4 * image->width();
191 size_t byteSize = info.computeByteSize(bytesPerRow);
192 data = SkData::MakeUninitialized(byteSize);
193 uint8_t* pixels = reinterpret_cast<uint8_t*>(data->writable_data());
194 bool success = image->readPixels(_grContext.get(), image->imageInfo(), pixels,
195 bytesPerRow, 0, 0);
196 if (!success) {
197 printf("Failed to read pixels from image!\n");
198 data = nullptr;
199 }
200 emscripten_async_run_in_main_runtime_thread(
201 EM_FUNC_SIG_VIII, fOnRasterizeComplete, this, data.release(), callbackId);
202}
203
204void Surface::_onRasterizeComplete(SkData* data, uint32_t callbackId) {
205 _callbackHandler(callbackId, data, __builtin_wasm_ref_null_extern());
206}
207
208// Main thread only
209void Surface::onRenderComplete(uint32_t callbackId, SkwasmObject imageBitmap) {
210 assert(emscripten_is_main_browser_thread());
211 _callbackHandler(callbackId, nullptr, imageBitmap);
212}
213
214void Surface::fDispose(Surface* surface) {
215 surface->_dispose();
216}
217
218void Surface::fOnRasterizeComplete(Surface* surface,
219 SkData* imageData,
220 uint32_t callbackId) {
221 surface->_onRasterizeComplete(imageData, callbackId);
222}
223
224void Surface::fRasterizeImage(Surface* surface,
225 SkImage* image,
227 uint32_t callbackId) {
228 surface->_rasterizeImage(image, format, callbackId);
229 image->unref();
230}
231
233 return new Surface();
234}
235
237 return surface->getThreadId();
238}
239
242 Surface::CallbackHandler* callbackHandler) {
243 surface->setCallbackHandler(callbackHandler);
244}
245
249
251 SkPicture** pictures,
252 int count) {
253 return surface->renderPictures(pictures, count);
254}
255
257 sk_sp<SkPicture>* pictures,
258 int pictureCount,
259 uint32_t callbackId,
260 double rasterStart) {
261 // This will release the pictures when they leave scope.
262 std::unique_ptr<sk_sp<SkPicture>[]> picturesPointer =
263 std::unique_ptr<sk_sp<SkPicture>[]>(pictures);
264 surface->renderPicturesOnWorker(pictures, pictureCount, callbackId,
265 rasterStart);
266}
267
269 SkImage* image,
271 return surface->rasterizeImage(image, format);
272}
273
274// This is used by the skwasm JS support code to call back into C++ when the
275// we finish creating the image bitmap, which is an asynchronous operation.
277 uint32_t callbackId,
278 SkwasmObject imageBitmap) {
279 return surface->onRenderComplete(callbackId, imageBitmap);
280}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
SK_API sk_sp< const GrGLInterface > GrGLMakeNativeInterface()
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
@ kMisc_GrGLBackendState
Definition GrTypes.h:169
@ kRenderTarget_GrGLBackendState
Definition GrTypes.h:157
SkAlphaType
Definition SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kUnpremul_SkAlphaType
pixel components are independent of alpha
Definition SkAlphaType.h:30
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
void resetContext(uint32_t state=kAll_GrBackendState)
GrSemaphoresSubmitted flush(const GrFlushInfo &info)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
const SkImageInfo & imageInfo() const
Definition SkImage.h:279
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Definition SkImage.cpp:42
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
virtual SkRect cullRect() const =0
void ref() const
Definition SkRefCnt.h:62
void unref() const
Definition SkRefCnt.h:72
SkCanvas * getCanvas()
Definition SkSurface.cpp:82
std::unique_ptr< TextureSourceWrapper > createTextureSourceWrapper(SkwasmObject textureSource)
Definition surface.cpp:73
void setCallbackHandler(CallbackHandler *callbackHandler)
Definition surface.cpp:80
void(uint32_t, void *, SkwasmObject) CallbackHandler
Definition surface.h:56
void onRenderComplete(uint32_t callbackId, SkwasmObject imageBitmap)
Definition surface.cpp:209
uint32_t rasterizeImage(SkImage *image, ImageByteFormat format)
Definition surface.cpp:62
void renderPicturesOnWorker(sk_sp< SkPicture > *picture, int pictureCount, uint32_t callbackId, double rasterStart)
Definition surface.cpp:150
uint32_t renderPictures(SkPicture **picture, int count)
Definition surface.cpp:45
T * get() const
Definition SkRefCnt.h:303
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
uint32_t uint32_t * format
uint32_t * target
SKWASM_EXPORT void surface_destroy(Surface *surface)
Definition surface.cpp:246
SKWASM_EXPORT uint32_t surface_renderPictures(Surface *surface, SkPicture **pictures, int count)
Definition surface.cpp:250
SKWASM_EXPORT void surface_renderPicturesOnWorker(Surface *surface, sk_sp< SkPicture > *pictures, int pictureCount, uint32_t callbackId, double rasterStart)
Definition surface.cpp:256
SKWASM_EXPORT void surface_onRenderComplete(Surface *surface, uint32_t callbackId, SkwasmObject imageBitmap)
Definition surface.cpp:276
SKWASM_EXPORT void surface_setCallbackHandler(Surface *surface, Surface::CallbackHandler *callbackHandler)
Definition surface.cpp:240
SKWASM_EXPORT unsigned long surface_getThreadId(Surface *surface)
Definition surface.cpp:236
SKWASM_EXPORT Surface * surface_create()
Definition surface.cpp:232
SKWASM_EXPORT uint32_t surface_rasterizeImage(Surface *surface, SkImage *image, ImageByteFormat format)
Definition surface.cpp:268
SK_API GrBackendRenderTarget MakeGL(int width, int height, int sampleCnt, int stencilBits, const GrGLFramebufferInfo &glInfo)
SK_API sk_sp< GrDirectContext > MakeGL()
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)
Definition wrappers.h:22
ImageByteFormat
Definition surface.h:30
int32_t height
int32_t width
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
Definition SkRect.h:165
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
constexpr int32_t width() const
Definition SkRect.h:158
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
void roundOut(SkIRect *dst) const
Definition SkRect.h:1241
#define SKWASM_EXPORT
Definition export.h:10