1/*
2 * Copyright (C) 2016 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 "Color.h"
29#include "DecodingOptions.h"
30#include "ImageBackingStore.h"
31#include "ImageOrientation.h"
32#include "ImageTypes.h"
33#include "IntSize.h"
34#include "NativeImage.h"
35#include <wtf/Seconds.h>
36
37namespace WebCore {
38
39class ImageFrame {
40 friend class ImageSource;
41public:
42 enum class Caching { Metadata, MetadataAndImage };
43
44 ImageFrame();
45 ImageFrame(const ImageFrame& other) { operator=(other); }
46
47 ~ImageFrame();
48
49 static const ImageFrame& defaultFrame();
50
51 ImageFrame& operator=(const ImageFrame& other);
52
53 unsigned clearImage();
54 unsigned clear();
55
56 void setDecodingStatus(DecodingStatus);
57 DecodingStatus decodingStatus() const;
58
59 bool isInvalid() const { return m_decodingStatus == DecodingStatus::Invalid; }
60 bool isPartial() const { return m_decodingStatus == DecodingStatus::Partial; }
61 bool isComplete() const { return m_decodingStatus == DecodingStatus::Complete; }
62
63 IntSize size() const;
64 IntSize sizeRespectingOrientation() const { return !m_orientation.usesWidthAsHeight() ? size() : size().transposedSize(); }
65 unsigned frameBytes() const { return hasNativeImage() ? (size().area() * sizeof(uint32_t)).unsafeGet() : 0; }
66 SubsamplingLevel subsamplingLevel() const { return m_subsamplingLevel; }
67
68 NativeImagePtr nativeImage() const { return m_nativeImage; }
69
70 void setOrientation(ImageOrientation orientation) { m_orientation = orientation; };
71 ImageOrientation orientation() const { return m_orientation; }
72
73 void setDuration(const Seconds& duration) { m_duration = duration; }
74 Seconds duration() const { return m_duration; }
75
76 void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
77 bool hasAlpha() const { return !hasMetadata() || m_hasAlpha; }
78
79 bool hasNativeImage(const Optional<SubsamplingLevel>& = { }) const;
80 bool hasFullSizeNativeImage(const Optional<SubsamplingLevel>& = { }) const;
81 bool hasDecodedNativeImageCompatibleWithOptions(const Optional<SubsamplingLevel>&, const DecodingOptions&) const;
82 bool hasMetadata() const { return !size().isEmpty(); }
83
84 Color singlePixelSolidColor() const;
85
86private:
87 DecodingStatus m_decodingStatus { DecodingStatus::Invalid };
88 IntSize m_size;
89
90 NativeImagePtr m_nativeImage;
91 SubsamplingLevel m_subsamplingLevel { SubsamplingLevel::Default };
92 DecodingOptions m_decodingOptions;
93
94 ImageOrientation m_orientation { DefaultImageOrientation };
95 Seconds m_duration;
96 bool m_hasAlpha { true };
97};
98
99}
100