• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/mman.h>
9 #include <time.h>
10 #include <ui/GraphicBuffer.h>
11 #include <ui/GraphicBufferAllocator.h>
12 #include <unistd.h>
13 #include <xf86drm.h>
14 #include <xf86drmMode.h>
15 
16 #include "HostConnection.h"
17 #include "cros_gralloc_handle.h"
18 
19 using namespace android;
20 
21 struct buffer_object {
22   uint32_t width;
23   uint32_t height;
24   uint32_t pitch;
25   uint32_t handle;
26   uint32_t size;
27   uint8_t *vaddr;
28   uint32_t fb_id;
29   const native_handle_t *fb;
30 };
31 
32 struct buffer_object buf;
33 
modeset_create_fb(int fd,struct buffer_object * bo)34 static int modeset_create_fb(int fd, struct buffer_object *bo) {
35   struct drm_mode_create_dumb create = {};
36   struct drm_mode_map_dumb map = {};
37 
38   create.width = bo->width;
39   create.height = bo->height;
40   create.bpp = 32;
41   drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
42   printf("create dumb w %d h %d\n", bo->width, bo->height);
43   getchar();
44 
45   bo->pitch = create.pitch;
46   bo->size = create.size;
47   bo->handle = create.handle;
48   drmModeAddFB(fd, bo->width, bo->height, 24, 32, bo->pitch, bo->handle,
49                &bo->fb_id);
50   printf("drmModeAddFB\n");
51   getchar();
52 
53   map.handle = create.handle;
54   drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
55   printf("map dumb\n");
56   getchar();
57   bo->vaddr = static_cast<unsigned char *>(mmap64(
58       0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, map.offset));
59   memset(bo->vaddr, 0xff, bo->size);
60   return 0;
61 }
62 
modeset_destroy_fb(int fd,struct buffer_object * bo)63 static void modeset_destroy_fb(int fd, struct buffer_object *bo) {
64   struct drm_mode_destroy_dumb destroy = {};
65 
66   drmModeRmFB(fd, bo->fb_id);
67   munmap(bo->vaddr, bo->size);
68   destroy.handle = bo->handle;
69   drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
70 }
71 
get_property_id(int fd,drmModeObjectProperties * props,const char * name)72 static uint32_t get_property_id(int fd, drmModeObjectProperties *props,
73                                 const char *name) {
74   drmModePropertyPtr property;
75   uint32_t i, id = 0;
76 
77   /* find property according to the name */
78   for (i = 0; i < props->count_props; i++) {
79     property = drmModeGetProperty(fd, props->props[i]);
80     if (!strcmp(property->name, name)) id = property->prop_id;
81     drmModeFreeProperty(property);
82     if (id) {
83       break;
84     }
85   }
86   return id;
87 }
88 
89 static std::unique_ptr<HostConnection> sHostCon;
90 
createOrGetHostConnection()91 static HostConnection *createOrGetHostConnection() {
92   if (!sHostCon) {
93     sHostCon = HostConnection::createUnique();
94   }
95   return sHostCon.get();
96 }
97 
98 #define DEFINE_AND_VALIDATE_HOST_CONNECTION                          \
99   HostConnection *hostCon = createOrGetHostConnection();             \
100   if (!hostCon) {                                                    \
101     ALOGE("drmTest: Failed to get host connection\n");               \
102     return;                                                          \
103   }                                                                  \
104   ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder();            \
105   if (!rcEnc) {                                                      \
106     ALOGE("drmTest: Failed to get renderControl encoder context\n"); \
107     return;                                                          \
108   }
109 
110 #include "include/drmhwcgralloc.h"
convertBoInfo(buffer_handle_t handle,hwc_drm_bo_t * bo)111 void convertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
112   cros_gralloc_handle *gr_handle = (cros_gralloc_handle *)handle;
113   if (!gr_handle) return;
114 
115   bo->width = gr_handle->width;
116   bo->height = gr_handle->height;
117   bo->hal_format = gr_handle->droid_format;
118   bo->format = gr_handle->format;
119   bo->usage = gr_handle->usage;
120   bo->prime_fds[0] = gr_handle->fds[0];
121   bo->pitches[0] = gr_handle->strides[0];
122   bo->offsets[0] = gr_handle->offsets[0];
123 }
124 
grallocAllocBuffer(int fd,struct buffer_object * bo)125 void grallocAllocBuffer(int fd, struct buffer_object *bo) {
126   buffer_handle_t h;
127   uint32_t stride;
128 
129   if (GraphicBufferAllocator::get().allocate(
130           bo->width, bo->height, android::PIXEL_FORMAT_RGBA_8888, 1,
131           (GraphicBuffer::USAGE_HW_COMPOSER | GraphicBuffer::USAGE_HW_RENDER),
132           &h, &stride, 0, "emulatorDrmTest") == android::OK) {
133     hwc_drm_bo tmp_bo{};
134     convertBoInfo(h, &tmp_bo);
135 
136     int ret = drmPrimeFDToHandle(fd, tmp_bo.prime_fds[0], tmp_bo.gem_handles);
137     for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
138       tmp_bo.gem_handles[i] = tmp_bo.gem_handles[0];
139     }
140     if (ret) {
141       printf("%s: DRM_IOCTL_PRIME_FD_TO_HANDLE failed: %s (errno %d)\n",
142              __func__, strerror(errno), errno);
143       return;
144     }
145     ret = drmModeAddFB2(fd, tmp_bo.width, tmp_bo.height, tmp_bo.format,
146                         tmp_bo.gem_handles, tmp_bo.pitches, tmp_bo.offsets,
147                         &bo->fb_id, 0);
148 
149     printf("allocate buffer\n");
150     DEFINE_AND_VALIDATE_HOST_CONNECTION
151     bo->fb = static_cast<const native_handle_t *>(h);
152     getchar();
153     printf("resource id is %d\n",
154            hostCon->grallocHelper()->getHostHandle(bo->fb));
155   } else {
156     bo->fb = nullptr;
157   }
158 }
159 
main(int argc,char ** argv)160 int main(int argc, char **argv) {
161   int fd;
162   drmModeConnector *conn;
163   drmModeRes *res;
164   drmModePlaneRes *plane_res = nullptr;
165   uint32_t conn_id;
166   uint32_t crtc_id;
167   uint32_t plane_id;
168 
169   fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
170 
171   int ret = drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 0);
172   if (ret) {
173     printf("fail to set universal plane %d\n", ret);
174   }
175 
176   res = drmModeGetResources(fd);
177   crtc_id = res->crtcs[0];
178   conn_id = res->connectors[0];
179 
180   plane_res = drmModeGetPlaneResources(fd);
181   plane_id = plane_res->planes[0];
182 
183   conn = drmModeGetConnector(fd, conn_id);
184   buf.width = conn->modes[0].hdisplay;
185   buf.height = conn->modes[0].vdisplay;
186   // modeset_create_fb(fd, &buf);
187   grallocAllocBuffer(fd, &buf);
188 
189   drmModeSetCrtc(fd, crtc_id, buf.fb_id, 0, 0, &conn_id, 1, &conn->modes[0]);
190   printf("drmModeSetCrtc\n");
191   getchar();
192 
193   drmModePageFlip(fd, crtc_id, buf.fb_id, DRM_MODE_PAGE_FLIP_EVENT, &crtc_id);
194   printf("drmModePageFlip\n");
195   getchar();
196 
197   //    drmModeSetPlane(fd, plane_id, crtc_id, buf.fb_id, 0, 50, 50, 320, 320,
198   //                    100 << 16, 150 << 16, 320 << 16, 320 << 16);
199   //    printf("drmModeSetPlane\n");
200   //    modeset_destroy_fb(fd, &buf);
201 
202   drmModeFreeConnector(conn);
203   drmModeFreeResources(res);
204   close(fd);
205 
206   return 0;
207 }
208 
main_atom(int argc,char ** argv)209 int main_atom(int argc, char **argv) {
210   int fd;
211   drmModeConnector *conn = nullptr;
212   drmModeRes *res = nullptr;
213   drmModePlaneRes *plane_res = nullptr;
214   drmModeObjectProperties *props = nullptr;
215   drmModeAtomicReq *req;
216   uint32_t conn_id;
217   uint32_t crtc_id;
218   uint32_t plane_id;
219   uint32_t blob_id;
220   uint32_t property_crtc_id;
221   uint32_t property_mode_id;
222   uint32_t property_active;
223 
224   printf("drm available %d\n", drmAvailable());
225   fd = open("/dev/dri/card0", O_RDWR);
226   printf("openg drm fd %d\n", fd);
227 
228   int ret = drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
229   if (ret) {
230     printf("fail to set universal plane %d\n", ret);
231   }
232 
233   ret = drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1);
234   if (ret) {
235     printf("fail to set atomic operation %d\n", ret);
236   }
237 
238   res = drmModeGetResources(fd);
239   if (!res) {
240     printf("error to get drmModeGetResources: %d\n", errno);
241   }
242 
243   crtc_id = res->crtcs[0];
244   conn_id = res->connectors[0];
245   plane_res = drmModeGetPlaneResources(fd);
246   plane_id = plane_res->planes[0];
247 
248   conn = drmModeGetConnector(fd, conn_id);
249   buf.width = conn->modes[0].hdisplay;
250   buf.height = conn->modes[0].vdisplay;
251   modeset_create_fb(fd, &buf);
252 
253   /* get connector properties */
254   props = drmModeObjectGetProperties(fd, conn_id, DRM_MODE_OBJECT_CONNECTOR);
255   property_crtc_id = get_property_id(fd, props, "CRTC_ID");
256   drmModeFreeObjectProperties(props);
257 
258   /* get crtc properties */
259   props = drmModeObjectGetProperties(fd, crtc_id, DRM_MODE_OBJECT_CRTC);
260   property_active = get_property_id(fd, props, "ACTIVE");
261   property_mode_id = get_property_id(fd, props, "MODE_ID");
262   drmModeFreeObjectProperties(props);
263 
264   /* create blob to store current mode, and return the blob id */
265   drmModeCreatePropertyBlob(fd, &conn->modes[0], sizeof(conn->modes[0]),
266                             &blob_id);
267 
268   /* start modeseting */
269   req = drmModeAtomicAlloc();
270   drmModeAtomicAddProperty(req, crtc_id, property_active, 1);
271   drmModeAtomicAddProperty(req, crtc_id, property_mode_id, blob_id);
272   drmModeAtomicAddProperty(req, conn_id, property_crtc_id, crtc_id);
273   drmModeAtomicCommit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
274   drmModeAtomicFree(req);
275   printf("drmModeAtomicCommit SetCrtc\n");
276   getchar();
277 
278   drmModeSetPlane(fd, plane_id, crtc_id, buf.fb_id, 0, 50, 50, 320, 320, 0, 0,
279                   320 << 16, 320 << 16);
280   printf("drmModeSetPlane\n");
281   getchar();
282 
283   modeset_destroy_fb(fd, &buf);
284   drmModeFreeConnector(conn);
285   drmModeFreePlaneResources(plane_res);
286   drmModeFreeResources(res);
287   close(fd);
288 
289   return 0;
290 }
291