platformdrmgeneric.cpp 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright (C) 2015 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.
 */

17
#define LOG_TAG "hwc-platform-drm-generic"
18

19
#include "platformdrmgeneric.h"
20
#include "drmdevice.h"
21
#include "platform.h"
22

23
24
#include <xf86drm.h>
#include <xf86drmMode.h>
25

26
#include <cutils/properties.h>
27
#include <gralloc_handle.h>
28
#include <hardware/gralloc.h>
29
#include <log/log.h>
30

31
namespace android {
32

33
34
#ifdef USE_DRM_GENERIC_IMPORTER
// static
35
Importer *Importer::CreateInstance(DrmDevice *drm) {
36
37
38
  DrmGenericImporter *importer = new DrmGenericImporter(drm);
  if (!importer)
    return NULL;
39

40
41
42
43
44
  int ret = importer->Init();
  if (ret) {
    ALOGE("Failed to initialize the nv importer %d", ret);
    delete importer;
    return NULL;
45
  }
46
47
48
49
  return importer;
}
#endif

50
51
DrmGenericImporter::DrmGenericImporter(DrmDevice *drm)
    : drm_(drm), exclude_non_hwfb_(false) {
52
53
54
55
}

DrmGenericImporter::~DrmGenericImporter() {
}
56

57
int DrmGenericImporter::Init() {
58
  int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
59
                          (const hw_module_t **)&gralloc_);
60
61
62
63
  if (ret) {
    ALOGE("Failed to open gralloc module");
    return ret;
  }
64
65
66
67

  ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
        gralloc_->common.author);

68
  char exclude_non_hwfb_prop[PROPERTY_VALUE_MAX];
69
70
  property_get("vendor.hwc.drm.exclude_non_hwfb_imports", exclude_non_hwfb_prop,
               "0");
71
72
  exclude_non_hwfb_ = static_cast<bool>(strncmp(exclude_non_hwfb_prop, "0", 1));

73
  return 0;
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
enum chroma_order {
  YCbCr,
  YCrCb,
};

struct droid_yuv_format {
  /* Lookup keys */
  int native;                     /* HAL_PIXEL_FORMAT_ */
  enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
  int chroma_step; /* Distance in bytes between subsequent chroma pixels. */

  /* Result */
  int fourcc; /* DRM_FORMAT_ */
};

/* The following table is used to look up a DRI image FourCC based
 * on native format and information contained in android_ycbcr struct. */
static const struct droid_yuv_format droid_yuv_formats[] = {
    /* Native format, YCrCb, Chroma step, DRI image FourCC */
    {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12},
    {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420},
    {HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420},
    {HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420},
    /* HACK: See droid_create_image_from_prime_fds() and
     * https://issuetracker.google.com/32077885. */
    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12},
    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420},
    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420},
    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV},
    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888},
};

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

static int get_fourcc_yuv(int native, enum chroma_order chroma_order,
                          int chroma_step) {
  for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
    if (droid_yuv_formats[i].native == native &&
        droid_yuv_formats[i].chroma_order == chroma_order &&
        droid_yuv_formats[i].chroma_step == chroma_step)
      return droid_yuv_formats[i].fourcc;

  return -1;
}

static bool is_yuv(int native) {
  for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
    if (droid_yuv_formats[i].native == native)
      return true;

  return false;
}

bool DrmGenericImporter::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
                                         hwc_drm_bo_t *bo) {
  struct android_ycbcr ycbcr;
  enum chroma_order chroma_order;
  int ret;

  if (!gralloc_->lock_ycbcr) {
    static std::once_flag once;
    std::call_once(once,
                   []() { ALOGW("Gralloc does not support lock_ycbcr()"); });
    return false;
  }

  memset(&ycbcr, 0, sizeof(ycbcr));
  ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr);
  if (ret) {
    ALOGW("gralloc->lock_ycbcr failed: %d", ret);
    return false;
  }
  gralloc_->unlock(gralloc_, handle);

  /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
   * it will return the .y/.cb/.cr pointers based on a NULL pointer,
   * so they can be interpreted as offsets. */
  bo->offsets[0] = (size_t)ycbcr.y;
  /* We assume here that all the planes are located in one DMA-buf. */
  if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
    chroma_order = YCrCb;
    bo->offsets[1] = (size_t)ycbcr.cr;
    bo->offsets[2] = (size_t)ycbcr.cb;
  } else {
    chroma_order = YCbCr;
    bo->offsets[1] = (size_t)ycbcr.cb;
    bo->offsets[2] = (size_t)ycbcr.cr;
  }

  /* .ystride is the line length (in bytes) of the Y plane,
   * .cstride is the line length (in bytes) of any of the remaining
   * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
   * planar formats. */
  bo->pitches[0] = ycbcr.ystride;
  bo->pitches[1] = bo->pitches[2] = ycbcr.cstride;

  /* .chroma_step is the byte distance between the same chroma channel
   * values of subsequent pixels, assumed to be the same for Cb and Cr. */
  bo->format = get_fourcc_yuv(bo->hal_format, chroma_order, ycbcr.chroma_step);
  if (bo->format == -1) {
    ALOGW(
        "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
        "%d",
        bo->hal_format, chroma_order == YCbCr ? "YCbCr" : "YCrCb",
        (int)ycbcr.chroma_step);
    return false;
  }

  /*
   * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
   * the single-fd case cannot happen.  So handle eithe single
   * fd or fd-per-plane case:
   */
  if (num_fds == 1) {
    bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0];
  } else {
    int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
    if (num_fds != expected_planes)
      return false;
  }

  return true;
}

200
uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  switch (hal_format) {
    case HAL_PIXEL_FORMAT_RGB_888:
      return DRM_FORMAT_BGR888;
    case HAL_PIXEL_FORMAT_BGRA_8888:
      return DRM_FORMAT_ARGB8888;
    case HAL_PIXEL_FORMAT_RGBX_8888:
      return DRM_FORMAT_XBGR8888;
    case HAL_PIXEL_FORMAT_RGBA_8888:
      return DRM_FORMAT_ABGR8888;
    case HAL_PIXEL_FORMAT_RGB_565:
      return DRM_FORMAT_BGR565;
    case HAL_PIXEL_FORMAT_YV12:
      return DRM_FORMAT_YVU420;
    default:
      ALOGE("Cannot convert hal format to drm format %u", hal_format);
216
      return DRM_FORMAT_INVALID;
217
  }
218
219
}

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
uint32_t DrmGenericImporter::DrmFormatToBitsPerPixel(uint32_t drm_format) {
  switch (drm_format) {
    case DRM_FORMAT_ARGB8888:
    case DRM_FORMAT_XBGR8888:
    case DRM_FORMAT_ABGR8888:
      return 32;
    case DRM_FORMAT_BGR888:
      return 24;
    case DRM_FORMAT_BGR565:
      return 16;
    case DRM_FORMAT_YVU420:
      return 12;
    default:
      ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format);
      return 32;
  }
}

238
239
int DrmGenericImporter::ConvertBoInfo(buffer_handle_t handle,
                                      hwc_drm_bo_t *bo) {
240
  gralloc_handle_t *gr_handle = gralloc_handle(handle);
241
242
243
244
245
  if (!gr_handle)
    return -EINVAL;

  bo->width = gr_handle->width;
  bo->height = gr_handle->height;
246
  bo->hal_format = gr_handle->format;
247
248
249
250
251
252
253
254
255
256

#if GRALLOC_HANDLE_VERSION < 4
#warning libdrm >= v2.4.97 is required to support modifiers
#endif
#if GRALLOC_HANDLE_VERSION == 4
  bo->modifiers[0] = gr_handle->modifier;
  bo->with_modifiers = gr_handle->modifier != DRM_FORMAT_MOD_NONE &&
                       gr_handle->modifier != DRM_FORMAT_MOD_INVALID;
#endif

257
  bo->usage = gr_handle->usage;
258
259
260
261
262
263
264
265
266
267
268
269
270
  bo->prime_fds[0] = gr_handle->prime_fd;

  if (is_yuv(gr_handle->format)) {
    if (!GetYuvPlaneInfo(handle->numFds, handle, bo))
      return -EINVAL;
  } else {
    bo->pitches[0] = gr_handle->stride;
    bo->offsets[0] = 0;
    bo->format = ConvertHalFormatToDrm(gr_handle->format);
    if (bo->format == DRM_FORMAT_INVALID)
      return -EINVAL;
  }

271
272
  bo->pixel_stride = (gr_handle->stride * 8) /
                     DrmFormatToBitsPerPixel(bo->format);
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  return 0;
}

int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
  memset(bo, 0, sizeof(hwc_drm_bo_t));

  int ret = ConvertBoInfo(handle, bo);
  if (ret)
    return ret;

  ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &bo->gem_handles[0]);
  if (ret) {
    ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
    return ret;
  }

  for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
    int fd = bo->prime_fds[i];
    if (fd != 0) {
      if (fd != bo->prime_fds[0]) {
        ALOGE("Multiplanar FBs are not supported by this version of composer");
        return -ENOTSUP;
      }
      bo->gem_handles[i] = bo->gem_handles[0];
    }
  }

  if (!bo->with_modifiers)
    ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
                        bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
                        0);
  else
    ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
                                     bo->format, bo->gem_handles, bo->pitches,
                                     bo->offsets, bo->modifiers, &bo->fb_id,
                                     bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
                                                      : 0);

312
313
314
315
316
  if (ret) {
    ALOGE("could not create drm fb %d", ret);
    return ret;
  }

317
  ImportHandle(bo->gem_handles[0]);
318

319
  return ret;
320
321
}

322
int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
323
  if (bo->fb_id)
324
    if (drmModeRmFB(drm_->fd(), bo->fb_id))
325
      ALOGE("Failed to rm fb");
326

327
  for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
328
329
330
    if (!bo->gem_handles[i])
      continue;

331
332
    if (ReleaseHandle(bo->gem_handles[i])) {
      ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
333
    } else {
334
      for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
335
336
        if (bo->gem_handles[j] == bo->gem_handles[i])
          bo->gem_handles[j] = 0;
337
      bo->gem_handles[i] = 0;
338
    }
339
340
341
  }
  return 0;
}
342

343
bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
344
345
346
347
  hwc_drm_bo_t bo;

  int ret = ConvertBoInfo(handle, &bo);
  if (ret)
348
    return false;
349

350
351
352
353
354
  if (bo.prime_fds[0] == 0)
    return false;

  if (exclude_non_hwfb_ && !(bo.usage & GRALLOC_USAGE_HW_FB))
    return false;
355

356
357
358
  return true;
}

359
#ifdef USE_DRM_GENERIC_IMPORTER
360
std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
361
362
363
364
365
  std::unique_ptr<Planner> planner(new Planner);
  planner->AddStage<PlanStageGreedy>();
  return planner;
}
#endif
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
  gem_refcount_[gem_handle]++;

  return 0;
}

int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
  if (--gem_refcount_[gem_handle])
    return 0;

  gem_refcount_.erase(gem_handle);

  return CloseHandle(gem_handle);
}

int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
  struct drm_gem_close gem_close;

  memset(&gem_close, 0, sizeof(gem_close));

  gem_close.handle = gem_handle;
  int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
  if (ret)
    ALOGE("Failed to close gem handle %d %d", gem_handle, ret);

  return ret;
}
394
}