FastUIDraw
painter_surface.hpp
Go to the documentation of this file.
1 /*!
2  * \file painter_surface.hpp
3  * \brief file painter_surface.hpp
4  *
5  * Copyright 2019 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
16  *
17  */
18 
19 
20 #ifndef FASTUIDRAW_PAINTER_SURFACE_HPP
21 #define FASTUIDRAW_PAINTER_SURFACE_HPP
22 
23 #include <fastuidraw/util/vecN.hpp>
25 #include <fastuidraw/util/rect.hpp>
26 #include <fastuidraw/image.hpp>
27 
28 namespace fastuidraw
29 {
30 /*!\addtogroup PainterBackend
31  * @{
32  */
33 
34  /*!
35  * \brief
36  * PainterSurface represents an interface to specify a buffer to
37  * which a PainterBackend renders content.
38  */
39  class PainterSurface:public reference_counted<PainterSurface>::concurrent
40  {
41  public:
42  /*!
43  * \brief
44  * Enumeration to specify the render target of a Surface
45  */
47  {
48  /*!
49  * Indicates that a \ref PainterSurface represents a color
50  * buffer; such \ref PainterSurface objects are to also have
51  * a depth buffer as well.
52  */
54 
55  /*!
56  * Indicates that a \ref PainterSurface represents a
57  * coverage buffer; such surfaces will have the
58  * blending set to \ref BlendMode::MAX and do
59  * not have a depth buffer.
60  */
62 
63  number_buffer_types,
64  };
65 
66  /*!
67  * A Viewport specifies the sub-region within a Surface
68  * to which one renders.
69  */
70  class Viewport
71  {
72  public:
73  Viewport(void):
74  m_origin(0, 0),
75  m_dimensions(1, 1)
76  {}
77 
78  /*!
79  * Ctor.
80  * \param x value with which to initialize x-coordinate of \ref m_origin
81  * \param y value with which to initialize y-coordinate of \ref m_origin
82  * \param w value with which to initialize x-coordinate of \ref m_dimensions
83  * \param h value with which to initialize y-coordinate of \ref m_dimensions
84  */
85  Viewport(int x, int y, int w, int h):
86  m_origin(x, y),
87  m_dimensions(w, h)
88  {}
89 
90  /*!
91  * Compute pixel coordinates from normalized device coords
92  * using this Viewport values. The pixel coordinates are so
93  * that (0, 0) is the bottom left.
94  * \param ndc normalized device coordinates
95  */
96  vec2
98  {
99  ndc += vec2(1.0f); // place in range [0, 2]
100  ndc *= 0.5f; // normalize to [0, 1]
101  ndc *= vec2(m_dimensions); // normalize to dimension
102  ndc += vec2(m_origin); // translate to origin
103  return ndc;
104  }
105 
106  /*!
107  * Compute viewport coordiantes from normalized device coordinates
108  * \param ndc normalized device coordinates
109  * \param dims size of viewport (i.e. the value of \ref m_dimensions)
110  */
111  static
112  vec2
114  {
115  ndc += vec2(1.0f); // place in range [0, 2]
116  ndc *= 0.5f; // normalize to [0, 1]
117  ndc *= dims; // normalize to dimension
118  return ndc;
119  }
120 
121  /*!
122  * Compute viewport coordiantes from normalized device coordinates
123  * \param ndc normalized device coordinates
124  * \param dims size of viewport (i.e. the value of \ref m_dimensions)
125  */
126  static
127  vec2
129  {
130  return compute_viewport_coordinates(ndc, vec2(dims));
131  }
132 
133  /*!
134  * Compute viewport coordinates from normalized device coords
135  * using this Viewport values. The viewport coordinates are so
136  * that (0, 0) corresponds to pixel coordinates of value \ref
137  * m_origin.
138  * \param ndc normalized device coordinates
139  */
140  vec2
142  {
144  }
145 
146  /*!
147  * Compute normalized device coordinates from pixel
148  * coordinates.
149  * \param pixel pixel coordinates where (0, 0) corresponds to bottom
150  * left of the surface
151  */
152  vec2
154  {
155  pixel -= vec2(m_origin); // translate from origin
156  pixel /= vec2(m_dimensions); // normalize to [0, 1]
157  pixel *= 2.0f; // normalize to [0, 2]
158  pixel -= vec2(1.0f); // palce in range [-1, 1]
159  return pixel;
160  }
161 
162  /*!
163  * Compute normalized device coordinates from viewport
164  * coordinates.
165  * \param viewport_coords viewport coordinates
166  */
167  vec2
169  {
170  viewport_coords /= vec2(m_dimensions); // normalize to [0, 1]
171  viewport_coords *= 2.0f; // normalize to [0, 2]
172  viewport_coords -= vec2(1.0f); // palce in range [-1, 1]
173  return viewport_coords;
174  }
175 
176  /*!
177  * Compute normalized device coordinates from viewport
178  * coordinates.
179  * \param viewport_coords viewport coordinates
180  */
181  vec2
183  {
185  }
186 
187  /*!
188  * Computes the clip-equations (in normalized device coordinates)
189  * of this Viewport against a surface with the given dimensions.
190  * \param surface_dims dimension of surface
191  * \param[out] out_clip_equations location to which to write the
192  * clip equations
193  */
194  void
195  compute_clip_equations(ivec2 surface_dims,
196  vecN<vec3, 4> *out_clip_equations) const;
197 
198  /*!
199  * Computes the rectangle in normalized device coordinates
200  * of the intersection of a backing surface with the given
201  * dimensions against this Viewport.
202  * \param surface_dims dimension of surface
203  * \param[out] out_rect location to which to write the
204  * clipping rectangle in normalized
205  * device coordinates
206  */
207  void
208  compute_normalized_clip_rect(ivec2 surface_dims,
209  Rect *out_rect) const;
210 
211  /*!
212  * Returne the size needed by a surface to contain
213  * the viewport, i.e. how many pixels the viewport
214  * covers.
215  */
216  ivec2
217  visible_dimensions(void) const
218  {
219  ivec2 return_value(m_dimensions);
220 
221  /* remove from the portion of the viewport that
222  * is below/left of the surface
223  */
224  return_value.x() += t_min(0, m_origin.x());
225  return_value.y() += t_min(0, m_origin.y());
226  return return_value;
227  }
228 
229  /*!
230  * Computes the dimensions of the intersection
231  * of this viewport against a surface with the
232  * given resolution.
233  * \param surface_dims dimension of surface
234  */
235  ivec2
236  compute_visible_dimensions(ivec2 surface_dims) const
237  {
238  ivec2 return_value(visible_dimensions());
239 
240  return_value.x() = t_min(return_value.x(), surface_dims.x());
241  return_value.y() = t_min(return_value.y(), surface_dims.y());
242  return return_value;
243  }
244 
245  /*!
246  * The origin of the viewport
247  */
249 
250  /*!
251  * The dimensions of the viewport
252  */
254  };
255 
256  virtual
257  ~PainterSurface()
258  {}
259 
260  /*!
261  * Return an \ref Image whose backing is the same as
262  * this PainterSurface. It is expected that
263  * backing \ref Image is the same for the lifetime of
264  * the PainterSurface. The caller gaurantees
265  * that the same ImageAtlas object will be passed on
266  * each call to image().
267  * \param atlas ImageAtlas to manage the returned Image
268  */
269  virtual
271  image(ImageAtlas &atlas) const = 0;
272 
273  /*!
274  * To be implemented by a derived class to return
275  * the viewport into the Surface.
276  */
277  virtual
278  const Viewport&
279  viewport(void) const = 0;
280 
281  /*!
282  * To be implemented by a derived class to set
283  * the viewport into the surface. The viewport
284  * cannot be changed while the Surface is in
285  * use by a \ref PainterBackend or \ref Painter.
286  * \param vwp new viewport into the surface to use
287  */
288  virtual
289  void
290  viewport(const Viewport &vwp) = 0;
291 
292  /*!
293  * To be implemented by a derived class to return
294  * the clear color.
295  */
296  virtual
297  const vec4&
298  clear_color(void) const = 0;
299 
300  /*!
301  * To be implemented by a derived class to set
302  * the clear color.
303  */
304  virtual
305  void
306  clear_color(const vec4&) = 0;
307 
308  /*!
309  * To be implemented by a derived class to return
310  * the dimensions of the Surface backing store.
311  */
312  virtual
313  ivec2
314  dimensions(void) const = 0;
315 
316  /*!
317  * To be implemented by a derived class to return
318  * the surface type of the Surface (i.e. is it
319  * a color buffer or a deferred coverage buffer).
320  */
321  virtual
322  enum render_type_t
323  render_type(void) const = 0;
324 
325  /*!
326  * Provided as a conveniance, equivalent to
327  * \code
328  * viewport().compute_visible_dimensions(dimensions())
329  * \endcode
330  */
331  ivec2
333  {
335  }
336  };
337 /*! @} */
338 
339 }
340 
341 #endif
void compute_clip_equations(ivec2 surface_dims, vecN< vec3, 4 > *out_clip_equations) const
render_type_t
Enumeration to specify the render target of a Surface.
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
PainterSurface represents an interface to specify a buffer to which a PainterBackend renders content...
A wrapper over a pointer to implement reference counting.
static vec2 compute_viewport_coordinates(vec2 ndc, ivec2 dims)
vec2 compute_pixel_coordinates(vec2 ndc) const
vec2 compute_normalized_device_coords(vec2 pixel) const
ivec2 compute_visible_dimensions(void) const
ivec2 compute_visible_dimensions(ivec2 surface_dims) const
virtual reference_counted_ptr< const Image > image(ImageAtlas &atlas) const =0
vecN< float, 2 > vec2
Definition: vecN.hpp:1231
vec2 compute_normalized_device_coords_from_viewport_coords(ivec2 viewport_coords) const
vec2 compute_viewport_coordinates(vec2 ndc) const
reference y(void)
Definition: vecN.hpp:443
Viewport(int x, int y, int w, int h)
const T & t_min(const T &a, const T &b)
Definition: math.hpp:43
virtual const Viewport & viewport(void) const =0
static vec2 compute_viewport_coordinates(vec2 ndc, vec2 dims)
file vecN.hpp
Defines default reference counting base classes.
vec2 compute_normalized_device_coords_from_viewport_coords(vec2 viewport_coords) const
An ImageAtlas is a common location to place images of an application.
virtual enum render_type_t render_type(void) const =0
reference x(void)
Definition: vecN.hpp:435
virtual const vec4 & clear_color(void) const =0
void compute_normalized_clip_rect(ivec2 surface_dims, Rect *out_rect) const
virtual ivec2 dimensions(void) const =0
file image.hpp
file reference_counted.hpp