Beatmup
renderer.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "scene.h"
21 #include "../gpu/gpu_task.h"
22 #include <map>
23 
24 namespace Beatmup {
25  /**
26  AbstractTask rendering a Scene.
27  The rendering may be done to a given bitmap or on screen, if the platform supports on-screen rendering.
28  */
29  class SceneRenderer : public GpuTask {
30  public:
31  /**
32  Scene coordinates to output (screen or bitmap) pixel coordinates mapping
33  */
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  };
40 
41  private:
42  Scene* scene; //!< content to render
43  AbstractBitmap* background; //!< used to pave the screen before rendering
44  AbstractBitmap* output; //!< output bitmap
45  OutputMapping outputMapping; //!< specifies how the scene coordinates [0,1] are mapped to the output (screen or bitmap)
46  AffineMapping outputCoords; //!< the actual output mapping used during the last rendering
47  ImageResolution resolution; //!< last rendered resolution
48  int referenceWidth; //!< value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture
49  bool outputPixelsFetching; //!< if `true`, the output bitmap data is fetched from GPU to CPU RAM every time the rendering is done
50  GL::TextureHandler* cameraFrame; //!< last got camera frame; set to NULL before rendering, then asked from outside through eventListener
52 
53  static const unsigned int
55  void renderLayer(RenderingContext& context, TaskThread& thread, Scene::Layer& layer, const AffineMapping& base, unsigned int recursionLevel = 0);
56  bool doRender(GraphicPipeline& gpu, TaskThread& thread);
57 
58  protected:
59  bool process(TaskThread& thread);
60  bool processOnGPU(GraphicPipeline& gpu, TaskThread& thread);
61  void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline* gpu);
62  public:
63  SceneRenderer();
65 
66  /**
67  Attaches a bitmap to the renderer output
68  */
70 
71  /**
72  \return a pointer to output bitmap
73  */
74  AbstractBitmap* getOutput() const;
75 
76  /**
77  Removes a bitmap from the renderer output, if any, and switches to on-screen rendering.
78  The rendering is done on the display currently connected to the Context running the rendering task.
79  */
80  void resetOutput();
81 
82  const Scene* getScene() const;
83 
84  void setScene(Scene* scene);
85 
86  /**
87  Specifies the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (screen or bitmap) pixel coordinates.
88  \param mapping The coordinates mapping
89  */
91 
92  /**
93  Retrieves the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (screen or bitmap) pixel coordinates.
94  \return the mapping.
95  */
97 
98  /**
99  Sets a value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture.
100  \param newWidth The new reference width in pixels. If set negative or zero, the actual output image width is used.
101  */
102  void setOutputReferenceWidth(int newWidth);
103 
104  /**
105  \return value overriding output width for elements that have their size in pixels, in order to render a resolution-independent picture.
106  */
107  int getOutputReferenceWidth() const;
108 
109  /**
110  Specifies whether the output image data is pulled from GPU to CPU memory every time the rendering is done.
111  This is convenient if the rendered image is an application output result, and is further stored or sent through the network.
112  Otherwise, if the image is to be further processed inside %Beatmup, the pixel transfer likely introduces an unnecessary latency and may
113  cause FPS drop in real-time rendering.
114  Has no effect in on-screen rendering.
115  \param[in] fetch If `true`, pixels are pulled to CPU memory.
116  */
117  void setOutputPixelsFetching(bool fetch);
118 
119  /**
120  Reports whether the output bitmap pixels are automatically offloaded from GPU to CPU memory every time the rendering is done.
121  */
122  bool getOutputPixelsFetching() const;
123 
124  /**
125  Sets an image to pave the background.
126  */
128 
129  /**
130  \return the bitmap currently set to fill the background, null if not set.
131  */
132  inline AbstractBitmap* getBackgroundImage() const { return background; }
133 
134  /**
135  Retrieves a scene layer visible at a given point, if any.
136  In contrast to Scene::getLayer() it takes into account the output mapping.
137  "Phantom" layers are ignored.
138  \param x Horizontal coordinate.
139  \param y Vertical coordinate.
140  \param inPixels If `true`, the coordinates are taken in pixels.
141  \return the topmost layer at the given position if any, `null` if no layer found.
142  */
143  Scene::Layer* pickLayer(float x, float y, bool inPixels) const;
144 
146  };}
A very basic class for any image.
2x3 affine mapping containing a 2x2 matrix and a 2D point
Definition: geometry.h:639
Template of a task using GPU.
Definition: gpu_task.h:27
Internal low-level GPU control API.
Definition: pipeline.h:33
Represents image size in pixels.
Stores the rendering context: current program, current mapping in the scene space,...
AbstractTask rendering a Scene.
Definition: renderer.h:29
AbstractBitmap * getBackgroundImage() const
Definition: renderer.h:132
bool getOutputPixelsFetching() const
Reports whether the output bitmap pixels are automatically offloaded from GPU to CPU memory every tim...
Definition: renderer.cpp:116
bool process(TaskThread &thread)
Executes the task on CPU within a given thread.
Definition: renderer.cpp:230
RenderingContext::EventListener * eventListener
Definition: renderer.h:51
void setScene(Scene *scene)
Definition: renderer.cpp:71
AffineMapping outputCoords
the actual output mapping used during the last rendering
Definition: renderer.h:46
OutputMapping
Scene coordinates to output (screen or bitmap) pixel coordinates mapping.
Definition: renderer.h:34
@ 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
int referenceWidth
value overriding output width for elements that have their size in pixels, in order to render a resol...
Definition: renderer.h:48
void resetOutput()
Removes a bitmap from the renderer output, if any, and switches to on-screen rendering.
Definition: renderer.cpp:81
void setOutputPixelsFetching(bool fetch)
Specifies whether the output image data is pulled from GPU to CPU memory every time the rendering is ...
Definition: renderer.cpp:111
void setOutputMapping(const OutputMapping mapping)
Specifies the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (sc...
Definition: renderer.cpp:86
ImageResolution resolution
last rendered resolution
Definition: renderer.h:47
AbstractBitmap * output
output bitmap
Definition: renderer.h:44
bool processOnGPU(GraphicPipeline &gpu, TaskThread &thread)
Executes the task on GPU.
Definition: renderer.cpp:220
Scene * scene
content to render
Definition: renderer.h:42
void setRenderingEventListener(RenderingContext::EventListener *eventListener)
Definition: renderer.cpp:133
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
static const unsigned int MAX_RECURSION_LEVEL
Definition: renderer.h:54
void setOutput(AbstractBitmap *bitmap)
Attaches a bitmap to the renderer output.
Definition: renderer.cpp:76
const Scene * getScene() const
Definition: renderer.cpp:61
void setBackgroundImage(AbstractBitmap *)
Sets an image to pave the background.
Definition: renderer.cpp:106
Scene::Layer * pickLayer(float x, float y, bool inPixels) const
Retrieves a scene layer visible at a given point, if any.
Definition: renderer.cpp:121
OutputMapping outputMapping
specifies how the scene coordinates [0,1] are mapped to the output (screen or bitmap)
Definition: renderer.h:45
void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
Instruction called before the task is executed.
Definition: renderer.cpp:138
void renderLayer(RenderingContext &context, TaskThread &thread, Scene::Layer &layer, const AffineMapping &base, unsigned int recursionLevel=0)
Definition: renderer.cpp:37
GL::TextureHandler * cameraFrame
last got camera frame; set to NULL before rendering, then asked from outside through eventListener
Definition: renderer.h:50
OutputMapping getOutputMapping() const
Retrieves the output mapping specifying how the scene coordinates [0,1]² are mapped to the output (sc...
Definition: renderer.cpp:91
int getOutputReferenceWidth() const
Definition: renderer.cpp:101
bool doRender(GraphicPipeline &gpu, TaskThread &thread)
Definition: renderer.cpp:147
AbstractBitmap * background
used to pave the screen before rendering
Definition: renderer.h:43
AbstractBitmap * getOutput() const
Definition: renderer.cpp:66
void setOutputReferenceWidth(int newWidth)
Sets a value overriding output width for elements that have their size in pixels, in order to render ...
Definition: renderer.cpp:96
Abstract scene layer having name, type, geometry and some content to display.
Definition: scene.h:64
An ordered set of layers representing a renderable content.
Definition: scene.h:37
Thread executing tasks.
Definition: parallelism.h:154
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
ProcessingTarget
Definition: basic_types.h:55
jobject jlong jint jint y
jobject jlong jint x
Beatmup::InternalBitmap * bitmap
Beatmup::Scene::Layer * layer
jlong jboolean inPixels
jlong jboolean fetch
Beatmup::AffineMapping & mapping