Skip to content

Commit cb62dfe

Browse files
xiaowei-guanJSUYA
authored andcommitted
[Tizen] Support EmbedderExternalTextureGL for impeller (flutter-tizen#368)
1 parent f4d4895 commit cb62dfe

7 files changed

+256
-6
lines changed

shell/platform/embedder/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ template("embedder_source_set") {
124124

125125
if (impeller_supports_rendering) {
126126
sources += [
127+
"embedder_external_texture_gl_impeller.cc",
128+
"embedder_external_texture_gl_impeller.h",
127129
"embedder_surface_gl_impeller.cc",
128130
"embedder_surface_gl_impeller.h",
129131
]

shell/platform/embedder/embedder.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -2067,8 +2067,8 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
20672067
}
20682068
return texture;
20692069
};
2070-
external_texture_resolver =
2071-
std::make_unique<ExternalTextureResolver>(external_texture_callback);
2070+
external_texture_resolver = std::make_unique<ExternalTextureResolver>(
2071+
external_texture_callback, settings.enable_impeller);
20722072
}
20732073
}
20742074
#endif

shell/platform/embedder/embedder.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ typedef struct {
296296
} FlutterTransformation;
297297

298298
typedef void (*VoidCallback)(void* /* user data */);
299+
typedef bool (*BoolCallback)(void* /* user data */);
299300

300301
typedef enum {
301302
/// Specifies an OpenGL texture target type. Textures are specified using
@@ -363,6 +364,11 @@ typedef enum {
363364
kFlutterSoftwarePixelFormatNative32,
364365
} FlutterSoftwarePixelFormat;
365366

367+
typedef enum {
368+
kFlutterGLImpellerTexturePixelBuffer,
369+
kFlutterGLImpellerTextureGpuSurface,
370+
} FlutterGLImpellerTextureType;
371+
366372
typedef struct {
367373
/// Target texture of the active texture unit (example GL_TEXTURE_2D or
368374
/// GL_TEXTURE_RECTANGLE).
@@ -371,6 +377,14 @@ typedef struct {
371377
uint32_t name;
372378
/// The texture format (example GL_RGBA8).
373379
uint32_t format;
380+
/// The pixel data buffer.
381+
const uint8_t* buffer;
382+
/// The size of pixel buffer.
383+
size_t buffer_size;
384+
/// Callback invoked that the gpu surface texture start binding.
385+
BoolCallback bind_callback;
386+
/// The type of the texture.
387+
FlutterGLImpellerTextureType impeller_texture_type;
374388
/// User data to be returned on the invocation of the destruction callback.
375389
void* user_data;
376390
/// Callback invoked (on an engine managed thread) that asks the embedder to
@@ -403,7 +417,6 @@ typedef struct {
403417
VoidCallback destruction_callback;
404418
} FlutterOpenGLFramebuffer;
405419

406-
typedef bool (*BoolCallback)(void* /* user data */);
407420
typedef FlutterTransformation (*TransformationCallback)(void* /* user data */);
408421
typedef uint32_t (*UIntCallback)(void* /* user data */);
409422
typedef bool (*SoftwareSurfacePresentCallback)(void* /* user data */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 "flutter/shell/platform/embedder/embedder_external_texture_gl_impeller.h"
6+
7+
#include "flutter/fml/logging.h"
8+
#include "flutter/impeller/display_list/dl_image_impeller.h"
9+
#include "flutter/impeller/renderer/backend/gles/context_gles.h"
10+
#include "flutter/impeller/renderer/backend/gles/texture_gles.h"
11+
#include "impeller/aiks/aiks_context.h"
12+
#include "impeller/renderer/backend/gles/gles.h"
13+
#include "include/core/SkCanvas.h"
14+
#include "include/core/SkPaint.h"
15+
16+
namespace flutter {
17+
18+
EmbedderExternalTextureGLImpeller::EmbedderExternalTextureGLImpeller(
19+
int64_t texture_identifier,
20+
const EmbedderExternalTextureGL::ExternalTextureCallback& callback)
21+
: Texture(texture_identifier), external_texture_callback_(callback) {
22+
FML_DCHECK(external_texture_callback_);
23+
}
24+
25+
EmbedderExternalTextureGLImpeller::~EmbedderExternalTextureGLImpeller() =
26+
default;
27+
28+
// |flutter::Texture|
29+
void EmbedderExternalTextureGLImpeller::Paint(PaintContext& context,
30+
const SkRect& bounds,
31+
bool freeze,
32+
const DlImageSampling sampling) {
33+
if (last_image_ == nullptr) {
34+
last_image_ =
35+
ResolveTexture(Id(), //
36+
context, //
37+
SkISize::Make(bounds.width(), bounds.height()) //
38+
);
39+
}
40+
41+
DlCanvas* canvas = context.canvas;
42+
const DlPaint* paint = context.paint;
43+
44+
if (last_image_) {
45+
SkRect image_bounds = SkRect::Make(last_image_->bounds());
46+
if (bounds != image_bounds) {
47+
canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint);
48+
} else {
49+
canvas->DrawImage(last_image_, {bounds.x(), bounds.y()}, sampling, paint);
50+
}
51+
}
52+
}
53+
54+
// |flutter::Texture|
55+
void EmbedderExternalTextureGLImpeller::OnGrContextCreated() {}
56+
57+
// |flutter::Texture|
58+
void EmbedderExternalTextureGLImpeller::OnGrContextDestroyed() {}
59+
60+
// |flutter::Texture|
61+
void EmbedderExternalTextureGLImpeller::MarkNewFrameAvailable() {
62+
last_image_ = nullptr;
63+
}
64+
65+
// |flutter::Texture|
66+
void EmbedderExternalTextureGLImpeller::OnTextureUnregistered() {}
67+
68+
sk_sp<DlImage> EmbedderExternalTextureGLImpeller::ResolveTexture(
69+
int64_t texture_id,
70+
PaintContext& context,
71+
const SkISize& size) {
72+
std::unique_ptr<FlutterOpenGLTexture> texture =
73+
external_texture_callback_(texture_id, size.width(), size.height());
74+
if (!texture) {
75+
return nullptr;
76+
}
77+
size_t width = size.width();
78+
size_t height = size.height();
79+
if (texture->width != 0 && texture->height != 0) {
80+
width = texture->width;
81+
height = texture->height;
82+
}
83+
if (texture->impeller_texture_type ==
84+
FlutterGLImpellerTextureType::kFlutterGLImpellerTexturePixelBuffer) {
85+
return ResolvePixelBufferTexture(texture.get(), context,
86+
SkISize::Make(width, height));
87+
} else {
88+
return ResolveGpuSurfaceTexture(texture.get(), context,
89+
SkISize::Make(width, height));
90+
}
91+
}
92+
93+
sk_sp<DlImage> EmbedderExternalTextureGLImpeller::ResolvePixelBufferTexture(
94+
FlutterOpenGLTexture* texture,
95+
PaintContext& context,
96+
const SkISize& size) {
97+
impeller::TextureDescriptor desc;
98+
desc.type = impeller::TextureType::kTexture2D;
99+
impeller::AiksContext* aiks_context = context.aiks_context;
100+
const auto& gl_context =
101+
impeller::ContextGLES::Cast(*aiks_context->GetContext());
102+
desc.storage_mode = impeller::StorageMode::kDevicePrivate;
103+
desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt;
104+
desc.size = {static_cast<int>(size.width()), static_cast<int>(size.height())};
105+
desc.mip_count = 1;
106+
107+
auto textureGLES =
108+
std::make_shared<impeller::TextureGLES>(gl_context.GetReactor(), desc);
109+
if (!textureGLES->SetContents(texture->buffer, texture->buffer_size)) {
110+
if (texture->destruction_callback) {
111+
texture->destruction_callback(texture->user_data);
112+
}
113+
return nullptr;
114+
}
115+
if (texture->destruction_callback) {
116+
texture->destruction_callback(texture->user_data);
117+
}
118+
return impeller::DlImageImpeller::Make(textureGLES);
119+
}
120+
121+
sk_sp<DlImage> EmbedderExternalTextureGLImpeller::ResolveGpuSurfaceTexture(
122+
FlutterOpenGLTexture* texture,
123+
PaintContext& context,
124+
const SkISize& size) {
125+
impeller::TextureDescriptor desc;
126+
desc.type = impeller::TextureType::kTextureExternalOES;
127+
impeller::AiksContext* aiks_context = context.aiks_context;
128+
const auto& gl_context =
129+
impeller::ContextGLES::Cast(*aiks_context->GetContext());
130+
desc.storage_mode = impeller::StorageMode::kDevicePrivate;
131+
desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt;
132+
desc.size = {static_cast<int>(size.width()), static_cast<int>(size.height())};
133+
desc.mip_count = 1;
134+
auto textureGLES = std::make_shared<impeller::TextureGLES>(
135+
gl_context.GetReactor(), desc,
136+
impeller::TextureGLES::IsWrapped::kWrapped);
137+
textureGLES->SetCoordinateSystem(
138+
impeller::TextureCoordinateSystem::kUploadFromHost);
139+
if (!textureGLES->Bind()) {
140+
if (texture->destruction_callback) {
141+
texture->destruction_callback(texture->user_data);
142+
}
143+
return nullptr;
144+
}
145+
146+
if (!texture->bind_callback(texture->user_data)) {
147+
if (texture->destruction_callback) {
148+
texture->destruction_callback(texture->user_data);
149+
}
150+
return nullptr;
151+
}
152+
153+
if (texture->destruction_callback) {
154+
texture->destruction_callback(texture->user_data);
155+
}
156+
157+
return impeller::DlImageImpeller::Make(textureGLES);
158+
}
159+
160+
} // namespace flutter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_IMPELLER_H_
6+
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_IMPELLER_H_
7+
8+
#include "flutter/common/graphics/texture.h"
9+
#include "flutter/fml/macros.h"
10+
#include "flutter/shell/platform/embedder/embedder.h"
11+
#include "flutter/shell/platform/embedder/embedder_external_texture_gl.h"
12+
#include "third_party/skia/include/core/SkSize.h"
13+
14+
namespace flutter {
15+
16+
class EmbedderExternalTextureGLImpeller : public flutter::Texture {
17+
public:
18+
EmbedderExternalTextureGLImpeller(
19+
int64_t texture_identifier,
20+
const EmbedderExternalTextureGL::ExternalTextureCallback& callback);
21+
22+
~EmbedderExternalTextureGLImpeller();
23+
24+
private:
25+
const EmbedderExternalTextureGL::ExternalTextureCallback&
26+
external_texture_callback_;
27+
sk_sp<DlImage> last_image_;
28+
29+
sk_sp<DlImage> ResolveTexture(int64_t texture_id,
30+
PaintContext& context,
31+
const SkISize& size);
32+
sk_sp<DlImage> ResolveGpuSurfaceTexture(FlutterOpenGLTexture* texture,
33+
PaintContext& context,
34+
const SkISize& size);
35+
sk_sp<DlImage> ResolvePixelBufferTexture(FlutterOpenGLTexture* texture,
36+
PaintContext& context,
37+
const SkISize& size);
38+
39+
// |flutter::Texture|
40+
void Paint(PaintContext& context,
41+
const SkRect& bounds,
42+
bool freeze,
43+
const DlImageSampling sampling) override;
44+
45+
// |flutter::Texture|
46+
void OnGrContextCreated() override;
47+
48+
// |flutter::Texture|
49+
void OnGrContextDestroyed() override;
50+
51+
// |flutter::Texture|
52+
void MarkNewFrameAvailable() override;
53+
54+
// |flutter::Texture|
55+
void OnTextureUnregistered() override;
56+
57+
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureGLImpeller);
58+
};
59+
60+
} // namespace flutter
61+
62+
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_IMPELLER_H_

shell/platform/embedder/embedder_external_texture_resolver.cc

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
#include "flutter/shell/platform/embedder/embedder_external_texture_resolver.h"
66

7+
#if defined(SHELL_ENABLE_GL) && defined(IMPELLER_SUPPORTS_RENDERING)
8+
#include "flutter/shell/platform/embedder/embedder_external_texture_gl_impeller.h"
9+
#endif
10+
711
#include <memory>
812
#include <utility>
913

1014
namespace flutter {
1115

1216
#ifdef SHELL_ENABLE_GL
1317
EmbedderExternalTextureResolver::EmbedderExternalTextureResolver(
14-
EmbedderExternalTextureGL::ExternalTextureCallback gl_callback)
15-
: gl_callback_(std::move(gl_callback)) {}
18+
EmbedderExternalTextureGL::ExternalTextureCallback gl_callback,
19+
bool enable_impeller)
20+
: gl_callback_(std::move(gl_callback)), enable_impeller_(enable_impeller) {}
1621
#endif
1722

1823
#ifdef SHELL_ENABLE_METAL
@@ -25,6 +30,12 @@ std::unique_ptr<Texture>
2530
EmbedderExternalTextureResolver::ResolveExternalTexture(int64_t texture_id) {
2631
#ifdef SHELL_ENABLE_GL
2732
if (gl_callback_) {
33+
#ifdef IMPELLER_SUPPORTS_RENDERING
34+
if (enable_impeller_) {
35+
return std::make_unique<EmbedderExternalTextureGLImpeller>(texture_id,
36+
gl_callback_);
37+
}
38+
#endif
2839
return std::make_unique<EmbedderExternalTextureGL>(texture_id,
2940
gl_callback_);
3041
}

shell/platform/embedder/embedder_external_texture_resolver.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class EmbedderExternalTextureResolver {
2626

2727
#ifdef SHELL_ENABLE_GL
2828
explicit EmbedderExternalTextureResolver(
29-
EmbedderExternalTextureGL::ExternalTextureCallback gl_callback);
29+
EmbedderExternalTextureGL::ExternalTextureCallback gl_callback,
30+
bool enable_impeller);
3031
#endif
3132

3233
#ifdef SHELL_ENABLE_METAL
@@ -46,6 +47,7 @@ class EmbedderExternalTextureResolver {
4647
#ifdef SHELL_ENABLE_METAL
4748
EmbedderExternalTextureMetal::ExternalTextureCallback metal_callback_;
4849
#endif
50+
bool enable_impeller_ = false;
4951

5052
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureResolver);
5153
};

0 commit comments

Comments
 (0)