1/*
2 * Copyright (C) 2018 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#pragma once
27
28#if ENABLE(CSS_PAINTING_API)
29
30#include "AffineTransform.h"
31#include "CanvasBase.h"
32#include "EventTarget.h"
33#include "ExceptionOr.h"
34#include "ImageBuffer.h"
35#include "IntSize.h"
36#include "JSDOMPromiseDeferred.h"
37#include "ScriptWrappable.h"
38#include <wtf/Forward.h>
39#include <wtf/RefCounted.h>
40#include <wtf/text/WTFString.h>
41
42namespace WebCore {
43
44class ImageBitmap;
45class PaintRenderingContext2D;
46
47class CustomPaintCanvas final : public RefCounted<CustomPaintCanvas>, public CanvasBase, private ContextDestructionObserver {
48 WTF_MAKE_FAST_ALLOCATED;
49public:
50
51 static Ref<CustomPaintCanvas> create(ScriptExecutionContext&, unsigned width, unsigned height);
52 virtual ~CustomPaintCanvas();
53 bool isCustomPaintCanvas() const final { return true; }
54
55 unsigned width() const final;
56 void setWidth(unsigned);
57 unsigned height() const final;
58 void setHeight(unsigned);
59
60 const IntSize& size() const final;
61 void setSize(const IntSize&) final;
62
63 ExceptionOr<RefPtr<PaintRenderingContext2D>> getContext();
64
65 GraphicsContext* drawingContext() const final;
66 GraphicsContext* existingDrawingContext() const final;
67
68 void makeRenderingResultsAvailable() final;
69 void didDraw(const FloatRect&) final { }
70
71 AffineTransform baseTransform() const final { ASSERT(m_destinationGraphicsContext && m_copiedBuffer); return m_copiedBuffer->baseTransform(); }
72 Image* copiedImage() const final;
73 void replayDisplayList(GraphicsContext*) const;
74
75 using RefCounted::ref;
76 using RefCounted::deref;
77
78private:
79 CustomPaintCanvas(ScriptExecutionContext&, unsigned width, unsigned height);
80
81 void refCanvasBase() final { ref(); }
82 void derefCanvasBase() final { deref(); }
83 ScriptExecutionContext* canvasBaseScriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
84
85 mutable GraphicsContext* m_destinationGraphicsContext = nullptr;
86 mutable IntSize m_size;
87 mutable std::unique_ptr<ImageBuffer> m_copiedBuffer;
88 mutable RefPtr<Image> m_copiedImage;
89};
90
91}
92SPECIALIZE_TYPE_TRAITS_CANVAS(WebCore::CustomPaintCanvas, isCustomPaintCanvas())
93#endif
94