X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fpbo_frame_allocator.cpp;h=709a2bf359e505ab7e089f567fce33b502427834;hb=HEAD;hp=ba5322602a47e4f0903fda950a96e597bd6c5660;hpb=2f8f882defb23abe8b2c54e195b72c57fba55cd6;p=nageru diff --git a/nageru/pbo_frame_allocator.cpp b/nageru/pbo_frame_allocator.cpp index ba53226..6ebe13a 100644 --- a/nageru/pbo_frame_allocator.cpp +++ b/nageru/pbo_frame_allocator.cpp @@ -1,16 +1,22 @@ #include "pbo_frame_allocator.h" #include +#include +#include #include +#include #include #include #include #include +#include +#include -#include "flags.h" #include "mjpeg_encoder.h" +#include "defs.h" +#include "shared/va_resource_pool.h" #include "v210_converter.h" -#include "va_display_with_cleanup.h" +#include "shared/va_display.h" using namespace std; @@ -29,7 +35,7 @@ void set_clamp_to_edge() } // namespace PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, unsigned card_index, MJPEGEncoder *mjpeg_encoder, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits) - : card_index(card_index), + : card_index(card_index), mjpeg_encoder(mjpeg_encoder), pixel_format(pixel_format), buffer(buffer), @@ -42,7 +48,9 @@ PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t fra { userdata.reset(new Userdata[num_queued_frames]); for (size_t i = 0; i < num_queued_frames; ++i) { - init_frame(i, frame_size, width, height, permissions, map_bits, generation); + Frame frame; + init_frame(frame, &userdata[i], this, pixel_format, frame_size, width, height, permissions, map_bits, buffer, generation); + freelist.push(frame); } glBindBuffer(buffer, 0); check_error(); @@ -50,7 +58,7 @@ PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t fra check_error(); } -void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation) +void PBOFrameAllocator::init_frame(Frame &frame, Userdata *ud, PBOFrameAllocator *owner, bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, GLenum buffer, int generation) { GLuint pbo; glGenBuffers(1, &pbo); @@ -60,18 +68,16 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT); check_error(); - Frame frame; frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT); frame.data2 = frame.data + frame_size / 2; check_error(); frame.size = frame_size; - Userdata *ud = &userdata[frame_idx]; frame.userdata = ud; ud->generation = generation; ud->pbo = pbo; ud->pixel_format = pixel_format; ud->data_copy_malloc = new uint8_t[frame_size]; - frame.owner = this; + frame.owner = owner; // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr // into separate textures. For 10-bit, the input format (v210) @@ -209,8 +215,6 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w assert(false); } } - - freelist.push(frame); } PBOFrameAllocator::~PBOFrameAllocator() @@ -236,7 +240,7 @@ void PBOFrameAllocator::destroy_frame(Frame *frame) check_error(); glDeleteBuffers(1, &pbo); check_error(); - switch (pixel_format) { + switch (ud->pixel_format) { case bmusb::PixelFormat_10BitYCbCr: glDeleteTextures(2, ud->tex_v210); check_error(); @@ -277,7 +281,7 @@ void PBOFrameAllocator::destroy_frame(Frame *frame) bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame() { - Frame vf; + Frame vf; lock_guard lock(freelist_mutex); // Meh. if (freelist.empty()) { @@ -305,7 +309,12 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame() bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride) { - Frame vf; + Frame vf; + + size_t desired_frame_bytes = width * stride; + if (stride > 8192 * 4 || height > 8192 || desired_frame_bytes > MAX_FRAME_SIZE) { + return vf; + } { lock_guard lock(freelist_mutex); @@ -319,17 +328,29 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_ freelist.pop(); } } - vf.len = 0; - vf.overflow = 0; Userdata *userdata = (Userdata *)vf.userdata; + assert(generation == userdata->generation); + if (vf.size < desired_frame_bytes || (vf.size > FRAME_SIZE && vf.size > desired_frame_bytes * 2)) { + // Frame is either too small or way too large, so reallocate it. + // Note that width and height now automatically becomes the right size + // (the one we just asked for, instead of the default for the allocator, + // which is generally the global resolution); it doesn't matter + // for correctness, since we'll recreate the texture on upload if needed, + // but it is nice to save that step. + destroy_frame(&vf); + init_frame(vf, userdata, this, pixel_format, std::max(desired_frame_bytes, FRAME_SIZE), width, height, permissions, map_bits, buffer, generation); + }; + + vf.len = 0; + vf.overflow = 0; if (mjpeg_encoder != nullptr && mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) { if (mjpeg_encoder->using_vaapi()) { VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy; - MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height, VA_FOURCC_UYVY); // Only used by DeckLinkCapture, so always 4:2:2. - MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources); + VAResourcePool::VAResources resources = mjpeg_encoder->get_va_pool()->get_va_resources(width, height, VA_FOURCC_UYVY); // Only used by DeckLinkCapture, so always 4:2:2. + ReleaseVAResources release(mjpeg_encoder->get_va_pool(), resources); if (resources.image.pitches[0] == stride) { userdata->va_resources = move(resources); @@ -393,8 +414,8 @@ void PBOFrameAllocator::release_frame(Frame frame) { // 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); + VAResourcePool::VAResources resources __attribute__((unused)) = move(userdata->va_resources); + 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; @@ -460,7 +481,9 @@ void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format, userdata.reset(new Userdata[num_queued_frames]); for (size_t i = 0; i < num_queued_frames; ++i) { - init_frame(i, frame_size, width, height, permissions, map_bits, generation); + Frame frame; + init_frame(frame, &userdata[i], this, pixel_format, frame_size, width, height, permissions, map_bits, buffer, generation); + freelist.push(frame); } // There may still be frames out with the old configuration