~ubuntu-branches/ubuntu/utopic/mir/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/draw/android_graphics_region_factory.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-03-10 19:28:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: package-import@ubuntu.com-20140310192846-rq9qm3ec26yrelo2
Tags: upstream-0.1.6+14.04.20140310
Import upstream version 0.1.6+14.04.20140310

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012, 2014 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 
17
 */
 
18
 
 
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"
 
23
 
 
24
#include <hardware/gralloc.h>
 
25
#include <stdexcept>
 
26
 
 
27
namespace mtd=mir::test::draw;
 
28
 
 
29
namespace
 
30
{
 
31
struct RegionDeleter
 
32
{
 
33
    RegionDeleter(gralloc_module_t* grmod, native_handle_t const* handle)
 
34
     : grmod(grmod),
 
35
       handle(handle)
 
36
    {
 
37
    }
 
38
 
 
39
    void operator()(MirGraphicsRegion* region)
 
40
    {
 
41
        grmod->unlock(grmod, handle);
 
42
        delete region;
 
43
    }
 
44
 
 
45
    gralloc_module_t *grmod;
 
46
    native_handle_t const* handle;
 
47
};
 
48
 
 
49
class AndroidGraphicsRegionFactory : public mir::test::draw::GraphicsRegionFactory
 
50
{
 
51
public:
 
52
    AndroidGraphicsRegionFactory()
 
53
    {
 
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;
 
59
    }
 
60
 
 
61
    ~AndroidGraphicsRegionFactory()
 
62
    {
 
63
        gralloc_close(alloc_dev);
 
64
    }
 
65
 
 
66
    std::shared_ptr<MirGraphicsRegion> graphic_region_from_handle(
 
67
        mir::graphics::NativeBuffer const& native_buffer)
 
68
    {
 
69
        auto anwb = native_buffer.anwb();
 
70
        int *vaddr;
 
71
        int usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
 
72
        module->lock(
 
73
            module,
 
74
            anwb->handle, usage,
 
75
            0, 0, anwb->width, anwb->height,
 
76
            (void**) &vaddr);
 
77
 
 
78
        MirGraphicsRegion* region = new MirGraphicsRegion;
 
79
        RegionDeleter del(module, anwb->handle);
 
80
 
 
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;
 
86
 
 
87
        return std::shared_ptr<MirGraphicsRegion>(region, del);
 
88
    }
 
89
 
 
90
private:
 
91
    gralloc_module_t* module;
 
92
    alloc_device_t* alloc_dev;
 
93
};
 
94
 
 
95
}
 
96
 
 
97
std::shared_ptr<mtd::GraphicsRegionFactory> mtd::create_graphics_region_factory()
 
98
{
 
99
    return std::make_shared<AndroidGraphicsRegionFactory>();
 
100
}