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 #include "flutter/shell/platform/embedder/tests/embedder_test_compositor_gl.h"
7 
8 #include "flutter/fml/make_copyable.h"
9 #include "flutter/fml/paths.h"
10 #include "flutter/runtime/dart_vm.h"
11 #include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
12 #include "flutter/testing/testing.h"
13 #include "third_party/dart/runtime/bin/elf_loader.h"
14 #include "third_party/skia/include/core/SkSurface.h"
15 
16 namespace flutter {
17 namespace testing {
18 
20  : EmbedderTestContext(assets_path) {}
21 
23  SetGLGetFBOCallback(nullptr);
24 }
25 
26 void EmbedderTestContextGL::SetupSurface(SkISize surface_size) {
27  FML_CHECK(!gl_surface_);
28  gl_surface_ = std::make_unique<TestGLSurface>(surface_size);
29 }
30 
31 bool EmbedderTestContextGL::GLMakeCurrent() {
32  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
33  return gl_surface_->MakeCurrent();
34 }
35 
36 bool EmbedderTestContextGL::GLClearCurrent() {
37  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
38  return gl_surface_->ClearCurrent();
39 }
40 
41 bool EmbedderTestContextGL::GLPresent(uint32_t fbo_id) {
42  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
43  gl_surface_present_count_++;
44 
45  GLPresentCallback callback;
46  {
47  std::scoped_lock lock(gl_callback_mutex_);
48  callback = gl_present_callback_;
49  }
50 
51  if (callback) {
52  callback(fbo_id);
53  }
54 
56  [&]() { return gl_surface_->GetRasterSurfaceSnapshot(); });
57 
58  if (!gl_surface_->Present()) {
59  return false;
60  }
61 
62  return true;
63 }
64 
66  std::scoped_lock lock(gl_callback_mutex_);
67  gl_get_fbo_callback_ = callback;
68 }
69 
71  std::scoped_lock lock(gl_callback_mutex_);
72  gl_present_callback_ = callback;
73 }
74 
75 uint32_t EmbedderTestContextGL::GLGetFramebuffer(FlutterFrameInfo frame_info) {
76  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
77 
78  GLGetFBOCallback callback;
79  {
80  std::scoped_lock lock(gl_callback_mutex_);
81  callback = gl_get_fbo_callback_;
82  }
83 
84  if (callback) {
85  callback(frame_info);
86  }
87 
88  const auto size = frame_info.size;
89  return gl_surface_->GetFramebuffer(size.width, size.height);
90 }
91 
92 bool EmbedderTestContextGL::GLMakeResourceCurrent() {
93  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
94  return gl_surface_->MakeResourceCurrent();
95 }
96 
97 void* EmbedderTestContextGL::GLGetProcAddress(const char* name) {
98  FML_CHECK(gl_surface_) << "GL surface must be initialized.";
99  return gl_surface_->GetProcAddress(name);
100 }
101 
103  return gl_surface_present_count_;
104 }
105 
107  FML_CHECK(gl_surface_);
108  return gl_surface_->GetWindowFBOId();
109 }
110 
112  FML_CHECK(!compositor_) << "Already ssetup a compositor in this context.";
113  FML_CHECK(gl_surface_)
114  << "Setup the GL surface before setting up a compositor.";
115  compositor_ = std::make_unique<EmbedderTestCompositorGL>(
116  gl_surface_->GetSurfaceSize(), gl_surface_->GetGrContext());
117 }
118 
119 } // namespace testing
120 } // 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...
FlutterUIntSize size
The size of the surface that will be backed by the fbo.
Definition: embedder.h:354
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