Beatmup
Beatmup::Filters::PixelwiseFilter Class Referenceabstract

Base class for image filters processing a given bitmap in a pixelwise fashion. More...

#include <pixelwise_filter.h>

Inheritance diagram for Beatmup::Filters::PixelwiseFilter:
Beatmup::AbstractTask Beatmup::BitmapContentLock Beatmup::Object Beatmup::Filters::ColorMatrix Beatmup::Filters::Sepia

Public Member Functions

virtual ~PixelwiseFilter ()
 
virtual void setInput (AbstractBitmap *input)
 
virtual void setOutput (AbstractBitmap *output)
 
AbstractBitmapgetInput ()
 
AbstractBitmapgetOutput ()
 
ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Member Functions

virtual void apply (int x, int y, msize nPix, TaskThread &thread)=0
 Applies filtering to given pixel data. More...
 
virtual std::string getGlslDeclarations () const
 Provides GLSL declarations used in the GLSL code. More...
 
virtual std::string getGlslSourceCode () const =0
 Provides GLSL source code of the filter. More...
 
virtual void setup (bool useGpu)
 A callback run every time before the filter is applied to the image. More...
 
virtual bool process (TaskThread &thread) final
 Executes the task on CPU within a given thread. More...
 
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread) final
 Executes the task on GPU. More...
 
virtual void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 
virtual void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 
virtual TaskDeviceRequirement getUsedDevices () const final
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
 PixelwiseFilter ()
 

Protected Attributes

AbstractBitmapinputBitmap
 
AbstractBitmapoutputBitmap
 
ImageShadershader
 

Static Protected Attributes

static const std::string GLSL_RGBA_INPUT
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 
- Private Member Functions inherited from Beatmup::BitmapContentLock
 BitmapContentLock ()
 
 ~BitmapContentLock ()
 
void readLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for reading using a specific processing target device. More...
 
void writeLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for writing using a specific processing target device. More...
 
void unlock (AbstractBitmap *bitmap)
 Drops a lock to the bitmap. More...
 
void unlockAll ()
 Unlocks all the locked bitmaps unconditionally. More...
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
 
void lock (GraphicPipeline *gpu, ProcessingTarget target, AbstractBitmap *input, AbstractBitmap *output)
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, std::initializer_list< AbstractBitmap * > read, std::initializer_list< AbstractBitmap * > write)
 
template<typename ... Args>
void unlock (AbstractBitmap *first, Args ... others)
 

Detailed Description

Base class for image filters processing a given bitmap in a pixelwise fashion.

Definition at line 33 of file pixelwise_filter.h.

Constructor & Destructor Documentation

◆ PixelwiseFilter()

Filters::PixelwiseFilter::PixelwiseFilter ( )
protected

Definition at line 27 of file pixelwise_filter.cpp.

27  :
28  inputBitmap(nullptr), outputBitmap(nullptr), shader(nullptr)
29 {}

◆ ~PixelwiseFilter()

Filters::PixelwiseFilter::~PixelwiseFilter ( )
virtual

Definition at line 32 of file pixelwise_filter.cpp.

32  {
33  delete shader;
34 }

Member Function Documentation

◆ apply()

virtual void Beatmup::Filters::PixelwiseFilter::apply ( int  x,
int  y,
msize  nPix,
TaskThread thread 
)
protectedpure virtual

Applies filtering to given pixel data.

Parameters
xHorizontal position of the first pixel in image
yVertical position of the first pixel in image
nPixNumber of pixels to process
threadCalling thread descriptor

Implemented in Beatmup::Filters::Sepia, and Beatmup::Filters::ColorMatrix.

◆ getGlslDeclarations()

std::string Filters::PixelwiseFilter::getGlslDeclarations ( ) const
protectedvirtual

Provides GLSL declarations used in the GLSL code.

Reimplemented in Beatmup::Filters::ColorMatrix.

Definition at line 119 of file pixelwise_filter.cpp.

119  {
120  return "";
121 }

◆ getGlslSourceCode()

virtual std::string Beatmup::Filters::PixelwiseFilter::getGlslSourceCode ( ) const
protectedpure virtual

Provides GLSL source code of the filter.

Implemented in Beatmup::Filters::Sepia, and Beatmup::Filters::ColorMatrix.

◆ setup()

void Filters::PixelwiseFilter::setup ( bool  useGpu)
protectedvirtual

A callback run every time before the filter is applied to the image.

Parameters
useGpuIf true, the filter will be run on GPU.

Reimplemented in Beatmup::Filters::ColorMatrix.

Definition at line 124 of file pixelwise_filter.cpp.

124  {
125  // nothing to do by default
126 }

◆ process()

bool Filters::PixelwiseFilter::process ( TaskThread thread)
finalprotectedvirtual

Executes the task on CPU within a given thread.

Generally called by multiple threads.

Parameters
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Implements Beatmup::AbstractTask.

Definition at line 92 of file pixelwise_filter.cpp.

92  {
93  // each thread receives a single part of bitmap to deal with
94  const int w = inputBitmap->getWidth();
95  const msize
96  nPix = w * inputBitmap->getHeight(),
97  start = nPix * thread.currentThread() / thread.numThreads(),
98  stop = nPix * (1 + thread.currentThread()) / thread.numThreads();
99 
100  apply((int)(start % w), (int)(start / w), stop - start, thread);
101  return true;
102 }
virtual void apply(int x, int y, msize nPix, TaskThread &thread)=0
Applies filtering to given pixel data.
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual const int getWidth() const =0
Width of the texture in pixels.
virtual ThreadIndex numThreads() const =0
ThreadIndex currentThread() const
Definition: parallelism.h:165
uint32_t msize
memory size
Definition: basic_types.h:30
jlong jstring jint jint jint jint w
jlong jint start

◆ processOnGPU()

bool Filters::PixelwiseFilter::processOnGPU ( GraphicPipeline gpu,
TaskThread thread 
)
finalprotectedvirtual

Executes the task on GPU.

Parameters
gpugraphic pipeline instance
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Reimplemented from Beatmup::AbstractTask.

Definition at line 105 of file pixelwise_filter.cpp.

105  {
107  shader->process(gpu);
108  return true;
109 }
void process(GraphicPipeline &gpu)
Apply the shader to produce an image.
void prepare(GraphicPipeline &gpu, GL::TextureHandler *input, const TextureParam texParam, AbstractBitmap *output, const AffineMapping &mapping)
Conducts required preparations for blending.

◆ beforeProcessing()

void Filters::PixelwiseFilter::beforeProcessing ( ThreadIndex  threadCount,
ProcessingTarget  target,
GraphicPipeline gpu 
)
protectedvirtual

Instruction called before the task is executed.

Parameters
threadCountNumber of threads used to perform the task
targetDevice used to perform the task
gpuA graphic pipeline instance; may be null.

Reimplemented from Beatmup::AbstractTask.

Definition at line 45 of file pixelwise_filter.cpp.

45  {
46  NullTaskInput::check(inputBitmap, "input bitmap");
47  NullTaskInput::check(outputBitmap, "output bitmap");
51  "Incompatible input and output bitmaps sizes");
52 
53  // use GPU is available and the input bitmap is up to date on GPU
54  const bool useGpu = (target == ProcessingTarget::GPU && inputBitmap->isUpToDate(ProcessingTarget::GPU));
55 
56  // deferred shader preparation
57  if (useGpu) {
59  "Input and output bitmaps are not attached to the same context");
61  delete shader;
62  shader = nullptr;
63  }
64  if (!shader) {
67  getGlslDeclarations() + "\n" +
68  "void main() {" +
69  "lowp vec4 " +GLSL_RGBA_INPUT+ " = texture2D(" +
72  ");\n" +
74  "}"
75  );
76  }
77  }
78 
79  // lock bitmaps content
81 
82  // setup
83  setup(useGpu);
84 }
Context & getContext() const
bool isUpToDate(ProcessingTarget) const
void lock(GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
Definition: content_lock.h:84
virtual std::string getGlslDeclarations() const
Provides GLSL declarations used in the GLSL code.
virtual std::string getGlslSourceCode() const =0
Provides GLSL source code of the filter.
virtual void setup(bool useGpu)
A callback run every time before the filter is applied to the image.
static const std::string GLSL_RGBA_INPUT
static const char * TEXTURE_COORDINATES_ID
Texture coordinates shader variable name in vertex shader.
A GLSL program to process images.
Definition: image_shader.h:34
static const std::string CODE_HEADER
Shader code header containing necessary declarations.
Definition: image_shader.h:114
bool usesContext(Context &context) const
Returns true if the shader has ressources attached to a given context.
Definition: image_shader.h:98
void setSourceCode(const std::string &sourceCode)
Passes new source code to the fragment shader.
static const std::string INPUT_IMAGE_ID
Shader variable name referring to the input image.
Definition: image_shader.h:109
static void check(const void *pointer, const char *which)
Definition: exception.h:115
static void check(const bool condition, const std::string &message)
Definition: exception.h:64

◆ afterProcessing()

void Filters::PixelwiseFilter::afterProcessing ( ThreadIndex  threadCount,
GraphicPipeline gpu,
bool  aborted 
)
protectedvirtual

Instruction called after the task is executed.

Parameters
threadCountNumber of threads used to perform the task
gpuGPU to be used to execute the task; may be null.
abortedtrue if the task was aborted

Reimplemented from Beatmup::AbstractTask.

Definition at line 87 of file pixelwise_filter.cpp.

87  {
89 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.

◆ getUsedDevices()

AbstractTask::TaskDeviceRequirement Filters::PixelwiseFilter::getUsedDevices ( ) const
finalprotectedvirtual

Communicates devices (CPU and/or GPU) the task is run on.

Reimplemented from Beatmup::AbstractTask.

Definition at line 112 of file pixelwise_filter.cpp.

112  {
113  NullTaskInput::check(inputBitmap, "input bitmap");
116 }
@ CPU_ONLY
this task does not use GPU
@ GPU_ONLY
this task requires GPU, otherwise it cannot run

◆ setInput()

virtual void Beatmup::Filters::PixelwiseFilter::setInput ( AbstractBitmap input)
inlinevirtual

Definition at line 75 of file pixelwise_filter.h.

75 { this->inputBitmap = input; }

◆ setOutput()

virtual void Beatmup::Filters::PixelwiseFilter::setOutput ( AbstractBitmap output)
inlinevirtual

Definition at line 76 of file pixelwise_filter.h.

76 { this->outputBitmap = output; }

◆ getInput()

AbstractBitmap* Beatmup::Filters::PixelwiseFilter::getInput ( )
inline

Definition at line 78 of file pixelwise_filter.h.

78 { return inputBitmap; }

◆ getOutput()

AbstractBitmap* Beatmup::Filters::PixelwiseFilter::getOutput ( )
inline

Definition at line 79 of file pixelwise_filter.h.

79 { return outputBitmap; }

◆ getMaxThreads()

ThreadIndex Filters::PixelwiseFilter::getMaxThreads ( ) const
virtual

Gives the upper limint on the number of threads the task may be performed by.

The actual number of threads running a specific task may be less or equal to the returned value, depending on the number of workers in ThreadPool running the task.

Reimplemented from Beatmup::AbstractTask.

Definition at line 37 of file pixelwise_filter.cpp.

37  {
38  NullTaskInput::check(inputBitmap, "input bitmap");
39  // if there are few pixels, do not use many threads
40  static const int MIN_PIXEL_COUNT_PER_THREAD = 64;
41  return AbstractTask::validThreadCount(inputBitmap->getWidth() * inputBitmap->getHeight() / MIN_PIXEL_COUNT_PER_THREAD);
42 }
static ThreadIndex validThreadCount(int number)
Valid thread count from a given integer value.
Definition: parallelism.cpp:45

Member Data Documentation

◆ GLSL_RGBA_INPUT

const std::string Filters::PixelwiseFilter::GLSL_RGBA_INPUT
staticprotected

Definition at line 35 of file pixelwise_filter.h.

◆ inputBitmap

AbstractBitmap* Beatmup::Filters::PixelwiseFilter::inputBitmap
protected

Definition at line 37 of file pixelwise_filter.h.

◆ outputBitmap

AbstractBitmap * Beatmup::Filters::PixelwiseFilter::outputBitmap
protected

Definition at line 37 of file pixelwise_filter.h.

◆ shader

ImageShader* Beatmup::Filters::PixelwiseFilter::shader
protected

Definition at line 38 of file pixelwise_filter.h.


The documentation for this class was generated from the following files: