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