Flutter Engine
ios_surface_gl.mm
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 #import "flutter/shell/platform/darwin/ios/ios_surface_gl.h"
6 
7 #include "flutter/fml/trace_event.h"
8 #include "flutter/shell/gpu/gpu_surface_gl.h"
9 #import "flutter/shell/platform/darwin/ios/ios_context_gl.h"
10 
11 namespace flutter {
12 
13 static IOSContextGL* CastToGLContext(const std::shared_ptr<IOSContext>& context) {
14  return reinterpret_cast<IOSContextGL*>(context.get());
15 }
16 
18  std::shared_ptr<IOSContext> context)
19  : IOSSurface(context) {
20  render_target_ = CastToGLContext(context)->CreateRenderTarget(std::move(layer));
21 }
22 
23 IOSSurfaceGL::~IOSSurfaceGL() = default;
24 
25 // |IOSSurface|
26 bool IOSSurfaceGL::IsValid() const {
27  return render_target_->IsValid();
28 }
29 
30 // |IOSSurface|
32  if (IsValid()) {
33  render_target_->UpdateStorageSizeIfNecessary();
34  }
35 }
36 
37 // |IOSSurface|
38 std::unique_ptr<Surface> IOSSurfaceGL::CreateGPUSurface(GrDirectContext* gr_context) {
39  if (gr_context) {
40  return std::make_unique<GPUSurfaceGL>(sk_ref_sp(gr_context), this, true);
41  } else {
42  IOSContextGL* gl_context = CastToGLContext(GetContext());
43  sk_sp<GrDirectContext> context = gl_context->GetMainContext();
44  if (!context) {
45  context = GPUSurfaceGL::MakeGLContext(this);
46  gl_context->SetMainContext(context);
47  }
48 
49  return std::make_unique<GPUSurfaceGL>(context, this, true);
50  }
51 }
52 
53 // |GPUSurfaceGLDelegate|
54 intptr_t IOSSurfaceGL::GLContextFBO(GLFrameInfo frame_info) const {
55  return IsValid() ? render_target_->GetFramebuffer() : GL_NONE;
56 }
57 
58 // |GPUSurfaceGLDelegate|
61  // The onscreen surface wraps a GL renderbuffer, which is extremely slow to read on iOS.
62  // Certain filter effects, in particular BackdropFilter, require making a copy of
63  // the current destination. For performance, the iOS surface will specify that it
64  // does not support readback so that the engine compositor can implement a workaround
65  // such as rendering the scene to an offscreen surface or Skia saveLayer.
66  res.supports_readback = false;
67  return res;
68 }
69 
70 // |GPUSurfaceGLDelegate|
71 std::unique_ptr<GLContextResult> IOSSurfaceGL::GLContextMakeCurrent() {
72  if (!IsValid()) {
73  return std::make_unique<GLContextDefaultResult>(false);
74  }
75  bool update_if_necessary = render_target_->UpdateStorageSizeIfNecessary();
76  if (!update_if_necessary) {
77  return std::make_unique<GLContextDefaultResult>(false);
78  }
79  return GetContext()->MakeCurrent();
80 }
81 
82 // |GPUSurfaceGLDelegate|
84  // |GLContextMakeCurrent| should handle the scope of the gl context.
85  return true;
86 }
87 
88 // |GPUSurfaceGLDelegate|
89 bool IOSSurfaceGL::GLContextPresent(fml::TimePoint target_time, uint32_t fbo_id) {
90  TRACE_EVENT0("flutter", "IOSSurfaceGL::GLContextPresent");
91  return IsValid() && render_target_->PresentRenderBuffer();
92 }
93 
94 // |GPUSurfaceGLDelegate|
96  return false;
97 }
98 
99 } // namespace flutter
std::shared_ptr< IOSContext > GetContext() const
Definition: ios_surface.mm:55
std::unique_ptr< IOSRenderTargetGL > CreateRenderTarget(fml::scoped_nsobject< CAEAGLLayer > layer)
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
~IOSSurfaceGL() override
void SetMainContext(const sk_sp< GrDirectContext > &main_context)
bool GLContextPresent(fml::TimePoint target_time, uint32_t fbo_id) override
std::unique_ptr< Surface > CreateGPUSurface(GrDirectContext *gr_context) override
sk_sp< GrDirectContext > GetMainContext() const override
Accessor for the Skia context associated with IOSSurfaces and the raster thread.
static IOSContextGL * CastToGLContext(const std::shared_ptr< IOSContext > &context)
SurfaceFrame::FramebufferInfo GLContextFramebufferInfo() const override
IOSSurfaceGL(fml::scoped_nsobject< CAEAGLLayer > layer, std::shared_ptr< IOSContext > context)
static sk_sp< GrDirectContext > MakeGLContext(GPUSurfaceGLDelegate *delegate)
intptr_t GLContextFBO(GLFrameInfo frame_info) const override
bool GLContextClearCurrent() override
bool AllowsDrawingWhenGpuDisabled() const override
std::unique_ptr< GLContextResult > GLContextMakeCurrent() override
void UpdateStorageSizeIfNecessary() override
bool IsValid() const override