blob: 4d248bf6c7768d34a1c52b34fb0a879b1634b1fc [file] [log] [blame]
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001/*
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
25template <typename T>
26using DUniquePtr = std::unique_ptr<T, std::function<void(T *)>>;
27
28using DrmModeAtomicReqUnique = DUniquePtr<drmModeAtomicReq>;
29auto inline MakeDrmModeAtomicReqUnique() {
30 return DrmModeAtomicReqUnique(drmModeAtomicAlloc(), [](drmModeAtomicReq *it) {
31 drmModeAtomicFree(it);
32 });
33};
34
35using DrmModeConnectorUnique = DUniquePtr<drmModeConnector>;
36auto inline MakeDrmModeConnectorUnique(int fd, uint32_t connector_id) {
37 return DrmModeConnectorUnique(drmModeGetConnector(fd, connector_id),
38 [](drmModeConnector *it) {
39 drmModeFreeConnector(it);
40 });
41}
42
43using DrmModeCrtcUnique = DUniquePtr<drmModeCrtc>;
44auto inline MakeDrmModeCrtcUnique(int fd, uint32_t crtc_id) {
45 return DrmModeCrtcUnique(drmModeGetCrtc(fd, crtc_id),
46 [](drmModeCrtc *it) { drmModeFreeCrtc(it); });
47}
48
49using DrmModeEncoderUnique = DUniquePtr<drmModeEncoder>;
50auto inline MakeDrmModeEncoderUnique(int fd, uint32_t encoder_id) {
51 return DrmModeEncoderUnique(drmModeGetEncoder(fd, encoder_id),
52 [](drmModeEncoder *it) {
53 drmModeFreeEncoder(it);
54 });
55}
56
57using DrmModePlaneUnique = DUniquePtr<drmModePlane>;
58auto inline MakeDrmModePlaneUnique(int fd, uint32_t plane_id) {
59 return DrmModePlaneUnique(drmModeGetPlane(fd, plane_id),
60 [](drmModePlane *it) { drmModeFreePlane(it); });
61}
62
63using DrmModePlaneResUnique = DUniquePtr<drmModePlaneRes>;
64auto inline MakeDrmModePlaneResUnique(int fd) {
65 return DrmModePlaneResUnique(drmModeGetPlaneResources(fd),
66 [](drmModePlaneRes *it) {
67 drmModeFreePlaneResources(it);
68 });
69}
70
71using DrmModePropertyBlobUnique = DUniquePtr<drmModePropertyBlobRes>;
72auto inline MakeDrmModePropertyBlobUnique(int fd, uint32_t blob_id) {
73 return DrmModePropertyBlobUnique(drmModeGetPropertyBlob(fd, blob_id),
74 [](drmModePropertyBlobRes *it) {
75 drmModeFreePropertyBlob(it);
76 });
77}
78
79using DrmModeResUnique = DUniquePtr<drmModeRes>;
80auto inline MakeDrmModeResUnique(int fd) {
81 return DrmModeResUnique(drmModeGetResources(fd),
82 [](drmModeRes *it) { drmModeFreeResources(it); });
83}
84
85#endif