summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/core/style/StyleFetchedImage.cpp
blob: 135b2b4414de6d4b5f96a962a432ec5c0b072734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 *           (C) 2000 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "core/style/StyleFetchedImage.h"

#include "core/css/CSSImageValue.h"
#include "core/fetch/ImageResource.h"
#include "core/layout/LayoutObject.h"
#include "core/svg/graphics/SVGImage.h"
#include "core/svg/graphics/SVGImageForContainer.h"

namespace blink {

StyleFetchedImage::StyleFetchedImage(ImageResource* image, Document* document, const KURL& url)
    : m_image(image)
    , m_document(document)
    , m_url(url)
{
    m_isImageResource = true;
    m_image->addClient(this);
#if ENABLE(OILPAN)
    ThreadState::current()->registerPreFinalizer(this);
#endif
}

StyleFetchedImage::~StyleFetchedImage()
{
#if !ENABLE(OILPAN)
    dispose();
#endif
}

void StyleFetchedImage::dispose()
{
    m_image->removeClient(this);
    m_image = nullptr;
}

WrappedImagePtr StyleFetchedImage::data() const
{
    return m_image.get();
}

ImageResource* StyleFetchedImage::cachedImage() const
{
    return m_image.get();
}

PassRefPtrWillBeRawPtr<CSSValue> StyleFetchedImage::cssValue() const
{
    return CSSImageValue::create(m_image->url(), const_cast<StyleFetchedImage*>(this));
}

PassRefPtrWillBeRawPtr<CSSValue> StyleFetchedImage::computedCSSValue() const
{
    return cssValue();
}

bool StyleFetchedImage::canRender() const
{
    return !m_image->errorOccurred() && !m_image->getImage()->isNull();
}

bool StyleFetchedImage::isLoaded() const
{
    return m_image->isLoaded();
}

bool StyleFetchedImage::errorOccurred() const
{
    return m_image->errorOccurred();
}

LayoutSize StyleFetchedImage::imageSize(const LayoutObject&, float multiplier, const LayoutSize& defaultObjectSize) const
{
    if (m_image->getImage() && m_image->getImage()->isSVGImage())
        return imageSizeForSVGImage(toSVGImage(m_image->getImage()), multiplier, defaultObjectSize);

    // Image orientation should only be respected for content images,
    // not decorative images such as StyleImage (backgrounds,
    // border-image, etc.)
    //
    // https://drafts.csswg.org/css-images-3/#the-image-orientation
    return m_image->imageSize(DoNotRespectImageOrientation, multiplier);
}

bool StyleFetchedImage::imageHasRelativeSize() const
{
    return m_image->imageHasRelativeSize();
}

bool StyleFetchedImage::usesImageContainerSize() const
{
    return m_image->usesImageContainerSize();
}

void StyleFetchedImage::addClient(LayoutObject* layoutObject)
{
    m_image->addClient(layoutObject);
}

void StyleFetchedImage::removeClient(LayoutObject* layoutObject)
{
    m_image->removeClient(layoutObject);
}

void StyleFetchedImage::notifyFinished(Resource* resource)
{
    if (m_document && m_image && m_image->getImage() && m_image->getImage()->isSVGImage())
        toSVGImage(m_image->getImage())->updateUseCounters(*m_document);
    // Oilpan: do not prolong the Document's lifetime.
    m_document.clear();
}

PassRefPtr<Image> StyleFetchedImage::image(const LayoutObject&, const IntSize& containerSize, float zoom) const
{
    if (!m_image->getImage()->isSVGImage())
        return m_image->getImage();

    return SVGImageForContainer::create(toSVGImage(m_image->getImage()), containerSize, zoom, m_url);
}

bool StyleFetchedImage::knownToBeOpaque(const LayoutObject& layoutObject) const
{
    TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "PaintImage", "data", InspectorPaintImageEvent::data(&layoutObject, *m_image.get()));
    return m_image->getImage()->currentFrameKnownToBeOpaque(Image::PreCacheMetadata);
}

DEFINE_TRACE(StyleFetchedImage)
{
    visitor->trace(m_image);
    visitor->trace(m_document);
    StyleImage::trace(visitor);
}

} // namespace blink