Flutter Engine
gpu_surface_vulkan.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/gpu/gpu_surface_vulkan.h"
6 
7 #include "flutter/fml/logging.h"
8 
9 namespace flutter {
10 
12  GPUSurfaceVulkanDelegate* delegate,
13  std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
14  bool render_to_surface)
15  : GPUSurfaceVulkan(/*context=*/nullptr,
16  delegate,
17  std::move(native_surface),
18  render_to_surface) {}
19 
21  const sk_sp<GrDirectContext>& context,
22  GPUSurfaceVulkanDelegate* delegate,
23  std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
24  bool render_to_surface)
25  : window_(context,
26  delegate->vk(),
27  std::move(native_surface),
28  render_to_surface),
29  render_to_surface_(render_to_surface),
30  weak_factory_(this) {}
31 
33 
35  return window_.IsValid();
36 }
37 
38 std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
39  const SkISize& size) {
40  // TODO(38466): Refactor GPU surface APIs take into account the fact that an
41  // external view embedder may want to render to the root surface.
42  if (!render_to_surface_) {
43  return std::make_unique<SurfaceFrame>(
44  nullptr, true, [](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
45  return true;
46  });
47  }
48 
49  auto surface = window_.AcquireSurface();
50 
51  if (surface == nullptr) {
52  return nullptr;
53  }
54 
56  [weak_this = weak_factory_.GetWeakPtr()](const SurfaceFrame&,
57  SkCanvas* canvas) -> bool {
58  // Frames are only ever acquired on the raster thread. This is also the
59  // thread on which the weak pointer factory is collected (as this instance
60  // is owned by the rasterizer). So this use of weak pointers is safe.
61  if (canvas == nullptr || !weak_this) {
62  return false;
63  }
64  return weak_this->window_.SwapBuffers();
65  };
66  return std::make_unique<SurfaceFrame>(std::move(surface), true,
67  std::move(callback));
68 }
69 
71  // This backend does not support delegating to the underlying platform to
72  // query for root surface transformations. Just return identity.
73  SkMatrix matrix;
74  matrix.reset();
75  return matrix;
76 }
77 
78 GrDirectContext* GPUSurfaceVulkan::GetContext() {
79  return window_.GetSkiaGrContext();
80 }
81 
82 } // namespace flutter
std::function< bool(const SurfaceFrame &surface_frame, SkCanvas *canvas)> SubmitCallback
Definition: surface_frame.h:22
std::unique_ptr< SurfaceFrame > AcquireFrame(const SkISize &size) override
GPUSurfaceVulkan(GPUSurfaceVulkanDelegate *delegate, std::unique_ptr< vulkan::VulkanNativeSurface > native_surface, bool render_to_surface)
Create a GPUSurfaceVulkan which implicitly creates its own GrDirectContext for Skia.
Definition: ref_ptr.h:252
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
FlKeyEvent FlKeyResponderAsyncCallback callback
GrDirectContext * GetSkiaGrContext()
GrDirectContext * GetContext() override
sk_sp< SkSurface > AcquireSurface()
bool IsValid() const
SkMatrix GetRootTransformation() const override