Beatmup
Beatmup::SceneRenderer Class Reference

AbstractTask rendering a Scene. More...

#include <renderer.h>

Inheritance diagram for Beatmup::SceneRenderer:
Beatmup::GpuTask Beatmup::AbstractTask Beatmup::Object

Public Types

enum  OutputMapping { STRETCH = 0 , FIT_WIDTH_TO_TOP , FIT_WIDTH , FIT_HEIGHT }
 Scene coordinates to output (screen or bitmap) pixel coordinates mapping. More...
 
- 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...
 

Public Member Functions

 SceneRenderer ()
 
 ~SceneRenderer ()
 
void setOutput (AbstractBitmap *bitmap)
 Attaches a bitmap to the renderer output. More...
 
AbstractBitmapgetOutput () const
 
void resetOutput ()
 Removes a bitmap from the renderer output, if any, and switches to on-screen rendering. More...
 
const ScenegetScene () const
 
void setScene (Scene *scene)
 
void setOutputMapping (const OutputMapping mapping)
 Specifies the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (screen or bitmap) pixel coordinates. More...
 
OutputMapping getOutputMapping () const
 Retrieves the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (screen or bitmap) pixel coordinates. More...
 
void setOutputReferenceWidth (int newWidth)
 Sets a value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture. More...
 
int getOutputReferenceWidth () const
 
void setOutputPixelsFetching (bool fetch)
 Specifies whether the output image data is pulled from GPU to CPU memory every time the rendering is done. More...
 
bool getOutputPixelsFetching () const
 Reports whether the output bitmap pixels are automatically offloaded from GPU to CPU memory every time the rendering is done. More...
 
void setBackgroundImage (AbstractBitmap *)
 Sets an image to pave the background. More...
 
AbstractBitmapgetBackgroundImage () const
 
Scene::LayerpickLayer (float x, float y, bool inPixels) const
 Retrieves a scene layer visible at a given point, if any. More...
 
void setRenderingEventListener (RenderingContext::EventListener *eventListener)
 
- Public Member Functions inherited from Beatmup::AbstractTask
virtual void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Member Functions

bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 

Private Member Functions

void renderLayer (RenderingContext &context, TaskThread &thread, Scene::Layer &layer, const AffineMapping &base, unsigned int recursionLevel=0)
 
bool doRender (GraphicPipeline &gpu, TaskThread &thread)
 

Private Attributes

Scenescene
 content to render More...
 
AbstractBitmapbackground
 used to pave the screen before rendering More...
 
AbstractBitmapoutput
 output bitmap More...
 
OutputMapping outputMapping
 specifies how the scene coordinates [0,1] are mapped to the output (screen or bitmap) More...
 
AffineMapping outputCoords
 the actual output mapping used during the last rendering More...
 
ImageResolution resolution
 last rendered resolution More...
 
int referenceWidth
 value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture More...
 
bool outputPixelsFetching
 if true, the output bitmap data is fetched from GPU to CPU RAM every time the rendering is done More...
 
GL::TextureHandlercameraFrame
 last got camera frame; set to NULL before rendering, then asked from outside through eventListener More...
 
RenderingContext::EventListenereventListener
 

Static Private Attributes

static const unsigned int MAX_RECURSION_LEVEL = 256
 

Additional Inherited Members

- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Detailed Description

AbstractTask rendering a Scene.

The rendering may be done to a given bitmap or on screen, if the platform supports on-screen rendering.

Definition at line 29 of file renderer.h.

Member Enumeration Documentation

◆ OutputMapping

Scene coordinates to output (screen or bitmap) pixel coordinates mapping.

Enumerator
STRETCH 

output viewport covers entirely the scene axis span, aspect ratio is not preserved in general

FIT_WIDTH_TO_TOP 

width is covered entirely, height is resized to keep aspect ratio, the top borders are aligned

FIT_WIDTH 

width is covered entirely, height is resized to keep aspect ratio, point (0.5, 0.5) is mapped to the output center

FIT_HEIGHT 

height is covered entirely, width is resized to keep aspect ratio, point (0.5, 0.5) is mapped to the output center

Definition at line 34 of file renderer.h.

34  {
35  STRETCH = 0, //!< output viewport covers entirely the scene axis span, aspect ratio is not preserved in general
36  FIT_WIDTH_TO_TOP, //!< width is covered entirely, height is resized to keep aspect ratio, the top borders are aligned
37  FIT_WIDTH, //!< width is covered entirely, height is resized to keep aspect ratio, point (0.5, 0.5) is mapped to the output center
38  FIT_HEIGHT //!< height is covered entirely, width is resized to keep aspect ratio, point (0.5, 0.5) is mapped to the output center
39  };
@ STRETCH
output viewport covers entirely the scene axis span, aspect ratio is not preserved in general
Definition: renderer.h:35
@ FIT_HEIGHT
height is covered entirely, width is resized to keep aspect ratio, point (0.5, 0.5) is mapped to the ...
Definition: renderer.h:38
@ FIT_WIDTH
width is covered entirely, height is resized to keep aspect ratio, point (0.5, 0.5) is mapped to the ...
Definition: renderer.h:37
@ FIT_WIDTH_TO_TOP
width is covered entirely, height is resized to keep aspect ratio, the top borders are aligned
Definition: renderer.h:36

Constructor & Destructor Documentation

◆ SceneRenderer()

SceneRenderer::SceneRenderer ( )

Definition at line 236 of file renderer.cpp.

236  :
237  scene(nullptr), background(nullptr), output(nullptr),
239  referenceWidth(0),
240  outputPixelsFetching(false),
241  eventListener(nullptr)
242 {}
RenderingContext::EventListener * eventListener
Definition: renderer.h:51
int referenceWidth
value overriding output width for elements that have their size in pixels, in order to render a resol...
Definition: renderer.h:48
AbstractBitmap * output
output bitmap
Definition: renderer.h:44
Scene * scene
content to render
Definition: renderer.h:42
bool outputPixelsFetching
if true, the output bitmap data is fetched from GPU to CPU RAM every time the rendering is done
Definition: renderer.h:49
OutputMapping outputMapping
specifies how the scene coordinates [0,1] are mapped to the output (screen or bitmap)
Definition: renderer.h:45
AbstractBitmap * background
used to pave the screen before rendering
Definition: renderer.h:43

◆ ~SceneRenderer()

SceneRenderer::~SceneRenderer ( )

Definition at line 245 of file renderer.cpp.

245 {}

Member Function Documentation

◆ renderLayer()

void SceneRenderer::renderLayer ( RenderingContext context,
TaskThread thread,
Scene::Layer layer,
const AffineMapping base,
unsigned int  recursionLevel = 0 
)
private

Definition at line 37 of file renderer.cpp.

37  {
38  if (recursionLevel >= MAX_RECURSION_LEVEL)
39  return;
40 
41  switch (layer.getType()) {
42 
45  for (int i = 0; i < scene.getLayerCount() && !thread.isTaskAborted(); ++i) {
46  Scene::Layer& l = scene.getLayer(i);
47  if (l.isVisible())
48  renderLayer(context, thread, l, base * layer.getMapping(), recursionLevel + 1);
49  }
50  }
51  break;
52 
53  default:
54  context.setMapping(base * layer.getMapping());
55  layer.render(context);
56  break;
57  }
58 }
void setMapping(const AffineMapping &mapping)
static const unsigned int MAX_RECURSION_LEVEL
Definition: renderer.h:54
const Scene * getScene() const
Definition: renderer.cpp:61
void renderLayer(RenderingContext &context, TaskThread &thread, Scene::Layer &layer, const AffineMapping &base, unsigned int recursionLevel=0)
Definition: renderer.cpp:37
Abstract scene layer having name, type, geometry and some content to display.
Definition: scene.h:64
@ SceneLayer
layer containing a scene
bool isVisible() const
Returns layer visibility flag.
Definition: scene.h:114
virtual void render(RenderingContext &context)
Definition: scene.h:78
Type getType() const
Definition: scene.h:92
AffineMapping & getMapping()
Definition: scene.h:97
LayerType & castTo() const
Definition: scene.h:132
Layer containing an entire scene.
Definition: scene.h:141
An ordered set of layers representing a renderable content.
Definition: scene.h:37
Layer * getLayer(const char *name) const
Retrieves a layer by its name or null if not found.
Definition: scene.cpp:163
int getLayerCount() const
Returns total number of layers in the scene.
Definition: scene.cpp:199
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.
Beatmup::Scene::Layer * layer

◆ doRender()

bool SceneRenderer::doRender ( GraphicPipeline gpu,
TaskThread thread 
)
private

Definition at line 147 of file renderer.cpp.

147  {
148  // check if there is the content to render
149  if (!scene)
150  return true;
151 
152  // init rendering context
154  RenderingContext context(gpu, eventListener,
155  resolution,
157  output == nullptr);
158 
159  // set output
160  if (output) {
161  context.writeLock(&gpu, output, ProcessingTarget::GPU);
162  gpu.bindOutput(*output);
163  }
164  else {
165  gpu.unbindOutput();
166  }
167 
168  // background
169  if (background) {
170  context.lockBitmap(background);
172  }
173 
174  // enable blending
176 
177  // compute initial mapping
179  switch (outputMapping) {
180  case FIT_WIDTH_TO_TOP:
182  break;
183  case FIT_WIDTH:
185  outputCoords.setCenterPosition(Point(0.5f, 0.5f));
186  break;
187  case FIT_HEIGHT:
189  outputCoords.setCenterPosition(Point(0.5f, 0.5f));
190  break;
191  case STRETCH:
192  // identity is okay
193  break;
194  }
195 
196  // go
197  for (int i = 0; i < scene->getLayerCount() && !thread.isTaskAborted(); ++i) {
199  if (layer.isVisible())
200  renderLayer(context, thread, layer, outputCoords);
201  }
202 
203  // finalize output
204  if (!thread.isTaskAborted()) {
205  if (!output)
206  gpu.swapBuffers();
207  else if (outputPixelsFetching) {
208  context.writeLock(&gpu, output, ProcessingTarget::CPU);
209  gpu.pullPixels(*output);
210  context.unlock(output);
211  }
212  }
213 
214  // unlock all
215  context.unlockAll();
216  return true;
217 }
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
void setCenterPosition(const Point &newPos)
Adjusts the mapping origin so that the center of the axes box matches a given point.
Definition: geometry.cpp:67
void scale(numeric factor)
Definition: geometry.h:399
void paveBackground(GraphicPipeline *gpu, TextureHandler &content, GL::TextureHandler *output)
Fills background with a repeated texture taking 1 pixel of this texture per 1 pixel of the output.
void switchMode(Mode mode)
Switches GPU mode.
Definition: pipeline.cpp:941
const ImageResolution & getDisplayResolution() const
Definition: pipeline.cpp:916
void pullPixels(AbstractBitmap &bitmap)
Transfers bitmap pixels from GPU to CPU.
Definition: pipeline.cpp:921
void bindOutput(AbstractBitmap &bitmap)
Binds a bitmap to the pipeline output.
Definition: pipeline.cpp:891
@ RENDERING
Textures are images to be blended together to produce another image.
void unbindOutput()
Unbinds a bitmap from output and switches to screen.
Definition: pipeline.cpp:911
GL::RenderingPrograms & getRenderingPrograms()
Definition: pipeline.h:125
unsigned int getWidth() const
Stores the rendering context: current program, current mapping in the scene space,...
AffineMapping outputCoords
the actual output mapping used during the last rendering
Definition: renderer.h:46
ImageResolution resolution
last rendered resolution
Definition: renderer.h:47
CustomPoint< float > Point
Definition: geometry.h:626

◆ process()

bool SceneRenderer::process ( TaskThread thread)
protectedvirtual

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

Reimplemented from Beatmup::GpuTask.

Definition at line 230 of file renderer.cpp.

230  {
231  throw RuntimeError("GPU is required for rendering");
232  return true;
233 }

◆ processOnGPU()

bool SceneRenderer::processOnGPU ( GraphicPipeline gpu,
TaskThread thread 
)
protectedvirtual

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 220 of file renderer.cpp.

220  {
221  if (!scene)
222  return true;
223  Scene::LockGuard lock(scene);
224 
225  // go
226  return doRender(gpu, thread);
227 }
bool doRender(GraphicPipeline &gpu, TaskThread &thread)
Definition: renderer.cpp:147

◆ beforeProcessing()

void SceneRenderer::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 138 of file renderer.cpp.

138  {
139  if (eventListener)
141 
142  // reset camera frame
143  cameraFrame = nullptr;
144 }
GL::TextureHandler * cameraFrame
last got camera frame; set to NULL before rendering, then asked from outside through eventListener
Definition: renderer.h:50

◆ setOutput()

void SceneRenderer::setOutput ( AbstractBitmap bitmap)

Attaches a bitmap to the renderer output.

Definition at line 76 of file renderer.cpp.

76  {
77  this->output = output;
78 }

◆ getOutput()

AbstractBitmap * SceneRenderer::getOutput ( ) const
Returns
a pointer to output bitmap

Definition at line 66 of file renderer.cpp.

66  {
67  return output;
68 }

◆ resetOutput()

void SceneRenderer::resetOutput ( )

Removes a bitmap from the renderer output, if any, and switches to on-screen rendering.

The rendering is done on the display currently connected to the Context running the rendering task.

Definition at line 81 of file renderer.cpp.

81  {
82  this->output = nullptr;
83 }

◆ getScene()

const Scene * SceneRenderer::getScene ( ) const

Definition at line 61 of file renderer.cpp.

61  {
62  return scene;
63 }

◆ setScene()

void SceneRenderer::setScene ( Scene scene)

Definition at line 71 of file renderer.cpp.

71  {
72  this->scene = scene;
73 }

◆ setOutputMapping()

void SceneRenderer::setOutputMapping ( const OutputMapping  mapping)

Specifies the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (screen or bitmap) pixel coordinates.

Parameters
mappingThe coordinates mapping

Definition at line 86 of file renderer.cpp.

86  {
87  outputMapping = newMapping;
88 }

◆ getOutputMapping()

SceneRenderer::OutputMapping SceneRenderer::getOutputMapping ( ) const

Retrieves the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (screen or bitmap) pixel coordinates.

Returns
the mapping.

Definition at line 91 of file renderer.cpp.

91  {
92  return outputMapping;
93 }

◆ setOutputReferenceWidth()

void SceneRenderer::setOutputReferenceWidth ( int  newWidth)

Sets a value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture.

Parameters
newWidthThe new reference width in pixels. If set negative or zero, the actual output image width is used.

Definition at line 96 of file renderer.cpp.

96  {
97  referenceWidth = newWidth;
98 }

◆ getOutputReferenceWidth()

int SceneRenderer::getOutputReferenceWidth ( ) const
Returns
value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture.

Definition at line 101 of file renderer.cpp.

101  {
102  return referenceWidth;
103 }

◆ setOutputPixelsFetching()

void SceneRenderer::setOutputPixelsFetching ( bool  fetch)

Specifies whether the output image data is pulled from GPU to CPU memory every time the rendering is done.

This is convenient if the rendered image is an application output result, and is further stored or sent through the network. Otherwise, if the image is to be further processed inside Beatmup, the pixel transfer likely introduces an unnecessary latency and may cause FPS drop in real-time rendering. Has no effect in on-screen rendering.

Parameters
[in]fetchIf true, pixels are pulled to CPU memory.

Definition at line 111 of file renderer.cpp.

111  {
112  this->outputPixelsFetching = fetch;
113 }
jlong jboolean fetch

◆ getOutputPixelsFetching()

bool SceneRenderer::getOutputPixelsFetching ( ) const

Reports whether the output bitmap pixels are automatically offloaded from GPU to CPU memory every time the rendering is done.

Definition at line 116 of file renderer.cpp.

116  {
117  return outputPixelsFetching;
118 }

◆ setBackgroundImage()

void SceneRenderer::setBackgroundImage ( AbstractBitmap bitmap)

Sets an image to pave the background.

Definition at line 106 of file renderer.cpp.

106  {
107  background = bitmap;
108 }
Beatmup::InternalBitmap * bitmap

◆ getBackgroundImage()

AbstractBitmap* Beatmup::SceneRenderer::getBackgroundImage ( ) const
inline
Returns
the bitmap currently set to fill the background, null if not set.

Definition at line 132 of file renderer.h.

132 { return background; }

◆ pickLayer()

Scene::Layer * SceneRenderer::pickLayer ( float  x,
float  y,
bool  inPixels 
) const

Retrieves a scene layer visible at a given point, if any.

In contrast to Scene::getLayer() it takes into account the output mapping. "Phantom" layers are ignored.

Parameters
xHorizontal coordinate.
yVertical coordinate.
inPixelsIf true, the coordinates are taken in pixels.
Returns
the topmost layer at the given position if any, null if no layer found.

Definition at line 121 of file renderer.cpp.

121  {
122  if (scene == nullptr)
123  return nullptr;
124  if (inPixels) {
126  return scene->getLayer(p.x, p.y);
127  }
128  else
129  return scene->getLayer(x, y);
130 }
AffineMapping getInverse() const
Returns inverse mapping.
Definition: geometry.cpp:52
unsigned int getHeight() const
jobject jlong jint jint y
jobject jlong jint x
jlong jboolean inPixels
Beatmup::IntPoint p((int) x,(int) y)

◆ setRenderingEventListener()

void SceneRenderer::setRenderingEventListener ( RenderingContext::EventListener eventListener)

Definition at line 133 of file renderer.cpp.

133  {
134  this->eventListener = eventListener;
135 }

Member Data Documentation

◆ scene

Scene* Beatmup::SceneRenderer::scene
private

content to render

Definition at line 42 of file renderer.h.

◆ background

AbstractBitmap* Beatmup::SceneRenderer::background
private

used to pave the screen before rendering

Definition at line 43 of file renderer.h.

◆ output

AbstractBitmap* Beatmup::SceneRenderer::output
private

output bitmap

Definition at line 44 of file renderer.h.

◆ outputMapping

OutputMapping Beatmup::SceneRenderer::outputMapping
private

specifies how the scene coordinates [0,1] are mapped to the output (screen or bitmap)

Definition at line 45 of file renderer.h.

◆ outputCoords

AffineMapping Beatmup::SceneRenderer::outputCoords
private

the actual output mapping used during the last rendering

Definition at line 46 of file renderer.h.

◆ resolution

ImageResolution Beatmup::SceneRenderer::resolution
private

last rendered resolution

Definition at line 47 of file renderer.h.

◆ referenceWidth

int Beatmup::SceneRenderer::referenceWidth
private

value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture

Definition at line 48 of file renderer.h.

◆ outputPixelsFetching

bool Beatmup::SceneRenderer::outputPixelsFetching
private

if true, the output bitmap data is fetched from GPU to CPU RAM every time the rendering is done

Definition at line 49 of file renderer.h.

◆ cameraFrame

GL::TextureHandler* Beatmup::SceneRenderer::cameraFrame
private

last got camera frame; set to NULL before rendering, then asked from outside through eventListener

Definition at line 50 of file renderer.h.

◆ eventListener

RenderingContext::EventListener* Beatmup::SceneRenderer::eventListener
private

Definition at line 51 of file renderer.h.

◆ MAX_RECURSION_LEVEL

const unsigned int Beatmup::SceneRenderer::MAX_RECURSION_LEVEL = 256
staticprivate

Definition at line 54 of file renderer.h.


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