Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
images_skia.cc
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
6
7#include <GLES2/gl2.h>
8#include <GLES2/gl2ext.h>
9#include <emscripten/html5_webgl.h>
10
14#include "third_party/skia/include/core/SkColorSpace.h"
15#include "third_party/skia/include/core/SkData.h"
16#include "third_party/skia/include/core/SkImage.h"
17#include "third_party/skia/include/core/SkImageInfo.h"
18#include "third_party/skia/include/core/SkPicture.h"
19#include "third_party/skia/include/core/SkPictureRecorder.h"
20#include "third_party/skia/include/gpu/GpuTypes.h"
21#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
22#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
23#include "third_party/skia/include/gpu/ganesh/GrExternalTextureGenerator.h"
24#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
25#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
26#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
27#include "third_party/skia/include/gpu/ganesh/gl/GrGLInterface.h"
28#include "third_party/skia/include/gpu/ganesh/gl/GrGLTypes.h"
29
30namespace {
31
32SkColorType ColorTypeForPixelFormat(Skwasm::PixelFormat format) {
33 switch (format) {
35 return SkColorType::kRGBA_8888_SkColorType;
37 return SkColorType::kBGRA_8888_SkColorType;
39 return SkColorType::kRGBA_F32_SkColorType;
40 }
41}
42
43SkAlphaType AlphaTypeForPixelFormat(Skwasm::PixelFormat format) {
44 switch (format) {
47 return SkAlphaType::kPremul_SkAlphaType;
49 return SkAlphaType::kUnpremul_SkAlphaType;
50 }
51}
52
53class ExternalWebGLTexture : public GrExternalTexture {
54 public:
55 ExternalWebGLTexture(GrBackendTexture backend_texture,
56 GLuint texture_id,
57 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context)
58 : backend_texture_(backend_texture),
59 texture_id_(texture_id),
60 web_gl_context_(context) {}
61
62 GrBackendTexture getBackendTexture() override { return backend_texture_; }
63
64 void dispose() override {
65 Skwasm::makeCurrent(web_gl_context_);
66 glDeleteTextures(1, &texture_id_);
67 }
68
69 private:
70 GrBackendTexture backend_texture_;
71 GLuint texture_id_;
72 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE web_gl_context_;
73};
74
75class TextureSourceImageGenerator : public GrExternalTextureGenerator {
76 public:
77 TextureSourceImageGenerator(SkImageInfo ii,
78 Skwasm::SkwasmObject texture_source,
79 Skwasm::Surface* surface)
80 : GrExternalTextureGenerator(ii),
81 texture_source_wrapper_(
82 surface->CreateTextureSourceWrapper(texture_source)) {}
83
84 std::unique_ptr<GrExternalTexture> generateExternalTexture(
85 GrRecordingContext* context,
86 skgpu::Mipmapped mipmapped) override {
87 GrGLTextureInfo gl_info;
89 texture_source_wrapper_->GetTextureSource(), fInfo.width(),
90 fInfo.height());
91 gl_info.fFormat = GL_RGBA8_OES;
92 gl_info.fTarget = GL_TEXTURE_2D;
93
94 auto backend_texture = GrBackendTextures::MakeGL(
95 fInfo.width(), fInfo.height(), skgpu::Mipmapped::kNo, gl_info);
96
97 // In order to bind the image source to the texture, makeTexture has changed
98 // which texture is "in focus" for the WebGL context.
99 GrAsDirectContext(context)->resetContext(kTextureBinding_GrGLBackendState);
100 return std::make_unique<ExternalWebGLTexture>(
101 backend_texture, gl_info.fID, emscripten_webgl_get_current_context());
102 }
103
104 private:
105 std::unique_ptr<Skwasm::TextureSourceWrapper> texture_source_wrapper_;
106};
107
108} // namespace
109
110namespace Skwasm {
111
112sk_sp<flutter::DlImage> MakeImageFromPicture(flutter::DisplayList* display_list,
113 int32_t width,
114 int32_t height) {
115 SkPictureRecorder recorder;
116 SkCanvas* canvas =
117 recorder.beginRecording(flutter::ToSkRect(display_list->GetBounds()));
118 flutter::DlSkCanvasDispatcher dispatcher(canvas);
119 dispatcher.drawDisplayList(sk_ref_sp(display_list), 1.0f);
120
121 return flutter::DlImage::Make(SkImages::DeferredFromPicture(
122 recorder.finishRecordingAsPicture(), {width, height}, nullptr, nullptr,
123 SkImages::BitDepth::kU8, SkColorSpace::MakeSRGB()));
124}
125
126sk_sp<flutter::DlImage> MakeImageFromTexture(SkwasmObject texture_source,
127 int width,
128 int height,
129 Skwasm::Surface* surface) {
130 return flutter::DlImage::Make(SkImages::DeferredFromTextureGenerator(
131 std::unique_ptr<TextureSourceImageGenerator>(
132 new TextureSourceImageGenerator(
133 SkImageInfo::Make(width, height,
134 SkColorType::kRGBA_8888_SkColorType,
135 SkAlphaType::kPremul_SkAlphaType),
136 texture_source, surface))));
137}
138
139sk_sp<flutter::DlImage> MakeImageFromPixels(SkData* data,
140 int width,
141 int height,
142 Skwasm::PixelFormat pixel_format,
143 size_t row_byte_count) {
144 return flutter::DlImage::Make(SkImages::RasterFromData(
145 SkImageInfo::Make(width, height, ColorTypeForPixelFormat(pixel_format),
146 AlphaTypeForPixelFormat(pixel_format),
147 SkColorSpace::MakeSRGB()),
148 sk_ref_sp(data), row_byte_count));
149}
150} // namespace Skwasm
const DlRect & GetBounds() const
static sk_sp< DlImage > Make(const SkImage *image)
Definition dl_image.cc:11
Backend implementation of |DlOpReceiver| for |SkCanvas|.
VkSurfaceKHR surface
Definition main.cc:65
sk_sp< flutter::DlImage > MakeImageFromTexture(SkwasmObject texture_source, int width, int height, Skwasm::Surface *surface)
__externref_t SkwasmObject
Definition wrappers.h:18
void makeCurrent(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE handle)
Definition wrappers.h:26
sk_sp< flutter::DlImage > MakeImageFromPicture(flutter::DisplayList *display_list, int32_t width, int32_t height)
PixelFormat
Definition images.h:13
sk_sp< flutter::DlImage > MakeImageFromPixels(SkData *data, int width, int height, PixelFormat pixel_format, size_t row_byte_count)
const SkRect & ToSkRect(const DlRect &rect)
int32_t height
int32_t width
__externref_t SkwasmObject
unsigned int skwasm_createGlTextureFromTextureSource(SkwasmObject texture_source, int width, int height)
std::shared_ptr< const fml::Mapping > data
int64_t texture_id