Flutter Engine
flutter::GPUSurfaceGL Class Reference

#include <gpu_surface_gl.h>

Inheritance diagram for flutter::GPUSurfaceGL:
flutter::Surface

Public Member Functions

 GPUSurfaceGL (GPUSurfaceGLDelegate *delegate, bool render_to_surface)
 
 GPUSurfaceGL (sk_sp< GrDirectContext > gr_context, GPUSurfaceGLDelegate *delegate, bool render_to_surface)
 
 ~GPUSurfaceGL () override
 
bool IsValid () override
 
std::unique_ptr< SurfaceFrameAcquireFrame (const SkISize &size) override
 
SkMatrix GetRootTransformation () const override
 
GrDirectContext * GetContext () override
 
flutter::ExternalViewEmbedderGetExternalViewEmbedder () override
 
std::unique_ptr< GLContextResultMakeRenderContextCurrent () override
 
bool ClearRenderContext () override
 
- Public Member Functions inherited from flutter::Surface
 Surface ()
 
virtual ~Surface ()
 

Detailed Description

Definition at line 21 of file gpu_surface_gl.h.

Constructor & Destructor Documentation

◆ GPUSurfaceGL() [1/2]

flutter::GPUSurfaceGL::GPUSurfaceGL ( GPUSurfaceGLDelegate delegate,
bool  render_to_surface 
)

Definition at line 37 of file gpu_surface_gl.cc.

References flutter::PersistentCache::cache_sksl(), FML_LOG, flutter::PersistentCache::GetCacheForProcess(), flutter::GPUSurfaceGLDelegate::GetGLInterface(), flutter::GPUSurfaceGLDelegate::GLContextClearCurrent(), flutter::GPUSurfaceGLDelegate::GLContextMakeCurrent(), flutter::PersistentCache::LoadSkSLs(), and flutter::PersistentCache::MarkStrategySet().

39  : delegate_(delegate),
40  render_to_surface_(render_to_surface),
41  weak_factory_(this) {
42  auto context_switch = delegate_->GLContextMakeCurrent();
43  if (!context_switch->GetResult()) {
44  FML_LOG(ERROR)
45  << "Could not make the context current to setup the gr context.";
46  return;
47  }
48 
49  GrContextOptions options;
50 
52  FML_LOG(INFO) << "Cache SkSL";
53  options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL;
54  }
56  options.fPersistentCache = PersistentCache::GetCacheForProcess();
57 
58  options.fAvoidStencilBuffers = true;
59 
60  // To get video playback on the widest range of devices, we limit Skia to
61  // ES2 shading language when the ES3 external image extension is missing.
62  options.fPreferExternalImagesOverES3 = true;
63 
64  // TODO(goderbauer): remove option when skbug.com/7523 is fixed.
65  // A similar work-around is also used in shell/common/io_manager.cc.
66  options.fDisableGpuYUVConversion = true;
67 
68  auto context = GrDirectContext::MakeGL(delegate_->GetGLInterface(), options);
69 
70  if (context == nullptr) {
71  FML_LOG(ERROR) << "Failed to setup Skia Gr context.";
72  return;
73  }
74 
75  context_ = std::move(context);
76 
77  context_->setResourceCacheLimits(kGrCacheMaxCount, kGrCacheMaxByteSize);
78 
79  context_owner_ = true;
80 
81  valid_ = true;
82 
83  std::vector<PersistentCache::SkSLCache> caches =
85  int compiled_count = 0;
86  for (const auto& cache : caches) {
87  compiled_count += context_->precompileShader(*cache.first, *cache.second);
88  }
89  FML_LOG(INFO) << "Found " << caches.size() << " SkSL shaders; precompiled "
90  << compiled_count;
91 
92  delegate_->GLContextClearCurrent();
93 }
static const size_t kGrCacheMaxByteSize
#define FML_LOG(severity)
Definition: logging.h:65
static const int kGrCacheMaxCount
virtual bool GLContextClearCurrent()=0
std::vector< SkSLCache > LoadSkSLs()
Load all the SkSL shader caches in the right directory.
static PersistentCache * GetCacheForProcess()
virtual std::unique_ptr< GLContextResult > GLContextMakeCurrent()=0
virtual sk_sp< const GrGLInterface > GetGLInterface() const

◆ GPUSurfaceGL() [2/2]

flutter::GPUSurfaceGL::GPUSurfaceGL ( sk_sp< GrDirectContext >  gr_context,
GPUSurfaceGLDelegate delegate,
bool  render_to_surface 
)

Definition at line 95 of file gpu_surface_gl.cc.

References FML_LOG, flutter::GPUSurfaceGLDelegate::GLContextClearCurrent(), and flutter::GPUSurfaceGLDelegate::GLContextMakeCurrent().

98  : delegate_(delegate),
99  context_(gr_context),
100  render_to_surface_(render_to_surface),
101  weak_factory_(this) {
102  auto context_switch = delegate_->GLContextMakeCurrent();
103  if (!context_switch->GetResult()) {
104  FML_LOG(ERROR)
105  << "Could not make the context current to setup the gr context.";
106  return;
107  }
108 
109  delegate_->GLContextClearCurrent();
110 
111  valid_ = true;
112  context_owner_ = false;
113 }
#define FML_LOG(severity)
Definition: logging.h:65
virtual bool GLContextClearCurrent()=0
virtual std::unique_ptr< GLContextResult > GLContextMakeCurrent()=0

◆ ~GPUSurfaceGL()

flutter::GPUSurfaceGL::~GPUSurfaceGL ( )
override

Definition at line 115 of file gpu_surface_gl.cc.

References FML_LOG, flutter::GPUSurfaceGLDelegate::GLContextClearCurrent(), and flutter::GPUSurfaceGLDelegate::GLContextMakeCurrent().

115  {
116  if (!valid_) {
117  return;
118  }
119  auto context_switch = delegate_->GLContextMakeCurrent();
120  if (!context_switch->GetResult()) {
121  FML_LOG(ERROR) << "Could not make the context current to destroy the "
122  "GrDirectContext resources.";
123  return;
124  }
125 
126  onscreen_surface_ = nullptr;
127  fbo_id_ = 0;
128  if (context_owner_) {
129  context_->releaseResourcesAndAbandonContext();
130  }
131  context_ = nullptr;
132 
133  delegate_->GLContextClearCurrent();
134 }
#define FML_LOG(severity)
Definition: logging.h:65
virtual bool GLContextClearCurrent()=0
virtual std::unique_ptr< GLContextResult > GLContextMakeCurrent()=0

Member Function Documentation

◆ AcquireFrame()

std::unique_ptr< SurfaceFrame > flutter::GPUSurfaceGL::AcquireFrame ( const SkISize &  size)
overridevirtual

Implements flutter::Surface.

Definition at line 233 of file gpu_surface_gl.cc.

References FML_LOG, GetRootTransformation(), flutter::GPUSurfaceGLDelegate::GLContextFBO(), flutter::GPUSurfaceGLDelegate::GLContextFBOResetAfterPresent(), flutter::GPUSurfaceGLDelegate::GLContextMakeCurrent(), flutter::GPUSurfaceGLDelegate::GLContextPresent(), flutter::GPUSurfaceGLDelegate::SurfaceSupportsReadback(), TRACE_EVENT0, and flutter::WrapOnscreenSurface().

233  {
234  if (delegate_ == nullptr) {
235  return nullptr;
236  }
237  auto context_switch = delegate_->GLContextMakeCurrent();
238  if (!context_switch->GetResult()) {
239  FML_LOG(ERROR)
240  << "Could not make the context current to acquire the frame.";
241  return nullptr;
242  }
243 
244  // TODO(38466): Refactor GPU surface APIs take into account the fact that an
245  // external view embedder may want to render to the root surface.
246  if (!render_to_surface_) {
247  return std::make_unique<SurfaceFrame>(
248  nullptr, true, [](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
249  return true;
250  });
251  }
252 
253  const auto root_surface_transformation = GetRootTransformation();
254 
255  sk_sp<SkSurface> surface =
256  AcquireRenderSurface(size, root_surface_transformation);
257 
258  if (surface == nullptr) {
259  return nullptr;
260  }
261 
262  surface->getCanvas()->setMatrix(root_surface_transformation);
263  SurfaceFrame::SubmitCallback submit_callback =
264  [weak = weak_factory_.GetWeakPtr()](const SurfaceFrame& surface_frame,
265  SkCanvas* canvas) {
266  return weak ? weak->PresentSurface(canvas) : false;
267  };
268 
269  return std::make_unique<SurfaceFrame>(
270  surface, delegate_->SurfaceSupportsReadback(), submit_callback,
271  std::move(context_switch));
272 }
std::function< bool(const SurfaceFrame &surface_frame, SkCanvas *canvas)> SubmitCallback
Definition: surface_frame.h:22
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define FML_LOG(severity)
Definition: logging.h:65
virtual bool SurfaceSupportsReadback() const
SkMatrix GetRootTransformation() const override
virtual std::unique_ptr< GLContextResult > GLContextMakeCurrent()=0

◆ ClearRenderContext()

bool flutter::GPUSurfaceGL::ClearRenderContext ( )
overridevirtual

Reimplemented from flutter::Surface.

Definition at line 347 of file gpu_surface_gl.cc.

References flutter::GPUSurfaceGLDelegate::GLContextClearCurrent().

347  {
348  return delegate_->GLContextClearCurrent();
349 }
virtual bool GLContextClearCurrent()=0

◆ GetContext()

GrDirectContext * flutter::GPUSurfaceGL::GetContext ( )
overridevirtual

Implements flutter::Surface.

Definition at line 332 of file gpu_surface_gl.cc.

332  {
333  return context_.get();
334 }

◆ GetExternalViewEmbedder()

flutter::ExternalViewEmbedder * flutter::GPUSurfaceGL::GetExternalViewEmbedder ( )
overridevirtual

Reimplemented from flutter::Surface.

Definition at line 337 of file gpu_surface_gl.cc.

References flutter::GPUSurfaceGLDelegate::GetExternalViewEmbedder().

337  {
338  return delegate_->GetExternalViewEmbedder();
339 }
ExternalViewEmbedder * GetExternalViewEmbedder() override
Gets the view embedder that controls how the Flutter layer hierarchy split into multiple chunks shoul...

◆ GetRootTransformation()

SkMatrix flutter::GPUSurfaceGL::GetRootTransformation ( ) const
overridevirtual

Implements flutter::Surface.

Definition at line 228 of file gpu_surface_gl.cc.

References flutter::GPUSurfaceGLDelegate::GLContextSurfaceTransformation().

Referenced by AcquireFrame().

228  {
229  return delegate_->GLContextSurfaceTransformation();
230 }
virtual SkMatrix GLContextSurfaceTransformation() const

◆ IsValid()

bool flutter::GPUSurfaceGL::IsValid ( )
overridevirtual

Implements flutter::Surface.

Definition at line 137 of file gpu_surface_gl.cc.

137  {
138  return valid_;
139 }

◆ MakeRenderContextCurrent()

std::unique_ptr< GLContextResult > flutter::GPUSurfaceGL::MakeRenderContextCurrent ( )
overridevirtual

Reimplemented from flutter::Surface.

Definition at line 342 of file gpu_surface_gl.cc.

References flutter::GPUSurfaceGLDelegate::GLContextMakeCurrent().

342  {
343  return delegate_->GLContextMakeCurrent();
344 }
virtual std::unique_ptr< GLContextResult > GLContextMakeCurrent()=0

The documentation for this class was generated from the following files: