Skip to content

Commit 62be13d

Browse files
authored
Minor refactor for vulkan renderer (#176)
1 parent b086ea3 commit 62be13d

8 files changed

Lines changed: 72 additions & 64 deletions

flutter/shell/platform/tizen/external_texture_pixel_vulkan.cc

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,26 @@ bool ExternalTexturePixelVulkan::PopulateVulkanTexture(
3737
return false;
3838
}
3939

40+
if (!pixel_buffer->buffer) {
41+
FT_LOG(Error) << "pixel_buffer->buffer is nullptr";
42+
return false;
43+
}
44+
45+
if (pixel_buffer->width == 0 || pixel_buffer->height == 0) {
46+
FT_LOG(Error) << "Invalid pixel buffer dimensions: " << pixel_buffer->width
47+
<< "x" << pixel_buffer->height;
48+
return false;
49+
}
50+
4051
if (!CreateOrUpdateImage(pixel_buffer->width, pixel_buffer->height)) {
4152
FT_LOG(Error) << "Fail to create image";
4253
ReleaseImage();
4354
return false;
4455
}
4556

4657
VkDeviceSize required_staging_size =
47-
pixel_buffer->width * pixel_buffer->height * 4;
58+
static_cast<VkDeviceSize>(pixel_buffer->width) *
59+
static_cast<VkDeviceSize>(pixel_buffer->height) * 4;
4860
if (!CreateOrUpdateBuffer(required_staging_size)) {
4961
FT_LOG(Error) << "Fail to create buffer";
5062
ReleaseBuffer();
@@ -54,7 +66,10 @@ bool ExternalTexturePixelVulkan::PopulateVulkanTexture(
5466
width_ = pixel_buffer->width;
5567
height_ = pixel_buffer->height;
5668

57-
CopyBufferToImage(pixel_buffer->buffer, required_staging_size);
69+
if (!CopyBufferToImage(pixel_buffer->buffer, required_staging_size)) {
70+
FT_LOG(Error) << "Failed to copy buffer to image";
71+
return false;
72+
}
5873

5974
FlutterVulkanTexture* vulkan_texture =
6075
static_cast<FlutterVulkanTexture*>(flutter_texture);
@@ -127,27 +142,6 @@ bool ExternalTexturePixelVulkan::CreateImage(size_t width, size_t height) {
127142
return true;
128143
}
129144

130-
bool ExternalTexturePixelVulkan::FindMemoryType(
131-
uint32_t type_filter,
132-
VkMemoryPropertyFlags properties,
133-
uint32_t& index_out) {
134-
VkPhysicalDeviceMemoryProperties memory_properties;
135-
vkGetPhysicalDeviceMemoryProperties(
136-
static_cast<VkPhysicalDevice>(
137-
vulkan_renderer_->GetPhysicalDeviceHandle()),
138-
&memory_properties);
139-
140-
for (uint32_t i = 0; i < memory_properties.memoryTypeCount; i++) {
141-
if ((type_filter & (1 << i)) &&
142-
(memory_properties.memoryTypes[i].propertyFlags & properties) ==
143-
properties) {
144-
index_out = i;
145-
return true;
146-
}
147-
}
148-
return false;
149-
}
150-
151145
bool ExternalTexturePixelVulkan::CreateBuffer(VkDeviceSize required_size) {
152146
VkBufferCreateInfo buffer_info{};
153147
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
@@ -194,13 +188,23 @@ void ExternalTexturePixelVulkan::ReleaseBuffer() {
194188
staging_buffer_size_ = 0;
195189
}
196190

197-
void ExternalTexturePixelVulkan::CopyBufferToImage(const uint8_t* src_buffer,
191+
bool ExternalTexturePixelVulkan::CopyBufferToImage(const uint8_t* src_buffer,
198192
VkDeviceSize size) {
199193
void* data;
200-
vkMapMemory(GetDevice(), staging_buffer_memory_, 0, size, 0, &data);
194+
VkResult result =
195+
vkMapMemory(GetDevice(), staging_buffer_memory_, 0, size, 0, &data);
196+
if (result != VK_SUCCESS) {
197+
FT_LOG(Error) << "Failed to map staging buffer memory";
198+
return false;
199+
}
201200
memcpy(data, src_buffer, static_cast<size_t>(size));
202201
vkUnmapMemory(GetDevice(), staging_buffer_memory_);
202+
203203
VkCommandBuffer command_buffer = vulkan_renderer_->BeginSingleTimeCommands();
204+
if (command_buffer == VK_NULL_HANDLE) {
205+
FT_LOG(Error) << "Failed to begin single time commands";
206+
return false;
207+
}
204208

205209
VkBufferImageCopy region{};
206210
region.bufferOffset = 0;
@@ -218,6 +222,7 @@ void ExternalTexturePixelVulkan::CopyBufferToImage(const uint8_t* src_buffer,
218222
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
219223

220224
vulkan_renderer_->EndSingleTimeCommands(command_buffer);
225+
return true;
221226
}
222227

223228
void ExternalTexturePixelVulkan::ReleaseImage() {
@@ -237,8 +242,8 @@ bool ExternalTexturePixelVulkan::AllocateMemory(
237242
VkDeviceMemory& memory,
238243
VkMemoryPropertyFlags properties) {
239244
uint32_t memory_type_index;
240-
if (!FindMemoryType(memory_requirements.memoryTypeBits, properties,
241-
memory_type_index)) {
245+
if (!vulkan_renderer_->FindMemoryType(memory_requirements.memoryTypeBits,
246+
properties, &memory_type_index)) {
242247
FT_LOG(Error) << "Fail to find memory type";
243248
return false;
244249
}
@@ -255,7 +260,7 @@ bool ExternalTexturePixelVulkan::AllocateMemory(
255260
return true;
256261
}
257262

258-
VkDevice ExternalTexturePixelVulkan::GetDevice() {
263+
VkDevice ExternalTexturePixelVulkan::GetDevice() const {
259264
return static_cast<VkDevice>(vulkan_renderer_->GetDeviceHandle());
260265
}
261266

flutter/shell/platform/tizen/external_texture_pixel_vulkan.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ class ExternalTexturePixelVulkan : public ExternalVulkanTexture {
3232
bool CreateImage(size_t width, size_t height);
3333
bool CreateOrUpdateBuffer(VkDeviceSize required_size);
3434
bool CreateOrUpdateImage(size_t width, size_t height);
35-
void CopyBufferToImage(const uint8_t* src_buffer, VkDeviceSize size);
36-
VkDevice GetDevice();
35+
bool CopyBufferToImage(const uint8_t* src_buffer, VkDeviceSize size);
36+
VkDevice GetDevice() const;
3737
void ReleaseBuffer();
3838
void ReleaseImage();
39-
bool FindMemoryType(uint32_t typeFilter,
40-
VkMemoryPropertyFlags properties,
41-
uint32_t& index_out);
4239
FlutterDesktopPixelBufferTextureCallback texture_callback_ = nullptr;
4340
size_t width_ = 0;
4441
size_t height_ = 0;

flutter/shell/platform/tizen/external_texture_surface_vulkan.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ bool ExternalTextureSurfaceVulkan::CreateBuffer(
4747
}
4848
if (!vulkan_buffer_->AllocateAndBindMemory(tbm_surface)) {
4949
FT_LOG(Error) << "Fail to allocate memory";
50+
vulkan_buffer_->ReleaseImage();
5051
return false;
5152
}
5253

flutter/shell/platform/tizen/external_texture_surface_vulkan_buffer.cc

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,7 @@ VkFormat ExternalTextureSurfaceVulkanBuffer::ConvertFormat(tbm_format& format) {
3131
}
3232
}
3333

34-
bool ExternalTextureSurfaceVulkanBuffer::FindMemoryType(
35-
uint32_t type_filter,
36-
VkMemoryPropertyFlags properties,
37-
uint32_t& index_out) {
38-
VkPhysicalDeviceMemoryProperties memory_properties;
39-
vkGetPhysicalDeviceMemoryProperties(
40-
static_cast<VkPhysicalDevice>(
41-
vulkan_renderer_->GetPhysicalDeviceHandle()),
42-
&memory_properties);
43-
44-
for (uint32_t i = 0; i < memory_properties.memoryTypeCount; i++) {
45-
if ((type_filter & (1 << i)) &&
46-
(memory_properties.memoryTypes[i].propertyFlags & properties) ==
47-
properties) {
48-
index_out = i;
49-
return true;
50-
}
51-
}
52-
return false;
53-
}
54-
55-
VkDevice ExternalTextureSurfaceVulkanBuffer::GetDevice() {
34+
VkDevice ExternalTextureSurfaceVulkanBuffer::GetDevice() const {
5635
return static_cast<VkDevice>(vulkan_renderer_->GetDeviceHandle());
5736
}
5837

flutter/shell/platform/tizen/external_texture_surface_vulkan_buffer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ class ExternalTextureSurfaceVulkanBuffer {
3131

3232
protected:
3333
VkFormat ConvertFormat(tbm_format& format);
34-
VkDevice GetDevice();
35-
bool FindMemoryType(uint32_t memory_type_bits_requirement,
36-
VkMemoryPropertyFlags required_properties,
37-
uint32_t& index_out);
34+
VkDevice GetDevice() const;
3835

3936
private:
4037
TizenRendererVulkan* vulkan_renderer_ = nullptr;

flutter/shell/platform/tizen/external_texture_surface_vulkan_buffer_dma.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include "flutter/shell/platform/tizen/external_texture_surface_vulkan_buffer_dma.h"
6+
#include <unistd.h>
67
#include "flutter/shell/platform/tizen/logger.h"
78

89
namespace flutter {
@@ -31,7 +32,11 @@ VkDeviceMemory ExternalTextureSurfaceVulkanBufferDma::GetMemory() {
3132
bool ExternalTextureSurfaceVulkanBufferDma::CreateImage(
3233
tbm_surface_h tbm_surface) {
3334
tbm_surface_info_s tbm_surface_info;
34-
tbm_surface_get_info(tbm_surface, &tbm_surface_info);
35+
if (tbm_surface_get_info(tbm_surface, &tbm_surface_info) !=
36+
TBM_SURFACE_ERROR_NONE) {
37+
FT_LOG(Error) << "Fail to get tbm_surface info";
38+
return false;
39+
}
3540
texture_format_ = ConvertFormat(tbm_surface_info.format);
3641

3742
VkExternalMemoryImageCreateInfoKHR external_image_create_info = {};
@@ -108,9 +113,10 @@ bool ExternalTextureSurfaceVulkanBufferDma::AllocateAndBindMemory(
108113
int bo_fd = tbm_bo_export_fd(bo);
109114
int bo_size = tbm_bo_size(bo);
110115

111-
uint32_t memory_type_index = -1;
116+
uint32_t memory_type_index = 0;
112117
if (!GetFdMemoryTypeIndex(bo_fd, memory_type_index)) {
113118
FT_LOG(Error) << "Fail to get memory type index";
119+
close(bo_fd);
114120
return false;
115121
}
116122

@@ -129,15 +135,16 @@ bool ExternalTextureSurfaceVulkanBufferDma::AllocateAndBindMemory(
129135
if (vkAllocateMemory(GetDevice(), &alloc_info, nullptr,
130136
&texture_device_memory_) != VK_SUCCESS) {
131137
FT_LOG(Error) << "Fail to allocate memory";
138+
close(bo_fd);
132139
return false;
133140
}
134141

135142
if (vkBindImageMemory(GetDevice(), texture_image_, texture_device_memory_,
136143
0u) != VK_SUCCESS) {
137144
FT_LOG(Error) << "Fail to bind image memory";
145+
close(bo_fd);
138146
return false;
139147
}
140-
close(bo_fd);
141148
return true;
142149
}
143150

flutter/shell/platform/tizen/tizen_renderer_vulkan.cc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool TizenRendererVulkan::InitVulkan(TizenViewBase* view) {
102102
}
103103

104104
void TizenRendererVulkan::Cleanup() {
105-
if (logical_device_) {
105+
if (logical_device_ != VK_NULL_HANDLE) {
106106
// Ensure all GPU work is complete before destroying anything.
107107
vkDeviceWaitIdle(logical_device_);
108108

@@ -749,7 +749,6 @@ bool TizenRendererVulkan::InitializeSwapchain() {
749749
FT_LOG(Error) << "Could not get swap chain images count";
750750
return false;
751751
}
752-
// swapchain_images_.reserve(image_count);
753752
swapchain_images_.resize(image_count);
754753
if (vkGetSwapchainImagesKHR(logical_device_, swapchain_, &image_count,
755754
swapchain_images_.data()) != VK_SUCCESS) {
@@ -1022,4 +1021,24 @@ void TizenRendererVulkan::EndSingleTimeCommands(VkCommandBuffer commandBuffer) {
10221021
&commandBuffer);
10231022
}
10241023

1024+
bool TizenRendererVulkan::FindMemoryType(uint32_t type_filter,
1025+
VkMemoryPropertyFlags properties,
1026+
uint32_t* index_out) {
1027+
if (!index_out) {
1028+
return false;
1029+
}
1030+
VkPhysicalDeviceMemoryProperties memory_properties;
1031+
vkGetPhysicalDeviceMemoryProperties(physical_device_, &memory_properties);
1032+
1033+
for (uint32_t i = 0; i < memory_properties.memoryTypeCount; i++) {
1034+
if ((type_filter & (1 << i)) &&
1035+
(memory_properties.memoryTypes[i].propertyFlags & properties) ==
1036+
properties) {
1037+
*index_out = i;
1038+
return true;
1039+
}
1040+
}
1041+
return false;
1042+
}
1043+
10251044
} // namespace flutter

flutter/shell/platform/tizen/tizen_renderer_vulkan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class TizenRendererVulkan : public TizenRenderer {
5151
bool Present(const FlutterVulkanImage* image);
5252
VkCommandBuffer BeginSingleTimeCommands();
5353
void EndSingleTimeCommands(VkCommandBuffer commandBuffer);
54+
bool FindMemoryType(uint32_t type_filter,
55+
VkMemoryPropertyFlags properties,
56+
uint32_t* index_out);
5457

5558
private:
5659
bool CreateCommandPool();

0 commit comments

Comments
 (0)