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#pragma once
27
28#include "JSDOMPromiseDeferred.h"
29#include "ScriptWrappable.h"
30#include <wtf/RefCounted.h>
31
32namespace WebCore {
33
34class Blob;
35class HTMLCanvasElement;
36class HTMLImageElement;
37class HTMLVideoElement;
38class ImageBitmapImageObserver;
39class ImageBuffer;
40class ImageData;
41class IntRect;
42class IntSize;
43class PendingImageBitmap;
44class ScriptExecutionContext;
45class TypedOMCSSImageValue;
46struct ImageBitmapOptions;
47
48class ImageBitmap final : public ScriptWrappable, public RefCounted<ImageBitmap> {
49 WTF_MAKE_ISO_ALLOCATED(ImageBitmap);
50public:
51 using Source = Variant<
52 RefPtr<HTMLImageElement>,
53#if ENABLE(VIDEO)
54 RefPtr<HTMLVideoElement>,
55#endif
56 RefPtr<HTMLCanvasElement>,
57 RefPtr<ImageBitmap>,
58#if ENABLE(CSS_TYPED_OM)
59 RefPtr<TypedOMCSSImageValue>,
60#endif
61 RefPtr<Blob>,
62 RefPtr<ImageData>
63 >;
64
65 using Promise = DOMPromiseDeferred<IDLInterface<ImageBitmap>>;
66
67 static void createPromise(ScriptExecutionContext&, Source&&, ImageBitmapOptions&&, Promise&&);
68 static void createPromise(ScriptExecutionContext&, Source&&, ImageBitmapOptions&&, int sx, int sy, int sw, int sh, Promise&&);
69
70 static Ref<ImageBitmap> create(IntSize);
71 static Ref<ImageBitmap> create(std::pair<std::unique_ptr<ImageBuffer>, bool>&&);
72
73 ~ImageBitmap();
74
75 unsigned width() const;
76 unsigned height() const;
77 void close();
78
79 bool isDetached() const { return m_detached; }
80
81 ImageBuffer* buffer() { return m_bitmapData.get(); }
82
83 bool originClean() const { return m_originClean; }
84
85 std::unique_ptr<ImageBuffer> transferOwnershipAndClose();
86
87 static Vector<std::pair<std::unique_ptr<ImageBuffer>, bool>> detachBitmaps(Vector<RefPtr<ImageBitmap>>&&);
88
89private:
90 friend class ImageBitmapImageObserver;
91 friend class PendingImageBitmap;
92
93 static Ref<ImageBitmap> create(std::unique_ptr<ImageBuffer>&&);
94 ImageBitmap(std::unique_ptr<ImageBuffer>&&);
95
96 static void createPromise(ScriptExecutionContext&, RefPtr<HTMLImageElement>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
97#if ENABLE(VIDEO)
98 static void createPromise(ScriptExecutionContext&, RefPtr<HTMLVideoElement>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
99#endif
100 static void createPromise(ScriptExecutionContext&, RefPtr<HTMLCanvasElement>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
101 static void createPromise(ScriptExecutionContext&, RefPtr<ImageBitmap>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
102 static void createPromise(ScriptExecutionContext&, RefPtr<Blob>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
103 static void createPromise(ScriptExecutionContext&, RefPtr<ImageData>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
104 static void createPromise(ScriptExecutionContext&, RefPtr<TypedOMCSSImageValue>&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
105 static void createFromBuffer(Ref<ArrayBuffer>&&, String mimeType, long long expectedContentLength, const URL&, ImageBitmapOptions&&, Optional<IntRect>, Promise&&);
106
107 std::unique_ptr<ImageBuffer> m_bitmapData;
108 bool m_detached { false };
109 bool m_originClean { true };
110};
111
112}
113