1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2011 Brent Fulgham <bfulgham@webkit.org> 4 * Copyright (C) 2011 Igalia S.L. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 25 * THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "config.h" 29#include "ShareableBitmap.h" 30 31#include <WebCore/BitmapImage.h> 32#include <WebCore/CairoUtilities.h> 33#include <WebCore/GraphicsContext.h> 34#include <WebCore/PlatformContextCairo.h> 35#include <WebCore/NotImplemented.h> 36 37using namespace WebCore; 38 39namespace WebKit { 40 41static const cairo_format_t cairoFormat = CAIRO_FORMAT_ARGB32; 42 43size_t ShareableBitmap::numBytesForSize(const WebCore::IntSize& size) 44{ 45 return cairo_format_stride_for_width(cairoFormat, size.width()) * size.height(); 46} 47 48static inline PassRefPtr<cairo_surface_t> createSurfaceFromData(void* data, const WebCore::IntSize& size) 49{ 50 const int stride = cairo_format_stride_for_width(cairoFormat, size.width()); 51 return adoptRef(cairo_image_surface_create_for_data(static_cast<unsigned char*>(data), cairoFormat, size.width(), size.height(), stride)); 52} 53 54std::unique_ptr<GraphicsContext> ShareableBitmap::createGraphicsContext() 55{ 56 RefPtr<cairo_surface_t> image = createCairoSurface(); 57 RefPtr<cairo_t> bitmapContext = adoptRef(cairo_create(image.get())); 58 return std::make_unique<GraphicsContext>(bitmapContext.get()); 59} 60 61void ShareableBitmap::paint(GraphicsContext& context, const IntPoint& dstPoint, const IntRect& srcRect) 62{ 63 paint(context, 1, dstPoint, srcRect); 64} 65 66void ShareableBitmap::paint(GraphicsContext& context, float scaleFactor, const IntPoint& dstPoint, const IntRect& srcRect) 67{ 68 RefPtr<cairo_surface_t> surface = createSurfaceFromData(data(), m_size); 69 FloatRect destRect(dstPoint, srcRect.size()); 70 FloatRect srcRectScaled(srcRect); 71 srcRectScaled.scale(scaleFactor); 72 context.platformContext()->drawSurfaceToContext(surface.get(), destRect, srcRectScaled, &context); 73} 74 75PassRefPtr<cairo_surface_t> ShareableBitmap::createCairoSurface() 76{ 77 RefPtr<cairo_surface_t> image = createSurfaceFromData(data(), m_size); 78 79 ref(); // Balanced by deref in releaseSurfaceData. 80 static cairo_user_data_key_t dataKey; 81 cairo_surface_set_user_data(image.get(), &dataKey, this, releaseSurfaceData); 82 return image.release(); 83} 84 85void ShareableBitmap::releaseSurfaceData(void* typelessBitmap) 86{ 87 static_cast<ShareableBitmap*>(typelessBitmap)->deref(); // Balanced by ref in createCairoSurface. 88} 89 90PassRefPtr<Image> ShareableBitmap::createImage() 91{ 92 RefPtr<cairo_surface_t> surface = createCairoSurface(); 93 if (!surface) 94 return 0; 95 96 return BitmapImage::create(surface.release()); 97} 98 99} // namespace WebKit 100