summaryrefslogtreecommitdiffstats
blob: 04c41ddb73a95d5988525c984ee962a334a42478 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) Texas Instruments Incorporated - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cutils/log.h>
#include <drm/drm_fourcc.h>
#include <hardware/hwcomposer.h>

#include <xf86drm.h>
#include <xf86drmMode.h>

#include "drmfb.h"
#include "format.h"
#include "hal_public.h"

DRMFramebuffer::DRMFramebuffer(int drm_fd, buffer_handle_t handle, bool is_overlay) :
    bo(), pitches(), offsets()
{
    if (!handle)
        return;

    uint32_t gem_handle;
    IMG_native_handle_t* img_hnd = (IMG_native_handle_t*)handle;
    int ret = drmPrimeFDToHandle(drm_fd, img_hnd->fd[0], &gem_handle);
    if (ret) {
        ALOGE("Failed to get DRM buffer object from handle");
        return;
    }

    this->width = img_hnd->iWidth;
    this->height = img_hnd->iHeight;
    this->format = convert_hal_to_drm_format(img_hnd->iFormat, true);
    this->bo[0] = gem_handle;
    this->pitches[0] = ALIGN(img_hnd->iWidth, HW_ALIGN) * get_format_bpp(img_hnd->iFormat) >> 3;
    this->offsets[0] = 0;
    this->drm_fd = drm_fd;

    if (is_overlay) {
        switch (this->format) {
        case DRM_FORMAT_NV12:
            this->bo[1] = gem_handle;

            this->pitches[0] = ALIGN(img_hnd->iWidth, HW_ALIGN);
            this->pitches[1] = this->pitches[0];

            this->offsets[1] = this->pitches[0] * img_hnd->iHeight;
            break;
        case DRM_FORMAT_ARGB8888:
        case DRM_FORMAT_XRGB8888:
        case DRM_FORMAT_RGB565:
            break;
        default:
            ALOGE("Bad format for overlay");
            return;
        }
    }

    ret = drmModeAddFB2(drm_fd, this->width, this->height,
                        this->format, this->bo,
                        this->pitches, this->offsets,
                        &this->fb_id, 0);
    if (ret) {
        ALOGE("Could not create DRM frame buffer %d", ret);
        return;
    }
}

DRMFramebuffer::~DRMFramebuffer()
{
    if (this->fb_id) {
        if (drmModeRmFB(this->drm_fd, this->fb_id))
            ALOGE("Failed to remove DRM frame buffer");
    }

    for (size_t i = 0; i < 4; i++) {
        if (this->bo[i]) {
            struct drm_gem_close close_args = {
                close_args.handle = this->bo[i],
                close_args.pad = 0,
            };
            int ret = drmIoctl(this->drm_fd, DRM_IOCTL_GEM_CLOSE, &close_args);
            if (ret) {
                ALOGE("Failed to close GEM handle");
                return;
            }
        }
    }
}