1/*
2 * Copyright (C) 2017 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 "ImageBitmapRenderingContext.h"
28
29#include "ImageBitmap.h"
30#include "ImageBuffer.h"
31#include "InspectorInstrumentation.h"
32#include <wtf/IsoMallocInlines.h>
33
34namespace WebCore {
35
36WTF_MAKE_ISO_ALLOCATED_IMPL(ImageBitmapRenderingContext);
37
38#if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
39static RenderingMode bufferRenderingMode = Accelerated;
40#else
41static RenderingMode bufferRenderingMode = Unaccelerated;
42#endif
43
44std::unique_ptr<ImageBitmapRenderingContext> ImageBitmapRenderingContext::create(CanvasBase& canvas, ImageBitmapRenderingContextSettings&& settings)
45{
46 auto renderingContext = std::unique_ptr<ImageBitmapRenderingContext>(new ImageBitmapRenderingContext(canvas, WTFMove(settings)));
47
48 InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
49
50 return renderingContext;
51}
52
53ImageBitmapRenderingContext::ImageBitmapRenderingContext(CanvasBase& canvas, ImageBitmapRenderingContextSettings&& settings)
54 : CanvasRenderingContext(canvas)
55 , m_settings(WTFMove(settings))
56{
57 setOutputBitmap(nullptr);
58}
59
60ImageBitmapRenderingContext::~ImageBitmapRenderingContext() = default;
61
62HTMLCanvasElement* ImageBitmapRenderingContext::canvas() const
63{
64 auto& base = canvasBase();
65 if (!is<HTMLCanvasElement>(base))
66 return nullptr;
67 return &downcast<HTMLCanvasElement>(base);
68}
69
70bool ImageBitmapRenderingContext::isAccelerated() const
71{
72 return bufferRenderingMode == Accelerated;
73}
74
75void ImageBitmapRenderingContext::setOutputBitmap(RefPtr<ImageBitmap> imageBitmap)
76{
77 // 1. If a bitmap argument was not provided, then:
78
79 if (!imageBitmap) {
80
81 // 1.1. Set context's bitmap mode to blank.
82
83 m_bitmapMode = BitmapMode::Blank;
84
85 // 1.2. Let canvas be the canvas element to which context is bound.
86
87 // 1.3. Set context's output bitmap to be transparent black with an
88 // intrinsic width equal to the numeric value of canvas's width attribute
89 // and an intrinsic height equal to the numeric value of canvas's height
90 // attribute, those values being interpreted in CSS pixels.
91
92 // FIXME: What is the point of creating a full size transparent buffer that
93 // can never be changed? Wouldn't a 1x1 buffer give the same rendering? The
94 // only reason I can think of is toDataURL(), but that doesn't seem like
95 // a good enough argument to waste memory.
96
97 canvas()->setImageBufferAndMarkDirty(ImageBuffer::create(FloatSize(canvas()->width(), canvas()->height()), bufferRenderingMode));
98
99 // 1.4. Set the output bitmap's origin-clean flag to true.
100
101 canvas()->setOriginClean();
102 return;
103 }
104
105 // 2. If a bitmap argument was provided, then:
106
107 // 2.1. Set context's bitmap mode to valid.
108
109 m_bitmapMode = BitmapMode::Valid;
110
111 // 2.2. Set context's output bitmap to refer to the same underlying
112 // bitmap data as bitmap, without making a copy.
113 // Note: the origin-clean flag of bitmap is included in the
114 // bitmap data to be referenced by context's output bitmap.
115
116 if (imageBitmap->originClean())
117 canvas()->setOriginClean();
118 else
119 canvas()->setOriginTainted();
120 canvas()->setImageBufferAndMarkDirty(imageBitmap->transferOwnershipAndClose());
121}
122
123ExceptionOr<void> ImageBitmapRenderingContext::transferFromImageBitmap(RefPtr<ImageBitmap> imageBitmap)
124{
125 // 1. Let bitmapContext be the ImageBitmapRenderingContext object on which
126 // the transferFromImageBitmap() method was called.
127
128 // 2. If imageBitmap is null, then run the steps to set an ImageBitmapRenderingContext's
129 // output bitmap, with bitmapContext as the context argument and no bitmap argument,
130 // then abort these steps.
131
132 if (!imageBitmap) {
133 setOutputBitmap(nullptr);
134 return { };
135 }
136
137 // 3. If the value of imageBitmap's [[Detached]] internal slot is set to true,
138 // then throw an "InvalidStateError" DOMException and abort these steps.
139
140 if (imageBitmap->isDetached())
141 return Exception { InvalidStateError };
142
143 // 4. Run the steps to set an ImageBitmapRenderingContext's output bitmap,
144 // with the context argument equal to bitmapContext, and the bitmap
145 // argument referring to imageBitmap's underlying bitmap data.
146
147 setOutputBitmap(imageBitmap);
148
149 // 5. Set the value of imageBitmap's [[Detached]] internal slot to true.
150 // 6. Unset imageBitmap's bitmap data.
151
152 // Note that the algorithm in the specification is currently a bit
153 // muddy here. The setOutputBitmap step above had to transfer ownership
154 // from the imageBitmap to this object, which requires a detach and unset,
155 // so this step isn't necessary, but we'll do it anyway.
156
157 imageBitmap->close();
158
159 return { };
160}
161
162}
163