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 #ifndef SHELL_GPU_GPU_SURFACE_GL_H_ 6 #define SHELL_GPU_GPU_SURFACE_GL_H_ 7 8 #include <functional> 9 #include <memory> 10 11 #include "flutter/flow/embedded_views.h" 12 #include "flutter/fml/macros.h" 13 #include "flutter/fml/memory/weak_ptr.h" 14 #include "flutter/shell/common/surface.h" 15 #include "flutter/shell/gpu/gpu_surface_gl_delegate.h" 16 #include "third_party/skia/include/gpu/GrContext.h" 17 18 namespace flutter { 19 20 class GPUSurfaceGL : public Surface { 21 public: 22 GPUSurfaceGL(GPUSurfaceGLDelegate* delegate, bool render_to_surface); 23 24 // Creates a new GL surface reusing an existing GrContext. 25 GPUSurfaceGL(sk_sp<GrContext> gr_context, 26 GPUSurfaceGLDelegate* delegate, 27 bool render_to_surface); 28 29 ~GPUSurfaceGL() override; 30 31 // |Surface| 32 bool IsValid() override; 33 34 // |Surface| 35 std::unique_ptr<SurfaceFrame> AcquireFrame(const SkISize& size) override; 36 37 // |Surface| 38 SkMatrix GetRootTransformation() const override; 39 40 // |Surface| 41 GrContext* GetContext() override; 42 43 // |Surface| 44 flutter::ExternalViewEmbedder* GetExternalViewEmbedder() override; 45 46 // |Surface| 47 bool MakeRenderContextCurrent() override; 48 49 private: 50 GPUSurfaceGLDelegate* delegate_; 51 sk_sp<GrContext> context_; 52 sk_sp<SkSurface> onscreen_surface_; 53 sk_sp<SkSurface> offscreen_surface_; 54 bool context_owner_; 55 // TODO(38466): Refactor GPU surface APIs take into account the fact that an 56 // external view embedder may want to render to the root surface. This is a 57 // hack to make avoid allocating resources for the root surface when an 58 // external view embedder is present. 59 const bool render_to_surface_; 60 bool valid_ = false; 61 fml::WeakPtrFactory<GPUSurfaceGL> weak_factory_; 62 63 bool CreateOrUpdateSurfaces(const SkISize& size); 64 65 sk_sp<SkSurface> AcquireRenderSurface( 66 const SkISize& untransformed_size, 67 const SkMatrix& root_surface_transformation); 68 69 bool PresentSurface(SkCanvas* canvas); 70 71 FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceGL); 72 }; 73 74 } // namespace flutter 75 76 #endif // SHELL_GPU_GPU_SURFACE_GL_H_ 77