[cairo-qpainter] Create QPixmaps with an alpha channel if required

This commit is contained in:
Vladimir Vukicevic 2008-04-28 18:15:30 -07:00
Родитель 464e39c53b
Коммит b45c5dd1f4
8 изменённых файлов: 52 добавлений и 21 удалений

Просмотреть файл

@ -392,14 +392,13 @@ _cairo_qpainter_surface_create_similar (void *abstract_surface,
if (!do_image && (!_qpixmaps_have_no_alpha || content == CAIRO_CONTENT_COLOR)) {
cairo_surface_t *result =
cairo_qpainter_surface_create_with_qpixmap (width, height);
cairo_qpainter_surface_create_with_qpixmap (content, width, height);
if (cairo_surface_get_content (result) == content) {
D(fprintf(stderr, "qpixmap\n"));
D(fprintf(stderr, "qpixmap content: %d\n", content));
return result;
}
fprintf (stderr, " pixmaps have no alpha!\n");
_qpixmaps_have_no_alpha = TRUE;
cairo_surface_destroy (result);
}
@ -602,7 +601,7 @@ _cairo_qpainter_surface_clone_similar (void *abstract_surface,
if (qs->image == NULL && (!_qpixmaps_have_no_alpha || src->content == CAIRO_CONTENT_COLOR))
{
new_surf = cairo_qpainter_surface_create_with_qpixmap
(width, height);
(src->content, width, height);
if (cairo_surface_get_content (new_surf) != src->content) {
cairo_surface_destroy (new_surf);
_qpixmaps_have_no_alpha = TRUE;
@ -1571,11 +1570,16 @@ cairo_qpainter_surface_create_with_qimage (cairo_format_t format,
}
cairo_surface_t *
cairo_qpainter_surface_create_with_qpixmap (int width,
cairo_qpainter_surface_create_with_qpixmap (cairo_content_t content,
int width,
int height)
{
cairo_qpainter_surface_t *qs;
if (content != CAIRO_CONTENT_COLOR &&
content != CAIRO_CONTENT_COLOR_ALPHA)
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
qs = (cairo_qpainter_surface_t *) malloc (sizeof(cairo_qpainter_surface_t));
if (qs == NULL)
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
@ -1584,9 +1588,14 @@ cairo_qpainter_surface_create_with_qpixmap (int width,
QPixmap *pixmap = new QPixmap (width, height);
_cairo_surface_init (&qs->base, &cairo_qpainter_surface_backend,
pixmap->hasAlphaChannel() ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR);
// By default, a QPixmap is opaque; however, if it's filled
// with a color with a transparency component, it is converted
// to a format that preserves transparency.
if (content == CAIRO_CONTENT_COLOR_ALPHA)
pixmap->fill(Qt::transparent);
_cairo_surface_init (&qs->base, &cairo_qpainter_surface_backend,
content);
qs->pixmap = pixmap;

Просмотреть файл

@ -55,7 +55,8 @@ cairo_qpainter_surface_create_with_qimage (cairo_format_t format,
int height);
cairo_public cairo_surface_t *
cairo_qpainter_surface_create_with_qpixmap (int width,
cairo_qpainter_surface_create_with_qpixmap (cairo_content_t content,
int width,
int height);
cairo_public QPainter *

Просмотреть файл

@ -140,6 +140,8 @@ public:
*/
virtual PRInt32 GetDefaultContextFlags() const { return 0; }
static gfxContentType ContentFromFormat(gfxImageFormat format);
protected:
gfxASurface() : mSurface(nsnull), mFloatingRefs(0), mSurfaceValid(PR_FALSE) { }

Просмотреть файл

@ -48,7 +48,7 @@ class THEBES_API gfxQPainterSurface : public gfxASurface {
public:
gfxQPainterSurface(QPainter *painter);
gfxQPainterSurface(const gfxIntSize& size, gfxImageFormat format);
gfxQPainterSurface(const gfxIntSize& size);
gfxQPainterSurface(const gfxIntSize& size, gfxContentType content);
gfxQPainterSurface(cairo_surface_t *csurf);

Просмотреть файл

@ -354,3 +354,21 @@ gfxASurface::EndPage()
{
return NS_OK;
}
gfxASurface::gfxContentType
gfxASurface::ContentFromFormat(gfxImageFormat format)
{
switch (format) {
case ImageFormatARGB32:
return CONTENT_COLOR_ALPHA;
case ImageFormatRGB24:
return CONTENT_COLOR;
case ImageFormatA8:
case ImageFormatA1:
return CONTENT_ALPHA;
case ImageFormatUnknown:
default:
return CONTENT_COLOR;
}
}

Просмотреть файл

@ -59,9 +59,10 @@ gfxQPainterSurface::gfxQPainterSurface(const gfxIntSize& size, gfxImageFormat fo
Init (csurf);
}
gfxQPainterSurface::gfxQPainterSurface(const gfxIntSize& size)
gfxQPainterSurface::gfxQPainterSurface(const gfxIntSize& size, gfxContentType content)
{
cairo_surface_t *csurf = cairo_qpainter_surface_create_with_qpixmap (size.width,
cairo_surface_t *csurf = cairo_qpainter_surface_create_with_qpixmap ((cairo_content_t) content,
size.width,
size.height);
mPainter = cairo_qpainter_surface_get_qpainter (csurf);

Просмотреть файл

@ -63,6 +63,10 @@
#include <ft2build.h>
#include FT_FREETYPE_H
#ifndef Q_WS_X11
#define OPTIMIZE_TO_QPIXMAP
#endif
PRInt32 gfxQtPlatform::sDPI = -1;
gfxFontconfigUtils *gfxQtPlatform::sFontconfigUtils = nsnull;
static cairo_user_data_key_t cairo_qt_pixmap_key;
@ -124,9 +128,8 @@ already_AddRefed<gfxASurface>
gfxQtPlatform::CreateOffscreenSurface(const gfxIntSize& size,
gfxASurface::gfxImageFormat imageFormat)
{
// XXX we may be able to create a QPixmap here, instead of a QImage always
nsRefPtr<gfxASurface> newSurface =
new gfxQPainterSurface (size, imageFormat);
new gfxQPainterSurface (size, gfxASurface::ContentFromFormat(imageFormat));
return newSurface.forget();
}
@ -135,10 +138,7 @@ already_AddRefed<gfxASurface>
gfxQtPlatform::OptimizeImage(gfxImageSurface *aSurface,
gfxASurface::gfxImageFormat format)
{
NS_ADDREF(aSurface);
return aSurface;
#if 0
#ifdef OPTIMIZE_TO_QPIXMAP
const gfxIntSize& surfaceSize = aSurface->GetSize();
nsRefPtr<gfxASurface> optSurface = CreateOffscreenSurface(surfaceSize, format);
@ -150,9 +150,9 @@ gfxQtPlatform::OptimizeImage(gfxImageSurface *aSurface,
tmpCtx.SetSource(aSurface);
tmpCtx.Paint();
gfxASurface *ret = optSurface;
NS_ADDREF(ret);
return ret;
return optSurface.forget();
#else
return nsnull;
#endif
}

Просмотреть файл

@ -2389,7 +2389,7 @@ nsWindow::GetThebesSurface()
mThebesSurface->SetDeviceOffset(gfxPoint(-x_offset, -y_offset));
}
#else
mThebesSurface = new gfxQPainterSurface(gfxIntSize(5,5));
mThebesSurface = new gfxQPainterSurface(gfxIntSize(5,5), gfxASurface::CONTENT_COLOR_ALPHA);
#endif
}