Gnash  0.8.10
Renderer.h
Go to the documentation of this file.
00001 // 
00002 //     Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //     Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef RENDER_HANDLER_H
00020 #define RENDER_HANDLER_H
00021 
00096 
00097     
00142 
00143 
00144 #include <vector>
00145 #include <boost/noncopyable.hpp>
00146 
00147 #include "dsodefs.h" // for DSOEXPORT
00148 
00149 #include "boost/shared_array.hpp"
00150 #include "boost/scoped_ptr.hpp"
00151 #include "GnashEnums.h" 
00152 #include "Range2d.h"
00153 #include "Point2d.h"
00154 #include "RGBA.h"
00155 #include "log.h"
00156 #include "snappingrange.h"
00157 #include "SWFRect.h"
00158 
00159 // Forward declarations.
00160 namespace gnash {
00161     class IOChannel;
00162     class CachedBitmap;
00163     class rgba;
00164     class Transform;
00165     class SWFMatrix;
00166     class FillStyle;
00167     class LineStyle;
00168     class Shape;
00169     class MorphShape;
00170 
00171     // XXX: GnashImageProxy (delayed image rendering)
00172     class GnashVaapiImageProxy;
00173 
00174     namespace SWF {
00175         class ShapeRecord;
00176     }
00177     namespace image {
00178         class GnashImage;
00179     }
00180 }
00181 
00182 namespace gnash {
00183 
00185 //
00190 class DSOEXPORT Renderer : boost::noncopyable
00191 {
00192 public:
00193 
00194     Renderer(): _quality(QUALITY_HIGH) { }
00195     
00196     virtual ~Renderer() {}
00197 
00199     virtual std::string description() const = 0;
00200 
00204 
00206     virtual void set_scale(float /*xscale*/, float /*yscale*/) {} 
00207 
00211     virtual void set_translation(float /*xoff*/, float /*yoff*/) {}
00212 
00213     void setQuality(Quality q) { _quality = q; }
00214         
00218     
00223     virtual CachedBitmap *
00224         createCachedBitmap(std::auto_ptr<image::GnashImage> im) = 0;
00225 
00226 
00230 
00232     //
00252     virtual void drawVideoFrame(image::GnashImage* frame,
00253             const Transform& xform, const SWFRect* bounds, bool smooth) = 0;
00254 
00256     //
00267     virtual void drawLine(const std::vector<point>& coords,
00268             const rgba& color, const SWFMatrix& mat) = 0;
00269         
00271     //
00284     virtual void draw_poly(const std::vector<point>& corners, 
00285         const rgba& fill, const rgba& outline, const SWFMatrix& mat,
00286         bool masked) = 0;
00287         
00288     virtual void drawShape(const SWF::ShapeRecord& shape,
00289             const Transform& xform) = 0;
00290         
00293     //
00305     virtual void drawGlyph(const SWF::ShapeRecord& rec, const rgba& color,
00306            const SWFMatrix& mat) = 0;
00307 
00309     //
00313     //
00318     virtual void renderToImage(boost::shared_ptr<IOChannel> /*io*/,
00319         FileType /*type*/, int /*quality*/) const {
00320 
00321         log_debug(_("Rendering to image not implemented for this "
00322             "renderer"));
00323     }
00324         
00325 
00329     
00331     //
00342     virtual void set_invalidated_regions(const InvalidatedRanges& /*ranges*/)
00343     {        
00344     }
00345 
00349 
00351     typedef boost::shared_ptr<GnashVaapiImageProxy> RenderImage;
00352     typedef std::vector<RenderImage> RenderImages;
00353 
00354     // Get first render image
00355     virtual RenderImages::const_iterator getFirstRenderImage() const
00356             { return _render_images.begin(); }
00357 
00358     // Get last render image
00359     virtual RenderImages::const_iterator getLastRenderImage() const
00360             { return _render_images.end(); }
00361     
00363         
00377     virtual void begin_submit_mask() = 0;
00378     virtual void end_submit_mask() = 0;
00379     virtual void disable_mask() = 0;
00381     
00385     
00387     virtual geometry::Range2d<int> world_to_pixel(const SWFRect& worldbounds)
00388         const = 0;
00389     
00390     geometry::Range2d<int> world_to_pixel(const geometry::Range2d<int>& wb)
00391         const
00392     {
00393         if ((wb.isNull() || wb.isWorld())) return wb;
00394         return world_to_pixel(SWFRect(wb.getMinX(), wb.getMinY(),
00395                        wb.getMaxX(), wb.getMaxY()));
00396     }
00397         
00399     virtual point pixel_to_world(int x, int y) const = 0;
00400     
00401     geometry::Range2d<int> pixel_to_world(
00402                     const geometry::Range2d<int>& pixelbounds) const
00403     {
00404         point topleft = pixel_to_world(
00405                         pixelbounds.getMinX(), pixelbounds.getMinY());
00406         point bottomright = pixel_to_world(
00407                         pixelbounds.getMaxX(), pixelbounds.getMaxY());
00408         
00409         return geometry::Range2d<int> (topleft.x, topleft.y, 
00410             bottomright.x, bottomright.y);
00411     }
00412 
00416     //
00424     virtual bool bounds_in_clipping_area(const geometry::Range2d<int>& /*b*/)
00425         const {
00426         return true;
00427     }
00428 
00429 #ifdef USE_TESTSUITE
00430 
00431         
00435 
00436 
00443     virtual bool getPixel(rgba& /*color_return*/, int /*x*/, int /*y*/) const {
00444 
00445         log_debug("getPixel() not implemented for this renderer");
00446         abort();        
00447         return false; // avoid compiler warning        
00448     }
00449 
00450     void addRenderImage(boost::shared_ptr<GnashVaapiImageProxy> image) {
00451         _render_images.push_back(image);
00452     }
00453     
00464     virtual bool getAveragePixel(rgba& color_return, int x, int y, 
00465         unsigned int radius) const
00466     {
00467     
00468         assert(radius>0); 
00469     
00470         // optimization:
00471         if (radius==1) return getPixel(color_return, x, y);
00472     
00473         unsigned int r=0, g=0, b=0, a=0;
00474         
00475         x -= radius/2;
00476         y -= radius/2;
00477         
00478         int xe = x+radius;
00479         int ye = y+radius;
00480 
00481         rgba pixel;
00482         
00483         for (int yp=y; yp<ye; yp++)
00484         for (int xp=x; xp<xe; xp++)
00485         {
00486             if (!getPixel(pixel, xp, yp))
00487                 return false;
00488                 
00489             r += pixel.m_r;            
00490             g += pixel.m_g;            
00491             b += pixel.m_b;            
00492             a += pixel.m_a;            
00493         }
00494         
00495         int pcount = radius*radius; 
00496         color_return.m_r = r / pcount; 
00497         color_return.m_g = g / pcount; 
00498         color_return.m_b = b / pcount; 
00499         color_return.m_a = a / pcount; 
00500         
00501         return true;
00502     }
00503     
00524     virtual bool initTestBuffer(unsigned /*width*/, unsigned /*height*/) {
00525         return false;
00526     }
00527 
00529     //
00535     virtual unsigned int getBitsPerPixel() const {
00536         return 0;
00537     }
00538     
00539 #endif
00540 
00541     class External 
00542     {
00543     public:
00545         //
00548         External(Renderer& r, const rgba& c, int w = 0, int h = 0,
00549                 float x0 = 0, float x1 = 0, float y0 = 0, float y1 = 0)
00550             :
00551             _r(r)
00552         {
00553             _r.begin_display(c, w, h, x0, x1, y0, y1);
00554         }
00555 
00556         ~External() {
00557             _r.end_display();
00558         }
00559 
00560     private:
00561         Renderer& _r;
00562     };
00563     
00564     class Internal 
00565     {
00566     public:
00568         Internal(Renderer& r, image::GnashImage& im)
00569             :
00570             _r(r),
00571             _ext(_r.startInternalRender(im))
00572         {
00573         }
00574 
00575         Renderer* renderer() const {
00576             return _ext;
00577         }
00578 
00579         ~Internal() {
00580             _r.endInternalRender();
00581         }
00582 
00583     private:
00584         Renderer& _r;
00585         Renderer* _ext;
00586     };
00587 
00588 protected:
00589 
00591     Quality _quality;
00592 
00593     // Delayed imaged to render
00594     RenderImages _render_images;
00595 
00596 private:
00598     //
00603     //
00606     virtual void begin_display(const rgba& background_color, 
00607                     int viewport_width, int viewport_height,
00608                     float x0, float x1, float y0, float y1) = 0;
00609 
00610     virtual void end_display() = 0;
00611 
00613     //
00615     //
00617     virtual Renderer* startInternalRender(image::GnashImage& buffer) = 0;
00618 
00620     //
00623     virtual void endInternalRender() = 0;
00624 
00625 }; 
00626 
00627 } // namespace gnash
00628 
00629 #endif 
00630 
00631 
00632 // Local Variables:
00633 // mode: C++
00634 // indent-tabs-mode: nil
00635 // End: