1 #ifndef ANDROID_DVR_DELETER_H_
2 #define ANDROID_DVR_DELETER_H_
3 
4 #include <sys/cdefs.h>
5 
6 #include <memory>
7 
8 // Header-only C++ helper to delete opaque DVR objects.
9 
10 __BEGIN_DECLS
11 
12 // Use forward declarations to avoid dependency on other headers.
13 typedef struct DvrBuffer DvrBuffer;
14 typedef struct DvrReadBuffer DvrReadBuffer;
15 typedef struct DvrWriteBuffer DvrWriteBuffer;
16 typedef struct DvrReadBufferQueue DvrReadBufferQueue;
17 typedef struct DvrWriteBufferQueue DvrWriteBufferQueue;
18 typedef struct DvrDisplayManager DvrDisplayManager;
19 typedef struct DvrSurfaceState DvrSurfaceState;
20 typedef struct DvrSurface DvrSurface;
21 typedef struct DvrHwcClient DvrHwcClient;
22 typedef struct DvrHwcFrame DvrHwcFrame;
23 
24 void dvrBufferDestroy(DvrBuffer* buffer);
25 void dvrReadBufferDestroy(DvrReadBuffer* read_buffer);
26 void dvrWriteBufferDestroy(DvrWriteBuffer* write_buffer);
27 void dvrReadBufferQueueDestroy(DvrReadBufferQueue* read_queue);
28 void dvrWriteBufferQueueDestroy(DvrWriteBufferQueue* write_queue);
29 void dvrDisplayManagerDestroy(DvrDisplayManager* client);
30 void dvrSurfaceStateDestroy(DvrSurfaceState* surface_state);
31 void dvrSurfaceDestroy(DvrSurface* surface);
32 void dvrHwcClientDestroy(DvrHwcClient* client);
33 void dvrHwcFrameDestroy(DvrHwcFrame* frame);
34 
35 __END_DECLS
36 
37 // Avoid errors if this header is included in C code.
38 #if defined(__cplusplus)
39 
40 namespace android {
41 namespace dvr {
42 
43 // Universal DVR object deleter. May be passed to smart pointer types to handle
44 // deletion of DVR API objects.
45 struct DvrObjectDeleter {
operatorDvrObjectDeleter46   void operator()(DvrBuffer* p) { dvrBufferDestroy(p); }
operatorDvrObjectDeleter47   void operator()(DvrReadBuffer* p) { dvrReadBufferDestroy(p); }
operatorDvrObjectDeleter48   void operator()(DvrWriteBuffer* p) { dvrWriteBufferDestroy(p); }
operatorDvrObjectDeleter49   void operator()(DvrReadBufferQueue* p) { dvrReadBufferQueueDestroy(p); }
operatorDvrObjectDeleter50   void operator()(DvrWriteBufferQueue* p) { dvrWriteBufferQueueDestroy(p); }
operatorDvrObjectDeleter51   void operator()(DvrDisplayManager* p) { dvrDisplayManagerDestroy(p); }
operatorDvrObjectDeleter52   void operator()(DvrSurfaceState* p) { dvrSurfaceStateDestroy(p); }
operatorDvrObjectDeleter53   void operator()(DvrSurface* p) { dvrSurfaceDestroy(p); }
operatorDvrObjectDeleter54   void operator()(DvrHwcClient* p) { dvrHwcClientDestroy(p); }
operatorDvrObjectDeleter55   void operator()(DvrHwcFrame* p) { dvrHwcFrameDestroy(p); }
56 };
57 
58 // Helper to define unique pointers for DVR object types.
59 template <typename T>
60 using MakeUniqueDvrPointer = std::unique_ptr<T, DvrObjectDeleter>;
61 
62 // Unique pointer types for DVR objects.
63 using UniqueDvrBuffer = MakeUniqueDvrPointer<DvrBuffer>;
64 using UniqueDvrReadBuffer = MakeUniqueDvrPointer<DvrReadBuffer>;
65 using UniqueDvrWriteBuffer = MakeUniqueDvrPointer<DvrWriteBuffer>;
66 using UniqueDvrReadBufferQueue = MakeUniqueDvrPointer<DvrReadBufferQueue>;
67 using UniqueDvrWriteBufferQueue = MakeUniqueDvrPointer<DvrWriteBufferQueue>;
68 using UniqueDvrDisplayManager = MakeUniqueDvrPointer<DvrDisplayManager>;
69 using UniqueDvrSurfaceState = MakeUniqueDvrPointer<DvrSurfaceState>;
70 using UniqueDvrSurface = MakeUniqueDvrPointer<DvrSurface>;
71 using UniqueDvrHwcClient = MakeUniqueDvrPointer<DvrHwcClient>;
72 using UniqueDvrHwcFrame = MakeUniqueDvrPointer<DvrHwcFrame>;
73 
74 // TODO(eieio): Add an adapter for std::shared_ptr that injects the deleter into
75 // the relevant constructors.
76 
77 }  // namespace dvr
78 }  // namespace android
79 
80 #endif // defined(__cplusplus)
81 
82 #endif  // ANDROID_DVR_DELETER_H_
83