[cairo] canvas drawing on itself doesn't work with accelerated canvas

https://bugs.webkit.org/show_bug.cgi?id=118808

Reviewed by Martin Robinson.

Source/WebCore:

When copying an accelerated image, we try to get its dimensions with
cairo_image_surface_get_width/cairo_image_surface_get_height. As
surface is not an image, this returns width and height of 0.

Many other places use cairo_image_surface_get although the surface may
be a gl surface.

This patch fixes those issues by implementing a cairoSurfaceSize
helper that returns the surface size whatever type it is.

It use cairo_surface_create_similar instead of
cairo_image_surface_create in copyCairoImageSurface. It also calls
cairo_paint in encodeImage when a drawing over a black background is
needed.

It copies the surface to an image surface if needed in extractImage.

No new tests. Covered by existing tests.

* platform/graphics/cairo/BitmapImageCairo.cpp:
(WebCore::BitmapImage::BitmapImage):
(WebCore::BitmapImage::draw):
(WebCore::BitmapImage::checkForSolidColor):
* platform/graphics/cairo/CairoUtilities.cpp:
(WebCore::copyCairoImageSurface):
(WebCore::cairoSurfaceSize):
* platform/graphics/cairo/CairoUtilities.h:
* platform/graphics/cairo/GraphicsContext3DCairo.cpp:
(WebCore::GraphicsContext3D::ImageExtractor::extractImage):
* platform/graphics/gtk/GdkCairoUtilities.cpp:
(cairoSurfaceToGdkPixbuf):
* platform/graphics/gtk/GdkCairoUtilities.h:
* platform/graphics/gtk/ImageBufferGtk.cpp:
(WebCore::encodeImage):
* platform/graphics/gtk/ImageGtk.cpp:
(WebCore::BitmapImage::getGdkPixbuf):
* platform/gtk/DragIcon.cpp:
(WebCore::DragIcon::setImage):

Source/WebKit/gtk:

Change cairoImageSurfaceToGdkPixbuf to cairoSurfaceToGdkPixbuf.

* webkit/webkitfavicondatabase.cpp:
(getIconPixbufSynchronously):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@154819 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent c8d15608
2013-08-29 Arnaud Renevier <a.renevier@samsung.com>
[cairo] canvas drawing on itself doesn't work with accelerated canvas
https://bugs.webkit.org/show_bug.cgi?id=118808
Reviewed by Martin Robinson.
When copying an accelerated image, we try to get its dimensions with
cairo_image_surface_get_width/cairo_image_surface_get_height. As
surface is not an image, this returns width and height of 0.
Many other places use cairo_image_surface_get although the surface may
be a gl surface.
This patch fixes those issues by implementing a cairoSurfaceSize
helper that returns the surface size whatever type it is.
It use cairo_surface_create_similar instead of
cairo_image_surface_create in copyCairoImageSurface. It also calls
cairo_paint in encodeImage when a drawing over a black background is
needed.
It copies the surface to an image surface if needed in extractImage.
No new tests. Covered by existing tests.
* platform/graphics/cairo/BitmapImageCairo.cpp:
(WebCore::BitmapImage::BitmapImage):
(WebCore::BitmapImage::draw):
(WebCore::BitmapImage::checkForSolidColor):
* platform/graphics/cairo/CairoUtilities.cpp:
(WebCore::copyCairoImageSurface):
(WebCore::cairoSurfaceSize):
* platform/graphics/cairo/CairoUtilities.h:
* platform/graphics/cairo/GraphicsContext3DCairo.cpp:
(WebCore::GraphicsContext3D::ImageExtractor::extractImage):
* platform/graphics/gtk/GdkCairoUtilities.cpp:
(cairoSurfaceToGdkPixbuf):
* platform/graphics/gtk/GdkCairoUtilities.h:
* platform/graphics/gtk/ImageBufferGtk.cpp:
(WebCore::encodeImage):
* platform/graphics/gtk/ImageGtk.cpp:
(WebCore::BitmapImage::getGdkPixbuf):
* platform/gtk/DragIcon.cpp:
(WebCore::DragIcon::setImage):
2013-08-29 Tamas Czene <tczene@inf.u-szeged.hu>
Buildfix after r154806 for !ENABLE(CSS_REGIONS) platforms.
......@@ -28,6 +28,7 @@
#include "config.h"
#include "BitmapImage.h"
#include "CairoUtilities.h"
#include "ImageObserver.h"
#include "PlatformContextCairo.h"
#include <cairo.h>
......@@ -36,6 +37,7 @@ namespace WebCore {
BitmapImage::BitmapImage(PassRefPtr<cairo_surface_t> nativeImage, ImageObserver* observer)
: Image(observer)
, m_size(cairoSurfaceSize(nativeImage.get()))
, m_currentFrame(0)
, m_frames(0)
, m_frameTimer(0)
......@@ -52,10 +54,7 @@ BitmapImage::BitmapImage(PassRefPtr<cairo_surface_t> nativeImage, ImageObserver*
, m_sizeAvailable(true)
, m_haveFrameCount(true)
{
int width = cairo_image_surface_get_width(nativeImage.get());
int height = cairo_image_surface_get_height(nativeImage.get());
m_decodedSize = width * height * 4;
m_size = IntSize(width, height);
m_decodedSize = m_size.width() * m_size.height() * 4;
m_frames.grow(1);
m_frames[0].m_hasAlpha = cairo_surface_get_content(nativeImage.get()) != CAIRO_CONTENT_COLOR;
......@@ -95,7 +94,7 @@ void BitmapImage::draw(GraphicsContext* context, const FloatRect& dst, const Flo
context->setCompositeOperation(op, blendMode);
#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
IntSize scaledSize(cairo_image_surface_get_width(surface.get()), cairo_image_surface_get_height(surface.get()));
IntSize scaledSize = cairoSurfaceSize(surface.get());
FloatRect adjustedSrcRect = adjustSourceRectForDownSampling(src, scaledSize);
#else
FloatRect adjustedSrcRect(src);
......@@ -139,12 +138,12 @@ void BitmapImage::checkForSolidColor()
if (!surface) // If it's too early we won't have an image yet.
return;
ASSERT(cairo_surface_get_type(surface.get()) == CAIRO_SURFACE_TYPE_IMAGE);
if (cairo_surface_get_type(surface.get()) != CAIRO_SURFACE_TYPE_IMAGE)
return;
int width = cairo_image_surface_get_width(surface.get());
int height = cairo_image_surface_get_height(surface.get());
IntSize size = cairoSurfaceSize(surface.get());
if (width != 1 || height != 1)
if (size.width() != 1 || size.height() != 1)
return;
unsigned* pixelColor = reinterpret_cast_ptr<unsigned*>(cairo_image_surface_get_data(surface.get()));
......
......@@ -38,6 +38,10 @@
#include "RefPtrCairo.h"
#include <wtf/Vector.h>
#if ENABLE(ACCELERATED_2D_CANVAS)
#include <cairo-gl.h>
#endif
namespace WebCore {
void copyContextProperties(cairo_t* srcCr, cairo_t* dstCr)
......@@ -214,9 +218,9 @@ PassRefPtr<cairo_surface_t> copyCairoImageSurface(cairo_surface_t* originalSurfa
// Cairo doesn't provide a way to copy a cairo_surface_t.
// See http://lists.cairographics.org/archives/cairo/2007-June/010877.html
// Once cairo provides the way, use the function instead of this.
RefPtr<cairo_surface_t> newSurface = adoptRef(cairo_image_surface_create(cairo_image_surface_get_format(originalSurface),
cairo_image_surface_get_width(originalSurface),
cairo_image_surface_get_height(originalSurface)));
IntSize size = cairoSurfaceSize(originalSurface);
RefPtr<cairo_surface_t> newSurface = adoptRef(cairo_surface_create_similar(originalSurface,
cairo_surface_get_content(originalSurface), size.width(), size.height()));
RefPtr<cairo_t> cr = adoptRef(cairo_create(newSurface.get()));
cairo_set_source_surface(cr.get(), originalSurface, 0, 0);
......@@ -240,4 +244,19 @@ void copyRectFromOneSurfaceToAnother(cairo_surface_t* from, cairo_surface_t* to,
copyRectFromCairoSurfaceToContext(from, context.get(), sourceOffset, rect);
}
IntSize cairoSurfaceSize(cairo_surface_t* surface)
{
switch (cairo_surface_get_type(surface)) {
case CAIRO_SURFACE_TYPE_IMAGE:
return IntSize(cairo_image_surface_get_width(surface), cairo_image_surface_get_height(surface));
#if ENABLE(ACCELERATED_2D_CANVAS)
case CAIRO_SURFACE_TYPE_GL:
return IntSize(cairo_gl_surface_get_width(surface), cairo_gl_surface_get_height(surface));
#endif
default:
ASSERT_NOT_REACHED();
return IntSize();
}
}
} // namespace WebCore
......@@ -55,6 +55,8 @@ PassRefPtr<cairo_surface_t> copyCairoImageSurface(cairo_surface_t*);
void copyRectFromCairoSurfaceToContext(cairo_surface_t* from, cairo_t* to, const IntSize& offset, const IntRect&);
void copyRectFromOneSurfaceToAnother(cairo_surface_t* from, cairo_surface_t* to, const IntSize& offset, const IntRect&, const IntSize& = IntSize(), cairo_operator_t = CAIRO_OPERATOR_OVER);
IntSize cairoSurfaceSize(cairo_surface_t*);
} // namespace WebCore
#endif // CairoUtilities_h
......@@ -30,6 +30,7 @@
#if USE(3D_GRAPHICS)
#include "CairoUtilities.h"
#include "GraphicsContext3DPrivate.h"
#include "Image.h"
#include "ImageSource.h"
......@@ -201,13 +202,23 @@ bool GraphicsContext3D::ImageExtractor::extractImage(bool premultiplyAlpha, bool
// do AlphaDoUnmultiply if UNPACK_PREMULTIPLY_ALPHA_WEBGL is set to false.
if (!premultiplyAlpha && m_imageHtmlDomSource != HtmlDomVideo)
m_alphaOp = AlphaDoUnmultiply;
// if m_imageSurface is not an image, extract a copy of the surface
if (m_imageSurface && cairo_surface_get_type(m_imageSurface.get()) != CAIRO_SURFACE_TYPE_IMAGE) {
RefPtr<cairo_surface_t> tmpSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, m_imageWidth, m_imageHeight));
copyRectFromOneSurfaceToAnother(m_imageSurface.get(), tmpSurface.get(), IntSize(), IntRect(0, 0, m_imageWidth, m_imageHeight), IntSize(), CAIRO_OPERATOR_SOURCE);
m_imageSurface = tmpSurface.release();
}
}
if (!m_imageSurface)
return false;
m_imageWidth = cairo_image_surface_get_width(m_imageSurface.get());
m_imageHeight = cairo_image_surface_get_height(m_imageSurface.get());
ASSERT(cairo_surface_get_type(m_imageSurface.get()) == CAIRO_SURFACE_TYPE_IMAGE);
IntSize imageSize = cairoSurfaceSize(m_imageSurface.get());
m_imageWidth = imageSize.width();
m_imageHeight = imageSize.height();
if (!m_imageWidth || !m_imageHeight)
return false;
......
......@@ -24,16 +24,19 @@
*/
#include "config.h"
#include "CairoUtilities.h"
#include "GdkCairoUtilities.h"
#include "GtkVersioning.h"
#include "IntSize.h"
#include <cairo.h>
#include <gtk/gtk.h>
GdkPixbuf* cairoImageSurfaceToGdkPixbuf(cairo_surface_t* surface)
using namespace WebCore;
GdkPixbuf* cairoSurfaceToGdkPixbuf(cairo_surface_t* surface)
{
return gdk_pixbuf_get_from_surface(surface, 0, 0,
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface));
IntSize size = cairoSurfaceSize(surface);
return gdk_pixbuf_get_from_surface(surface, 0, 0, size.width(), size.height());
}
......@@ -26,6 +26,6 @@
#ifndef GdkCairoUtilities_h
#define GdkCairoUtilities_h
GdkPixbuf* cairoImageSurfaceToGdkPixbuf(cairo_surface_t* surface);
GdkPixbuf* cairoSurfaceToGdkPixbuf(cairo_surface_t*);
#endif // GdkCairoUtilities_h
......@@ -19,6 +19,7 @@
#include "config.h"
#include "ImageBuffer.h"
#include "CairoUtilities.h"
#include "GdkCairoUtilities.h"
#include <wtf/gobject/GOwnPtr.h>
#include "GRefPtrGtk.h"
......@@ -44,14 +45,23 @@ static bool encodeImage(cairo_surface_t* surface, const String& mimeType, const
if (type == "jpeg") {
// JPEG doesn't support alpha channel. The <canvas> spec states that toDataURL() must encode a Porter-Duff
// composite source-over black for image types that do not support alpha.
RefPtr<cairo_surface_t> newSurface = adoptRef(cairo_image_surface_create_for_data(cairo_image_surface_get_data(surface),
CAIRO_FORMAT_RGB24,
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
cairo_image_surface_get_stride(surface)));
pixbuf = adoptGRef(cairoImageSurfaceToGdkPixbuf(newSurface.get()));
RefPtr<cairo_surface_t> newSurface;
if (cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE) {
newSurface = adoptRef(cairo_image_surface_create_for_data(cairo_image_surface_get_data(surface),
CAIRO_FORMAT_RGB24,
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
cairo_image_surface_get_stride(surface)));
} else {
IntSize size = cairoSurfaceSize(surface);
newSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_RGB24, size.width(), size.height()));
RefPtr<cairo_t> cr = adoptRef(cairo_create(newSurface.get()));
cairo_set_source_surface(cr.get(), surface, 0, 0);
cairo_paint(cr.get());
}
pixbuf = adoptGRef(cairoSurfaceToGdkPixbuf(newSurface.get()));
} else
pixbuf = adoptGRef(cairoImageSurfaceToGdkPixbuf(surface));
pixbuf = adoptGRef(cairoSurfaceToGdkPixbuf(surface));
if (!pixbuf)
return false;
......
......@@ -109,7 +109,7 @@ PassRefPtr<Image> Image::loadPlatformThemeIcon(const char* name, int size)
GdkPixbuf* BitmapImage::getGdkPixbuf()
{
RefPtr<cairo_surface_t> surface = nativeImageForCurrentFrame();
return surface ? cairoImageSurfaceToGdkPixbuf(surface.get()) : 0;
return surface ? cairoSurfaceToGdkPixbuf(surface.get()) : 0;
}
}
......@@ -103,7 +103,7 @@ void DragIcon::setImage(cairo_surface_t* image)
}
#ifdef GTK_API_VERSION_2
m_pixbuf = adoptGRef(cairoImageSurfaceToGdkPixbuf(image));
m_pixbuf = adoptGRef(cairoSurfaceToGdkPixbuf(image));
#endif
}
......
2013-08-29 Arnaud Renevier <a.renevier@samsung.com>
[cairo] canvas drawing on itself doesn't work with accelerated canvas
https://bugs.webkit.org/show_bug.cgi?id=118808
Reviewed by Martin Robinson.
Change cairoImageSurfaceToGdkPixbuf to cairoSurfaceToGdkPixbuf.
* webkit/webkitfavicondatabase.cpp:
(getIconPixbufSynchronously):
2013-08-26 Zan Dobersek <zdobersek@igalia.com>
Unreviewed GTK build fix.
......
......@@ -396,7 +396,7 @@ static GdkPixbuf* getIconPixbufSynchronously(WebKitFaviconDatabase* database, co
if (!surface)
return 0;
GRefPtr<GdkPixbuf> pixbuf = adoptGRef(cairoImageSurfaceToGdkPixbuf(surface.get()));
GRefPtr<GdkPixbuf> pixbuf = adoptGRef(cairoSurfaceToGdkPixbuf(surface.get()));
if (!pixbuf)
return 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment