| Message ID | 20260706222245.247396-7-bryan.odonoghue@linaro.org |
|---|---|
| State | Accepted |
| Headers | show |
| Series |
|
| Related | show |
Hi Bryan, On 07.07.26 00:22, Bryan O'Donoghue wrote: > Implement a texture caching mechanism for both input and output frames and > for both types of input frame. > > The before/after on a Qualcomm x1e is: > > 9.737ms per frame > 5.691ms per frame > > The before/after on a Qualcomm sm8250 is: > > 21.710ms per frame > 17.336ms per frame > > for i in {1..20} do > cam -c /base/soc@0/cci@ac16000/i2c-bus@1/camera@10 -s width=1920,height=1080 --capture=60 > > Interestingly there appears to be an absolute ~ 4.x ms per frame uplift as > opposed to what intuition might suggest a proportional. > > Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> thanks, looks great to me! Reviewed-by: Robert Mader <robert.mader@collabora.com> > --- > src/libcamera/software_isp/debayer_egl.cpp | 125 ++++++++++++++++----- > src/libcamera/software_isp/debayer_egl.h | 10 +- > 2 files changed, 104 insertions(+), 31 deletions(-) > > diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp > index c3a7b7952..a7cca5dde 100644 > --- a/src/libcamera/software_isp/debayer_egl.cpp > +++ b/src/libcamera/software_isp/debayer_egl.cpp > @@ -345,6 +345,9 @@ int DebayerEGL::configure(const StreamConfiguration &inputCfg, > */ > stats_->setWindow(Rectangle(window_.size())); > > + inputBufferCount_ = inputCfg.bufferCount; > + outputBufferCount_ = outputCfg.bufferCount; > + > return 0; > } > > @@ -504,36 +507,106 @@ void DebayerEGL::setShaderVariableValues(eGLImage &eglImageIn, const DebayerPara > return; > } > > -int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) > +eGLImage *DebayerEGL::getCachedInputFrameBuffer(FrameBuffer *input, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) > { > - bool dmabuf_import_succeeded = false; > + const SharedFD &fd = input->planes()[0].fd; > + bool cache_miss = true; > + eGLImage *eglImageIn; > + > + for (auto &[ifd, img] : eglImageInCache_) { > + if (ifd == fd) { > + eglImageIn = img.get(); > + cache_miss = false; > + break; > + } > + } > > - /* eGL context switch */ > - egl_.makeCurrent(); > + if (cache_miss) { > + if (eglImageInCache_.size() >= inputBufferCount_) { > + eglImageInCache_.pop_front(); > + LOG(Debayer, Debug) << "Input cache " << inputBufferCount_ << " exceeded evicted entry"; > + } > + > + eglImageInCache_.emplace_back(fd, std::make_unique<eGLImage>(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0)); > + eglImageIn = eglImageInCache_.back().second.get(); > + > + if (egl_.createInputDMABufTexture2D(*eglImageIn, input->planes()[0].fd.get()) == 0) > + return eglImageIn; > + > + } else if (!eglImageIn->dmabuf_import_failed_) { > + egl_.activateBindTexture(*eglImageIn); > + return eglImageIn; > + } > + > + /* DMA mode fail create/update an existing texture using the slow path */ > + inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); > + inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); > + if (!inMapped->value().isValid()) { > + LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; > + if (cache_miss) { > + eglImageInCache_.pop_back(); > + } > + return nullptr; > + } > + if (cache_miss) > + egl_.createTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); > + else > + egl_.updateTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); > + > + return eglImageIn; > +} > > - /* Try to create texture for input buffer via dmabuf import */ > - if (!eglImageBayerIn_->dmabuf_import_failed_) { > - if (egl_.createInputDMABufTexture2D(*eglImageBayerIn_, input->planes()[0].fd.get()) == 0) > - dmabuf_import_succeeded = true; > - else > - LOG(Debayer, Info) << "Importing input buffer with DMABuf import failed, falling back to upload"; > +eGLImage *DebayerEGL::getCachedOutputFrameBuffer(FrameBuffer *output) > +{ > + const SharedFD &fd = output->planes()[0].fd; > + bool cache_miss = true; > + eGLImage *eglImageOut; > + > + for (auto &[ifd, img] : eglImageOutCache_) { > + if (ifd == fd) { > + eglImageOut = img.get(); > + cache_miss = false; > + break; > + } > } > > - /* Otherwise create texture for input buffer via upload from CPU */ > - if (!dmabuf_import_succeeded) { > - inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); > - inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); > - if (!inMapped->value().isValid()) { > - LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; > - return -ENODEV; > + if (cache_miss) { > + if (eglImageOutCache_.size() >= outputBufferCount_) { > + eglImageOutCache_.pop_front(); > + LOG(Debayer, Debug) << "Output cache " << outputBufferCount_ << " exceeded evicted entry"; > + } > + > + eglImageOutCache_.emplace_back(fd, std::make_unique<eGLImage>(GL_RGBA, outputSize_.width, > + outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1)); > + eglImageOut = eglImageOutCache_.back().second.get(); > + > + if (egl_.createOutputDMABufTexture2D(*eglImageOut, output->planes()[0].fd.get())) { > + eglImageOutCache_.pop_back(); > + return nullptr; > } > - egl_.createTexture2D(*eglImageBayerIn_, inMapped->value().planes()[0].data()); > } > > - /* Generate the output render framebuffer as render to texture */ > - egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, output->planes()[0].fd.get()); > + return eglImageOut; > +} > + > +int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) > +{ > + eGLImage *eglImageIn; > + eGLImage *eglImageOut; > + > + /* eGL context switch */ > + egl_.makeCurrent(); > + > + eglImageIn = getCachedInputFrameBuffer(input, inMapped, inDmaSyncer); > + if (!eglImageIn) > + return -ENOMEM; > + eglImageOut = getCachedOutputFrameBuffer(output); > + if (!eglImageOut) > + return -ENOMEM; > + > + egl_.attachTextureToFBO(*eglImageOut); > + setShaderVariableValues(*eglImageIn, params); > > - setShaderVariableValues(*eglImageBayerIn_, params); > glViewport(0, 0, width_, height_); > glClear(GL_COLOR_BUFFER_BIT); > glDrawArrays(GL_TRIANGLE_FAN, 0, DEBAYER_OPENGL_COORDS); > @@ -615,19 +688,13 @@ int DebayerEGL::start() > if (initBayerShaders(inputPixelFormat_, outputPixelFormat_)) > return -EINVAL; > > - /* Raw bayer input as texture */ > - eglImageBayerIn_ = std::make_unique<eGLImage>(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0); > - > - /* Texture we will render to */ > - eglImageBayerOut_ = std::make_unique<eGLImage>(GL_RGBA, outputSize_.width, outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1); > - > return 0; > } > > void DebayerEGL::stop() > { > - eglImageBayerOut_.reset(); > - eglImageBayerIn_.reset(); > + eglImageOutCache_.clear(); > + eglImageInCache_.clear(); > > if (programId_) > glDeleteProgram(programId_); > diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h > index 348d7305b..d6223f3a9 100644 > --- a/src/libcamera/software_isp/debayer_egl.h > +++ b/src/libcamera/software_isp/debayer_egl.h > @@ -9,6 +9,7 @@ > > #pragma once > > +#include <deque> > #include <memory> > #include <stdint.h> > #include <tuple> > @@ -68,14 +69,19 @@ private: > void setShaderVariableValues(eGLImage &eGLImageIn, const DebayerParams ¶ms); > int debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *mappedInputBuffer, std::optional<DmaSyncer> *inputBufferDmaSyncer); > > + eGLImage *getCachedInputFrameBuffer(FrameBuffer *input, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer); > + eGLImage *getCachedOutputFrameBuffer(FrameBuffer *output); > + > /* Shader program identifiers */ > GLuint vertexShaderId_ = 0; > GLuint fragmentShaderId_ = 0; > GLuint programId_ = 0; > > /* Pointer to object representing input texture */ > - std::unique_ptr<eGLImage> eglImageBayerIn_; > - std::unique_ptr<eGLImage> eglImageBayerOut_; > + std::deque<std::pair<SharedFD, std::unique_ptr<eGLImage>>> eglImageInCache_; > + std::deque<std::pair<SharedFD, std::unique_ptr<eGLImage>>> eglImageOutCache_; > + unsigned int inputBufferCount_; > + unsigned int outputBufferCount_; > > /* Shader parameters */ > float firstRed_x_;
Bryan O'Donoghue <bryan.odonoghue@linaro.org> writes: > Implement a texture caching mechanism for both input and output frames and > for both types of input frame. > > The before/after on a Qualcomm x1e is: > > 9.737ms per frame > 5.691ms per frame > > The before/after on a Qualcomm sm8250 is: > > 21.710ms per frame > 17.336ms per frame > > for i in {1..20} do > cam -c /base/soc@0/cci@ac16000/i2c-bus@1/camera@10 -s width=1920,height=1080 --capture=60 > > Interestingly there appears to be an absolute ~ 4.x ms per frame uplift as > opposed to what intuition might suggest a proportional. There seems to be a slight difference on RPi 4 too, ~3 ms of 170-175 ms with 3840x2464, ~1 ms of ~40 ms with 1916x1080. > Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> Looks plausible AFAICS: Reviewed-by: Milan Zamazal <mzamazal@redhat.com> > --- > src/libcamera/software_isp/debayer_egl.cpp | 125 ++++++++++++++++----- > src/libcamera/software_isp/debayer_egl.h | 10 +- > 2 files changed, 104 insertions(+), 31 deletions(-) > > diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp > index c3a7b7952..a7cca5dde 100644 > --- a/src/libcamera/software_isp/debayer_egl.cpp > +++ b/src/libcamera/software_isp/debayer_egl.cpp > @@ -345,6 +345,9 @@ int DebayerEGL::configure(const StreamConfiguration &inputCfg, > */ > stats_->setWindow(Rectangle(window_.size())); > > + inputBufferCount_ = inputCfg.bufferCount; > + outputBufferCount_ = outputCfg.bufferCount; > + > return 0; > } > > @@ -504,36 +507,106 @@ void DebayerEGL::setShaderVariableValues(eGLImage &eglImageIn, const DebayerPara > return; > } > > -int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) > +eGLImage *DebayerEGL::getCachedInputFrameBuffer(FrameBuffer *input, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) > { > - bool dmabuf_import_succeeded = false; > + const SharedFD &fd = input->planes()[0].fd; > + bool cache_miss = true; > + eGLImage *eglImageIn; > + > + for (auto &[ifd, img] : eglImageInCache_) { > + if (ifd == fd) { > + eglImageIn = img.get(); > + cache_miss = false; > + break; > + } > + } > > - /* eGL context switch */ > - egl_.makeCurrent(); > + if (cache_miss) { > + if (eglImageInCache_.size() >= inputBufferCount_) { > + eglImageInCache_.pop_front(); > + LOG(Debayer, Debug) << "Input cache " << inputBufferCount_ << " exceeded evicted entry"; > + } > + > + eglImageInCache_.emplace_back(fd, std::make_unique<eGLImage>(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0)); > + eglImageIn = eglImageInCache_.back().second.get(); > + > + if (egl_.createInputDMABufTexture2D(*eglImageIn, input->planes()[0].fd.get()) == 0) > + return eglImageIn; > + > + } else if (!eglImageIn->dmabuf_import_failed_) { > + egl_.activateBindTexture(*eglImageIn); > + return eglImageIn; > + } > + > + /* DMA mode fail create/update an existing texture using the slow path */ > + inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); > + inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); > + if (!inMapped->value().isValid()) { > + LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; > + if (cache_miss) { > + eglImageInCache_.pop_back(); > + } > + return nullptr; > + } > + if (cache_miss) > + egl_.createTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); > + else > + egl_.updateTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); > + > + return eglImageIn; > +} > > - /* Try to create texture for input buffer via dmabuf import */ > - if (!eglImageBayerIn_->dmabuf_import_failed_) { > - if (egl_.createInputDMABufTexture2D(*eglImageBayerIn_, input->planes()[0].fd.get()) == 0) > - dmabuf_import_succeeded = true; > - else > - LOG(Debayer, Info) << "Importing input buffer with DMABuf import failed, falling back to upload"; > +eGLImage *DebayerEGL::getCachedOutputFrameBuffer(FrameBuffer *output) > +{ > + const SharedFD &fd = output->planes()[0].fd; > + bool cache_miss = true; > + eGLImage *eglImageOut; > + > + for (auto &[ifd, img] : eglImageOutCache_) { > + if (ifd == fd) { > + eglImageOut = img.get(); > + cache_miss = false; > + break; > + } > } > > - /* Otherwise create texture for input buffer via upload from CPU */ > - if (!dmabuf_import_succeeded) { > - inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); > - inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); > - if (!inMapped->value().isValid()) { > - LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; > - return -ENODEV; > + if (cache_miss) { > + if (eglImageOutCache_.size() >= outputBufferCount_) { > + eglImageOutCache_.pop_front(); > + LOG(Debayer, Debug) << "Output cache " << outputBufferCount_ << " exceeded evicted entry"; > + } > + > + eglImageOutCache_.emplace_back(fd, std::make_unique<eGLImage>(GL_RGBA, outputSize_.width, > + outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1)); > + eglImageOut = eglImageOutCache_.back().second.get(); > + > + if (egl_.createOutputDMABufTexture2D(*eglImageOut, output->planes()[0].fd.get())) { > + eglImageOutCache_.pop_back(); > + return nullptr; > } > - egl_.createTexture2D(*eglImageBayerIn_, inMapped->value().planes()[0].data()); > } > > - /* Generate the output render framebuffer as render to texture */ > - egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, output->planes()[0].fd.get()); > + return eglImageOut; > +} > + > +int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) > +{ > + eGLImage *eglImageIn; > + eGLImage *eglImageOut; > + > + /* eGL context switch */ > + egl_.makeCurrent(); > + > + eglImageIn = getCachedInputFrameBuffer(input, inMapped, inDmaSyncer); > + if (!eglImageIn) > + return -ENOMEM; > + eglImageOut = getCachedOutputFrameBuffer(output); > + if (!eglImageOut) > + return -ENOMEM; > + > + egl_.attachTextureToFBO(*eglImageOut); > + setShaderVariableValues(*eglImageIn, params); > > - setShaderVariableValues(*eglImageBayerIn_, params); > glViewport(0, 0, width_, height_); > glClear(GL_COLOR_BUFFER_BIT); > glDrawArrays(GL_TRIANGLE_FAN, 0, DEBAYER_OPENGL_COORDS); > @@ -615,19 +688,13 @@ int DebayerEGL::start() > if (initBayerShaders(inputPixelFormat_, outputPixelFormat_)) > return -EINVAL; > > - /* Raw bayer input as texture */ > - eglImageBayerIn_ = std::make_unique<eGLImage>(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0); > - > - /* Texture we will render to */ > - eglImageBayerOut_ = std::make_unique<eGLImage>(GL_RGBA, outputSize_.width, outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1); > - > return 0; > } > > void DebayerEGL::stop() > { > - eglImageBayerOut_.reset(); > - eglImageBayerIn_.reset(); > + eglImageOutCache_.clear(); > + eglImageInCache_.clear(); > > if (programId_) > glDeleteProgram(programId_); > diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h > index 348d7305b..d6223f3a9 100644 > --- a/src/libcamera/software_isp/debayer_egl.h > +++ b/src/libcamera/software_isp/debayer_egl.h > @@ -9,6 +9,7 @@ > > #pragma once > > +#include <deque> > #include <memory> > #include <stdint.h> > #include <tuple> > @@ -68,14 +69,19 @@ private: > void setShaderVariableValues(eGLImage &eGLImageIn, const DebayerParams ¶ms); > int debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *mappedInputBuffer, std::optional<DmaSyncer> *inputBufferDmaSyncer); > > + eGLImage *getCachedInputFrameBuffer(FrameBuffer *input, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer); > + eGLImage *getCachedOutputFrameBuffer(FrameBuffer *output); > + > /* Shader program identifiers */ > GLuint vertexShaderId_ = 0; > GLuint fragmentShaderId_ = 0; > GLuint programId_ = 0; > > /* Pointer to object representing input texture */ > - std::unique_ptr<eGLImage> eglImageBayerIn_; > - std::unique_ptr<eGLImage> eglImageBayerOut_; > + std::deque<std::pair<SharedFD, std::unique_ptr<eGLImage>>> eglImageInCache_; > + std::deque<std::pair<SharedFD, std::unique_ptr<eGLImage>>> eglImageOutCache_; > + unsigned int inputBufferCount_; > + unsigned int outputBufferCount_; > > /* Shader parameters */ > float firstRed_x_;
diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index c3a7b7952..a7cca5dde 100644 --- a/src/libcamera/software_isp/debayer_egl.cpp +++ b/src/libcamera/software_isp/debayer_egl.cpp @@ -345,6 +345,9 @@ int DebayerEGL::configure(const StreamConfiguration &inputCfg, */ stats_->setWindow(Rectangle(window_.size())); + inputBufferCount_ = inputCfg.bufferCount; + outputBufferCount_ = outputCfg.bufferCount; + return 0; } @@ -504,36 +507,106 @@ void DebayerEGL::setShaderVariableValues(eGLImage &eglImageIn, const DebayerPara return; } -int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) +eGLImage *DebayerEGL::getCachedInputFrameBuffer(FrameBuffer *input, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) { - bool dmabuf_import_succeeded = false; + const SharedFD &fd = input->planes()[0].fd; + bool cache_miss = true; + eGLImage *eglImageIn; + + for (auto &[ifd, img] : eglImageInCache_) { + if (ifd == fd) { + eglImageIn = img.get(); + cache_miss = false; + break; + } + } - /* eGL context switch */ - egl_.makeCurrent(); + if (cache_miss) { + if (eglImageInCache_.size() >= inputBufferCount_) { + eglImageInCache_.pop_front(); + LOG(Debayer, Debug) << "Input cache " << inputBufferCount_ << " exceeded evicted entry"; + } + + eglImageInCache_.emplace_back(fd, std::make_unique<eGLImage>(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0)); + eglImageIn = eglImageInCache_.back().second.get(); + + if (egl_.createInputDMABufTexture2D(*eglImageIn, input->planes()[0].fd.get()) == 0) + return eglImageIn; + + } else if (!eglImageIn->dmabuf_import_failed_) { + egl_.activateBindTexture(*eglImageIn); + return eglImageIn; + } + + /* DMA mode fail create/update an existing texture using the slow path */ + inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); + inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); + if (!inMapped->value().isValid()) { + LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; + if (cache_miss) { + eglImageInCache_.pop_back(); + } + return nullptr; + } + if (cache_miss) + egl_.createTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); + else + egl_.updateTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); + + return eglImageIn; +} - /* Try to create texture for input buffer via dmabuf import */ - if (!eglImageBayerIn_->dmabuf_import_failed_) { - if (egl_.createInputDMABufTexture2D(*eglImageBayerIn_, input->planes()[0].fd.get()) == 0) - dmabuf_import_succeeded = true; - else - LOG(Debayer, Info) << "Importing input buffer with DMABuf import failed, falling back to upload"; +eGLImage *DebayerEGL::getCachedOutputFrameBuffer(FrameBuffer *output) +{ + const SharedFD &fd = output->planes()[0].fd; + bool cache_miss = true; + eGLImage *eglImageOut; + + for (auto &[ifd, img] : eglImageOutCache_) { + if (ifd == fd) { + eglImageOut = img.get(); + cache_miss = false; + break; + } } - /* Otherwise create texture for input buffer via upload from CPU */ - if (!dmabuf_import_succeeded) { - inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); - inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); - if (!inMapped->value().isValid()) { - LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; - return -ENODEV; + if (cache_miss) { + if (eglImageOutCache_.size() >= outputBufferCount_) { + eglImageOutCache_.pop_front(); + LOG(Debayer, Debug) << "Output cache " << outputBufferCount_ << " exceeded evicted entry"; + } + + eglImageOutCache_.emplace_back(fd, std::make_unique<eGLImage>(GL_RGBA, outputSize_.width, + outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1)); + eglImageOut = eglImageOutCache_.back().second.get(); + + if (egl_.createOutputDMABufTexture2D(*eglImageOut, output->planes()[0].fd.get())) { + eglImageOutCache_.pop_back(); + return nullptr; } - egl_.createTexture2D(*eglImageBayerIn_, inMapped->value().planes()[0].data()); } - /* Generate the output render framebuffer as render to texture */ - egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, output->planes()[0].fd.get()); + return eglImageOut; +} + +int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer) +{ + eGLImage *eglImageIn; + eGLImage *eglImageOut; + + /* eGL context switch */ + egl_.makeCurrent(); + + eglImageIn = getCachedInputFrameBuffer(input, inMapped, inDmaSyncer); + if (!eglImageIn) + return -ENOMEM; + eglImageOut = getCachedOutputFrameBuffer(output); + if (!eglImageOut) + return -ENOMEM; + + egl_.attachTextureToFBO(*eglImageOut); + setShaderVariableValues(*eglImageIn, params); - setShaderVariableValues(*eglImageBayerIn_, params); glViewport(0, 0, width_, height_); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLE_FAN, 0, DEBAYER_OPENGL_COORDS); @@ -615,19 +688,13 @@ int DebayerEGL::start() if (initBayerShaders(inputPixelFormat_, outputPixelFormat_)) return -EINVAL; - /* Raw bayer input as texture */ - eglImageBayerIn_ = std::make_unique<eGLImage>(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0); - - /* Texture we will render to */ - eglImageBayerOut_ = std::make_unique<eGLImage>(GL_RGBA, outputSize_.width, outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1); - return 0; } void DebayerEGL::stop() { - eglImageBayerOut_.reset(); - eglImageBayerIn_.reset(); + eglImageOutCache_.clear(); + eglImageInCache_.clear(); if (programId_) glDeleteProgram(programId_); diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h index 348d7305b..d6223f3a9 100644 --- a/src/libcamera/software_isp/debayer_egl.h +++ b/src/libcamera/software_isp/debayer_egl.h @@ -9,6 +9,7 @@ #pragma once +#include <deque> #include <memory> #include <stdint.h> #include <tuple> @@ -68,14 +69,19 @@ private: void setShaderVariableValues(eGLImage &eGLImageIn, const DebayerParams ¶ms); int debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional<MappedFrameBuffer> *mappedInputBuffer, std::optional<DmaSyncer> *inputBufferDmaSyncer); + eGLImage *getCachedInputFrameBuffer(FrameBuffer *input, std::optional<MappedFrameBuffer> *inMapped, std::optional<DmaSyncer> *inDmaSyncer); + eGLImage *getCachedOutputFrameBuffer(FrameBuffer *output); + /* Shader program identifiers */ GLuint vertexShaderId_ = 0; GLuint fragmentShaderId_ = 0; GLuint programId_ = 0; /* Pointer to object representing input texture */ - std::unique_ptr<eGLImage> eglImageBayerIn_; - std::unique_ptr<eGLImage> eglImageBayerOut_; + std::deque<std::pair<SharedFD, std::unique_ptr<eGLImage>>> eglImageInCache_; + std::deque<std::pair<SharedFD, std::unique_ptr<eGLImage>>> eglImageOutCache_; + unsigned int inputBufferCount_; + unsigned int outputBufferCount_; /* Shader parameters */ float firstRed_x_;
Implement a texture caching mechanism for both input and output frames and for both types of input frame. The before/after on a Qualcomm x1e is: 9.737ms per frame 5.691ms per frame The before/after on a Qualcomm sm8250 is: 21.710ms per frame 17.336ms per frame for i in {1..20} do cam -c /base/soc@0/cci@ac16000/i2c-bus@1/camera@10 -s width=1920,height=1080 --capture=60 Interestingly there appears to be an absolute ~ 4.x ms per frame uplift as opposed to what intuition might suggest a proportional. Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> --- src/libcamera/software_isp/debayer_egl.cpp | 125 ++++++++++++++++----- src/libcamera/software_isp/debayer_egl.h | 10 +- 2 files changed, 104 insertions(+), 31 deletions(-)