~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to src/server/graphics/android/android_alloc_adaptor.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-08 02:04:38 UTC
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: package-import@ubuntu.com-20140108020438-e1npu0pm7qdv5wc4
Tags: upstream-0.1.3+14.04.20140108
Import upstream version 0.1.3+14.04.20140108

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3,
6
 
 * as 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:
17
 
 *   Kevin DuBois <kevin.dubois@canonical.com>
18
 
 */
19
 
 
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"
24
 
 
25
 
#include <boost/throw_exception.hpp>
26
 
#include <stdexcept>
27
 
 
28
 
namespace mg=mir::graphics;
29
 
namespace mga=mir::graphics::android;
30
 
namespace geom=mir::geometry;
31
 
 
32
 
namespace
33
 
{
34
 
struct AndroidBufferHandleDeleter
35
 
{
36
 
    AndroidBufferHandleDeleter(std::shared_ptr<alloc_device_t> const& alloc_dev)
37
 
        : alloc_device(alloc_dev)
38
 
    {}
39
 
 
40
 
    void operator()(native_handle_t const* t)
41
 
    {
42
 
        alloc_device->free(alloc_device.get(), t);
43
 
    }
44
 
private:
45
 
    std::shared_ptr<alloc_device_t> const alloc_device;
46
 
};
47
 
}
48
 
 
49
 
mga::AndroidAllocAdaptor::AndroidAllocAdaptor(const std::shared_ptr<struct alloc_device_t>& alloc_device)
50
 
    : alloc_dev(alloc_device)
51
 
{
52
 
}
53
 
 
54
 
std::shared_ptr<mg::NativeBuffer> mga::AndroidAllocAdaptor::alloc_buffer(
55
 
    geometry::Size size, geometry::PixelFormat pf, BufferUsage usage)
56
 
{
57
 
    buffer_handle_t buf_handle = NULL;
58
 
    auto stride = 0;
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);
65
 
 
66
 
    if (( ret ) || (buf_handle == NULL) || (stride == 0))
67
 
    {
68
 
        BOOST_THROW_EXCEPTION(std::runtime_error("buffer allocation failed\n"));
69
 
    }
70
 
 
71
 
    AndroidBufferHandleDeleter del1(alloc_dev);
72
 
    std::shared_ptr<native_handle_t> handle(buf_handle, del1);
73
 
 
74
 
    auto ops = std::make_shared<mga::RealSyncFileOps>();
75
 
    auto fence = std::make_shared<mga::SyncFence>(ops, -1);
76
 
 
77
 
    auto anwb = std::shared_ptr<mga::RefCountedNativeBuffer>(
78
 
        new mga::RefCountedNativeBuffer(handle),
79
 
        [](mga::RefCountedNativeBuffer* buffer)
80
 
        {
81
 
            buffer->mir_dereference();
82
 
        });
83
 
 
84
 
    anwb->width = width;
85
 
    anwb->height = height;
86
 
    anwb->stride = stride;
87
 
    anwb->handle = buf_handle;
88
 
    anwb->format = format;
89
 
    anwb->usage = usage_flag;
90
 
 
91
 
    return std::make_shared<mga::AndroidNativeBuffer>(anwb, fence);
92
 
}
93
 
 
94
 
int mga::AndroidAllocAdaptor::convert_to_android_usage(BufferUsage usage)
95
 
{
96
 
    switch (usage)
97
 
    {
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);
104
 
    default:
105
 
        return -1;
106
 
    }
107
 
}