Flutter Engine
The 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#include "flutter/fml/trace_event.h"
9
17#include "vulkan/vulkan_core.h"
18
19namespace flutter {
20
22 const sk_sp<GrDirectContext>& skia_context,
23 bool render_to_surface)
24 : delegate_(delegate),
25 skia_context_(skia_context),
26 render_to_surface_(render_to_surface),
27 weak_factory_(this) {}
28
30
32 return skia_context_ != nullptr;
33}
34
35std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
36 const SkISize& frame_size) {
37 if (!IsValid()) {
38 FML_LOG(ERROR) << "Vulkan surface was invalid.";
39 return nullptr;
40 }
41
42 if (frame_size.isEmpty()) {
43 FML_LOG(ERROR) << "Vulkan surface was asked for an empty frame.";
44 return nullptr;
45 }
46
47 if (!render_to_surface_) {
48 return std::make_unique<SurfaceFrame>(
50 [](const SurfaceFrame& surface_frame, DlCanvas* canvas) {
51 return true;
52 },
53 frame_size);
54 }
55
56 FlutterVulkanImage image = delegate_->AcquireImage(frame_size);
57 if (!image.image) {
58 FML_LOG(ERROR) << "Invalid VkImage given by the embedder.";
59 return nullptr;
60 }
61
62 sk_sp<SkSurface> surface = CreateSurfaceFromVulkanImage(
63 reinterpret_cast<VkImage>(image.image),
64 static_cast<VkFormat>(image.format), frame_size);
65 if (!surface) {
66 FML_LOG(ERROR) << "Could not create the SkSurface from the Vulkan image.";
67 return nullptr;
68 }
69
70 SurfaceFrame::SubmitCallback callback = [image = image, delegate = delegate_](
71 const SurfaceFrame&,
72 DlCanvas* canvas) -> bool {
73 TRACE_EVENT0("flutter", "GPUSurfaceVulkan::PresentImage");
74 if (canvas == nullptr) {
75 FML_DLOG(ERROR) << "Canvas not available.";
76 return false;
77 }
78
79 canvas->Flush();
80
81 return delegate->PresentImage(reinterpret_cast<VkImage>(image.image),
82 static_cast<VkFormat>(image.format));
83 };
84
85 SurfaceFrame::FramebufferInfo framebuffer_info{.supports_readback = true};
86
87 return std::make_unique<SurfaceFrame>(std::move(surface), framebuffer_info,
88 std::move(callback), frame_size);
89}
90
92 // This backend does not support delegating to the underlying platform to
93 // query for root surface transformations. Just return identity.
95 matrix.reset();
96 return matrix;
97}
98
100 return skia_context_.get();
101}
102
103sk_sp<SkSurface> GPUSurfaceVulkan::CreateSurfaceFromVulkanImage(
104 const VkImage image,
105 const VkFormat format,
106 const SkISize& size) {
107#ifdef SK_VULKAN
108 GrVkImageInfo image_info = {
109 .fImage = image,
110 .fImageTiling = VK_IMAGE_TILING_OPTIMAL,
111 .fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED,
112 .fFormat = format,
113 .fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
117 .fSampleCount = 1,
118 .fLevelCount = 1,
119 };
120 auto backend_texture =
121 GrBackendTextures::MakeVk(size.width(), size.height(), image_info);
122
123 SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);
124
126 skia_context_.get(), // context
127 backend_texture, // back-end texture
128 kTopLeft_GrSurfaceOrigin, // surface origin
129 1, // sample count
130 ColorTypeFromFormat(format), // color type
131 SkColorSpace::MakeSRGB(), // color space
132 &surface_properties // surface properties
133 );
134#else
135 return nullptr;
136#endif // SK_VULKAN
137}
138
140 switch (format) {
147 default:
149 }
150}
151
152} // namespace flutter
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
@ kUnknown_SkPixelGeometry
static sk_sp< SkColorSpace > MakeSRGB()
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
Interface implemented by all platform surfaces that can present a Vulkan backing store to the "screen...
virtual FlutterVulkanImage AcquireImage(const SkISize &size)=0
Called by the engine to fetch a VkImage for writing the next frame.
GrDirectContext * GetContext() override
std::unique_ptr< SurfaceFrame > AcquireFrame(const SkISize &size) override
static SkColorType ColorTypeFromFormat(const VkFormat format)
SkMatrix GetRootTransformation() const override
GPUSurfaceVulkan(GPUSurfaceVulkanDelegate *delegate, const sk_sp< GrDirectContext > &context, bool render_to_surface)
Create a GPUSurfaceVulkan while letting it reuse an existing GrDirectContext.
std::function< bool(SurfaceFrame &surface_frame, DlCanvas *canvas)> SubmitCallback
Definition: surface_frame.h:27
T * get() const
Definition: SkRefCnt.h:303
MockDelegate delegate_
VkSurfaceKHR surface
Definition: main.cc:49
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_LOG(severity)
Definition: logging.h:82
SK_API GrBackendTexture MakeVk(int width, int height, const GrVkImageInfo &, std::string_view label={})
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent Remove all existing persistent cache This is mainly for debugging purposes such as reproducing the shader compilation jank trace to Write the timeline trace to a file at the specified path The file will be in Perfetto s proto format
Definition: switches.h:203
VkImage fImage
Definition: GrVkTypes.h:26
Definition: SkSize.h:16
bool isEmpty() const
Definition: SkSize.h:31
#define ERROR(message)
Definition: elf_loader.cc:260
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131
@ VK_IMAGE_LAYOUT_UNDEFINED
Definition: vulkan_core.h:1331
@ VK_IMAGE_TILING_OPTIMAL
Definition: vulkan_core.h:1767
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
Definition: vulkan_core.h:2353
@ VK_IMAGE_USAGE_SAMPLED_BIT
Definition: vulkan_core.h:2354
@ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
Definition: vulkan_core.h:2356
@ VK_IMAGE_USAGE_TRANSFER_SRC_BIT
Definition: vulkan_core.h:2352
VkFormat
Definition: vulkan_core.h:1458
@ VK_FORMAT_R8G8B8A8_SRGB
Definition: vulkan_core.h:1502
@ VK_FORMAT_B8G8R8A8_UNORM
Definition: vulkan_core.h:1503
@ VK_FORMAT_B8G8R8A8_SRGB
Definition: vulkan_core.h:1509
@ VK_FORMAT_R8G8B8A8_UNORM
Definition: vulkan_core.h:1496