1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/ozone/platform/flatland/flatland_sysmem_buffer_manager.h"
#include <zircon/rights.h>
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/koid.h"
#include "base/functional/bind.h"
#include "ui/ozone/platform/flatland/flatland_sysmem_buffer_collection.h"
namespace ui {
namespace {
std::string GetProcessName() {
char name[ZX_MAX_NAME_LEN] = {};
zx_status_t status =
zx::process::self()->get_property(ZX_PROP_NAME, name, sizeof(name));
return (status == ZX_OK) ? std::string(name) : "";
}
} // namespace
FlatlandSysmemBufferManager::FlatlandSysmemBufferManager(
FlatlandSurfaceFactory* flatland_surface_factory)
: flatland_surface_factory_(flatland_surface_factory) {}
FlatlandSysmemBufferManager::~FlatlandSysmemBufferManager() {
Shutdown();
}
void FlatlandSysmemBufferManager::Initialize(
fuchsia::sysmem2::AllocatorHandle sysmem_allocator,
fuchsia::ui::composition::AllocatorHandle flatland_allocator) {
base::AutoLock auto_lock(collections_lock_);
DCHECK(collections_.empty());
DCHECK(!sysmem_allocator_);
sysmem_allocator_.Bind(std::move(sysmem_allocator));
sysmem_allocator_->SetDebugClientInfo(
std::move(fuchsia::sysmem2::AllocatorSetDebugClientInfoRequest{}
.set_name(GetProcessName() + "-FlatlandSysmemBufferManager")
.set_id(base::GetCurrentProcId())));
DCHECK(!flatland_allocator_);
flatland_allocator_.Bind(std::move(flatland_allocator));
flatland_allocator_.set_error_handler(base::LogFidlErrorAndExitProcess(
FROM_HERE, "fuchsia::ui::composition::Allocator"));
}
void FlatlandSysmemBufferManager::Shutdown() {
base::AutoLock auto_lock(collections_lock_);
DCHECK(collections_.empty());
sysmem_allocator_ = nullptr;
flatland_allocator_ = nullptr;
}
scoped_refptr<gfx::NativePixmap>
FlatlandSysmemBufferManager::CreateNativePixmap(VkDevice vk_device,
gfx::Size size,
gfx::BufferFormat format,
gfx::BufferUsage usage) {
gfx::NativePixmapHandle pixmap_handle;
zx::eventpair service_handle;
auto status = zx::eventpair::create(
0, &pixmap_handle.buffer_collection_handle, &service_handle);
ZX_DCHECK(status == ZX_OK, status);
auto collection = base::MakeRefCounted<FlatlandSysmemBufferCollection>();
if (!collection->Initialize(
sysmem_allocator_.get(), flatland_allocator_.get(),
flatland_surface_factory_, std::move(service_handle),
/*token_channel=*/zx::channel(), size, format, usage, vk_device,
/*min_buffer_count=*/1, usage == gfx::BufferUsage::SCANOUT)) {
return nullptr;
}
auto result = collection->CreateNativePixmap(std::move(pixmap_handle), size);
if (result)
RegisterCollection(collection);
return result;
}
scoped_refptr<FlatlandSysmemBufferCollection>
FlatlandSysmemBufferManager::ImportSysmemBufferCollection(
VkDevice vk_device,
zx::eventpair service_handle,
zx::channel sysmem_token,
gfx::Size size,
gfx::BufferFormat format,
gfx::BufferUsage usage,
size_t min_buffer_count,
bool register_with_flatland_allocator) {
auto result = base::MakeRefCounted<FlatlandSysmemBufferCollection>();
if (!result->Initialize(sysmem_allocator_.get(), flatland_allocator_.get(),
flatland_surface_factory_, std::move(service_handle),
std::move(sysmem_token), size, format, usage,
vk_device, min_buffer_count,
register_with_flatland_allocator)) {
return nullptr;
}
RegisterCollection(result);
return result;
}
void FlatlandSysmemBufferManager::RegisterCollection(
scoped_refptr<FlatlandSysmemBufferCollection> collection) {
{
base::AutoLock auto_lock(collections_lock_);
collections_[collection->id()] = collection;
}
collection->AddOnReleasedCallback(
base::BindOnce(&FlatlandSysmemBufferManager::OnCollectionReleased,
base::Unretained(this), collection->id()));
}
scoped_refptr<FlatlandSysmemBufferCollection>
FlatlandSysmemBufferManager::GetCollectionByHandle(const zx::eventpair& token) {
auto koid = base::GetRelatedKoid(token);
if (!koid)
return nullptr;
base::AutoLock auto_lock(collections_lock_);
auto it = collections_.find(koid.value());
return it == collections_.end() ? nullptr : it->second;
}
void FlatlandSysmemBufferManager::OnCollectionReleased(zx_koid_t id) {
base::AutoLock auto_lock(collections_lock_);
int erased = collections_.erase(id);
DCHECK_EQ(erased, 1);
}
} // namespace ui
|