Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
Skwasm::Surface Class Reference

#include <surface.h>

Public Types

using CallbackHandler = void(uint32_t, void *, SkwasmObject)
 

Public Member Functions

 Surface ()
 
unsigned long GetThreadId ()
 
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE GetGlContext ()
 
void SetCallbackHandler (CallbackHandler *callback_handler)
 
void Dispose ()
 
uint32_t SetCanvas (SkwasmObject canvas)
 
void OnInitialized (uint32_t callback_id)
 
void ReceiveCanvasOnWorker (SkwasmObject canvas, uint32_t callback_id)
 
uint32_t SetSize (int width, int height)
 
void OnResizeComplete (uint32_t callback_id)
 
void ResizeOnWorker (int width, int height, uint32_t callback_id)
 
uint32_t RenderPictures (flutter::DisplayList **picture, int count)
 
void OnRenderComplete (uint32_t callback_id, SkwasmObject image_bitmap)
 
void RenderPicturesOnWorker (sk_sp< flutter::DisplayList > *picture, int picture_count, uint32_t callback_id, double raster_start)
 
uint32_t RasterizeImage (flutter::DlImage *image, ImageByteFormat format)
 
void OnRasterizeComplete (uint32_t callback_id, SkData *data)
 
void RasterizeImageOnWorker (flutter::DlImage *image, ImageByteFormat format, uint32_t callback_id)
 
uint32_t TriggerContextLoss ()
 
void OnContextLossTriggered (uint32_t callback_id)
 
void ReportContextLost (uint32_t callback_id)
 
void TriggerContextLossOnWorker (uint32_t callback_id)
 
void OnContextLost ()
 
void SetResourceCacheLimit (int bytes)
 
std::unique_ptr< TextureSourceWrapperCreateTextureSourceWrapper (SkwasmObject textureSource)
 

Detailed Description

Definition at line 36 of file surface.h.

Member Typedef Documentation

◆ CallbackHandler

using Skwasm::Surface::CallbackHandler = void(uint32_t, void*, SkwasmObject)

Definition at line 38 of file surface.h.

Constructor & Destructor Documentation

◆ Surface()

Skwasm::Surface::Surface ( )

Definition at line 44 of file surface.cc.

44 {
47 } else {
48 assert(emscripten_is_main_browser_thread());
49
50 thread_ = emscripten_malloc_wasm_worker(65536);
51 emscripten_wasm_worker_post_function_v(thread_, []() {
52 // Listen to the main thread from the worker
54 });
55
56 // Listen to messages from the worker
57 skwasm_connectThread(thread_);
58 }
59}
void skwasm_connectThread(pthread_t thread_id)
bool skwasm_isSingleThreaded()

References skwasm_connectThread(), and skwasm_isSingleThreaded().

Member Function Documentation

◆ CreateTextureSourceWrapper()

std::unique_ptr< Skwasm::TextureSourceWrapper > Skwasm::Surface::CreateTextureSourceWrapper ( SkwasmObject  textureSource)

Definition at line 294 of file surface.cc.

294 {
295 return std::unique_ptr<Skwasm::TextureSourceWrapper>(
296 new Skwasm::TextureSourceWrapper(thread_, texture_source));
297}

◆ Dispose()

void Skwasm::Surface::Dispose ( )

Definition at line 70 of file surface.cc.

70 {
71 if (gl_context_) {
72 skwasm_destroyContext(gl_context_);
73 }
74 delete this;
75}
void skwasm_destroyContext(uint32_t context_handle)

References skwasm_destroyContext().

◆ GetGlContext()

EMSCRIPTEN_WEBGL_CONTEXT_HANDLE Skwasm::Surface::GetGlContext ( )
inline

Definition at line 45 of file surface.h.

45{ return gl_context_; }

◆ GetThreadId()

unsigned long Skwasm::Surface::GetThreadId ( )
inline

Definition at line 44 of file surface.h.

44{ return thread_; }

◆ OnContextLossTriggered()

void Skwasm::Surface::OnContextLossTriggered ( uint32_t  callback_id)

Definition at line 263 of file surface.cc.

263 {
264 assert(emscripten_is_main_browser_thread());
265 callback_handler_(callback_id, nullptr, __builtin_wasm_ref_null_extern());
266}

◆ OnContextLost()

void Skwasm::Surface::OnContextLost ( )

Definition at line 279 of file surface.cc.

279 {
280 if (!context_lost_callback_id_) {
281 printf("Received context lost event but never set callback handler!\n");
282 return;
283 }
284 skwasm_reportContextLost(this, context_lost_callback_id_);
285}
void skwasm_reportContextLost(Skwasm::Surface *surface, uint32_t callback_id)

References skwasm_reportContextLost().

◆ OnInitialized()

void Skwasm::Surface::OnInitialized ( uint32_t  callback_id)

Definition at line 85 of file surface.cc.

85 {
86 assert(emscripten_is_main_browser_thread());
87 callback_handler_(callback_id, (void*)context_lost_callback_id_,
88 __builtin_wasm_ref_null_extern());
89}

◆ OnRasterizeComplete()

void Skwasm::Surface::OnRasterizeComplete ( uint32_t  callback_id,
SkData *  data 
)

Definition at line 214 of file surface.cc.

214 {
215 assert(emscripten_is_main_browser_thread());
216 callback_handler_(callback_id, data, __builtin_wasm_ref_null_extern());
217}
std::shared_ptr< const fml::Mapping > data

References data.

◆ OnRenderComplete()

void Skwasm::Surface::OnRenderComplete ( uint32_t  callback_id,
SkwasmObject  image_bitmap 
)

Definition at line 177 of file surface.cc.

178 {
179 assert(emscripten_is_main_browser_thread());
180 callback_handler_(callback_id, nullptr, image_bitmap);
181}

◆ OnResizeComplete()

void Skwasm::Surface::OnResizeComplete ( uint32_t  callback_id)

Definition at line 138 of file surface.cc.

138 {
139 assert(emscripten_is_main_browser_thread());
140 callback_handler_(callback_id, nullptr, __builtin_wasm_ref_null_extern());
141}

◆ RasterizeImage()

uint32_t Skwasm::Surface::RasterizeImage ( flutter::DlImage image,
Skwasm::ImageByteFormat  format 
)

Definition at line 204 of file surface.cc.

205 {
206 assert(emscripten_is_main_browser_thread());
207 uint32_t callback_id = ++current_callback_id_;
208 image->ref();
209
210 skwasm_dispatchRasterizeImage(thread_, this, image, format, callback_id);
211 return callback_id;
212}
FlutterVulkanImage * image
void skwasm_dispatchRasterizeImage(unsigned long thread_id, Skwasm::Surface *surface, flutter::DlImage *image, Skwasm::ImageByteFormat format, uint32_t callback_id)

References format, image, and skwasm_dispatchRasterizeImage().

◆ RasterizeImageOnWorker()

void Skwasm::Surface::RasterizeImageOnWorker ( flutter::DlImage image,
Skwasm::ImageByteFormat  format,
uint32_t  callback_id 
)

Definition at line 219 of file surface.cc.

221 {
222 // We handle PNG encoding with browser APIs so that we can omit libpng from
223 // skia to save binary size.
224 assert(format != Skwasm::ImageByteFormat::png);
225 Skwasm::makeCurrent(gl_context_);
227 ? SkAlphaType::kUnpremul_SkAlphaType
228 : SkAlphaType::kPremul_SkAlphaType;
229 SkImageInfo info = SkImageInfo::Make(image->width(), image->height(),
230 SkColorType::kRGBA_8888_SkColorType,
231 alpha_type, SkColorSpace::MakeSRGB());
232 sk_sp<SkData> data;
233 size_t bytes_per_row = 4 * image->width();
234 size_t byte_size = info.computeByteSize(bytes_per_row);
235 data = SkData::MakeUninitialized(byte_size);
236 uint8_t* pixels = reinterpret_cast<uint8_t*>(data->writable_data());
237
238 // TODO(jacksongardner):
239 // Normally we'd just call `readPixels` on the image. However, this doesn't
240 // actually work in some cases due to a skia bug. Instead, we just draw the
241 // image to our scratch canvas and grab the pixels out directly with
242 // `glReadPixels`. Once the skia bug is fixed, we should switch back to using
243 // `SkImage::readPixels` instead.
244 // See https://g-issues.skia.org/issues/349201915
245 render_context_->RenderImage(image, format);
246
247 emscripten_glReadPixels(0, 0, image->width(), image->height(), GL_RGBA,
248 GL_UNSIGNED_BYTE, reinterpret_cast<void*>(pixels));
249
250 image->unref();
251 skwasm_postRasterizeResult(this, data.release(), callback_id);
252}
uint32_t uint32_t * format
void makeCurrent(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE handle)
Definition wrappers.h:26
uint32_t alpha_type
void skwasm_postRasterizeResult(Skwasm::Surface *surface, SkData *data, uint32_t callback_id)

References alpha_type, data, format, image, Skwasm::makeCurrent(), Skwasm::png, Skwasm::rawStraightRgba, and skwasm_postRasterizeResult().

◆ ReceiveCanvasOnWorker()

void Skwasm::Surface::ReceiveCanvasOnWorker ( SkwasmObject  canvas,
uint32_t  callback_id 
)

Definition at line 92 of file surface.cc.

93 {
94 if (render_context_) {
95 render_context_.reset();
96 }
97 canvas_width_ = 1;
98 canvas_height_ = 1;
99 gl_context_ = skwasm_getGlContextForCanvas(canvas, this);
100 if (!gl_context_) {
101 printf("Failed to create context!\n");
102 return;
103 }
104
105 Skwasm::makeCurrent(gl_context_);
106 emscripten_webgl_enable_extension(gl_context_, "WEBGL_debug_renderer_info");
107
108 // WebGL should already be clearing the color and stencil buffers, but do it
109 // again here to ensure Skia receives them in the expected state.
110 emscripten_glBindFramebuffer(GL_FRAMEBUFFER, 0);
111 emscripten_glClearColor(0, 0, 0, 0);
112 emscripten_glClearStencil(0);
113 emscripten_glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
114
115 int sample_count;
116 int stencil;
117 emscripten_glGetIntegerv(GL_SAMPLES, &sample_count);
118 emscripten_glGetIntegerv(GL_STENCIL_BITS, &stencil);
119
120 render_context_ = Skwasm::RenderContext::Make(sample_count, stencil);
121 render_context_->Resize(canvas_width_, canvas_height_);
122
123 context_lost_callback_id_ = ++current_callback_id_;
124
125 skwasm_reportInitialized(this, context_lost_callback_id_, callback_id);
126}
static std::unique_ptr< RenderContext > Make(int sample_count, int stencil)
uint32_t skwasm_getGlContextForCanvas(SkwasmObject canvas, Skwasm::Surface *surface)
void skwasm_reportInitialized(Skwasm::Surface *surface, uint32_t callback_id, uint32_t context_lost_callback_id)

References Skwasm::RenderContext::Make(), Skwasm::makeCurrent(), skwasm_getGlContextForCanvas(), and skwasm_reportInitialized().

◆ RenderPictures()

uint32_t Skwasm::Surface::RenderPictures ( flutter::DisplayList **  picture,
int  count 
)

Definition at line 160 of file surface.cc.

161 {
162 assert(emscripten_is_main_browser_thread());
163 uint32_t callback_id = ++current_callback_id_;
164 std::unique_ptr<sk_sp<flutter::DisplayList>[]> picture_pointers =
165 std::make_unique<sk_sp<flutter::DisplayList>[]>(count);
166 for (int i = 0; i < count; i++) {
167 picture_pointers[i] = sk_ref_sp(pictures[i]);
168 }
169
170 // Releasing picture_pointers here and will recreate the unique_ptr on the
171 // other thread See surface_renderPicturesOnWorker
172 skwasm_dispatchRenderPictures(thread_, this, picture_pointers.release(),
173 count, callback_id);
174 return callback_id;
175}
void skwasm_dispatchRenderPictures(unsigned long thread_id, Skwasm::Surface *surface, sk_sp< flutter::DisplayList > *pictures, int count, uint32_t callback_id)

References i, and skwasm_dispatchRenderPictures().

◆ RenderPicturesOnWorker()

void Skwasm::Surface::RenderPicturesOnWorker ( sk_sp< flutter::DisplayList > *  picture,
int  picture_count,
uint32_t  callback_id,
double  raster_start 
)

Definition at line 183 of file surface.cc.

187 {
188 Skwasm::makeCurrent(gl_context_);
189 // This is initialized on the first call to `skwasm_captureImageBitmap` and
190 // then populated with more bitmaps on subsequent calls.
191 SkwasmObject image_bitmap_array = __builtin_wasm_ref_null_extern();
192 for (int i = 0; i < picture_count; i++) {
193 sk_sp<flutter::DisplayList> picture = pictures[i];
194 render_context_->RenderPicture(picture);
195 image_bitmap_array =
196 skwasm_captureImageBitmap(gl_context_, image_bitmap_array);
197 }
198 skwasm_resolveAndPostImages(this, image_bitmap_array, raster_start,
199 callback_id);
200}
void skwasm_resolveAndPostImages(Skwasm::Surface *surface, SkwasmObject image_bitmaps, double raster_start, uint32_t callback_id)
SkwasmObject skwasm_captureImageBitmap(uint32_t context_handle, SkwasmObject image_bitmaps)
__externref_t SkwasmObject

References i, Skwasm::makeCurrent(), skwasm_captureImageBitmap(), and skwasm_resolveAndPostImages().

◆ ReportContextLost()

void Skwasm::Surface::ReportContextLost ( uint32_t  callback_id)

Definition at line 268 of file surface.cc.

268 {
269 assert(emscripten_is_main_browser_thread());
270 callback_handler_(callback_id, nullptr, __builtin_wasm_ref_null_extern());
271}

◆ ResizeOnWorker()

void Skwasm::Surface::ResizeOnWorker ( int  width,
int  height,
uint32_t  callback_id 
)

Definition at line 143 of file surface.cc.

145 {
146 ResizeSurface(width, height);
147 skwasm_reportResizeComplete(this, callback_id);
148}
int32_t height
int32_t width
void skwasm_reportResizeComplete(Skwasm::Surface *surface, uint32_t callback_id)

References height, skwasm_reportResizeComplete(), and width.

◆ SetCallbackHandler()

void Skwasm::Surface::SetCallbackHandler ( CallbackHandler callback_handler)

Definition at line 65 of file surface.cc.

65 {
66 assert(emscripten_is_main_browser_thread());
67 callback_handler_ = callback_handler;
68}
CallbackHandler callback_handler

References callback_handler.

◆ SetCanvas()

uint32_t Skwasm::Surface::SetCanvas ( SkwasmObject  canvas)

Definition at line 78 of file surface.cc.

78 {
79 assert(emscripten_is_main_browser_thread());
80 uint32_t callback_id = ++current_callback_id_;
81 skwasm_dispatchTransferCanvas(thread_, this, canvas, callback_id);
82 return callback_id;
83}
void skwasm_dispatchTransferCanvas(unsigned long thread_id, Skwasm::Surface *surface, SkwasmObject canvas, uint32_t callback_id)

References skwasm_dispatchTransferCanvas().

◆ SetResourceCacheLimit()

void Skwasm::Surface::SetResourceCacheLimit ( int  bytes)

Definition at line 289 of file surface.cc.

289 {
290 render_context_->SetResourceCacheLimit(bytes);
291}

◆ SetSize()

uint32_t Skwasm::Surface::SetSize ( int  width,
int  height 
)

Definition at line 130 of file surface.cc.

130 {
131 assert(emscripten_is_main_browser_thread());
132 uint32_t callback_id = ++current_callback_id_;
133
134 skwasm_dispatchResizeSurface(thread_, this, width, height, callback_id);
135 return callback_id;
136}
void skwasm_dispatchResizeSurface(unsigned long thread_id, Skwasm::Surface *surface, int width, int height, uint32_t callback_id)

References height, skwasm_dispatchResizeSurface(), and width.

◆ TriggerContextLoss()

uint32_t Skwasm::Surface::TriggerContextLoss ( )

Definition at line 256 of file surface.cc.

256 {
257 assert(emscripten_is_main_browser_thread());
258 uint32_t callback_id = ++current_callback_id_;
259 skwasm_dispatchTriggerContextLoss(thread_, this, callback_id);
260 return callback_id;
261}
void skwasm_dispatchTriggerContextLoss(unsigned long thread_id, Skwasm::Surface *surface, uint32_t callback_id)

References skwasm_dispatchTriggerContextLoss().

◆ TriggerContextLossOnWorker()

void Skwasm::Surface::TriggerContextLossOnWorker ( uint32_t  callback_id)

Definition at line 273 of file surface.cc.

273 {
274 Skwasm::makeCurrent(gl_context_);
276 skwasm_reportContextLossTriggered(this, callback_id);
277}
void skwasm_reportContextLossTriggered(Skwasm::Surface *surface, uint32_t callback_id)
void skwasm_triggerContextLossOnCanvas()

References Skwasm::makeCurrent(), skwasm_reportContextLossTriggered(), and skwasm_triggerContextLossOnCanvas().


The documentation for this class was generated from the following files: