| 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 | #include "config.h" |
| 27 | #include "ImageFrame.h" |
| 28 | |
| 29 | #include <wtf/NeverDestroyed.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | ImageFrame::ImageFrame() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | ImageFrame::~ImageFrame() |
| 38 | { |
| 39 | clearImage(); |
| 40 | } |
| 41 | |
| 42 | const ImageFrame& ImageFrame::defaultFrame() |
| 43 | { |
| 44 | static NeverDestroyed<ImageFrame> sharedInstance; |
| 45 | return sharedInstance; |
| 46 | } |
| 47 | |
| 48 | ImageFrame& ImageFrame::operator=(const ImageFrame& other) |
| 49 | { |
| 50 | if (this == &other) |
| 51 | return *this; |
| 52 | |
| 53 | m_decodingStatus = other.m_decodingStatus; |
| 54 | m_size = other.m_size; |
| 55 | |
| 56 | m_nativeImage = other.m_nativeImage; |
| 57 | m_subsamplingLevel = other.m_subsamplingLevel; |
| 58 | m_decodingOptions = other.m_decodingOptions; |
| 59 | |
| 60 | m_orientation = other.m_orientation; |
| 61 | m_duration = other.m_duration; |
| 62 | m_hasAlpha = other.m_hasAlpha; |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | void ImageFrame::setDecodingStatus(DecodingStatus decodingStatus) |
| 67 | { |
| 68 | ASSERT(decodingStatus != DecodingStatus::Decoding); |
| 69 | m_decodingStatus = decodingStatus; |
| 70 | } |
| 71 | |
| 72 | DecodingStatus ImageFrame::decodingStatus() const |
| 73 | { |
| 74 | ASSERT(m_decodingStatus != DecodingStatus::Decoding); |
| 75 | return m_decodingStatus; |
| 76 | } |
| 77 | |
| 78 | unsigned ImageFrame::clearImage() |
| 79 | { |
| 80 | if (!hasNativeImage()) |
| 81 | return 0; |
| 82 | |
| 83 | unsigned frameBytes = this->frameBytes(); |
| 84 | |
| 85 | clearNativeImageSubimages(m_nativeImage); |
| 86 | m_nativeImage = nullptr; |
| 87 | m_decodingOptions = DecodingOptions(); |
| 88 | |
| 89 | return frameBytes; |
| 90 | } |
| 91 | |
| 92 | unsigned ImageFrame::clear() |
| 93 | { |
| 94 | unsigned frameBytes = clearImage(); |
| 95 | *this = ImageFrame(); |
| 96 | return frameBytes; |
| 97 | } |
| 98 | |
| 99 | IntSize ImageFrame::size() const |
| 100 | { |
| 101 | return m_size; |
| 102 | } |
| 103 | |
| 104 | bool ImageFrame::hasNativeImage(const Optional<SubsamplingLevel>& subsamplingLevel) const |
| 105 | { |
| 106 | return m_nativeImage && (!subsamplingLevel || *subsamplingLevel >= m_subsamplingLevel); |
| 107 | } |
| 108 | |
| 109 | bool ImageFrame::hasFullSizeNativeImage(const Optional<SubsamplingLevel>& subsamplingLevel) const |
| 110 | { |
| 111 | return hasNativeImage(subsamplingLevel) && (m_decodingOptions.isSynchronous() || m_decodingOptions.hasFullSize()); |
| 112 | } |
| 113 | |
| 114 | bool ImageFrame::hasDecodedNativeImageCompatibleWithOptions(const Optional<SubsamplingLevel>& subsamplingLevel, const DecodingOptions& decodingOptions) const |
| 115 | { |
| 116 | return hasNativeImage(subsamplingLevel) && m_decodingOptions.isAsynchronousCompatibleWith(decodingOptions); |
| 117 | } |
| 118 | |
| 119 | Color ImageFrame::singlePixelSolidColor() const |
| 120 | { |
| 121 | if (!hasNativeImage() || m_size != IntSize(1, 1)) |
| 122 | return Color(); |
| 123 | |
| 124 | return nativeImageSinglePixelSolidColor(m_nativeImage); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |