Plug more leaks (in the test, not the core)
[gnash.git] / librender / Renderer.h
blobbc153cfcf72301de4e06740babc4cc7623dd694e
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
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, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 //
22 #ifndef RENDER_HANDLER_H
23 #define RENDER_HANDLER_H
25 /// \page Renderer_intro Render handler introduction
26 ///
27 /// Information for writing new render handlers.
28 ///
29 /// The most important thing about drawing Flash shapes is to understand how
30 /// their fill styles work.
31 /// A single Flash character can contain any number shapes that use any number
32 /// of different fill styles and line styles. The shapes of a character are
33 /// defined by a number of "paths". Some important things about paths:
34 ///
35 /// - A path is a list of connected straight lines and (quadratic bezier)
36 /// curves (=edges). Interesting to note is that in the Flash world there are
37 /// *no* primitive objects like circles, rectangles or similar. These objects
38 /// are always translated to lines and curves (a circle is a set of eight
39 /// curves).
40 ///
41 /// - All paths together must by definition always build a fully closed shape.
42 /// You can't draw a rectangle with three edges, for example, contrary to
43 /// most graphics library polygon routines that connect the last anchor to
44 /// the first. However, a *single* path does *not* have to be closed. The
45 /// missing parts may be defined by other paths (you will see this makes
46 /// sense).
47 ///
48 /// - Each path has up to two fill styles and no or one line style. The line
49 /// style should be obvious. The two fill styles define the fill to the left
50 /// (fill style zero) and to the right (fill style one) of the path if you
51 /// think of it like a vector. The fill style is defined by a index to a
52 /// list of previously defined fill style definitions. Index 0 means "no
53 /// style" and is equal to a fully transparent fill style ("hole", if you
54 /// wish).
55 ///
56 /// - Paths are *never* self-intersecting.
57 ///
58 /// Simple examples to understand this concept:
59 ///
60 /// - A rectangle that contains another rectangle. Only the area between the
61 /// two rectangles is filled (so it looks like a "o"). In this case Flash
62 /// fill create two paths (one for each rectangle) and one fill style.
63 /// Assume both paths come in clockwise order, then the outer rectangle
64 /// will have fillstyle0=0 and fillstyle1=1. The inner rectangle will have
65 /// fillstyle0=1 and fillstyle1=0.
66 ///
67 /// \code
68 /// +--------------------------------+
69 /// |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
70 /// |XXX+------------------------+XXX|
71 /// |XXX| |XXX|
72 /// |XXX+------------------------+XXX|
73 /// |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
74 /// +--------------------------------+
75 /// \endcode
76 ///
77 /// - A rectangle is divided vertically in two halves, both having different
78 /// colors:
79 ///
80 /// \code
81 /// +-------A-------+-------B--------+
82 /// |...............|################|
83 /// A...............C################B
84 /// |...............|################|
85 /// +-------A-------+-------B--------+
86 /// \endcode
87 ///
88 /// Flash will probably produce three paths (A,B,C) and two fill styles.
89 /// Paths "A" and "B" will have just one fill style (fillstyle1 will be
90 /// zero) while path "C" (which contains only one straight line!!) will
91 /// have two fill styles. To be exact the horizontal edges would not even
92 /// be necessary to render this shape (for a scanline based renderer) but
93 /// they are necessary then the character is about to be rotated.
94 ///
95 /// Now, these are simple examples but complex graphics can be compressed very
96 /// efficiently this way. Also, this method was most probably intended for a
97 /// renderer engine that can produce the final character in just one pass
98 /// (like the AGG backend does too).
101 /// \page region_update Detection of updated regions
103 /// (this applies to the whole Gnash playback architecture)
105 /// After advancing the root movie (see gnash::Gui::advance_movie) it is checked
106 /// which region of the stage has been changed visibly (by computing the
107 /// bounds around updated characters). This has two advantages:
108 ///
109 /// 1st, it allows a renderer/gui combination to avoid re-rendering of
110 /// unchanged parts in the scene. When supported by the rendering engine
111 /// this can be a huge performance gain. The original Flash player does
112 /// that too, btw. Altough he is able to define multiple smaller regions
113 /// for one frame. This could be implemented in Gnash, too.
114 ///
115 /// 2nd, it can detect still frames (like a stopped movie). gui.cpp can
116 /// detect these and completely avoid calling any rendering function.
117 ///
118 /// Of course, the most critical part is detection of changes. There is a
119 /// method gnash::character::set_invalidated() which gets called whenever a
120 /// critical property of a instance gets updated, like when it changes
121 /// position, for example.
122 /// It's really important to *always* call set_invalidated() *before*
123 /// any call that changes the character instance in a visible way.
124 ///
125 /// Even if no renderer really uses this information it has effects when
126 /// skipping unchanged frames. If necessary, this feature can be switched
127 /// off easily in gui.cpp (maybe using a runtime option?).
129 /// Note the updated region is only passed to the gnash::Gui, which is itself
130 /// responsible of informing the renderer (see gnash::Gui::set_invalidated_region).
131 /// This is because it's pointless
132 /// to have a renderer which updates only a small part of the stage when
133 /// the GUI shows it all since the area around the region is undefined.
134 /// However, there can be a GUI which supports update regions without needing
135 /// the renderer to do so (for example, to save time during blitting).
136 /// The GUI can also completely ignore the region information.
138 /// It's also importanto to note that the bounds passed to the GUI are just
139 /// a hint and the GUI /is/ allowed to further process and alter the information
140 /// in any way.
141 ///
142 /// As for the integer/float discussion: I used SWFRect (floats) because all
143 /// the bounds calculation involves floats anyway and so it's probably
144 /// faster than converting between ints and floats all the way.
147 #include <vector>
148 #include <boost/noncopyable.hpp>
150 #include "dsodefs.h" // for DSOEXPORT
152 #include "GnashEnums.h"
153 #include "Range2d.h"
154 #include "Point2d.h"
155 #include "RGBA.h"
156 #include "log.h"
157 #include "snappingrange.h"
158 #include "SWFRect.h"
160 // Forward declarations.
161 namespace gnash {
162 class IOChannel;
163 class CachedBitmap;
164 class rgba;
165 class Transform;
166 class SWFMatrix;
167 class FillStyle;
168 class LineStyle;
169 class Shape;
170 class MorphShape;
172 // XXX: GnashImageProxy (delayed image rendering)
173 class GnashVaapiImageProxy;
175 namespace SWF {
176 class ShapeRecord;
178 namespace image {
179 class GnashImage;
183 namespace gnash {
185 /// Base class for render handlers.
187 /// You must define a subclass of Renderer, and pass an
188 /// instance to the core (RunResources) *before* any SWF parsing begins.
190 /// For more info see page \ref Renderer_intro.
191 class DSOEXPORT Renderer : boost::noncopyable
193 public:
195 Renderer()
197 _quality(QUALITY_HIGH)
200 virtual ~Renderer() {}
202 /// Return a description of this renderer.
203 virtual std::string description() const = 0;
205 /// ==================================================================
206 /// Interfaces for adjusting renderer output.
207 /// ==================================================================
209 /// Sets the x/y scale for the movie
210 virtual void set_scale(float /*xscale*/, float /*yscale*/) {}
212 /// Sets the x/y offset for the movie in pixels. This applies to all
213 /// graphics drawn except the background, which must be drawn for the
214 /// entire canvas, regardless of the translation.
215 virtual void set_translation(float /*xoff*/, float /*yoff*/) {}
217 void setQuality(Quality q) { _quality = q; }
219 /// ==================================================================
220 /// Caching utitilies for core.
221 /// ==================================================================
223 /// \brief
224 /// Given an image, returns a pointer to a bitmap_info class
225 /// that can later be passed to FillStyleX_bitmap(), to set a
226 /// bitmap fill style.
227 virtual CachedBitmap* createCachedBitmap(
228 std::auto_ptr<image::GnashImage> im) = 0;
231 /// ==================================================================
232 /// Rendering Interface.
233 /// ==================================================================
235 /// Draws a video frame.
237 /// The frame has already been decoded and is available in RGB format only.
238 ///
239 /// @param frame The RGB video buffer frame.
240 /// Ownership of the buffer is left to the caller.
242 /// @param mat The SWFMatrix with world coordinates used to retrieve the x
243 /// and y coordinate of the video object. The scaling of the SWFMatrix
244 /// only refers to the Flash instance, *not* to the video inside that
245 /// instance. When a video object is placed on the stage and the loaded
246 /// video is smaller, then the SWFMatrix is still an "identity
247 /// matrix". However, if the video object is scaled via ActionScript,
248 /// for example, then the SWFMatrix will change. This means the
249 /// renderer has to find the correct scaling for the video inside the
250 /// bounds.
252 /// @param bounds The minX/minY fields of this SWFRect are always zero.
253 /// The width and height determine the size of the Flash video instance
254 /// on the stage (in TWIPS) prior to SWFMatrix transformations.
256 virtual void drawVideoFrame(image::GnashImage* frame,
257 const Transform& xform, const SWFRect* bounds, bool smooth) = 0;
259 /// Draw a line-strip directly, using a thin, solid line.
261 /// Can be used to draw empty boxes and cursors.
263 /// @coords an array of 16-bit signed integer coordinates. Even indices
264 /// (and 0) are x coordinates, while uneven ones are y coordinates.
266 /// @vertex_count the number of x-y coordinates (vertices).
268 /// @color the color to be used to draw the line strip.
270 /// @mat the SWFMatrix to be used to transform the vertices.
271 virtual void drawLine(const std::vector<point>& coords,
272 const rgba& color, const SWFMatrix& mat) = 0;
274 /// Draw a simple, solid filled polygon with a thin (~1 pixel) outline.
276 /// This can't be used for
277 /// Flash shapes but is intended for internal drawings like bounding boxes
278 /// (editable text fields) and similar. The polygon should not contain
279 /// self-intersections. If you do not wish a outline or a fill, then simply
280 /// set the alpha value to zero.
282 /// The polygon need NOT be closed (ie: this function will automatically
283 /// add an additional vertex to close it.
285 /// When masked==false, then any potential mask currently active will be
286 /// ignored, otherwise it is respected.
288 virtual void draw_poly(const std::vector<point>& corners,
289 const rgba& fill, const rgba& outline, const SWFMatrix& mat,
290 bool masked) = 0;
292 virtual void drawShape(const SWF::ShapeRecord& shape,
293 const Transform& xform) = 0;
295 /// \brief
296 /// Draws a glyph (font character).
298 /// Glyphs are defined just like shape characters with the difference that
299 /// they do not have any fill or line styles.
300 /// Instead, the shape must be drawn using the given color (solid fill).
301 /// Please note that although the glyph paths may indicate subshapes,
302 /// the renderer is to ignore that information.
303 ///
304 /// @param def
306 /// @param mat
308 /// @param color
309 virtual void drawGlyph(const SWF::ShapeRecord& rec, const rgba& color,
310 const SWFMatrix& mat) = 0;
312 /// Draw the current rendering buffer to an image file.
314 /// Although this can be done at any time during the rendering cycle
315 /// without harmful side effects, it's advisable only to do it when
316 /// between advance() calls, when the frame is fully renderered.
318 /// @param io The IOChannel to write to.
319 /// @param type The type of image output required (PNG, JPEG, GIF).
320 /// Note that not all FileTypes are images: rendering
321 /// to an FLV will not work.
322 virtual void renderToImage(boost::shared_ptr<IOChannel> /*io*/,
323 FileType /*type*/, int /*quality*/) const {
325 log_debug(_("Rendering to image not implemented for this "
326 "renderer"));
330 /// ==================================================================
331 /// Prepare drawing area and other utilities
332 /// ==================================================================
334 /// Sets the update region (called prior to begin_display).
336 /// The renderer
337 /// might do clipping and leave the region outside these bounds unchanged,
338 /// but he is allowed to change them if that makes sense. After rendering
339 /// a frame the area outside the invalidated region can be undefined and
340 /// is not used.
342 /// It is not required for all renderers.
343 /// Parameters are world coordinates (TWIPS).
345 /// For more info see page \ref region_update.
346 virtual void set_invalidated_regions(const InvalidatedRanges& /*ranges*/)
350 /// ==================================================================
351 /// Machinery for delayed images rendering (e.g. Xv with YV12 or VAAPI)
352 /// ==================================================================
354 ///@{
355 typedef boost::shared_ptr<GnashVaapiImageProxy> RenderImage;
356 typedef std::vector<RenderImage> RenderImages;
358 // Get first render image
359 virtual RenderImages::const_iterator getFirstRenderImage() const
360 { return _render_images.begin(); }
362 // Get last render image
363 virtual RenderImages::const_iterator getLastRenderImage() const
364 { return _render_images.end(); }
366 ///@}
368 ///@{ Masks
370 /// Masks are defined by drawing calls enclosed by begin_submit_mask()
371 /// and end_submit_mask(). Between these two calls, no drawing is to
372 /// occur. The shapes rendered between the two calls define the
373 /// visible region of the mask. Graphics that are irrelevant in the
374 /// context of a mask (lines and fill styles, for example) should be
375 /// ignored. After use, disable_mask() is called to remove the mask.
377 /// Masks may be nested. That is, end_submit_mask() may be followed
378 /// by a call to begin_submit_mask(). The resulting mask shall be an
379 /// intersection of the previously created mask. disable_mask() shall
380 /// result in the disabling or destruction of the last created mask.
381 virtual void begin_submit_mask() = 0;
382 virtual void end_submit_mask() = 0;
383 virtual void disable_mask() = 0;
384 ///@}
386 /// ==================================================================
387 /// Interface for querying the renderer.
388 /// ==================================================================
390 /// Converts world coordinates to pixel coordinates
391 virtual geometry::Range2d<int> world_to_pixel(const SWFRect& worldbounds)
392 const = 0;
394 geometry::Range2d<int> world_to_pixel(const geometry::Range2d<int>& wb)
395 const
397 if ((wb.isNull() || wb.isWorld())) return wb;
398 return world_to_pixel(SWFRect(wb.getMinX(), wb.getMinY(),
399 wb.getMaxX(), wb.getMaxY()));
402 /// Converts pixel coordinates to world coordinates (TWIPS)
403 virtual point pixel_to_world(int x, int y) const = 0;
405 geometry::Range2d<int> pixel_to_world(
406 const geometry::Range2d<int>& pixelbounds) const
408 point topleft = pixel_to_world(
409 pixelbounds.getMinX(), pixelbounds.getMinY());
410 point bottomright = pixel_to_world(
411 pixelbounds.getMaxX(), pixelbounds.getMaxY());
413 return geometry::Range2d<int> (topleft.x, topleft.y,
414 bottomright.x, bottomright.y);
417 /// \brief
418 /// Checks if the given bounds are (partially) in the current drawing
419 /// clipping area.
421 /// A render handler implementing invalidated bounds should implement
422 /// this method to avoid rendering of characters that are not visible
423 /// anyway.
424 /// By default this method always returns true, which will ensure correct
425 /// rendering. If possible, it should be re-implemented by the renderer
426 /// handler for better performance.
427 /// b contains TWIPS coordinates.
428 virtual bool bounds_in_clipping_area(const geometry::Range2d<int>& /*b*/)
429 const {
430 return true;
433 #ifdef USE_TESTSUITE
435 /// ==================================================================
436 /// Interfaces for testing only. Disabled when the testsuite isn't built.
437 /// ==================================================================
440 /// This function returns the color at any position in the stage. It is used
441 /// for automatic testing only, it should not be used for anything else!
442 /// x and y are pixel coordinates (<0 won't make any sense) and the color of
443 /// the nearest pixel is returned.
444 /// The function returns false when the coordinates are outside the
445 /// main frame buffer.
446 virtual bool getPixel(rgba& /*color_return*/, int /*x*/, int /*y*/) const {
448 log_debug("getPixel() not implemented for this renderer");
449 abort();
450 return false; // avoid compiler warning
453 /// \brief
454 /// Initializes the renderer for off-screen rendering used by the
455 /// testsuite.
457 /// This is a special function used for testcases ONLY. It is used by
458 /// MovieTester to prepare the renderer for off-screen rendering
459 /// without any GUI. The renderer is responsible to do all required
460 /// steps so that rendering is possible after the call. This may mean
461 /// that the renderer allocates memory for the given stage size.
462 ///
463 /// The function returns false when the renderer is not able to do
464 /// off-screen rendering (default).
466 /// Note the function may be called again afterwards, resizing the stage.
467 /// Any number of calls to this function is possible and the renderer
468 /// is responsible to resize any buffer instead of wasting memory.
470 /// @param width stage width in pixels
472 /// @param height stage height in pixels
473 virtual bool initTestBuffer(unsigned /*width*/, unsigned /*height*/) {
474 return false;
477 /// Return color depth (bits per pixel) or 0 if unknown/unimplemented.
479 /// Default implementation returns 0 (unknown).
481 /// TODO: this should be a pure abstract function, just don't want
482 /// to scan ogl and cairo backend for an implementation *now*
483 /// but would be needed for automated testing... Quinn, can you help ?
484 virtual unsigned int getBitsPerPixel() const {
485 return 0;
488 #endif
490 class External
492 public:
493 /// Prepare the renderer for external rendering
495 /// Note that all arguments except the background colour are useless
496 /// outside the ogl renderer.
497 External(Renderer& r, const rgba& c, int w = 0, int h = 0,
498 float x0 = 0, float x1 = 0, float y0 = 0, float y1 = 0)
500 _r(r)
502 _r.begin_display(c, w, h, x0, x1, y0, y1);
505 ~External() {
506 _r.end_display();
509 private:
510 Renderer& _r;
513 class Internal
515 public:
516 /// Prepare the renderer for internal rendering
517 Internal(Renderer& r, image::GnashImage& im)
519 _r(r),
520 _ext(_r.startInternalRender(im))
524 Renderer* renderer() const {
525 return _ext;
528 ~Internal() {
529 _r.endInternalRender();
532 private:
533 Renderer& _r;
534 Renderer* _ext;
537 protected:
539 /// Kept in parallel with movie_root's setting.
540 Quality _quality;
542 // Delayed imaged to render
543 RenderImages _render_images;
545 private:
547 /// Bracket the displaying of a frame from a movie.
549 /// Set up to render a full frame from a movie and fills the
550 /// background. Sets up necessary transforms, to scale the
551 /// movie to fit within the given dimensions. Call
552 /// end_display() when you're done.
554 /// Most of the arguments are only for the ogl renderer. See documentation
555 /// in that class. Do not use these arguments for new renderers!
556 virtual void begin_display(const rgba& background_color,
557 int viewport_width, int viewport_height,
558 float x0, float x1, float y0, float y1) = 0;
560 virtual void end_display() = 0;
562 /// Setup the renderer to draw to an internal buffer.
564 /// Implementations are free to return a new renderer if they choose.
566 /// @return 0 if the renderer does not support this.
567 virtual Renderer* startInternalRender(image::GnashImage& buffer) = 0;
569 /// Finish internal rendering.
571 /// Any rendering after this function has been called must apply to the
572 /// external buffer.
573 virtual void endInternalRender() = 0;
577 } // namespace gnash
579 #endif
582 // Local Variables:
583 // mode: C++
584 // indent-tabs-mode: t
585 // End: