]> git.sesse.net Git - nageru/blobdiff - nageru/pbo_frame_allocator.cpp
Errors in flags do not need core dumps.
[nageru] / nageru / pbo_frame_allocator.cpp
index d0859b357d98a313fb40587b2663dad517c7e2e5..7fb06f849b1bff09c78be68ad9b388ff1cd8c7e9 100644 (file)
@@ -315,26 +315,18 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_
                MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height);
                MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources);
 
-               VAImage image;
-               VAStatus va_status = vaDeriveImage(va_dpy, resources.surface, &image);
-               CHECK_VASTATUS(va_status, "vaDeriveImage");
-
-               if (image.pitches[0] == stride) {
+               if (resources.image.pitches[0] == stride) {
                        userdata->va_resources = move(resources);
                        userdata->va_resources_release = move(release);
-                       userdata->va_image = move(image);
 
-                       va_status = vaMapBuffer(va_dpy, image.buf, (void **)&vf.data_copy);
+                       VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
                        CHECK_VASTATUS(va_status, "vaMapBuffer");
-                       vf.data_copy += image.offsets[0];
+                       vf.data_copy += resources.image.offsets[0];
                        userdata->data_copy_current_src = Userdata::FROM_VA_API;
                } else {
-                       printf("WARNING: Could not copy directly into VA-API MJPEG buffer for %zu x %zu, since producer and consumer disagreed on stride (%zu != %d).\n", width, height, stride, image.pitches[0]);
+                       printf("WARNING: Could not copy directly into VA-API MJPEG buffer for %zu x %zu, since producer and consumer disagreed on stride (%zu != %d).\n", width, height, stride, resources.image.pitches[0]);
                        vf.data_copy = userdata->data_copy_malloc;
                        userdata->data_copy_current_src = Userdata::FROM_MALLOC;
-
-                       va_status = vaDestroyImage(va_dpy, image.image_id);
-                       CHECK_VASTATUS(va_status, "vaDestroyImage");
                }
        } else {
                vf.data_copy = nullptr;
@@ -378,6 +370,21 @@ void PBOFrameAllocator::release_frame(Frame frame)
        }
 #endif
 
+       {
+               // In case we never got to upload the frame to MJPEGEncoder.
+               Userdata *userdata = (Userdata *)frame.userdata;
+               MJPEGEncoder::VAResources resources __attribute__((unused)) = move(userdata->va_resources);
+               MJPEGEncoder::ReleaseVAResources release = move(userdata->va_resources_release);
+
+               if (frame.data_copy != nullptr && userdata->data_copy_current_src == Userdata::FROM_VA_API) {
+                       VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
+                       VAStatus va_status = vaUnmapBuffer(va_dpy, resources.image.buf);
+                       CHECK_VASTATUS(va_status, "vaUnmapBuffer");
+
+                       frame.data_copy = nullptr;
+               }
+       }
+
        lock_guard<mutex> lock(freelist_mutex);
        freelist.push(frame);
        //--sumsum;