Flutter Engine
The Flutter Engine
image_external_texture_vk.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/platform/android/image_external_texture_vk.h"
6
7#include <cstdint>
8
9#include "flutter/impeller/core/formats.h"
10#include "flutter/impeller/core/texture_descriptor.h"
11#include "flutter/impeller/display_list/dl_image_impeller.h"
12#include "flutter/impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.h"
13#include "flutter/impeller/renderer/backend/vulkan/command_buffer_vk.h"
14#include "flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h"
15#include "flutter/impeller/renderer/backend/vulkan/texture_vk.h"
16#include "flutter/impeller/toolkit/android/hardware_buffer.h"
17
18namespace flutter {
19
21 const std::shared_ptr<impeller::ContextVK>& impeller_context,
22 int64_t id,
23 const fml::jni::ScopedJavaGlobalRef<jobject>& image_texture_entry,
24 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade)
25 : ImageExternalTexture(id, image_texture_entry, jni_facade),
26 impeller_context_(impeller_context) {}
27
29
30void ImageExternalTextureVK::Attach(PaintContext& context) {
32 // First processed frame we are attached.
34 }
35}
36
37void ImageExternalTextureVK::Detach() {}
38
39void ImageExternalTextureVK::ProcessFrame(PaintContext& context,
40 const SkRect& bounds) {
42 if (image.is_null()) {
43 return;
44 }
45 JavaLocalRef hardware_buffer = HardwareBufferFor(image);
46 AHardwareBuffer* latest_hardware_buffer = AHardwareBufferFor(hardware_buffer);
47
48 auto hb_desc =
49 impeller::android::HardwareBuffer::Describe(latest_hardware_buffer);
50 std::optional<HardwareBufferKey> key =
52 latest_hardware_buffer);
53 auto existing_image = image_lru_.FindImage(key);
54 if (existing_image != nullptr || !hb_desc.has_value()) {
55 dl_image_ = existing_image;
56
57 CloseHardwareBuffer(hardware_buffer);
58 return;
59 }
60
61 auto texture_source = std::make_shared<impeller::AHBTextureSourceVK>(
62 impeller_context_, latest_hardware_buffer, hb_desc.value());
63 if (!texture_source->IsValid()) {
64 CloseHardwareBuffer(hardware_buffer);
65 return;
66 }
67
68 auto texture =
69 std::make_shared<impeller::TextureVK>(impeller_context_, texture_source);
70 // Transition the layout to shader read.
71 {
72 auto buffer = impeller_context_->CreateCommandBuffer();
73 impeller::CommandBufferVK& buffer_vk =
75
76 impeller::BarrierVK barrier;
77 barrier.cmd_buffer = buffer_vk.GetEncoder()->GetCommandBuffer();
78 barrier.src_access = impeller::vk::AccessFlagBits::eColorAttachmentWrite |
79 impeller::vk::AccessFlagBits::eTransferWrite;
80 barrier.src_stage =
81 impeller::vk::PipelineStageFlagBits::eColorAttachmentOutput |
82 impeller::vk::PipelineStageFlagBits::eTransfer;
83 barrier.dst_access = impeller::vk::AccessFlagBits::eShaderRead;
84 barrier.dst_stage = impeller::vk::PipelineStageFlagBits::eFragmentShader;
85
86 barrier.new_layout = impeller::vk::ImageLayout::eShaderReadOnlyOptimal;
87
88 if (!texture->SetLayout(barrier)) {
89 return;
90 }
91 if (!impeller_context_->GetCommandQueue()->Submit({buffer}).ok()) {
92 return;
93 }
94 }
95
97 if (key.has_value()) {
99 }
100 CloseHardwareBuffer(hardware_buffer);
101}
102
103} // namespace flutter
struct AHardwareBuffer AHardwareBuffer
ImageExternalTextureVK(const std::shared_ptr< impeller::ContextVK > &impeller_context, int64_t id, const fml::jni::ScopedJavaGlobalRef< jobject > &hardware_buffer_texture_entry, const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade)
AHardwareBuffer * AHardwareBufferFor(const fml::jni::JavaRef< jobject > &hardware_buffer)
JavaLocalRef HardwareBufferFor(const fml::jni::JavaRef< jobject > &image)
void CloseHardwareBuffer(const fml::jni::JavaRef< jobject > &hardware_buffer)
sk_sp< flutter::DlImage > dl_image_
HardwareBufferKey AddImage(const sk_sp< flutter::DlImage > &image, HardwareBufferKey key)
Add a new image to the cache with a key, returning the key of the LRU entry that was removed.
Definition: image_lru.cc:42
sk_sp< flutter::DlImage > FindImage(std::optional< HardwareBufferKey > key)
Retrieve the image associated with the given [key], or nullptr.
Definition: image_lru.cc:9
static CommandBufferVK & Cast(CommandBuffer &base)
Definition: backend_cast.h:13
const std::shared_ptr< CommandEncoderVK > & GetEncoder()
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
static std::optional< AHardwareBuffer_Desc > Describe(AHardwareBuffer *buffer)
std::optional< uint64_t > GetSystemUniqueID() const
Get the system wide unique ID of the hardware buffer if possible. This is only available on Android A...
FlTexture * texture
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
std::nullptr_t JavaLocalRef
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 buffer
Definition: switches.h:126
Defines an operations and memory access barrier on a resource.
Definition: barrier_vk.h:27
vk::CommandBuffer cmd_buffer
Definition: barrier_vk.h:28
vk::AccessFlags src_access
Definition: barrier_vk.h:39
vk::PipelineStageFlags dst_stage
Definition: barrier_vk.h:44
vk::ImageLayout new_layout
Definition: barrier_vk.h:29
vk::PipelineStageFlags src_stage
Definition: barrier_vk.h:34
vk::AccessFlags dst_access
Definition: barrier_vk.h:49
const uintptr_t id