Flutter Engine
The Flutter Engine
image.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 "export.h"
6#include "skwasm_support.h"
7#include "surface.h"
8#include "wrappers.h"
9
24
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27#include <emscripten/html5_webgl.h>
28
29using namespace SkImages;
30
31namespace {
32
33enum class PixelFormat {
34 rgba8888,
35 bgra8888,
36 rgbaFloat32,
37};
38
39SkColorType colorTypeForPixelFormat(PixelFormat format) {
40 switch (format) {
41 case PixelFormat::rgba8888:
43 case PixelFormat::bgra8888:
45 case PixelFormat::rgbaFloat32:
47 }
48}
49
50SkAlphaType alphaTypeForPixelFormat(PixelFormat format) {
51 switch (format) {
52 case PixelFormat::rgba8888:
53 case PixelFormat::bgra8888:
55 case PixelFormat::rgbaFloat32:
57 }
58}
59
60class ExternalWebGLTexture : public GrExternalTexture {
61 public:
62 ExternalWebGLTexture(GrBackendTexture backendTexture,
63 GLuint textureId,
64 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context)
65 : _backendTexture(backendTexture),
66 _textureId(textureId),
67 _webGLContext(context) {}
68
69 GrBackendTexture getBackendTexture() override { return _backendTexture; }
70
71 void dispose() override {
72 Skwasm::makeCurrent(_webGLContext);
73 glDeleteTextures(1, &_textureId);
74 }
75
76 private:
77 GrBackendTexture _backendTexture;
78 GLuint _textureId;
79 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE _webGLContext;
80};
81} // namespace
82
84 public:
86 SkwasmObject textureSource,
89 _textureSourceWrapper(
90 surface->createTextureSourceWrapper(textureSource)) {}
91
92 std::unique_ptr<GrExternalTexture> generateExternalTexture(
93 GrRecordingContext* context,
94 skgpu::Mipmapped mipmapped) override {
95 GrGLTextureInfo glInfo;
97 _textureSourceWrapper->getTextureSource(), fInfo.width(),
98 fInfo.height());
99 glInfo.fFormat = GL_RGBA8_OES;
100 glInfo.fTarget = GL_TEXTURE_2D;
101
102 auto backendTexture = GrBackendTextures::MakeGL(
104
105 // In order to bind the image source to the texture, makeTexture has changed
106 // which texture is "in focus" for the WebGL context.
108 return std::make_unique<ExternalWebGLTexture>(
109 backendTexture, glInfo.fID, emscripten_webgl_get_current_context());
110 }
111
112 private:
113 std::unique_ptr<Skwasm::TextureSourceWrapper> _textureSourceWrapper;
114};
115
117 int32_t width,
118 int32_t height) {
119 return DeferredFromPicture(sk_ref_sp<SkPicture>(picture), {width, height},
120 nullptr, nullptr, BitDepth::kU8,
122 .release();
123}
124
126 int width,
127 int height,
128 PixelFormat pixelFormat,
129 size_t rowByteCount) {
132 colorTypeForPixelFormat(pixelFormat),
133 alphaTypeForPixelFormat(pixelFormat),
135 sk_ref_sp(data), rowByteCount)
136 .release();
137}
138
140 int width,
141 int height,
144 std::unique_ptr<TextureSourceImageGenerator>(
149 textureSource, surface)))
150 .release();
151}
152
154 image->ref();
155}
156
158 image->unref();
159}
160
162 return image->width();
163}
164
166 return image->height();
167}
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kTextureBinding_GrGLBackendState
Definition: GrTypes.h:159
kUnpremul_SkAlphaType
SkAlphaType
Definition: SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
void resetContext(uint32_t state=kAll_GrBackendState)
virtual void dispose()=0
virtual GrBackendTexture getBackendTexture()=0
static sk_sp< SkColorSpace > MakeSRGB()
Definition: SkData.h:25
const SkImageInfo fInfo
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
void ref() const
Definition: SkRefCnt.h:62
void unref() const
Definition: SkRefCnt.h:72
std::unique_ptr< GrExternalTexture > generateExternalTexture(GrRecordingContext *context, skgpu::Mipmapped mipmapped) override
Definition: image.cpp:92
TextureSourceImageGenerator(SkImageInfo ii, SkwasmObject textureSource, Skwasm::Surface *surface)
Definition: image.cpp:85
T * release()
Definition: SkRefCnt.h:324
VkSurfaceKHR surface
Definition: main.cc:49
uint32_t uint32_t * format
SKWASM_EXPORT void image_dispose(SkImage *image)
Definition: image.cpp:157
SKWASM_EXPORT SkImage * image_createFromPicture(SkPicture *picture, int32_t width, int32_t height)
Definition: image.cpp:116
SKWASM_EXPORT int image_getWidth(SkImage *image)
Definition: image.cpp:161
SKWASM_EXPORT SkImage * image_createFromPixels(SkData *data, int width, int height, PixelFormat pixelFormat, size_t rowByteCount)
Definition: image.cpp:125
SKWASM_EXPORT int image_getHeight(SkImage *image)
Definition: image.cpp:165
SKWASM_EXPORT SkImage * image_createFromTextureSource(SkwasmObject textureSource, int width, int height, Skwasm::Surface *surface)
Definition: image.cpp:139
SKWASM_EXPORT void image_ref(SkImage *image)
Definition: image.cpp:153
SK_API GrBackendTexture MakeGL(int width, int height, skgpu::Mipmapped, const GrGLTextureInfo &glInfo, std::string_view label={})
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
SK_API sk_sp< SkImage > DeferredFromTextureGenerator(std::unique_ptr< GrTextureGenerator > gen)
SK_API sk_sp< SkImage > RasterFromData(const SkImageInfo &info, sk_sp< SkData > pixels, size_t rowBytes)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
void makeCurrent(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE handle)
Definition: wrappers.h:22
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
Mipmapped
Definition: GpuTypes.h:53
int32_t height
int32_t width
__externref_t SkwasmObject
unsigned int skwasm_createGlTextureFromTextureSource(SkwasmObject textureSource, int width, int height)
GrGLuint fID
Definition: GrGLTypes.h:182
GrGLenum fFormat
Definition: GrGLTypes.h:183
GrGLenum fTarget
Definition: GrGLTypes.h:181
int width() const
Definition: SkImageInfo.h:365
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
int height() const
Definition: SkImageInfo.h:371
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define SKWASM_EXPORT
Definition: export.h:10