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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "IntSize.h"
29#include <wtf/Optional.h>
30#include <wtf/Variant.h>
31
32namespace WebCore {
33
34enum class DecodingMode {
35 Auto,
36 Synchronous,
37 Asynchronous
38};
39
40class DecodingOptions {
41public:
42 explicit DecodingOptions(DecodingMode decodingMode = DecodingMode::Auto)
43 : m_decodingModeOrSize(decodingMode)
44 {
45 }
46
47 DecodingOptions(const Optional<IntSize>& sizeForDrawing)
48 : m_decodingModeOrSize(sizeForDrawing)
49 {
50 }
51
52 bool operator==(const DecodingOptions& other) const
53 {
54 return m_decodingModeOrSize == other.m_decodingModeOrSize;
55 }
56
57 bool isAuto() const
58 {
59 return hasDecodingMode() && WTF::get<DecodingMode>(m_decodingModeOrSize) == DecodingMode::Auto;
60 }
61
62 bool isSynchronous() const
63 {
64 return hasDecodingMode() && WTF::get<DecodingMode>(m_decodingModeOrSize) == DecodingMode::Synchronous;
65 }
66
67 bool isAsynchronous() const
68 {
69 return hasDecodingMode() && WTF::get<DecodingMode>(m_decodingModeOrSize) == DecodingMode::Asynchronous;
70 }
71
72 bool isAsynchronousCompatibleWith(const DecodingOptions& decodingOptions) const
73 {
74 if (isAuto() || decodingOptions.isAuto())
75 return false;
76
77 // Comparing DecodingOptions with isAsynchronous() should not happen.
78 ASSERT(!isAsynchronous());
79 if (isAsynchronous() || decodingOptions.isSynchronous())
80 return false;
81
82 // If the image was synchronously decoded, then it should fit any size.
83 // If we want an image regardless of its size, then the current decoded
84 // image should be fine.
85 if (isSynchronous() || decodingOptions.isAsynchronous())
86 return true;
87
88 ASSERT(decodingOptions.hasSize());
89 if (decodingOptions.hasFullSize())
90 return hasFullSize();
91
92 ASSERT(decodingOptions.hasSizeForDrawing());
93 if (hasFullSize())
94 return true;
95
96 ASSERT(hasSizeForDrawing());
97 return maxDimension(*sizeForDrawing()) >= maxDimension(*decodingOptions.sizeForDrawing());
98 }
99
100 bool hasFullSize() const
101 {
102 return hasSize() && !sizeForDrawing();
103 }
104
105 bool hasSizeForDrawing() const
106 {
107 return hasSize() && sizeForDrawing();
108 }
109
110 Optional<IntSize> sizeForDrawing() const
111 {
112 ASSERT(hasSize());
113 return WTF::get<Optional<IntSize>>(m_decodingModeOrSize);
114 }
115
116 static int maxDimension(const IntSize& size)
117 {
118 return std::max(size.width(), size.height());
119 }
120
121private:
122 template<typename T>
123 bool has() const
124 {
125 return WTF::holds_alternative<T>(m_decodingModeOrSize);
126 }
127
128 bool hasDecodingMode() const
129 {
130 return has<DecodingMode>();
131 }
132
133 bool hasSize() const
134 {
135 return has<Optional<IntSize>>();
136 }
137
138 // Four states of the decoding:
139 // - Synchronous: DecodingMode::Synchronous
140 // - Asynchronous + anySize: DecodingMode::Asynchronous
141 // - Asynchronous + intrinsicSize: an empty Optional<IntSize>>
142 // - Asynchronous + sizeForDrawing: a none empty Optional<IntSize>>
143 using DecodingModeOrSize = Variant<DecodingMode, Optional<IntSize>>;
144 DecodingModeOrSize m_decodingModeOrSize;
145};
146
147}
148