1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
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. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "GraphicsContext3D.h"
30
31#if ENABLE(GRAPHICS_CONTEXT_3D) && USE(CAIRO)
32
33#include "CairoUtilities.h"
34#include "Image.h"
35#include "ImageSource.h"
36#include "PlatformContextCairo.h"
37#include "RefPtrCairo.h"
38#include <cairo.h>
39
40namespace WebCore {
41
42GraphicsContext3D::ImageExtractor::~ImageExtractor() = default;
43
44bool GraphicsContext3D::ImageExtractor::extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile)
45{
46 if (!m_image)
47 return false;
48 // We need this to stay in scope because the native image is just a shallow copy of the data.
49 AlphaOption alphaOption = premultiplyAlpha ? AlphaOption::Premultiplied : AlphaOption::NotPremultiplied;
50 GammaAndColorProfileOption gammaAndColorProfileOption = ignoreGammaAndColorProfile ? GammaAndColorProfileOption::Ignored : GammaAndColorProfileOption::Applied;
51 auto source = ImageSource::create(nullptr, alphaOption, gammaAndColorProfileOption);
52 m_alphaOp = AlphaDoNothing;
53
54 if (m_image->data()) {
55 source->setData(m_image->data(), true);
56 if (!source->frameCount())
57 return false;
58 m_imageSurface = source->createFrameImageAtIndex(0);
59 } else {
60 m_imageSurface = m_image->nativeImageForCurrentFrame();
61 // 1. For texImage2D with HTMLVideoElment input, assume no PremultiplyAlpha had been applied and the alpha value is 0xFF for each pixel,
62 // which is true at present and may be changed in the future and needs adjustment accordingly.
63 // 2. For texImage2D with HTMLCanvasElement input in which Alpha is already Premultiplied in this port,
64 // do AlphaDoUnmultiply if UNPACK_PREMULTIPLY_ALPHA_WEBGL is set to false.
65 if (!premultiplyAlpha && m_imageHtmlDomSource != HtmlDomVideo)
66 m_alphaOp = AlphaDoUnmultiply;
67
68 // if m_imageSurface is not an image, extract a copy of the surface
69 if (m_imageSurface && cairo_surface_get_type(m_imageSurface.get()) != CAIRO_SURFACE_TYPE_IMAGE) {
70 IntSize surfaceSize = cairoSurfaceSize(m_imageSurface.get());
71 auto tmpSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, surfaceSize.width(), surfaceSize.height()));
72 copyRectFromOneSurfaceToAnother(m_imageSurface.get(), tmpSurface.get(), IntSize(), IntRect(IntPoint(), surfaceSize), IntSize(), CAIRO_OPERATOR_SOURCE);
73 m_imageSurface = WTFMove(tmpSurface);
74 }
75 }
76
77 if (!m_imageSurface)
78 return false;
79
80 ASSERT(cairo_surface_get_type(m_imageSurface.get()) == CAIRO_SURFACE_TYPE_IMAGE);
81
82 IntSize imageSize = cairoSurfaceSize(m_imageSurface.get());
83 m_imageWidth = imageSize.width();
84 m_imageHeight = imageSize.height();
85 if (!m_imageWidth || !m_imageHeight)
86 return false;
87
88 if (cairo_image_surface_get_format(m_imageSurface.get()) != CAIRO_FORMAT_ARGB32)
89 return false;
90
91 unsigned int srcUnpackAlignment = 1;
92 size_t bytesPerRow = cairo_image_surface_get_stride(m_imageSurface.get());
93 size_t bitsPerPixel = 32;
94 unsigned padding = bytesPerRow - bitsPerPixel / 8 * m_imageWidth;
95 if (padding) {
96 srcUnpackAlignment = padding + 1;
97 while (bytesPerRow % srcUnpackAlignment)
98 ++srcUnpackAlignment;
99 }
100
101 m_imagePixelData = cairo_image_surface_get_data(m_imageSurface.get());
102 m_imageSourceFormat = DataFormatBGRA8;
103 m_imageSourceUnpackAlignment = srcUnpackAlignment;
104 return true;
105}
106
107void GraphicsContext3D::paintToCanvas(const unsigned char* imagePixels, const IntSize& imageSize, const IntSize& canvasSize, GraphicsContext& context)
108{
109 if (!imagePixels || imageSize.isEmpty() || canvasSize.isEmpty())
110 return;
111
112 PlatformContextCairo* platformContext = context.platformContext();
113 if (!platformContext)
114 return;
115
116 cairo_t* cr = platformContext->cr();
117 platformContext->save();
118
119 cairo_rectangle(cr, 0, 0, canvasSize.width(), canvasSize.height());
120 cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
121 cairo_paint(cr);
122
123 RefPtr<cairo_surface_t> imageSurface = adoptRef(cairo_image_surface_create_for_data(
124 const_cast<unsigned char*>(imagePixels), CAIRO_FORMAT_ARGB32, imageSize.width(), imageSize.height(), imageSize.width() * 4));
125
126 // OpenGL keeps the pixels stored bottom up, so we need to flip the image here.
127 cairo_translate(cr, 0, imageSize.height());
128 cairo_scale(cr, 1, -1);
129
130 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
131 cairo_set_source_surface(cr, imageSurface.get(), 0, 0);
132 cairo_rectangle(cr, 0, 0, canvasSize.width(), -canvasSize.height());
133
134 cairo_fill(cr);
135 platformContext->restore();
136}
137
138} // namespace WebCore
139
140#endif // ENABLE(GRAPHICS_CONTEXT_3D) && USE(CAIRO)
141