Home | History | Annotate | Download | only in cg
      1 /*
      2  * Copyright (C) 2010, 2011 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 <WebCore/GraphicsContext.h>
     30 #include <wtf/RetainPtr.h>
     31 #include "CGUtilities.h"
     32 
     33 using namespace WebCore;
     34 
     35 namespace WebKit {
     36 
     37 static CGBitmapInfo bitmapInfo(ShareableBitmap::Flags flags)
     38 {
     39     CGBitmapInfo info = kCGBitmapByteOrder32Host;
     40     if (flags & ShareableBitmap::SupportsAlpha)
     41         info |= kCGImageAlphaPremultipliedFirst;
     42     else
     43         info |= kCGImageAlphaNoneSkipFirst;
     44 
     45     return info;
     46 }
     47 
     48 PassOwnPtr<GraphicsContext> ShareableBitmap::createGraphicsContext()
     49 {
     50     RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
     51 
     52 
     53     ref(); // Balanced by deref in releaseBitmapContextData.
     54     RetainPtr<CGContextRef> bitmapContext(AdoptCF, CGBitmapContextCreateWithData(data(),
     55         m_size.width(), m_size.height(), 8, m_size.width() * 4, colorSpace.get(),
     56         bitmapInfo(m_flags), releaseBitmapContextData, this));
     57 
     58     // We want the origin to be in the top left corner so we flip the backing store context.
     59     CGContextTranslateCTM(bitmapContext.get(), 0, m_size.height());
     60     CGContextScaleCTM(bitmapContext.get(), 1, -1);
     61 
     62     return adoptPtr(new GraphicsContext(bitmapContext.get()));
     63 }
     64 
     65 void ShareableBitmap::paint(WebCore::GraphicsContext& context, const IntPoint& dstPoint, const IntRect& srcRect)
     66 {
     67     paintImage(context.platformContext(), makeCGImageCopy().get(), dstPoint, srcRect);
     68 }
     69 
     70 RetainPtr<CGImageRef> ShareableBitmap::makeCGImageCopy()
     71 {
     72     OwnPtr<GraphicsContext> graphicsContext = createGraphicsContext();
     73     RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(graphicsContext->platformContext()));
     74     return image;
     75 }
     76 
     77 RetainPtr<CGImageRef> ShareableBitmap::makeCGImage()
     78 {
     79     ref(); // Balanced by deref in releaseDataProviderData.
     80     RetainPtr<CGDataProvider> dataProvider(AdoptCF, CGDataProviderCreateWithData(this, data(), sizeInBytes(), releaseDataProviderData));
     81 
     82     RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
     83     RetainPtr<CGImageRef> image(AdoptCF, CGImageCreate(m_size.width(), m_size.height(), 8, 32, m_size.width() * 4, colorSpace.get(), bitmapInfo(m_flags), dataProvider.get(), 0, false, kCGRenderingIntentDefault));
     84     return image;
     85 }
     86 
     87 void ShareableBitmap::releaseBitmapContextData(void* typelessBitmap, void* typelessData)
     88 {
     89     ShareableBitmap* bitmap = static_cast<ShareableBitmap*>(typelessBitmap);
     90     ASSERT_UNUSED(typelessData, bitmap->data() == typelessData);
     91     bitmap->deref(); // Balanced by ref in createGraphicsContext.
     92 }
     93 
     94 void ShareableBitmap::releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t)
     95 {
     96     ShareableBitmap* bitmap = static_cast<ShareableBitmap*>(typelessBitmap);
     97     ASSERT_UNUSED(typelessData, bitmap->data() == typelessData);
     98     bitmap->deref(); // Balanced by ref in createCGImage.
     99 }
    100 
    101 } // namespace WebKit
    102