• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef DRM_UNIQUE_H_
18 #define DRM_UNIQUE_H_
19 
20 #include <xf86drmMode.h>
21 
22 #include <functional>
23 #include <memory>
24 
25 template <typename T>
26 using DUniquePtr = std::unique_ptr<T, std::function<void(T *)>>;
27 
28 using DrmModeAtomicReqUnique = DUniquePtr<drmModeAtomicReq>;
MakeDrmModeAtomicReqUnique()29 auto inline MakeDrmModeAtomicReqUnique() {
30   return DrmModeAtomicReqUnique(drmModeAtomicAlloc(), [](drmModeAtomicReq *it) {
31     drmModeAtomicFree(it);
32   });
33 };
34 
35 using DrmModeConnectorUnique = DUniquePtr<drmModeConnector>;
MakeDrmModeConnectorUnique(int fd,uint32_t connector_id)36 auto inline MakeDrmModeConnectorUnique(int fd, uint32_t connector_id) {
37   return DrmModeConnectorUnique(drmModeGetConnector(fd, connector_id),
38                                 [](drmModeConnector *it) {
39                                   drmModeFreeConnector(it);
40                                 });
41 }
42 
43 using DrmModeCrtcUnique = DUniquePtr<drmModeCrtc>;
MakeDrmModeCrtcUnique(int fd,uint32_t crtc_id)44 auto inline MakeDrmModeCrtcUnique(int fd, uint32_t crtc_id) {
45   return DrmModeCrtcUnique(drmModeGetCrtc(fd, crtc_id),
46                            [](drmModeCrtc *it) { drmModeFreeCrtc(it); });
47 }
48 
49 using DrmModeEncoderUnique = DUniquePtr<drmModeEncoder>;
MakeDrmModeEncoderUnique(int fd,uint32_t encoder_id)50 auto inline MakeDrmModeEncoderUnique(int fd, uint32_t encoder_id) {
51   return DrmModeEncoderUnique(drmModeGetEncoder(fd, encoder_id),
52                               [](drmModeEncoder *it) {
53                                 drmModeFreeEncoder(it);
54                               });
55 }
56 
57 using DrmModePlaneUnique = DUniquePtr<drmModePlane>;
MakeDrmModePlaneUnique(int fd,uint32_t plane_id)58 auto inline MakeDrmModePlaneUnique(int fd, uint32_t plane_id) {
59   return DrmModePlaneUnique(drmModeGetPlane(fd, plane_id),
60                             [](drmModePlane *it) { drmModeFreePlane(it); });
61 }
62 
63 using DrmModePlaneResUnique = DUniquePtr<drmModePlaneRes>;
MakeDrmModePlaneResUnique(int fd)64 auto inline MakeDrmModePlaneResUnique(int fd) {
65   return DrmModePlaneResUnique(drmModeGetPlaneResources(fd),
66                                [](drmModePlaneRes *it) {
67                                  drmModeFreePlaneResources(it);
68                                });
69 }
70 
71 using DrmModeUserPropertyBlobUnique = DUniquePtr<uint32_t /*id*/>;
72 
73 using DrmModePropertyBlobUnique = DUniquePtr<drmModePropertyBlobRes>;
MakeDrmModePropertyBlobUnique(int fd,uint32_t blob_id)74 auto inline MakeDrmModePropertyBlobUnique(int fd, uint32_t blob_id) {
75   return DrmModePropertyBlobUnique(drmModeGetPropertyBlob(fd, blob_id),
76                                    [](drmModePropertyBlobRes *it) {
77                                      drmModeFreePropertyBlob(it);
78                                    });
79 }
80 
81 using DrmModeResUnique = DUniquePtr<drmModeRes>;
MakeDrmModeResUnique(int fd)82 auto inline MakeDrmModeResUnique(int fd) {
83   return DrmModeResUnique(drmModeGetResources(fd),
84                           [](drmModeRes *it) { drmModeFreeResources(it); });
85 }
86 
87 #endif
88