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_surface_gl.h"
6
7 #include "flutter/shell/common/shell_io_manager.h"
8
9 namespace flutter {
10
EmbedderSurfaceGL(GLDispatchTable gl_dispatch_table,bool fbo_reset_after_present,std::unique_ptr<EmbedderExternalViewEmbedder> external_view_embedder)11 EmbedderSurfaceGL::EmbedderSurfaceGL(
12 GLDispatchTable gl_dispatch_table,
13 bool fbo_reset_after_present,
14 std::unique_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
15 : gl_dispatch_table_(gl_dispatch_table),
16 fbo_reset_after_present_(fbo_reset_after_present),
17 external_view_embedder_(std::move(external_view_embedder)) {
18 // Make sure all required members of the dispatch table are checked.
19 if (!gl_dispatch_table_.gl_make_current_callback ||
20 !gl_dispatch_table_.gl_clear_current_callback ||
21 !gl_dispatch_table_.gl_present_callback ||
22 !gl_dispatch_table_.gl_fbo_callback) {
23 return;
24 }
25
26 valid_ = true;
27 }
28
29 EmbedderSurfaceGL::~EmbedderSurfaceGL() = default;
30
31 // |EmbedderSurface|
IsValid() const32 bool EmbedderSurfaceGL::IsValid() const {
33 return valid_;
34 }
35
36 // |GPUSurfaceGLDelegate|
GLContextMakeCurrent()37 bool EmbedderSurfaceGL::GLContextMakeCurrent() {
38 return gl_dispatch_table_.gl_make_current_callback();
39 }
40
41 // |GPUSurfaceGLDelegate|
GLContextClearCurrent()42 bool EmbedderSurfaceGL::GLContextClearCurrent() {
43 return gl_dispatch_table_.gl_clear_current_callback();
44 }
45
46 // |GPUSurfaceGLDelegate|
GLContextPresent()47 bool EmbedderSurfaceGL::GLContextPresent() {
48 return gl_dispatch_table_.gl_present_callback();
49 }
50
51 // |GPUSurfaceGLDelegate|
GLContextSendSurface(const void * pixels,const size_t size,const int32_t width,const int32_t height)52 bool EmbedderSurfaceGL::GLContextSendSurface(const void* pixels,
53 const size_t size,
54 const int32_t width,
55 const int32_t height) {
56 return gl_dispatch_table_.gl_send_surface_callback(pixels, size, width,
57 height);
58 }
59
60 // |GPUSurfaceGLDelegate|
GLContextFBO() const61 intptr_t EmbedderSurfaceGL::GLContextFBO() const {
62 return gl_dispatch_table_.gl_fbo_callback();
63 }
64
65 // |GPUSurfaceGLDelegate|
GLContextFBOResetAfterPresent() const66 bool EmbedderSurfaceGL::GLContextFBOResetAfterPresent() const {
67 return fbo_reset_after_present_;
68 }
69
70 // |GPUSurfaceGLDelegate|
GLContextSurfaceTransformation() const71 SkMatrix EmbedderSurfaceGL::GLContextSurfaceTransformation() const {
72 auto callback = gl_dispatch_table_.gl_surface_transformation_callback;
73 if (!callback) {
74 SkMatrix matrix;
75 matrix.setIdentity();
76 return matrix;
77 }
78 return callback();
79 }
80
81 // |GPUSurfaceGLDelegate|
GetExternalViewEmbedder()82 ExternalViewEmbedder* EmbedderSurfaceGL::GetExternalViewEmbedder() {
83 return external_view_embedder_.get();
84 }
85
86 // |GPUSurfaceGLDelegate|
GetGLProcResolver() const87 EmbedderSurfaceGL::GLProcResolver EmbedderSurfaceGL::GetGLProcResolver() const {
88 return gl_dispatch_table_.gl_proc_resolver;
89 }
90
91 // |EmbedderSurface|
CreateGPUSurface()92 std::unique_ptr<Surface> EmbedderSurfaceGL::CreateGPUSurface() {
93 bool render_to_surface = !external_view_embedder_;
94 return std::make_unique<GPUSurfaceGL>(this, // GPU surface GL delegate
95 render_to_surface // render to surface
96
97 );
98 }
99
100 // |EmbedderSurface|
CreateResourceContext() const101 sk_sp<GrContext> EmbedderSurfaceGL::CreateResourceContext() const {
102 auto callback = gl_dispatch_table_.gl_make_resource_current_callback;
103 if (callback && callback()) {
104 if (auto context = ShellIOManager::CreateCompatibleResourceLoadingContext(
105 GrBackend::kOpenGL_GrBackend, GetGLInterface())) {
106 return context;
107 } else {
108 FML_LOG(ERROR)
109 << "Internal error: Resource context available but could not create "
110 "a compatible Skia context.";
111 return nullptr;
112 }
113 }
114
115 // The callback was not available or failed.
116 FML_LOG(ERROR)
117 << "Could not create a resource context for async texture uploads. "
118 "Expect degraded performance. Set a valid make_resource_current "
119 "callback on FlutterOpenGLRendererConfig.";
120 return nullptr;
121 }
122
123 } // namespace flutter
124