aboutsummaryrefslogtreecommitdiff
path: root/bufferinfo/legacy/BufferInfoMinigbm.cpp
blob: 777c2b72af7b3d5447f1ee817e7e6c73de6ce48a (plain)
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
/*
 * Copyright (C) 2018 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-bufferinfo-minigbm"

#include "BufferInfoMinigbm.h"

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

#include <cerrno>
#include <cstring>

#include "utils/log.h"
namespace android {

LEGACY_BUFFER_INFO_GETTER(BufferInfoMinigbm);

constexpr int CROS_GRALLOC_DRM_GET_FORMAT = 1;
constexpr int CROS_GRALLOC_DRM_GET_DIMENSIONS = 2;
constexpr int CROS_GRALLOC_DRM_GET_BUFFER_INFO = 4;
constexpr int CROS_GRALLOC_DRM_GET_USAGE = 5;

struct cros_gralloc0_buffer_info {
  uint32_t drm_fourcc;
  int num_fds;
  int fds[4];
  uint64_t modifier;
  int offset[4];
  int stride[4];
};

int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
  if (handle == nullptr) {
    return -EINVAL;
  }

  uint32_t width{};
  uint32_t height{};
  if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_DIMENSIONS, handle,
                        &width, &height) != 0) {
    ALOGE(
        "CROS_GRALLOC_DRM_GET_DIMENSIONS operation has failed. "
        "Please ensure you are using the latest minigbm.");
    return -EINVAL;
  }

  int32_t droid_format{};
  if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_FORMAT, handle,
                        &droid_format) != 0) {
    ALOGE(
        "CROS_GRALLOC_DRM_GET_FORMAT operation has failed. "
        "Please ensure you are using the latest minigbm.");
    return -EINVAL;
  }

  uint32_t usage{};
  if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_USAGE, handle, &usage) !=
      0) {
    ALOGE(
        "CROS_GRALLOC_DRM_GET_USAGE operation has failed. "
        "Please ensure you are using the latest minigbm.");
    return -EINVAL;
  }

  struct cros_gralloc0_buffer_info info {};
  if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_BUFFER_INFO, handle,
                        &info) != 0) {
    ALOGE(
        "CROS_GRALLOC_DRM_GET_BUFFER_INFO operation has failed. "
        "Please ensure you are using the latest minigbm.");
    return -EINVAL;
  }

  bo->width = width;
  bo->height = height;

  bo->hal_format = droid_format;

  bo->format = info.drm_fourcc;
  bo->usage = usage;

  for (int i = 0; i < info.num_fds; i++) {
    bo->modifiers[i] = info.modifier;
    bo->prime_fds[i] = info.fds[i];
    bo->pitches[i] = info.stride[i];
    bo->offsets[i] = info.offset[i];
  }

  return 0;
}

constexpr char cros_gralloc_module_name[] = "CrOS Gralloc";

int BufferInfoMinigbm::ValidateGralloc() {
  if (strcmp(gralloc_->common.name, cros_gralloc_module_name) != 0) {
    ALOGE("Gralloc name isn't valid: Expected: \"%s\", Actual: \"%s\"",
          cros_gralloc_module_name, gralloc_->common.name);
    return -EINVAL;
  }

  if (gralloc_->perform == nullptr) {
    ALOGE(
        "CrOS gralloc has no perform call implemented. Please upgrade your "
        "minigbm.");
    return -EINVAL;
  }

  return 0;
}

}  // namespace android