summaryrefslogtreecommitdiff
path: root/Source/WebCore/rendering/style/StyleCustomFilterProgram.h
blob: bff09a98ca89c7719fccd1e359c425da9bcbfb76 (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
/*
 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef StyleCustomFilterProgram_h
#define StyleCustomFilterProgram_h

#if ENABLE(CSS_SHADERS)
#include "CachedResourceClient.h"
#include "CachedResourceHandle.h"
#include "CachedShader.h"
#include "CustomFilterProgram.h"
#include "StyleShader.h"
#include <wtf/FastAllocBase.h>

namespace WebCore {

// CSS Shaders

class StyleCustomFilterProgram : public CustomFilterProgram, public CachedResourceClient {
    WTF_MAKE_FAST_ALLOCATED;
public:
    static PassRefPtr<StyleCustomFilterProgram> create(PassRefPtr<StyleShader> vertexShader, PassRefPtr<StyleShader> fragmentShader)
    {
        return adoptRef(new StyleCustomFilterProgram(vertexShader, fragmentShader));
    }
    
    void setVertexShader(PassRefPtr<StyleShader> shader) { m_vertexShader = shader; }
    StyleShader* vertexShader() const { return m_vertexShader.get(); }
    
    void setFragmentShader(PassRefPtr<StyleShader> shader) { m_fragmentShader = shader; }
    StyleShader* fragmentShader() const { return m_fragmentShader.get(); }
    
    virtual String vertexShaderString() const
    {
        ASSERT(isLoaded());
        return m_cachedVertexShader.get() ? m_cachedVertexShader->shaderString() : String();
    }
    
    virtual String fragmentShaderString() const
    {
        ASSERT(isLoaded());
        return m_cachedFragmentShader.get() ? m_cachedFragmentShader->shaderString() : String();
    }

    virtual bool isLoaded() const
    {
        // Do not use the CachedResource:isLoaded method here, because it actually means !isLoading(),
        // so missing and canceled resources will have isLoaded set to true, even if they are not loaded yet.
        return (!m_cachedVertexShader.get() || m_isVertexShaderLoaded)
            && (!m_cachedFragmentShader.get() || m_isFragmentShaderLoaded);
    }

    virtual void willHaveClients()
    {
        if (m_vertexShader) {
            m_cachedVertexShader = m_vertexShader->cachedShader();
            m_cachedVertexShader->addClient(this);
        }
        if (m_fragmentShader) {
            m_cachedFragmentShader = m_fragmentShader->cachedShader();
            m_cachedFragmentShader->addClient(this);
        }
    }
    
    virtual void didRemoveLastClient()
    {
        if (m_cachedVertexShader.get()) {
            m_cachedVertexShader->removeClient(this);
            m_cachedVertexShader = 0;
            m_isVertexShaderLoaded = false;
        }
        if (m_cachedFragmentShader.get()) {
            m_cachedFragmentShader->removeClient(this);
            m_cachedFragmentShader = 0;
            m_isFragmentShaderLoaded = false;
        }
    }
    
    virtual void notifyFinished(CachedResource* resource)
    {
        if (resource->errorOccurred())
            return;
        if (resource == m_cachedVertexShader.get())
            m_isVertexShaderLoaded = true;
        else if (resource == m_cachedFragmentShader.get())
            m_isFragmentShaderLoaded = true;
        if (isLoaded())
            notifyClients();
    }
    
    CachedShader* cachedVertexShader() const { return m_vertexShader ? m_vertexShader->cachedShader() : 0; }
    CachedShader* cachedFragmentShader() const { return m_fragmentShader ? m_fragmentShader->cachedShader() : 0; }
    
    virtual bool operator==(const CustomFilterProgram& o) const 
    {
        // The following cast is ugly, but StyleCustomFilterProgram is the single implementation of CustomFilterProgram.
        const StyleCustomFilterProgram* other = static_cast<const StyleCustomFilterProgram*>(&o);
        return cachedVertexShader() == other->cachedVertexShader() && cachedFragmentShader() == other->cachedFragmentShader();
    }

private:
    StyleCustomFilterProgram(PassRefPtr<StyleShader> vertexShader, PassRefPtr<StyleShader> fragmentShader)
        : m_vertexShader(vertexShader)
        , m_fragmentShader(fragmentShader)
        , m_isVertexShaderLoaded(false)
        , m_isFragmentShaderLoaded(false)
    {
    }
    
    RefPtr<StyleShader> m_vertexShader;
    RefPtr<StyleShader> m_fragmentShader;
    
    CachedResourceHandle<CachedShader> m_cachedVertexShader;
    CachedResourceHandle<CachedShader> m_cachedFragmentShader;
    
    bool m_isVertexShaderLoaded;
    bool m_isFragmentShaderLoaded;
};

} // namespace WebCore

#endif // ENABLE(CSS_SHADERS)

#endif // StyleCustomFilterProgram_h