-
Notifications
You must be signed in to change notification settings - Fork 0
/
platformia.cpp
290 lines (254 loc) · 8.86 KB
/
platformia.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.
*/
#define LOG_TAG "hwc-platform-IA"
#include <drm/drm_fourcc.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <cutils/log.h>
#include "platformia.h"
namespace android {
#define ALIGN(val, align) (((val) + (align)-1) & ~((align)-1))
#ifdef USE_IA_PLANNER
// static
Importer *Importer::CreateInstance(DrmResources *drm) {
IAImporter *importer = new IAImporter(drm);
if (!importer)
return NULL;
int ret = importer->Init();
if (ret) {
ALOGE("Failed to initialize the IA importer %d", ret);
delete importer;
return NULL;
}
return importer;
}
#endif
IAImporter::IAImporter(DrmResources *drm) : drm_(drm) {
}
IAImporter::~IAImporter() {
}
int IAImporter::Init() {
int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
(const hw_module_t **)&gralloc_);
if (ret) {
ALOGE("Failed to open gralloc module");
return ret;
}
return 0;
}
uint32_t IAImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
switch (hal_format) {
case HAL_PIXEL_FORMAT_RGBA_8888:
return DRM_FORMAT_ABGR8888;
case HAL_PIXEL_FORMAT_RGBX_8888:
return DRM_FORMAT_XBGR8888;
case HAL_PIXEL_FORMAT_RGB_888:
return DRM_FORMAT_BGR888;
case HAL_PIXEL_FORMAT_BGRA_8888:
return DRM_FORMAT_ARGB8888;
case HAL_PIXEL_FORMAT_RGB_565:
return DRM_FORMAT_RGB565;
case HAL_PIXEL_FORMAT_YV12:
case HAL_PIXEL_FORMAT_YCbCr_420_888:
return DRM_FORMAT_YUV420;
case HAL_PIXEL_FORMAT_YCbCr_422_I:
return DRM_FORMAT_YUYV;
case HAL_PIXEL_FORMAT_YCbCr_422_SP:
return DRM_FORMAT_NV16;
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
return DRM_FORMAT_NV21;
case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
return DRM_FORMAT_XBGR8888;
default:
ALOGI("Unknown HAL Format 0x%x", hal_format);
return 0;
}
}
uint32_t IAImporter::GetFormatForFrameBuffer(uint32_t fourcc_format,
uint32_t plane_type) {
if (plane_type == DRM_PLANE_TYPE_CURSOR)
return DRM_FORMAT_ARGB8888;
if (plane_type != DRM_PLANE_TYPE_PRIMARY)
return fourcc_format;
// We only support 24 bit colordepth for primary planes on
// pre SKL Hardware. Ideally, we query format support from
// plane to determine this.
switch (fourcc_format) {
case DRM_FORMAT_ABGR8888:
return DRM_FORMAT_XBGR8888;
case DRM_FORMAT_ARGB8888:
return DRM_FORMAT_XRGB8888;
default:
break;
}
return fourcc_format;
}
EGLImageKHR IAImporter::ImportImage(EGLDisplay egl_display,
DrmHwcBuffer *buffer,
buffer_handle_t handle) {
cros_gralloc_handle *gr_handle = (struct cros_gralloc_handle *) handle;
EGLImageKHR image = EGL_NO_IMAGE_KHR;
// Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
// target, the EGL will take a reference to the dma_buf.
if (buffer->operator->()->format == DRM_FORMAT_YUV420) {
const EGLint attr_list_yv12[] = {
EGL_WIDTH,
static_cast<EGLint>(buffer->operator->()->width),
EGL_HEIGHT,
static_cast<EGLint>(buffer->operator->()->height),
EGL_LINUX_DRM_FOURCC_EXT,
DRM_FORMAT_YUV420,
EGL_DMA_BUF_PLANE0_FD_EXT,
gr_handle->data.fds[0],
EGL_DMA_BUF_PLANE0_PITCH_EXT,
static_cast<EGLint>(buffer->operator->()->pitches[0]),
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
static_cast<EGLint>(buffer->operator->()->offsets[0]),
EGL_DMA_BUF_PLANE1_FD_EXT,
gr_handle->data.fds[0],
EGL_DMA_BUF_PLANE1_PITCH_EXT,
static_cast<EGLint>(buffer->operator->()->pitches[1]),
EGL_DMA_BUF_PLANE1_OFFSET_EXT,
static_cast<EGLint>(buffer->operator->()->offsets[1]),
EGL_DMA_BUF_PLANE2_FD_EXT,
gr_handle->data.fds[0],
EGL_DMA_BUF_PLANE2_PITCH_EXT,
static_cast<EGLint>(buffer->operator->()->pitches[2]),
EGL_DMA_BUF_PLANE2_OFFSET_EXT,
static_cast<EGLint>(buffer->operator->()->offsets[2]),
EGL_NONE,
0};
image = eglCreateImageKHR(
egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT,
static_cast<EGLClientBuffer>(nullptr), attr_list_yv12);
} else {
const EGLint attr_list[] = {
EGL_WIDTH,
static_cast<EGLint>(buffer->operator->()->width),
EGL_HEIGHT,
static_cast<EGLint>(buffer->operator->()->height),
EGL_LINUX_DRM_FOURCC_EXT,
static_cast<EGLint>(buffer->operator->()->format),
EGL_DMA_BUF_PLANE0_FD_EXT,
gr_handle->data.fds[0],
EGL_DMA_BUF_PLANE0_PITCH_EXT,
static_cast<EGLint>(buffer->operator->()->pitches[0]),
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
0,
EGL_NONE,
0};
image =
eglCreateImageKHR(egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT,
static_cast<EGLClientBuffer>(nullptr), attr_list);
}
return image;
}
int IAImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
cros_gralloc_handle *gr_handle = (struct cros_gralloc_handle *) handle;
if (!gr_handle)
return -EINVAL;
uint32_t gem_handle;
int ret = drmPrimeFDToHandle(drm_->fd(), gr_handle->data.fds[0], &gem_handle);
if (ret) {
ALOGE("Failed to import prime fd %d ret=%d", gr_handle->data.fds[0], ret);
return ret;
}
memset(bo, 0, sizeof(hwc_drm_bo_t));
bo->width = gr_handle->data.width;
bo->height = gr_handle->data.height;
bo->format = ConvertHalFormatToDrm(gr_handle->data.format);
bo->pitches[0] = gr_handle->data.strides[0];
bo->gem_handles[0] = gem_handle;
bo->offsets[0] = 0;
if (bo->format == DRM_FORMAT_YUV420) {
bo->pitches[1] = ALIGN(bo->pitches[0] / 2, 16);
bo->pitches[2] = ALIGN(bo->pitches[0] / 2, 16);
bo->offsets[2] = bo->offsets[0] + bo->pitches[0] * bo->height;
bo->offsets[1] = bo->offsets[2] + bo->pitches[2] * bo->height / 2;
}
return 0;
}
int IAImporter::CreateFrameBuffer(hwc_drm_bo_t *bo, uint32_t plane_type) {
uint32_t format = GetFormatForFrameBuffer(bo->format, plane_type);
uint32_t width = bo->width;
uint32_t height = bo->height;
if (plane_type == DRM_PLANE_TYPE_CURSOR) {
width = 256;
height = 256;
}
int ret = drmModeAddFB2(drm_->fd(), width, height, format, bo->gem_handles,
bo->pitches, bo->offsets, &bo->fb_id, 0);
if (ret) {
ALOGE("drmModeAddFB2 error (%dx%d, %c%c%c%c, handle %d pitch %d) (%s)",
bo->width, bo->height, format, format >> 8,
format >> 16, format >> 24, bo->gem_handles[0], bo->pitches[0],
strerror(-ret));
}
return ret;
}
int IAImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
if (bo->fb_id)
if (drmModeRmFB(drm_->fd(), bo->fb_id))
ALOGE("Failed to rm fb");
struct drm_gem_close gem_close;
memset(&gem_close, 0, sizeof(gem_close));
int num_gem_handles = sizeof(bo->gem_handles) / sizeof(bo->gem_handles[0]);
for (int i = 0; i < num_gem_handles; i++) {
if (!bo->gem_handles[i])
continue;
gem_close.handle = bo->gem_handles[i];
int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
if (ret)
ALOGE("Failed to close gem handle %d %d", i, ret);
else
bo->gem_handles[i] = 0;
}
return 0;
}
int PlanStagePrimary::ProvisionPlanes(
std::vector<DrmCompositionPlane> *composition,
std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
std::vector<DrmPlane *> *planes) {
// Ensure we always have a valid primary plane. On some platforms vblank is
// tied to primary and whole pipe can get disabled in case primary plane is
// disabled.
DrmPlane *primary_plane = NULL;
for (auto i = planes->begin(); i != planes->end(); ++i) {
if ((*i)->type() == DRM_PLANE_TYPE_PRIMARY) {
primary_plane = *i;
planes->erase(i);
break;
}
}
// If we dont have a free primary plane, than its being used as the precomp
// plane.
if (!primary_plane)
return 0;
auto precomp = GetPrecompIter(composition);
composition->emplace(precomp, DrmCompositionPlane::Type::kLayer,
primary_plane, crtc, layers.begin()->first);
layers.erase(layers.begin());
return 0;
}
#ifdef USE_IA_PLANNER
std::unique_ptr<Planner> Planner::CreateInstance(DrmResources *) {
std::unique_ptr<Planner> planner(new Planner);
planner->AddStage<PlanStagePrimary>();
planner->AddStage<PlanStageGreedy>();
return planner;
}
#endif
}