Update with current status
[gnash.git] / gui / qt / Qt4GlueCairo.cpp
blob47edffab9d4e257264907f344ff6b56704ce17eb
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software 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
20 #ifdef HAVE_CONFIG_H
21 #include "gnashconfig.h"
22 #endif
24 #include "Qt4Gui.h"
25 #include "Qt4GlueCairo.h"
26 #include "Renderer.h"
27 #include "Renderer_cairo.h"
28 #include <QWidget>
29 #include <QRect>
31 namespace gnash
34 Qt4CairoGlue::Qt4CairoGlue()
36 _width(0),
37 _height(0),
38 _renderer(nullptr),
39 _cairo_handle(nullptr),
40 _cairo_surface(nullptr)
44 Qt4CairoGlue::~Qt4CairoGlue()
46 if (_cairo_surface)
47 cairo_surface_destroy(_cairo_surface);
50 bool
51 Qt4CairoGlue::init(int /* argc */, char *** /* argv */)
53 return true;
56 void
57 Qt4CairoGlue::initBuffer(int width, int height)
59 if (! _drawing_area)
60 return;
62 _width = width;
63 _height = height;
65 cairo_format_t cairoFormat = CAIRO_FORMAT_ARGB32;
66 QImage::Format qtFormat = QImage::Format_ARGB32;
67 switch (_drawing_area->depth()) {
68 case 24:
69 cairoFormat = CAIRO_FORMAT_RGB24;
70 qtFormat = QImage::Format_RGB32;
71 break;
72 case 32:
73 cairoFormat = CAIRO_FORMAT_ARGB32;
74 qtFormat = QImage::Format_ARGB32;
75 break;
78 // Cairo uses 4 bits per pixel even for 24 bit color depth
79 int bufsize = _width * _height * 4;
80 _offscreenbuf.reset(new unsigned char[bufsize]);
81 _image.reset(new QImage(_offscreenbuf.get(),
82 _width, _height, qtFormat));
84 if (_cairo_surface)
85 cairo_surface_destroy(_cairo_surface);
86 if (_cairo_handle)
87 cairo_destroy(_cairo_handle);
89 _cairo_surface = cairo_image_surface_create_for_data(
90 _offscreenbuf.get(), cairoFormat, _width, _height,
91 cairo_format_stride_for_width(cairoFormat, _width));
93 _cairo_handle = cairo_create(_cairo_surface);
94 cairo_set_source_surface(_cairo_handle,
95 cairo_get_target(_cairo_handle), 0, 0);
96 renderer::cairo::set_context(_renderer, _cairo_handle);
99 void
100 Qt4CairoGlue::prepDrawingArea(DrawingWidget *drawing_area)
102 assert(drawing_area);
103 _drawing_area = drawing_area;
107 void
108 Qt4CairoGlue::render()
110 QRect r(0, 0, _width, _height);
111 render(r);
115 void
116 Qt4CairoGlue::render(const QRect& updateRect)
118 assert(_drawing_area);
120 if (_cairo_handle) {
122 cairo_paint(_cairo_handle);
124 QPainter p(_drawing_area);
126 p.drawImage(updateRect, *_image, updateRect);
127 p.end();
131 void
132 Qt4CairoGlue::resize(int width, int height)
134 initBuffer(width, height);
139 Renderer*
140 Qt4CairoGlue::createRenderHandler()
142 _renderer = renderer::cairo::create_handler();
144 return _renderer;
147 // end of namespace gnash