blob: 7b7a1f1262c7672abc1085ecdbae4601292b9f09 [file] [log] [blame]
Neil Armstrong17d3c8b2019-04-25 15:17:22 +00001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "hwc-platform-meson"
18
19#include "platformmeson.h"
20#include "drmdevice.h"
21#include "platform.h"
22
Neil Armstrong17d3c8b2019-04-25 15:17:22 +000023#include <stdatomic.h>
24#include <xf86drm.h>
25#include <xf86drmMode.h>
26#include <cinttypes>
27
28#include <hardware/gralloc.h>
29#include <log/log.h>
30#include "gralloc_priv.h"
31
32namespace android {
33
34Importer *Importer::CreateInstance(DrmDevice *drm) {
35 MesonImporter *importer = new MesonImporter(drm);
36 if (!importer)
37 return NULL;
38
39 int ret = importer->Init();
40 if (ret) {
41 ALOGE("Failed to initialize the meson importer %d", ret);
42 delete importer;
43 return NULL;
44 }
45 return importer;
46}
47
48#if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
49 defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
50uint64_t MesonImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags) {
51 uint64_t features = 0UL;
52
Neil Armstrong6a1c38d2019-10-10 09:35:57 +000053 if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC) {
54 if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
55 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8;
56 else
57 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
58 }
Neil Armstrong17d3c8b2019-04-25 15:17:22 +000059
60 if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
61 features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE);
62
Neil Armstrong17d3c8b2019-04-25 15:17:22 +000063 if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
64 features |= AFBC_FORMAT_MOD_TILED;
65
66 if (features)
67 return DRM_FORMAT_MOD_ARM_AFBC(features | AFBC_FORMAT_MOD_YTR);
68
69 return 0;
70}
71#else
72uint64_t MesonImporter::ConvertGrallocFormatToDrmModifiers(
73 uint64_t /* flags */) {
74 return 0;
75}
76#endif
77
78int MesonImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
79 uint64_t modifiers[4] = {0};
80
81 memset(bo, 0, sizeof(hwc_drm_bo_t));
82
83 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
84 handle);
85 if (!hnd)
86 return -EINVAL;
87
88 // We can't import these types of buffers.
89 // These buffers should have been filtered out with CanImportBuffer()
90 if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
91 return -EINVAL;
92
93 uint32_t gem_handle;
94 int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle);
95 if (ret) {
96 ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret);
97 return ret;
98 }
99
Roman Stratiienkof63726c2019-11-06 15:03:12 +0200100 uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
101 if (fmt == DRM_FORMAT_INVALID)
102 return -EINVAL;
Neil Armstrong17d3c8b2019-04-25 15:17:22 +0000103
104 modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format);
105
106 bo->width = hnd->width;
107 bo->height = hnd->height;
108 bo->hal_format = hnd->req_format;
109 bo->format = fmt;
110 bo->usage = hnd->usage;
111 bo->pixel_stride = hnd->stride;
112 bo->pitches[0] = hnd->byte_stride;
113 bo->gem_handles[0] = gem_handle;
114 bo->offsets[0] = 0;
115
116 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
117 bo->format, bo->gem_handles, bo->pitches,
118 bo->offsets, modifiers, &bo->fb_id,
119 modifiers[0] ? DRM_MODE_FB_MODIFIERS : 0);
120
121 if (ret) {
122 ALOGE("could not create drm fb %d", ret);
123 return ret;
124 }
125
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100126 ImportHandle(gem_handle);
127
Neil Armstrong17d3c8b2019-04-25 15:17:22 +0000128 return ret;
129}
130
131bool MesonImporter::CanImportBuffer(buffer_handle_t handle) {
132 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
133 handle);
134 return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB);
135}
136
137std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
138 std::unique_ptr<Planner> planner(new Planner);
139 planner->AddStage<PlanStageGreedy>();
140 return planner;
141}
142} // namespace android