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