1 | /* |
2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2011 Brent Fulgham <[email protected]> |
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/CairoOperations.h> |
33 | #include <WebCore/CairoUtilities.h> |
34 | #include <WebCore/GraphicsContextImplCairo.h> |
35 | #include <WebCore/PlatformContextCairo.h> |
36 | #include <WebCore/NotImplemented.h> |
37 | |
38 | namespace WebKit { |
39 | using namespace WebCore; |
40 | |
41 | static const cairo_format_t cairoFormat = CAIRO_FORMAT_ARGB32; |
42 | |
43 | Checked<unsigned, RecordOverflow> ShareableBitmap::calculateBytesPerRow(WebCore::IntSize size, const Configuration&) |
44 | { |
45 | return cairo_format_stride_for_width(cairoFormat, size.width()); |
46 | } |
47 | |
48 | unsigned ShareableBitmap::calculateBytesPerPixel(const Configuration&) |
49 | { |
50 | return 4; |
51 | } |
52 | |
53 | static inline RefPtr<cairo_surface_t> createSurfaceFromData(void* data, const WebCore::IntSize& size) |
54 | { |
55 | const int stride = cairo_format_stride_for_width(cairoFormat, size.width()); |
56 | return adoptRef(cairo_image_surface_create_for_data(static_cast<unsigned char*>(data), cairoFormat, size.width(), size.height(), stride)); |
57 | } |
58 | |
59 | std::unique_ptr<GraphicsContext> ShareableBitmap::createGraphicsContext() |
60 | { |
61 | RefPtr<cairo_surface_t> image = createCairoSurface(); |
62 | RefPtr<cairo_t> bitmapContext = adoptRef(cairo_create(image.get())); |
63 | return std::make_unique<GraphicsContext>(GraphicsContextImplCairo::createFactory(bitmapContext.get())); |
64 | } |
65 | |
66 | void ShareableBitmap::paint(GraphicsContext& context, const IntPoint& dstPoint, const IntRect& srcRect) |
67 | { |
68 | paint(context, 1, dstPoint, srcRect); |
69 | } |
70 | |
71 | void ShareableBitmap::paint(GraphicsContext& context, float scaleFactor, const IntPoint& dstPoint, const IntRect& srcRect) |
72 | { |
73 | RefPtr<cairo_surface_t> surface = createSurfaceFromData(data(), m_size); |
74 | FloatRect destRect(dstPoint, srcRect.size()); |
75 | FloatRect srcRectScaled(srcRect); |
76 | srcRectScaled.scale(scaleFactor); |
77 | |
78 | ASSERT(context.hasPlatformContext()); |
79 | auto& state = context.state(); |
80 | Cairo::drawSurface(*context.platformContext(), surface.get(), destRect, srcRectScaled, state.imageInterpolationQuality, state.alpha, Cairo::ShadowState(state)); |
81 | } |
82 | |
83 | RefPtr<cairo_surface_t> ShareableBitmap::createCairoSurface() |
84 | { |
85 | RefPtr<cairo_surface_t> image = createSurfaceFromData(data(), m_size); |
86 | |
87 | ref(); // Balanced by deref in releaseSurfaceData. |
88 | static cairo_user_data_key_t dataKey; |
89 | cairo_surface_set_user_data(image.get(), &dataKey, this, releaseSurfaceData); |
90 | return image; |
91 | } |
92 | |
93 | void ShareableBitmap::releaseSurfaceData(void* typelessBitmap) |
94 | { |
95 | static_cast<ShareableBitmap*>(typelessBitmap)->deref(); // Balanced by ref in createCairoSurface. |
96 | } |
97 | |
98 | RefPtr<Image> ShareableBitmap::createImage() |
99 | { |
100 | RefPtr<cairo_surface_t> surface = createCairoSurface(); |
101 | if (!surface) |
102 | return nullptr; |
103 | |
104 | return BitmapImage::create(WTFMove(surface)); |
105 | } |
106 | |
107 | } // namespace WebKit |
108 | |