#include "model/processor/strategies/Persist.h" #include "model/processor/BIP.h" #include #include Persist::Persist(const Context context, const QString kernelPath, const QObject *parent = Q_NULLPTR) : IProcessStrategy(context, kernelPath, "Persist", parent), _kernelFunctor(KernelFunctor(_kernel)) { memset(&_kernelParameters, 0, sizeof (Persist_t)); _imageSum = Q_NULLPTR; _reCalc = true; } void Persist::cpuProcess(ScenGenOutput_t parameters) { if(parameters.persist.isUpdated) { _kernelParameters.persist = parameters.persist.value; _reCalc = true; } } void Persist::finalize() { foreach (auto frame, _queue) { delete frame; } _queue.clear(); if(_imageSum) delete _imageSum; } Image* Persist::processKernel(Image *frames, Buffer* scratchPad) { Image* temp; if(_queue.length() < PERSIST_MAX_SIZE) { _queue.push_front(frames); return frames; } if(_kernelParameters.persist != 1) { auto format = frames->getImageInfo(); auto width = frames->getImageInfo(); auto height = frames->getImageInfo(); //if width or height changes we should erase everything if(_lastWidth != width || _lastHeight != height) { _reCalc = false; if(_imageSum) delete _imageSum; _imageSum = new Image2D(_CLContext, CL_MEM_READ_WRITE, ImageFormat(format.image_channel_order, format.image_channel_data_type), width, height); foreach (auto frame, _queue) { delete frame; } _queue.clear(); _lastWidth = width; _lastHeight = height; } auto outputImage = new Image2D(_CLContext, CL_MEM_READ_WRITE, ImageFormat(format.image_channel_order, format.image_channel_data_type), width, height); cl::EnqueueArgs eargs(BIP::getInstance()->CLQueue, cl::NDRange(width, height)); if(_reCalc) { _reCalc = false; for(auto i = 0; i < _kernelParameters.persist; i++) { if(i == 0) _kernelParameters.mode = 0; else if(i < _kernelParameters.persist) _kernelParameters.mode = 1; _openCLHelper.runKernelFunctor(_kernelFunctor, eargs, *static_cast(_queue[i]), *outputImage, *static_cast(_imageSum), *static_cast(_queue[_kernelParameters.persist - 1]), _kernelParameters); } } _kernelParameters.mode = 2; _openCLHelper.runKernelFunctor(_kernelFunctor, eargs, *static_cast(frames), *outputImage, *static_cast(_imageSum), *static_cast(_queue[_kernelParameters.persist - 1]), _kernelParameters); temp = outputImage; } else { temp = frames; } delete _queue[PERSIST_MAX_SIZE - 1]; for(auto i = 0; i < PERSIST_MAX_SIZE - 1; i++) { _queue[i + 1] = _queue[i]; } _queue[0] = frames; return temp; }