2
* Copyright © 2012, 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* 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 General Public License for more details.
13
* You should have received a copy of the GNU 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 "mir_test/draw/graphics_region_factory.h"
20
#include "mir_toolkit/mir_client_library.h"
21
#include "mir/graphics/android/native_buffer.h"
22
#include "mir_toolkit/common.h"
24
#include <hardware/gralloc.h>
27
namespace mtd=mir::test::draw;
33
RegionDeleter(gralloc_module_t* grmod, native_handle_t const* handle)
39
void operator()(MirGraphicsRegion* region)
41
grmod->unlock(grmod, handle);
45
gralloc_module_t *grmod;
46
native_handle_t const* handle;
49
class AndroidGraphicsRegionFactory : public mir::test::draw::GraphicsRegionFactory
52
AndroidGraphicsRegionFactory()
54
const hw_module_t *hw_module;
55
if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hw_module) != 0)
56
throw std::runtime_error("error, hw module not available!\n");
57
gralloc_open(hw_module, &alloc_dev);
58
module = (gralloc_module_t*) hw_module;
61
~AndroidGraphicsRegionFactory()
63
gralloc_close(alloc_dev);
66
std::shared_ptr<MirGraphicsRegion> graphic_region_from_handle(
67
mir::graphics::NativeBuffer const& native_buffer)
69
auto anwb = native_buffer.anwb();
71
int usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
75
0, 0, anwb->width, anwb->height,
78
MirGraphicsRegion* region = new MirGraphicsRegion;
79
RegionDeleter del(module, anwb->handle);
81
region->vaddr = (char*) vaddr;
82
region->stride = anwb->stride * MIR_BYTES_PER_PIXEL(mir_pixel_format_abgr_8888);
83
region->width = anwb->width;
84
region->height = anwb->height;
85
region->pixel_format = mir_pixel_format_abgr_8888;
87
return std::shared_ptr<MirGraphicsRegion>(region, del);
91
gralloc_module_t* module;
92
alloc_device_t* alloc_dev;
97
std::shared_ptr<mtd::GraphicsRegionFactory> mtd::create_graphics_region_factory()
99
return std::make_shared<AndroidGraphicsRegionFactory>();