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