1/*
2 * Copyright (C) 2012 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#ifndef StyleCachedImageSet_h
27#define StyleCachedImageSet_h
28
29#if ENABLE(CSS_IMAGE_SET)
30
31#include "CachedImageClient.h"
32#include "CachedResourceHandle.h"
33#include "LayoutSize.h"
34#include "StyleImage.h"
35
36namespace WebCore {
37
38class CachedImage;
39class CSSImageSetValue;
40
41// This class keeps one cached image and has access to a set of alternatives.
42
43class StyleCachedImageSet final : public StyleImage, private CachedImageClient {
44    WTF_MAKE_FAST_ALLOCATED;
45public:
46    static PassRefPtr<StyleCachedImageSet> create(CachedImage* image, float imageScaleFactor, CSSImageSetValue* value)
47    {
48        return adoptRef(new StyleCachedImageSet(image, imageScaleFactor, value));
49    }
50    virtual ~StyleCachedImageSet();
51
52    virtual CachedImage* cachedImage() const override { return m_bestFitImage.get(); }
53
54    void clearImageSetValue() { m_imageSetValue = nullptr; }
55
56private:
57    virtual PassRefPtr<CSSValue> cssValue() const override;
58
59    // FIXME: This is used by StyleImage for equality comparison, but this implementation
60    // only looks at the image from the set that we have loaded. I'm not sure if that is
61    // meaningful enough or not.
62    virtual WrappedImagePtr data() const override { return m_bestFitImage.get(); }
63
64    virtual bool canRender(const RenderObject*, float multiplier) const override;
65    virtual bool isLoaded() const override;
66    virtual bool errorOccurred() const override;
67    virtual FloatSize imageSize(const RenderElement*, float multiplier) const override;
68    virtual bool imageHasRelativeWidth() const override;
69    virtual bool imageHasRelativeHeight() const override;
70    virtual void computeIntrinsicDimensions(const RenderElement*, Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) override;
71    virtual bool usesImageContainerSize() const override;
72    virtual void setContainerSizeForRenderer(const RenderElement*, const FloatSize&, float) override;
73    virtual void addClient(RenderElement*) override;
74    virtual void removeClient(RenderElement*) override;
75    virtual PassRefPtr<Image> image(RenderElement*, const FloatSize&) const override;
76    virtual float imageScaleFactor() const override { return m_imageScaleFactor; }
77    virtual bool knownToBeOpaque(const RenderElement*) const override;
78
79    StyleCachedImageSet(CachedImage*, float imageScaleFactor, CSSImageSetValue*);
80
81    CachedResourceHandle<CachedImage> m_bestFitImage;
82    float m_imageScaleFactor;
83    CSSImageSetValue* m_imageSetValue; // Not retained; it owns us.
84};
85
86STYLE_IMAGE_TYPE_CASTS(StyleCachedImageSet, StyleImage, isCachedImageSet)
87
88} // namespace WebCore
89
90#endif // ENABLE(CSS_IMAGE_SET)
91
92#endif // StyleCachedImageSet_h
93