1/*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Zoltan Herczeg
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef FELighting_h
28#define FELighting_h
29
30#if ENABLE(FILTERS)
31#include "Color.h"
32#include "Filter.h"
33#include "FilterEffect.h"
34#include "LightSource.h"
35#include "PointLightSource.h"
36#include "SpotLightSource.h"
37#include <wtf/Uint8ClampedArray.h>
38
39// Common base class for FEDiffuseLighting and FESpecularLighting
40
41namespace WebCore {
42
43struct FELightingPaintingDataForNeon;
44
45class FELighting : public FilterEffect {
46public:
47    virtual void platformApplySoftware();
48
49    virtual void determineAbsolutePaintRect() { setAbsolutePaintRect(enclosingIntRect(maxEffectRect())); }
50
51protected:
52    static const int s_minimalRectDimension = 100 * 100; // Empirical data limit for parallel jobs
53
54    enum LightingType {
55        DiffuseLighting,
56        SpecularLighting
57    };
58
59    struct LightingData {
60        // This structure contains only read-only (SMP safe) data
61        Uint8ClampedArray* pixels;
62        float surfaceScale;
63        int widthMultipliedByPixelSize;
64        int widthDecreasedByOne;
65        int heightDecreasedByOne;
66
67        inline void topLeft(int offset, IntPoint& normalVector);
68        inline void topRow(int offset, IntPoint& normalVector);
69        inline void topRight(int offset, IntPoint& normalVector);
70        inline void leftColumn(int offset, IntPoint& normalVector);
71        inline void interior(int offset, IntPoint& normalVector);
72        inline void rightColumn(int offset, IntPoint& normalVector);
73        inline void bottomLeft(int offset, IntPoint& normalVector);
74        inline void bottomRow(int offset, IntPoint& normalVector);
75        inline void bottomRight(int offset, IntPoint& normalVector);
76    };
77
78    template<typename Type>
79    friend class ParallelJobs;
80
81    struct PlatformApplyGenericParameters {
82        FELighting* filter;
83        LightingData data;
84        LightSource::PaintingData paintingData;
85        int yStart;
86        int yEnd;
87    };
88
89    static void platformApplyGenericWorker(PlatformApplyGenericParameters*);
90    static void platformApplyNeonWorker(FELightingPaintingDataForNeon*);
91
92    FELighting(Filter*, LightingType, const Color&, float, float, float, float, float, float, PassRefPtr<LightSource>);
93
94    bool drawLighting(Uint8ClampedArray*, int, int);
95    inline void inlineSetPixel(int offset, LightingData&, LightSource::PaintingData&,
96                               int lightX, int lightY, float factorX, float factorY, IntPoint& normalVector);
97
98    // Not worth to inline every occurence of setPixel.
99    void setPixel(int offset, LightingData&, LightSource::PaintingData&,
100                  int lightX, int lightY, float factorX, float factorY, IntPoint& normalVector);
101
102    inline void platformApply(LightingData&, LightSource::PaintingData&);
103
104    inline void platformApplyGenericPaint(LightingData&, LightSource::PaintingData&, int startX, int startY);
105    inline void platformApplyGeneric(LightingData&, LightSource::PaintingData&);
106
107    static int getPowerCoefficients(float exponent);
108    inline void platformApplyNeon(LightingData&, LightSource::PaintingData&);
109
110    LightingType m_lightingType;
111    RefPtr<LightSource> m_lightSource;
112
113    Color m_lightingColor;
114    float m_surfaceScale;
115    float m_diffuseConstant;
116    float m_specularConstant;
117    float m_specularExponent;
118    float m_kernelUnitLengthX;
119    float m_kernelUnitLengthY;
120};
121
122} // namespace WebCore
123
124#endif // ENABLE(FILTERS)
125
126#endif // FELighting_h
127