2
* Copyright © 2012 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Kevin DuBois <kevin.dubois@canonical.com>
20
#include "mir/graphics/android/android_native_buffer.h"
21
#include "mir/graphics/android/sync_fence.h"
22
#include "android_alloc_adaptor.h"
23
#include "android_format_conversion-inl.h"
25
#include <boost/throw_exception.hpp>
28
namespace mg=mir::graphics;
29
namespace mga=mir::graphics::android;
30
namespace geom=mir::geometry;
34
struct AndroidBufferHandleDeleter
36
AndroidBufferHandleDeleter(std::shared_ptr<alloc_device_t> const& alloc_dev)
37
: alloc_device(alloc_dev)
40
void operator()(native_handle_t const* t)
42
alloc_device->free(alloc_device.get(), t);
45
std::shared_ptr<alloc_device_t> const alloc_device;
49
mga::AndroidAllocAdaptor::AndroidAllocAdaptor(const std::shared_ptr<struct alloc_device_t>& alloc_device)
50
: alloc_dev(alloc_device)
54
std::shared_ptr<mg::NativeBuffer> mga::AndroidAllocAdaptor::alloc_buffer(
55
geometry::Size size, MirPixelFormat pf, BufferUsage usage)
57
buffer_handle_t buf_handle = NULL;
59
auto format = mga::to_android_format(pf);
60
auto width = static_cast<int>(size.width.as_uint32_t());
61
auto height = static_cast<int>(size.height.as_uint32_t());
62
auto usage_flag = convert_to_android_usage(usage);
63
auto ret = alloc_dev->alloc(alloc_dev.get(), width, height,
64
format, usage_flag, &buf_handle, &stride);
66
if (( ret ) || (buf_handle == NULL) || (stride == 0))
68
BOOST_THROW_EXCEPTION(std::runtime_error("buffer allocation failed\n"));
71
AndroidBufferHandleDeleter del1(alloc_dev);
72
std::shared_ptr<native_handle_t> handle(buf_handle, del1);
74
auto ops = std::make_shared<mga::RealSyncFileOps>();
75
auto fence = std::make_shared<mga::SyncFence>(ops, -1);
77
auto anwb = std::shared_ptr<mga::RefCountedNativeBuffer>(
78
new mga::RefCountedNativeBuffer(handle),
79
[](mga::RefCountedNativeBuffer* buffer)
81
buffer->mir_dereference();
85
anwb->height = height;
86
anwb->stride = stride;
87
anwb->handle = buf_handle;
88
anwb->format = format;
89
anwb->usage = usage_flag;
91
return std::make_shared<mga::AndroidNativeBuffer>(anwb, fence);
94
int mga::AndroidAllocAdaptor::convert_to_android_usage(BufferUsage usage)
98
case mga::BufferUsage::use_hardware:
99
return (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER);
100
case mga::BufferUsage::use_framebuffer_gles:
101
return (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB);
102
case mga::BufferUsage::use_software:
103
return (GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN);