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