-VAResources get_va_resources(unsigned width, unsigned height)
-{
- {
- lock_guard<mutex> lock(va_resources_mutex);
- for (auto it = va_resources_freelist.begin(); it != va_resources_freelist.end(); ++it) {
- if (it->width == width && it->height == height) {
- VAResources ret = *it;
- va_resources_freelist.erase(it);
- return ret;
- }
- }
- }
-
- VAResources ret;
-
- ret.width = width;
- ret.height = height;
-
- VAStatus va_status = vaCreateSurfaces(va_dpy->va_dpy, VA_RT_FORMAT_YUV422,
- width, height,
- &ret.surface, 1, nullptr, 0);
- CHECK_VASTATUS(va_status, "vaCreateSurfaces");
-
- va_status = vaCreateContext(va_dpy->va_dpy, config_id, width, height, 0, &ret.surface, 1, &ret.context);
- CHECK_VASTATUS(va_status, "vaCreateContext");
-
- va_status = vaCreateImage(va_dpy->va_dpy, &uyvy_format, width, height, &ret.image);
- CHECK_VASTATUS(va_status, "vaCreateImage");
-
- return ret;
-}
-
-void release_va_resources(VAResources resources)
-{
- lock_guard<mutex> lock(va_resources_mutex);
- if (va_resources_freelist.size() > 10) {
- auto it = va_resources_freelist.end();
- --it;
-
- VAStatus va_status = vaDestroyImage(va_dpy->va_dpy, it->image.image_id);
- CHECK_VASTATUS(va_status, "vaDestroyImage");
-
- va_status = vaDestroyContext(va_dpy->va_dpy, it->context);
- CHECK_VASTATUS(va_status, "vaDestroyContext");
-
- va_status = vaDestroySurfaces(va_dpy->va_dpy, &it->surface, 1);
- CHECK_VASTATUS(va_status, "vaDestroySurfaces");
-
- va_resources_freelist.erase(it);
- }
-
- va_resources_freelist.push_front(resources);
-}
-
-// RAII wrapper to release VAResources on return (even on error).
-class ReleaseVAResources {
-public:
- ReleaseVAResources(const VAResources &resources)
- : resources(resources) {}
- ~ReleaseVAResources()
- {
- if (!committed) {
- release_va_resources(resources);
- }
- }
-
- void commit() { committed = true; }
-
-private:
- const VAResources &resources;
- bool committed = false;
-};
-
-VADisplayWithCleanup::~VADisplayWithCleanup()
-{
- if (va_dpy != nullptr) {
- vaTerminate(va_dpy);
- }
- if (x11_display != nullptr) {
- XCloseDisplay(x11_display);
- }
- if (drm_fd != -1) {
- close(drm_fd);
- }
-}
-
-unique_ptr<VADisplayWithCleanup> va_open_display(const string &va_display)