Flutter Engine
The Flutter Engine
surface.h
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#ifndef FLUTTER_LIB_WEB_UI_SKWASM_SURFACE_H_
6#define FLUTTER_LIB_WEB_UI_SKWASM_SURFACE_H_
7
8#include <GLES2/gl2.h>
9#include <GLES2/gl2ext.h>
10#include <emscripten.h>
11#include <emscripten/html5_webgl.h>
12#include <emscripten/threading.h>
13#include <webgl/webgl1.h>
14#include <cassert>
15#include "export.h"
16#include "skwasm_support.h"
26#include "wrappers.h"
27
28namespace Skwasm {
29// This must be kept in sync with the `ImageByteFormat` enum in dart:ui.
30enum class ImageByteFormat {
31 rawRgba,
34 png,
35};
36
38 public:
39 TextureSourceWrapper(unsigned long threadId, SkwasmObject textureSource)
40 : _rasterThreadId(threadId) {
41 skwasm_setAssociatedObjectOnThread(_rasterThreadId, this, textureSource);
42 }
43
45 skwasm_disposeAssociatedObjectOnThread(_rasterThreadId, this);
46 }
47
49
50 private:
51 unsigned long _rasterThreadId;
52};
53
54class Surface {
55 public:
56 using CallbackHandler = void(uint32_t, void*, SkwasmObject);
57
58 // Main thread only
59 Surface();
60
61 unsigned long getThreadId() { return _thread; }
62
63 // Main thread only
64 void dispose();
65 uint32_t renderPictures(SkPicture** picture, int count);
67 void setCallbackHandler(CallbackHandler* callbackHandler);
68 void onRenderComplete(uint32_t callbackId, SkwasmObject imageBitmap);
69
70 // Any thread
71 std::unique_ptr<TextureSourceWrapper> createTextureSourceWrapper(
72 SkwasmObject textureSource);
73
74 // Worker thread
76 int pictureCount,
77 uint32_t callbackId,
78 double rasterStart);
79
80 private:
81 void _runWorker();
82 void _init();
83 void _dispose();
84 void _resizeCanvasToFit(int width, int height);
85 void _recreateSurface();
86 void _rasterizeImage(SkImage* image,
88 uint32_t callbackId);
89 void _onRasterizeComplete(SkData* data, uint32_t callbackId);
90
91 std::string _canvasID;
92 CallbackHandler* _callbackHandler = nullptr;
93 uint32_t _currentCallbackId = 0;
94
95 int _canvasWidth = 0;
96 int _canvasHeight = 0;
97
98 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE _glContext = 0;
99 sk_sp<GrDirectContext> _grContext = nullptr;
100 sk_sp<SkSurface> _surface = nullptr;
101 GrGLFramebufferInfo _fbInfo;
102 GrGLint _sampleCount;
103 GrGLint _stencil;
104
105 pthread_t _thread;
106
107 static void fDispose(Surface* surface);
108 static void fOnRenderComplete(Surface* surface,
109 uint32_t callbackId,
110 SkwasmObject imageBitmap);
111 static void fRasterizeImage(Surface* surface,
112 SkImage* image,
114 uint32_t callbackId);
115 static void fOnRasterizeComplete(Surface* surface,
116 SkData* imageData,
117 uint32_t callbackId);
118};
119} // namespace Skwasm
120
121#endif // FLUTTER_LIB_WEB_UI_SKWASM_SURFACE_H_
int count
Definition: FontMgrTest.cpp:50
int GrGLint
Definition: GrGLTypes.h:108
Definition: SkData.h:25
unsigned long getThreadId()
Definition: surface.h:61
void dispose()
Definition: surface.cpp:38
std::unique_ptr< TextureSourceWrapper > createTextureSourceWrapper(SkwasmObject textureSource)
Definition: surface.cpp:74
void setCallbackHandler(CallbackHandler *callbackHandler)
Definition: surface.cpp:81
void(uint32_t, void *, SkwasmObject) CallbackHandler
Definition: surface.h:56
void onRenderComplete(uint32_t callbackId, SkwasmObject imageBitmap)
Definition: surface.cpp:210
uint32_t rasterizeImage(SkImage *image, ImageByteFormat format)
Definition: surface.cpp:63
void renderPicturesOnWorker(sk_sp< SkPicture > *picture, int pictureCount, uint32_t callbackId, double rasterStart)
Definition: surface.cpp:151
uint32_t renderPictures(SkPicture **picture, int count)
Definition: surface.cpp:46
TextureSourceWrapper(unsigned long threadId, SkwasmObject textureSource)
Definition: surface.h:39
SkwasmObject getTextureSource()
Definition: surface.h:48
VkSurfaceKHR surface
Definition: main.cc:49
uint32_t uint32_t * format
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
Definition: helpers.h:12
ImageByteFormat
Definition: surface.h:30
int32_t height
int32_t width
void skwasm_disposeAssociatedObjectOnThread(unsigned long threadId, void *pointer)
SkwasmObject skwasm_getAssociatedObject(void *pointer)
void skwasm_setAssociatedObjectOnThread(unsigned long threadId, void *pointer, SkwasmObject object)
__externref_t SkwasmObject
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63