Commit 9b16ce29 authored by mdelaney@apple.com's avatar mdelaney@apple.com

2010-12-09 Matthew Delaney <mdelaney@apple.com>

        Reviewed by Simon Fraser.

        Adopt new CG API for canvas
        https://bugs.webkit.org/show_bug.cgi?id=50591

        * WebCoreSupport/WebSystemInterface.mm:
2010-12-09  Matthew Delaney  <mdelaney@apple.com>

        Reviewed by Simon Fraser.

        Adopt new CG API for canvas
        https://bugs.webkit.org/show_bug.cgi?id=50591

        No new tests. All current layout tests are sufficient.

        * html/HTMLCanvasElement.cpp: Add in accelerateRendering flag for imagebuffer creation.
        * html/canvas/CanvasRenderingContext2D.cpp:
        (WebCore::CanvasRenderingContext2D::isAccelerated): Always return true if using CA on new platforms.
        (WebCore::CanvasRenderingContext2D::drawTextInternal): Use accelerateRendering for imagebuffer.
        * platform/graphics/ImageBuffer.h:
        (WebCore::ImageBuffer::create): Plumb through new flag.
        * platform/graphics/cairo/ImageBufferCairo.cpp: Update method sig.
        * platform/graphics/cg/ImageBufferCG.cpp: Switch off of accelerateRendering flag for new accelerated paths.
        * platform/graphics/cg/ImageBufferData.h: Add in surface ref
        * platform/graphics/haiku/ImageBufferHaiku.cpp: Update method sig.
        * platform/graphics/qt/ImageBufferQt.cpp: ^^
        * platform/graphics/skia/ImageBufferSkia.cpp: ^^
        * platform/graphics/wince/ImageBufferWinCE.cpp: ^^
        * platform/graphics/wx/ImageBufferWx.cpp: ^^
        * platform/mac/WebCoreSystemInterface.h: Add new method sigs
        * platform/mac/WebCoreSystemInterface.mm: ^^
        * rendering/RenderLayerBacking.cpp:
        * rendering/RenderLayerCompositor.cpp: Set acceleratesDrawing for canvas backings.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@73925 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 8582ca50
2010-12-09 Matthew Delaney <mdelaney@apple.com>
Reviewed by Simon Fraser.
Adopt new CG API for canvas
https://bugs.webkit.org/show_bug.cgi?id=50591
No new tests. All current layout tests are sufficient.
* html/HTMLCanvasElement.cpp: Add in accelerateRendering flag for imagebuffer creation.
* html/canvas/CanvasRenderingContext2D.cpp:
(WebCore::CanvasRenderingContext2D::isAccelerated): Always return true if using CA on new platforms.
(WebCore::CanvasRenderingContext2D::drawTextInternal): Use accelerateRendering for imagebuffer.
* platform/graphics/ImageBuffer.h:
(WebCore::ImageBuffer::create): Plumb through new flag.
* platform/graphics/cairo/ImageBufferCairo.cpp: Update method sig.
* platform/graphics/cg/ImageBufferCG.cpp: Switch off of accelerateRendering flag for new accelerated paths.
* platform/graphics/cg/ImageBufferData.h: Add in surface ref
* platform/graphics/haiku/ImageBufferHaiku.cpp: Update method sig.
* platform/graphics/qt/ImageBufferQt.cpp: ^^
* platform/graphics/skia/ImageBufferSkia.cpp: ^^
* platform/graphics/wince/ImageBufferWinCE.cpp: ^^
* platform/graphics/wx/ImageBufferWx.cpp: ^^
* platform/mac/WebCoreSystemInterface.h: Add new method sigs
* platform/mac/WebCoreSystemInterface.mm: ^^
* rendering/RenderLayerBacking.cpp:
* rendering/RenderLayerCompositor.cpp: Set acceleratesDrawing for canvas backings.
2010-12-13 Benjamin Kalman <kalman@chromium.org>
Reviewed by Ojan Vafai.
......
......@@ -1256,6 +1256,8 @@ _wkSignalCFReadStreamHasBytes
#if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
_wkCreateCTTypesetterWithUniCharProviderAndOptions
_wkIOSurfaceContextCreate
_wkIOSurfaceContextCreateImage
#endif
#if ENABLE(3D_RENDERING)
......
......@@ -689,6 +689,7 @@
20D629261253690B00081543 /* InspectorInstrumentation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20D629241253690B00081543 /* InspectorInstrumentation.cpp */; };
20D629271253690B00081543 /* InspectorInstrumentation.h in Headers */ = {isa = PBXBuildFile; fileRef = 20D629251253690B00081543 /* InspectorInstrumentation.h */; };
228C284510D82500009D0D0E /* ScriptWrappable.h in Headers */ = {isa = PBXBuildFile; fileRef = 228C284410D82500009D0D0E /* ScriptWrappable.h */; settings = {ATTRIBUTES = (Private, ); }; };
2298CCE712AD8BCA00C42E51 /* IOSurface.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2298CCE612AD8BCA00C42E51 /* IOSurface.framework */; };
24F54EAC101FE914000AE741 /* ApplicationCacheHost.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 24F54EAA101FE914000AE741 /* ApplicationCacheHost.cpp */; };
24F54EAD101FE914000AE741 /* ApplicationCacheHost.h in Headers */ = {isa = PBXBuildFile; fileRef = 24F54EAB101FE914000AE741 /* ApplicationCacheHost.h */; settings = {ATTRIBUTES = (); }; };
2542F4DA1166C25A00E89A86 /* UserGestureIndicator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2542F4D81166C25A00E89A86 /* UserGestureIndicator.cpp */; };
......@@ -6972,6 +6973,7 @@
20D629241253690B00081543 /* InspectorInstrumentation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorInstrumentation.cpp; sourceTree = "<group>"; };
20D629251253690B00081543 /* InspectorInstrumentation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorInstrumentation.h; sourceTree = "<group>"; };
228C284410D82500009D0D0E /* ScriptWrappable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptWrappable.h; sourceTree = "<group>"; };
2298CCE612AD8BCA00C42E51 /* IOSurface.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOSurface.framework; path = /System/Library/Frameworks/IOSurface.framework; sourceTree = "<absolute>"; };
2442BBF81194C9D300D49469 /* HashChangeEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HashChangeEvent.h; sourceTree = "<group>"; };
24F54EAA101FE914000AE741 /* ApplicationCacheHost.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ApplicationCacheHost.cpp; sourceTree = "<group>"; };
24F54EAB101FE914000AE741 /* ApplicationCacheHost.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ApplicationCacheHost.h; sourceTree = "<group>"; };
......@@ -12229,6 +12231,7 @@
FD2DBF1312B048A300ED98C6 /* AudioToolbox.framework in Frameworks */,
FD2DBF1412B048A300ED98C6 /* AudioUnit.framework in Frameworks */,
FD2DBF1512B048A300ED98C6 /* CoreAudio.framework in Frameworks */,
2298CCE712AD8BCA00C42E51 /* IOSurface.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -12325,6 +12328,7 @@
F5C2869402846DCD018635CA /* Carbon.framework */,
F5C2869502846DCD018635CA /* Cocoa.framework */,
1AB33DA412551E320024457A /* IOKit.framework */,
2298CCE612AD8BCA00C42E51 /* IOSurface.framework */,
F8216299029F4FB501000131 /* JavaScriptCore.framework */,
93F1D31A0558CC5C00821BC0 /* libicucore.dylib */,
1CFAE3220A6D6A3F0032593D /* libobjc.dylib */,
......@@ -372,7 +372,11 @@ void HTMLCanvasElement::createImageBuffer() const
if (!size.width() || !size.height())
return;
#if defined(USE_IOSURFACE)
m_imageBuffer = ImageBuffer::create(size, ColorSpaceDeviceRGB, Accelerated);
#else
m_imageBuffer = ImageBuffer::create(size);
#endif
// The convertLogicalToDevice MaxCanvasArea check should prevent common cases
// where ImageBuffer::create() returns 0, however we could still be low on memory.
if (!m_imageBuffer)
......
......@@ -144,7 +144,9 @@ CanvasRenderingContext2D::~CanvasRenderingContext2D()
bool CanvasRenderingContext2D::isAccelerated() const
{
#if ENABLE(ACCELERATED_2D_CANVAS)
#if PLATFORM(MAC) && PLATFORM(CA) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
return true;
#elif ENABLE(ACCELERATED_2D_CANVAS)
return m_context3D;
#else
return false;
......@@ -1819,7 +1821,11 @@ void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, flo
// FIXME: The rect is not big enough for miters on stroked text.
IntRect maskRect = enclosingIntRect(textRect);
#if defined(USE_IOSURFACE)
OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskRect.size(), ColorSpaceDeviceRGB, Accelerated);
#else
OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskRect.size());
#endif
GraphicsContext* maskImageContext = maskImage->context();
......
......@@ -39,6 +39,10 @@
#include <wtf/PassOwnPtr.h>
#include <wtf/PassRefPtr.h>
#if (PLATFORM(MAC) && PLATFORM(CA) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD))
#define USE_IOSURFACE 1
#endif
namespace WebCore {
class GraphicsContext;
......@@ -51,13 +55,18 @@ namespace WebCore {
Unmultiplied
};
enum RenderingMode {
Unaccelerated,
Accelerated
};
class ImageBuffer : public Noncopyable {
public:
// Will return a null pointer on allocation failure.
static PassOwnPtr<ImageBuffer> create(const IntSize& size, ColorSpace colorSpace = ColorSpaceDeviceRGB)
static PassOwnPtr<ImageBuffer> create(const IntSize& size, ColorSpace colorSpace = ColorSpaceDeviceRGB, RenderingMode renderingMode = Unaccelerated)
{
bool success = false;
OwnPtr<ImageBuffer> buf(new ImageBuffer(size, colorSpace, success));
OwnPtr<ImageBuffer> buf(new ImageBuffer(size, colorSpace, renderingMode == Accelerated, success));
if (success)
return buf.release();
return 0;
......@@ -104,6 +113,7 @@ namespace WebCore {
ImageBufferData m_data;
IntSize m_size;
bool m_accelerateRendering;
OwnPtr<GraphicsContext> m_context;
#if !PLATFORM(CG)
......@@ -113,7 +123,7 @@ namespace WebCore {
// This constructor will place its success into the given out-variable
// so that create() knows when it should return failure.
ImageBuffer(const IntSize&, ColorSpace colorSpace, bool& success);
ImageBuffer(const IntSize&, ColorSpace colorSpace, bool accelerateRendering, bool& success);
};
} // namespace WebCore
......
......@@ -69,7 +69,7 @@ ImageBufferData::ImageBufferData(const IntSize& size)
{
}
ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace, bool& success)
ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace, bool, bool& success)
: m_data(size)
, m_size(size)
{
......
......@@ -42,10 +42,57 @@
#include <wtf/Threading.h>
#include <math.h>
#if defined(USE_IOSURFACE)
#include <IOSurface/IOSurface.h>
#endif
#if PLATFORM(MAC) || PLATFORM(CHROMIUM)
#include "WebCoreSystemInterface.h"
#endif
using namespace std;
namespace WebCore {
#if defined(USE_IOSURFACE)
static RetainPtr<IOSurfaceRef> createIOSurface(const IntSize& size)
{
unsigned pixelFormat = 'BGRA';
unsigned bytesPerElement = 4;
int width = size.width();
int height = size.height();
unsigned long bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.width() * bytesPerElement);
if (!bytesPerRow || bytesPerRow != (size.width() * bytesPerElement))
return 0;
unsigned long allocSize = IOSurfaceAlignProperty(kIOSurfaceAllocSize, size.height() * bytesPerRow);
if (!allocSize || allocSize != (size.height() * bytesPerRow))
return 0;
const void *keys[6];
const void *values[6];
keys[0] = kIOSurfaceWidth;
values[0] = CFNumberCreate(0, kCFNumberIntType, &width);
keys[1] = kIOSurfaceHeight;
values[1] = CFNumberCreate(0, kCFNumberIntType, &height);
keys[2] = kIOSurfacePixelFormat;
values[2] = CFNumberCreate(0, kCFNumberIntType, &pixelFormat);
keys[3] = kIOSurfaceBytesPerElement;
values[3] = CFNumberCreate(0, kCFNumberIntType, &bytesPerElement);
keys[4] = kIOSurfaceBytesPerRow;
values[4] = CFNumberCreate(0, kCFNumberLongType, &bytesPerRow);
keys[5] = kIOSurfaceAllocSize;
values[5] = CFNumberCreate(0, kCFNumberLongType, &allocSize);
RetainPtr<CFDictionaryRef> dict(AdoptCF, CFDictionaryCreate(0, keys, values, 6, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
for (unsigned i = 0; i < 6; i++)
CFRelease(values[i]);
return RetainPtr<IOSurfaceRef>(AdoptCF, IOSurfaceCreate(dict.get()));
}
#endif
static void releaseImageData(void*, const void* data, size_t)
{
fastFree(const_cast<void*>(data));
......@@ -53,32 +100,29 @@ static void releaseImageData(void*, const void* data, size_t)
ImageBufferData::ImageBufferData(const IntSize&)
: m_data(0)
#if defined(USE_IOSURFACE)
, m_surface(0)
#endif
{
}
ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace imageColorSpace, bool& success)
ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace imageColorSpace, bool accelerateRendering, bool& success)
: m_data(size)
, m_size(size)
, m_accelerateRendering(accelerateRendering)
{
success = false; // Make early return mean failure.
if (size.width() < 0 || size.height() < 0)
return;
unsigned bytesPerRow = size.width();
// Protect against overflow
if (bytesPerRow > 0x3FFFFFFF)
if (bytesPerRow > 0x3FFFFFFF) // Protect against overflow
return;
bytesPerRow *= 4;
m_data.m_bytesPerRow = bytesPerRow;
size_t dataSize = size.height() * bytesPerRow;
if (!tryFastCalloc(size.height(), bytesPerRow).getValue(m_data.m_data))
return;
ASSERT((reinterpret_cast<size_t>(m_data.m_data) & 2) == 0);
switch(imageColorSpace) {
switch (imageColorSpace) {
case ColorSpaceDeviceRGB:
m_data.m_colorSpace = deviceRGBColorSpaceRef();
break;
......@@ -90,9 +134,25 @@ ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace imageColorSpace, bool&
break;
}
m_data.m_bitmapInfo = kCGImageAlphaPremultipliedLast;
RetainPtr<CGContextRef> cgContext(AdoptCF, CGBitmapContextCreate(m_data.m_data, size.width(), size.height(), 8, bytesPerRow,
m_data.m_colorSpace, m_data.m_bitmapInfo));
RetainPtr<CGContextRef> cgContext;
if (!m_accelerateRendering) {
if (!tryFastCalloc(size.height(), bytesPerRow).getValue(m_data.m_data))
return;
ASSERT(!(reinterpret_cast<size_t>(m_data.m_data) & 2));
m_data.m_bitmapInfo = kCGImageAlphaPremultipliedLast;
cgContext.adoptCF(CGBitmapContextCreate(m_data.m_data, size.width(), size.height(), 8, bytesPerRow, m_data.m_colorSpace, m_data.m_bitmapInfo));
// Create a live image that wraps the data.
m_data.m_dataProvider.adoptCF(CGDataProviderCreateWithData(0, m_data.m_data, dataSize, releaseImageData));
} else {
#if defined(USE_IOSURFACE)
m_data.m_surface = createIOSurface(size);
cgContext.adoptCF(wkIOSurfaceContextCreate(m_data.m_surface.get(), size.width(), size.height(), m_data.m_colorSpace));
#else
m_accelerateRendering = false; // Force to false on older platforms
#endif
}
if (!cgContext)
return;
......@@ -100,9 +160,6 @@ ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace imageColorSpace, bool&
m_context->scale(FloatSize(1, -1));
m_context->translate(0, -size.height());
success = true;
// Create a live image that wraps the data.
m_data.m_dataProvider.adoptCF(CGDataProviderCreateWithData(0, m_data.m_data, dataSize, releaseImageData));
}
ImageBuffer::~ImageBuffer()
......@@ -122,7 +179,14 @@ bool ImageBuffer::drawsUsingCopy() const
PassRefPtr<Image> ImageBuffer::copyImage() const
{
// BitmapImage will release the passed in CGImage on destruction
return BitmapImage::create(CGBitmapContextCreateImage(context()->platformContext()));
CGImageRef ctxImage = 0;
if (!m_accelerateRendering)
ctxImage = CGBitmapContextCreateImage(context()->platformContext());
#if defined(USE_IOSURFACE)
else
ctxImage = wkIOSurfaceContextCreateImage(context()->platformContext());
#endif
return BitmapImage::create(ctxImage);
}
static CGImageRef cgImage(const IntSize& size, const ImageBufferData& data)
......@@ -134,34 +198,50 @@ static CGImageRef cgImage(const IntSize& size, const ImageBufferData& data)
void ImageBuffer::draw(GraphicsContext* destContext, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect,
CompositeOperator op, bool useLowQualityScale)
{
if (destContext == context()) {
// We're drawing into our own buffer. In order for this to work, we need to copy the source buffer first.
RefPtr<Image> copy = copyImage();
destContext->drawImage(copy.get(), ColorSpaceDeviceRGB, destRect, srcRect, op, useLowQualityScale);
if (!m_accelerateRendering) {
if (destContext == context()) {
// We're drawing into our own buffer. In order for this to work, we need to copy the source buffer first.
RefPtr<Image> copy = copyImage();
destContext->drawImage(copy.get(), ColorSpaceDeviceRGB, destRect, srcRect, op, useLowQualityScale);
} else {
RefPtr<Image> imageForRendering = BitmapImage::create(cgImage(m_size, m_data));
destContext->drawImage(imageForRendering.get(), styleColorSpace, destRect, srcRect, op, useLowQualityScale);
}
} else {
RefPtr<Image> imageForRendering = BitmapImage::create(cgImage(m_size, m_data));
destContext->drawImage(imageForRendering.get(), styleColorSpace, destRect, srcRect, op, useLowQualityScale);
RefPtr<Image> copy = copyImage();
ColorSpace colorSpace = (destContext == context()) ? ColorSpaceDeviceRGB : styleColorSpace;
destContext->drawImage(copy.get(), colorSpace, destRect, srcRect, op, useLowQualityScale);
}
}
void ImageBuffer::drawPattern(GraphicsContext* destContext, const FloatRect& srcRect, const AffineTransform& patternTransform,
const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect)
{
if (destContext == context()) {
// We're drawing into our own buffer. In order for this to work, we need to copy the source buffer first.
if (!m_accelerateRendering) {
if (destContext == context()) {
// We're drawing into our own buffer. In order for this to work, we need to copy the source buffer first.
RefPtr<Image> copy = copyImage();
copy->drawPattern(destContext, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
} else {
RefPtr<Image> imageForRendering = BitmapImage::create(cgImage(m_size, m_data));
imageForRendering->drawPattern(destContext, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
}
} else {
RefPtr<Image> copy = copyImage();
copy->drawPattern(destContext, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
} else {
RefPtr<Image> imageForRendering = BitmapImage::create(cgImage(m_size, m_data));
imageForRendering->drawPattern(destContext, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
}
}
void ImageBuffer::clip(GraphicsContext* context, const FloatRect& rect) const
{
RetainPtr<CGImageRef> image(AdoptCF, cgImage(m_size, m_data));
CGContextRef platformContext = context->platformContext();
RetainPtr<CGImageRef> image;
if (!m_accelerateRendering)
image.adoptCF(cgImage(m_size, m_data));
#if defined(USE_IOSURFACE)
else
image.adoptCF(wkIOSurfaceContextCreateImage(platformContext));
#endif
CGContextTranslateCTM(platformContext, rect.x(), rect.y() + rect.height());
CGContextScaleCTM(platformContext, 1, -1);
CGContextClipToMask(platformContext, FloatRect(FloatPoint(), rect.size()), image.get());
......@@ -170,7 +250,7 @@ void ImageBuffer::clip(GraphicsContext* context, const FloatRect& rect) const
}
template <Multiply multiplied>
PassRefPtr<ImageData> getImageData(const IntRect& rect, const ImageBufferData& imageData, const IntSize& size)
PassRefPtr<ImageData> getImageData(const IntRect& rect, const ImageBufferData& imageData, const IntSize& size, bool accelerateRendering)
{
PassRefPtr<ImageData> result = ImageData::create(rect.width(), rect.height());
unsigned char* data = result->data()->data()->data();
......@@ -199,43 +279,83 @@ PassRefPtr<ImageData> getImageData(const IntRect& rect, const ImageBufferData& i
if (endy > size.height())
endy = size.height();
int numRows = endy - originy;
unsigned srcBytesPerRow = 4 * size.width();
unsigned destBytesPerRow = 4 * rect.width();
// ::create ensures that all ImageBuffers have valid data, so we don't need to check it here.
unsigned char* srcRows = reinterpret_cast<unsigned char*>(imageData.m_data) + originy * srcBytesPerRow + originx * 4;
unsigned char* destRows = data + desty * destBytesPerRow + destx * 4;
for (int y = 0; y < numRows; ++y) {
for (int x = 0; x < numColumns; x++) {
int basex = x * 4;
unsigned char alpha = srcRows[basex + 3];
if (multiplied == Unmultiplied && alpha) {
destRows[basex] = (srcRows[basex] * 255) / alpha;
destRows[basex + 1] = (srcRows[basex + 1] * 255) / alpha;
destRows[basex + 2] = (srcRows[basex + 2] * 255) / alpha;
destRows[basex + 3] = alpha;
} else
reinterpret_cast<uint32_t*>(destRows + basex)[0] = reinterpret_cast<uint32_t*>(srcRows + basex)[0];
unsigned srcBytesPerRow;
unsigned char* srcRows;
if (!accelerateRendering) {
srcBytesPerRow = 4 * size.width();
srcRows = reinterpret_cast<unsigned char*>(imageData.m_data) + originy * srcBytesPerRow + originx * 4;
for (int y = 0; y < numRows; ++y) {
for (int x = 0; x < numColumns; x++) {
int basex = x * 4;
unsigned char alpha = srcRows[basex + 3];
if (multiplied == Unmultiplied && alpha) {
destRows[basex] = (srcRows[basex] * 255) / alpha;
destRows[basex + 1] = (srcRows[basex + 1] * 255) / alpha;
destRows[basex + 2] = (srcRows[basex + 2] * 255) / alpha;
destRows[basex + 3] = alpha;
} else
reinterpret_cast<uint32_t*>(destRows + basex)[0] = reinterpret_cast<uint32_t*>(srcRows + basex)[0];
}
srcRows += srcBytesPerRow;
destRows += destBytesPerRow;
}
} else {
#if defined(USE_IOSURFACE)
IOSurfaceRef surface = imageData.m_surface.get();
IOSurfaceLock(surface, kIOSurfaceLockReadOnly, 0);
srcBytesPerRow = IOSurfaceGetBytesPerRow(surface);
srcRows = (unsigned char*)(IOSurfaceGetBaseAddress(surface)) + originy * srcBytesPerRow + originx * 4;
for (int y = 0; y < numRows; ++y) {
for (int x = 0; x < numColumns; x++) {
int basex = x * 4;
unsigned char alpha = srcRows[basex + 3];
if (multiplied == Unmultiplied && alpha) {
destRows[basex] = (srcRows[basex + 2] * 255) / alpha;
destRows[basex + 1] = (srcRows[basex + 1] * 255) / alpha;
destRows[basex + 2] = (srcRows[basex] * 255) / alpha;
destRows[basex + 3] = alpha;
} else {
destRows[basex] = srcRows[basex + 2];
destRows[basex + 1] = srcRows[basex + 1];
destRows[basex + 2] = srcRows[basex];
destRows[basex + 3] = alpha;
}
}
srcRows += srcBytesPerRow;
destRows += destBytesPerRow;
}
srcRows += srcBytesPerRow;
destRows += destBytesPerRow;
IOSurfaceUnlock(surface, kIOSurfaceLockReadOnly, 0);
#else
ASSERT_NOT_REACHED();
#endif
}
return result;
}
PassRefPtr<ImageData> ImageBuffer::getUnmultipliedImageData(const IntRect& rect) const
{
return getImageData<Unmultiplied>(rect, m_data, m_size);
if (m_accelerateRendering)
CGContextFlush(context()->platformContext());
return getImageData<Unmultiplied>(rect, m_data, m_size, m_accelerateRendering);
}
PassRefPtr<ImageData> ImageBuffer::getPremultipliedImageData(const IntRect& rect) const
{
return getImageData<Premultiplied>(rect, m_data, m_size);
if (m_accelerateRendering)
CGContextFlush(context()->platformContext());
return getImageData<Premultiplied>(rect, m_data, m_size, m_accelerateRendering);
}
template <Multiply multiplied>
void putImageData(ImageData*& source, const IntRect& sourceRect, const IntPoint& destPoint, ImageBufferData& imageData, const IntSize& size)
void putImageData(ImageData*& source, const IntRect& sourceRect, const IntPoint& destPoint, ImageBufferData& imageData, const IntSize& size, bool accelerateRendering)
{
ASSERT(sourceRect.width() > 0);
ASSERT(sourceRect.height() > 0);
......@@ -264,35 +384,73 @@ void putImageData(ImageData*& source, const IntRect& sourceRect, const IntPoint&
int numRows = endy - desty;
unsigned srcBytesPerRow = 4 * source->width();
unsigned destBytesPerRow = 4 * size.width();
unsigned char* srcRows = source->data()->data()->data() + originy * srcBytesPerRow + originx * 4;
unsigned char* destRows = reinterpret_cast<unsigned char*>(imageData.m_data) + desty * destBytesPerRow + destx * 4;
for (int y = 0; y < numRows; ++y) {
for (int x = 0; x < numColumns; x++) {
int basex = x * 4;
unsigned char alpha = srcRows[basex + 3];
if (multiplied == Unmultiplied && alpha != 255) {
destRows[basex] = (srcRows[basex] * alpha + 254) / 255;
destRows[basex + 1] = (srcRows[basex + 1] * alpha + 254) / 255;
destRows[basex + 2] = (srcRows[basex + 2] * alpha + 254) / 255;
destRows[basex + 3] = alpha;
} else
reinterpret_cast<uint32_t*>(destRows + basex)[0] = reinterpret_cast<uint32_t*>(srcRows + basex)[0];
unsigned destBytesPerRow;
unsigned char* destRows;
if (!accelerateRendering) {
destBytesPerRow = 4 * size.width();
destRows = reinterpret_cast<unsigned char*>(imageData.m_data) + desty * destBytesPerRow + destx * 4;
for (int y = 0; y < numRows; ++y) {
for (int x = 0; x < numColumns; x++) {
int basex = x * 4;
unsigned char alpha = srcRows[basex + 3];
if (multiplied == Unmultiplied && alpha != 255) {
destRows[basex] = (srcRows[basex] * alpha + 254) / 255;
destRows[basex + 1] = (srcRows[basex + 1] * alpha + 254) / 255;
destRows[basex + 2] = (srcRows[basex + 2] * alpha + 254) / 255;
destRows[basex + 3] = alpha;
} else
reinterpret_cast<uint32_t*>(destRows + basex)[0] = reinterpret_cast<uint32_t*>(srcRows + basex)[0];
}
destRows += destBytesPerRow;
srcRows += srcBytesPerRow;
}
} else {
#if defined(USE_IOSURFACE)
IOSurfaceRef surface = imageData.m_surface.get();
IOSurfaceLock(surface, 0, 0);
destBytesPerRow = IOSurfaceGetBytesPerRow(surface);
destRows = (unsigned char*)(IOSurfaceGetBaseAddress(surface)) + desty * destBytesPerRow + destx * 4;
for (int y = 0; y < numRows; ++y) {
for (int x = 0; x < numColumns; x++) {
int basex = x * 4;
unsigned char alpha = srcRows[basex + 3];
if (multiplied == Unmultiplied && alpha != 255) {
destRows[basex] = (srcRows[basex + 2] * alpha + 254) / 255;
destRows[basex + 1] = (srcRows[basex + 1] * alpha + 254) / 255;
destRows[basex + 2] = (srcRows[basex] * alpha + 254) / 255;
destRows[basex + 3] = alpha;
} else {
destRows[basex] = srcRows[basex + 2];
destRows[basex + 1] = srcRows[basex + 1];
destRows[basex + 2] = srcRows[basex];
destRows[basex + 3] = alpha;
}
}
destRows += destBytesPerRow;
srcRows += srcBytesPerRow;
}
destRows += destBytesPerRow;
srcRows += srcBytesPerRow;
IOSurfaceUnlock(surface, 0, 0);
#else
ASSERT_NOT_REACHED();
#endif
}
}
void ImageBuffer::putUnmultipliedImageData(ImageData* source, const IntRect& sourceRect, const IntPoint& destPoint)
{
putImageData<Unmultiplied>(source, sourceRect, destPoint, m_data, m_size);
if (m_accelerateRendering)
CGContextFlush(context()->platformContext());
putImageData<Unmultiplied>(source, sourceRect, destPoint, m_data, m_size, m_accelerateRendering);
}
void ImageBuffer::putPremultipliedImageData(ImageData* source, const IntRect& sourceRect, const IntPoint& destPoint)
{
putImageData<Premultiplied>(source, sourceRect, destPoint, m_data, m_size);
if (m_accelerateRendering)
CGContextFlush(context()->platformContext());
putImageData<Premultiplied>(source, sourceRect, destPoint, m_data, m_size, m_accelerateRendering);
}
static inline CFStringRef jpegUTI()
......@@ -332,7 +490,14 @@ String ImageBuffer::toDataURL(const String& mimeType, const double* quality) con
{
ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(context()->platformContext()));
RetainPtr<CGImageRef> image;
if (!m_accelerateRendering)
image.adoptCF(CGBitmapContextCreateImage(context()->platformContext()));
#if defined(USE_IOSURFACE)
else
image.adoptCF(wkIOSurfaceContextCreateImage(context()->platformContext()));
#endif
if (!image)
return "data:,";