1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "ShareableBitmap.h" 28 29#include "NativeImageQt.h" 30#include <QImage> 31#include <QPainter> 32#include <QtGlobal> 33#include <WebCore/BitmapImage.h> 34#include <WebCore/GraphicsContext.h> 35 36using namespace WebCore; 37 38namespace WebKit { 39 40QImage ShareableBitmap::createQImage() 41{ 42 ref(); // Balanced by deref in releaseSharedMemoryData 43 return QImage(reinterpret_cast<uchar*>(data()), m_size.width(), m_size.height(), m_size.width() * 4, 44 m_flags & SupportsAlpha ? NativeImageQt::defaultFormatForAlphaEnabledImages() : NativeImageQt::defaultFormatForOpaqueImages(), 45 releaseSharedMemoryData, this); 46} 47 48void ShareableBitmap::releaseSharedMemoryData(void* typelessBitmap) 49{ 50 static_cast<ShareableBitmap*>(typelessBitmap)->deref(); // Balanced by ref in createQImage. 51} 52 53PassRefPtr<Image> ShareableBitmap::createImage() 54{ 55 QPixmap* pixmap = new QPixmap(QPixmap::fromImage(createQImage())); 56 return BitmapImage::create(pixmap); 57} 58 59PassOwnPtr<GraphicsContext> ShareableBitmap::createGraphicsContext() 60{ 61 // FIXME: Should this be OwnPtr<QImage>? 62 QImage* image = new QImage(createQImage()); 63 QPainter* painter = new QPainter(image); 64 painter->setRenderHint(QPainter::SmoothPixmapTransform, true); 65 OwnPtr<GraphicsContext> context = adoptPtr(new GraphicsContext(painter)); 66 context->takeOwnershipOfPlatformContext(); 67 return context.release(); 68} 69 70void ShareableBitmap::paint(GraphicsContext& context, const IntPoint& dstPoint, const IntRect& srcRect) 71{ 72 QImage image = createQImage(); 73 QPainter* painter = context.platformContext(); 74 painter->drawImage(dstPoint, image, QRect(srcRect)); 75} 76 77void ShareableBitmap::paint(GraphicsContext& context, float scaleFactor, const IntPoint& dstPoint, const IntRect& srcRect) 78{ 79 if (qFuzzyCompare(scaleFactor, 1)) { 80 paint(context, dstPoint, srcRect); 81 return; 82 } 83 84 QImage image = createQImage(); 85 QPainter* painter = context.platformContext(); 86 87 painter->save(); 88 painter->scale(scaleFactor, scaleFactor); 89 painter->drawImage(dstPoint, image, QRect(srcRect)); 90 painter->restore(); 91} 92 93} 94// namespace WebKit 95