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/>.
16
* Authored by: Kevin DuBois<kevin.dubois@canonical.com>
19
#include "gralloc_registrar.h"
20
#include "../client_buffer.h"
22
#include <boost/throw_exception.hpp>
25
namespace mcl=mir::client;
26
namespace mcla=mir::client::android;
27
namespace geom=mir::geometry;
31
struct NativeHandleDeleter
33
NativeHandleDeleter(const std::shared_ptr<const gralloc_module_t>& mod)
37
void operator()(const native_handle_t* t)
39
module->unregisterBuffer(module.get(), t);
40
for(auto i = 0; i < t->numFds; i++)
44
::operator delete(const_cast<native_handle_t*>(t));
47
const std::shared_ptr<const gralloc_module_t> module;
50
struct MemoryRegionDeleter
52
MemoryRegionDeleter(const std::shared_ptr<const gralloc_module_t>& mod,
53
const std::shared_ptr<const native_handle_t>& han)
58
void operator()(char *)
60
module->unlock(module.get(), handle.get());
61
//we didn't alloc region(just mapped it), so we don't delete
64
const std::shared_ptr<const native_handle_t> handle;
65
const std::shared_ptr<const gralloc_module_t> module;
69
mcla::GrallocRegistrar::GrallocRegistrar(const std::shared_ptr<const gralloc_module_t>& gr_module)
70
: gralloc_module(gr_module)
74
std::shared_ptr<const native_handle_t> mcla::GrallocRegistrar::register_buffer(
75
std::shared_ptr<MirBufferPackage> const& package) const
77
int native_handle_header_size = sizeof(native_handle_t);
78
int total_size = sizeof(int) *
79
(package->fd_items + package->data_items + native_handle_header_size);
80
native_handle_t* handle = static_cast<native_handle_t*>(::operator new(total_size));
81
handle->version = native_handle_header_size;
82
handle->numFds = package->fd_items;
83
handle->numInts = package->data_items;
84
for (auto i=0; i< handle->numFds; i++)
86
handle->data[i] = package->fd[i];
89
int offset_i = handle->numFds;
90
for (auto i=0; i< handle->numInts; i++)
92
handle->data[offset_i+i] = package->data[i];
95
if ( gralloc_module->registerBuffer(gralloc_module.get(), handle) )
97
::operator delete(const_cast<native_handle_t*>(handle));
98
BOOST_THROW_EXCEPTION(std::runtime_error("error registering graphics buffer for client use\n"));
101
NativeHandleDeleter del(gralloc_module);
102
return std::shared_ptr<const native_handle_t>(handle, del);
105
std::shared_ptr<char> mcla::GrallocRegistrar::secure_for_cpu(
106
std::shared_ptr<const native_handle_t> handle,
107
const geometry::Rectangle rect)
110
int usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
111
int width = rect.size.width.as_uint32_t();
112
int height = rect.size.height.as_uint32_t();
113
int top = rect.top_left.x.as_uint32_t();
114
int left = rect.top_left.y.as_uint32_t();
115
if ( gralloc_module->lock(gralloc_module.get(), handle.get(),
116
usage, top, left, width, height, (void**) &vaddr) )
117
BOOST_THROW_EXCEPTION(std::runtime_error("error securing buffer for client cpu use"));
119
MemoryRegionDeleter del(gralloc_module, handle);
120
return std::shared_ptr<char>(vaddr, del);