Flutter Engine
embedder_test_context_gl.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 
5 #include "flutter/shell/platform/embedder/tests/embedder_test_context_gl.h"
6 
7 #include "flutter/fml/make_copyable.h"
8 #include "flutter/fml/paths.h"
9 #include "flutter/runtime/dart_vm.h"
10 #include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
11 #include "flutter/shell/platform/embedder/tests/embedder_test_compositor_gl.h"
12 #include "flutter/testing/testing.h"
13 #include "tests/embedder_test.h"
14 #include "third_party/dart/runtime/bin/elf_loader.h"
15 #include "third_party/skia/include/core/SkSurface.h"
16 
17 namespace flutter {
18 namespace testing {
19 
21  : EmbedderTestContext(assets_path) {}
22 
24  SetGLGetFBOCallback(nullptr);
25 }
26 
27 void EmbedderTestContextGL::SetupSurface(SkISize surface_size) {
28  FML_CHECK(!gl_surface_);
29  gl_surface_ = std::make_unique<TestGLSurface>(surface_size);
30 }
31 
32 bool EmbedderTestContextGL::GLMakeCurrent() {
33  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
34  return gl_surface_->MakeCurrent();
35 }
36 
37 bool EmbedderTestContextGL::GLClearCurrent() {
38  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
39  return gl_surface_->ClearCurrent();
40 }
41 
42 bool EmbedderTestContextGL::GLPresent(uint32_t fbo_id) {
43  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
44  gl_surface_present_count_++;
45 
47  {
48  std::scoped_lock lock(gl_callback_mutex_);
49  callback = gl_present_callback_;
50  }
51 
52  if (callback) {
53  callback(fbo_id);
54  }
55 
57  [&]() { return gl_surface_->GetRasterSurfaceSnapshot(); });
58 
59  return gl_surface_->Present();
60 }
61 
63  std::scoped_lock lock(gl_callback_mutex_);
64  gl_get_fbo_callback_ = callback;
65 }
66 
68  std::scoped_lock lock(gl_callback_mutex_);
69  gl_present_callback_ = callback;
70 }
71 
72 uint32_t EmbedderTestContextGL::GLGetFramebuffer(FlutterFrameInfo frame_info) {
73  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
74 
76  {
77  std::scoped_lock lock(gl_callback_mutex_);
78  callback = gl_get_fbo_callback_;
79  }
80 
81  if (callback) {
82  callback(frame_info);
83  }
84 
85  const auto size = frame_info.size;
86  return gl_surface_->GetFramebuffer(size.width, size.height);
87 }
88 
89 bool EmbedderTestContextGL::GLMakeResourceCurrent() {
90  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
91  return gl_surface_->MakeResourceCurrent();
92 }
93 
94 void* EmbedderTestContextGL::GLGetProcAddress(const char* name) {
95  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
96  return gl_surface_->GetProcAddress(name);
97 }
98 
100  return gl_surface_present_count_;
101 }
102 
105 }
106 
108  FML_CHECK(gl_surface_);
109  return gl_surface_->GetWindowFBOId();
110 }
111 
113  FML_CHECK(!compositor_) << "Already set up a compositor in this context.";
114  FML_CHECK(gl_surface_)
115  << "Set up the GL surface before setting up a compositor.";
116  compositor_ = std::make_unique<EmbedderTestCompositorGL>(
117  gl_surface_->GetSurfaceSize(), gl_surface_->GetGrContext());
118  GLClearCurrent();
119 }
120 
121 } // namespace testing
122 } // namespace flutter
std::function< void(FlutterFrameInfo frame_info)> GLGetFBOCallback
void FireRootSurfacePresentCallbackIfPresent(const std::function< sk_sp< SkImage >(void)> &image_callback)
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
void SetGLGetFBOCallback(GLGetFBOCallback callback)
Sets a callback that will be invoked (on the raster task runner) when the engine asks the embedder fo...
void SetGLPresentCallback(GLPresentCallback callback)
Sets a callback that will be invoked (on the raster task runner) when the engine presents an fbo that...
FlKeyEvent FlKeyResponderAsyncCallback callback
FlutterUIntSize size
The size of the surface that will be backed by the fbo.
Definition: embedder.h:363
EmbedderTestContextType GetContextType() const override
std::unique_ptr< EmbedderTestCompositor > compositor_
const char * name
Definition: fuchsia.cc:50
#define FML_CHECK(condition)
Definition: logging.h:68
std::function< void(uint32_t fbo_id)> GLPresentCallback