Make use of new SkLights class

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2026763002

Review-Url: https://codereview.chromium.org/2026763002
diff --git a/gm/lightingshader.cpp b/gm/lightingshader.cpp
index 54f698a..ae6a24f 100644
--- a/gm/lightingshader.cpp
+++ b/gm/lightingshader.cpp
@@ -46,13 +46,13 @@
     LightingShaderGM() {
         this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
 
-        SkLightingShader::Lights::Builder builder;
+        SkLights::Builder builder;
 
-        builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, 1.0f),
-                            SkVector3::Make(1.0f, 0.0f, 0.0f)));
-        builder.add(SkLight(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
+        builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
+                                    SkVector3::Make(1.0f, 0.0f, 0.0f)));
+        builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
 
-        fLights.reset(builder.finish());
+        fLights = builder.finish();
     }
 
 protected:
@@ -162,10 +162,10 @@
     static const int kTexSize = 128;
     static const int kGMSize  = 512;
 
-    SkBitmap                fDiffuse;
-    SkBitmap                fNormalMaps[kNormalMapCount];
+    SkBitmap        fDiffuse;
+    SkBitmap        fNormalMaps[kNormalMapCount];
 
-    SkAutoTUnref<const SkLightingShader::Lights>  fLights;
+    sk_sp<SkLights> fLights;
 
     typedef GM INHERITED;
 };
diff --git a/gyp/core.gypi b/gyp/core.gypi
index dfe2432..3319e63 100644
--- a/gyp/core.gypi
+++ b/gyp/core.gypi
@@ -151,7 +151,6 @@
         '<(skia_src_path)/core/SkImageGenerator.cpp',
         '<(skia_src_path)/core/SkImageGeneratorPriv.h',
         '<(skia_src_path)/core/SkLayerInfo.h',
-        '<(skia_src_path)/core/SkLight.h',
         '<(skia_src_path)/core/SkLightingShader.h',
         '<(skia_src_path)/core/SkLightingShader.cpp',
         '<(skia_src_path)/core/SkLinearBitmapPipeline.cpp',
diff --git a/samplecode/SampleLighting.cpp b/samplecode/SampleLighting.cpp
index 5171579..a27aa9d 100755
--- a/samplecode/SampleLighting.cpp
+++ b/samplecode/SampleLighting.cpp
@@ -12,16 +12,16 @@
 #include "SkLightingShader.h"
 #include "SkPoint3.h"
 
-static const SkLightingShader::Lights* create_lights(SkScalar angle, SkScalar blue) {
+static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
 
     const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
                                           SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
                                           SkScalarCos(SK_ScalarPI*0.25f));
 
-    SkLightingShader::Lights::Builder builder;
+    SkLights::Builder builder;
 
-    builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, blue), dir));
-    builder.add(SkLight(SkColor3f::Make(0.1f, 0.1f, 0.1f)));
+    builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, blue), dir));
+    builder.add(SkLights::Light(SkColor3f::Make(0.1f, 0.1f, 0.1f)));
 
     return builder.finish();
 }
@@ -62,11 +62,10 @@
             fColorFactor = 0.0f;
         }
 
-        SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle,
-                                                                          fColorFactor));
+        sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
         SkPaint paint;
         paint.setShader(SkLightingShader::Make(fDiffuseBitmap, fNormalBitmap,
-                                               lights, SkVector::Make(1.0f, 0.0f),
+                                               std::move(lights), SkVector::Make(1.0f, 0.0f),
                                                nullptr, nullptr));
         paint.setColor(SK_ColorBLACK);
 
diff --git a/src/core/SkLight.h b/src/core/SkLight.h
deleted file mode 100644
index d9eb78d..0000000
--- a/src/core/SkLight.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkLight_DEFINED
-#define SkLight_DEFINED
-
-#include "SkPoint3.h"
-
-class SK_API SkLight {
-public:
-    enum LightType {
-        kAmbient_LightType,       // only 'fColor' is used
-        kDirectional_LightType
-    };
-
-    SkLight() : fType(kAmbient_LightType) {
-        fColor.set(0.0f, 0.0f, 0.0f);
-        fDirection.set(0.0f, 0.0f, 1.0f);
-    }
-
-    SkLight(const SkColor3f& color)
-        : fType(kAmbient_LightType)
-        , fColor(color) {
-        fDirection.set(0.0f, 0.0f, 1.0f);
-    }
-
-    SkLight(const SkColor3f& color, const SkVector3& dir)
-        : fType(kDirectional_LightType)
-        , fColor(color)
-        , fDirection(dir) {
-        if (!fDirection.normalize()) {
-            fDirection.set(0.0f, 0.0f, 1.0f);
-        }
-    }
-
-    LightType type() const { return fType; }
-    const SkColor3f& color() const { return fColor; }
-    const SkVector3& dir() const {
-        SkASSERT(kAmbient_LightType != fType);
-        return fDirection;
-    }
-
-private:
-    LightType   fType;
-    SkColor3f   fColor;           // linear (unpremul) color. Range is 0..1 in each channel.
-    SkVector3   fDirection;       // direction towards the light (+Z is out of the screen).
-                                  // If degenerate, it will be replaced with (0, 0, 1).
-};
-
-
-#endif
diff --git a/src/core/SkLightingShader.cpp b/src/core/SkLightingShader.cpp
index f37f664..cb25944 100644
--- a/src/core/SkLightingShader.cpp
+++ b/src/core/SkLightingShader.cpp
@@ -50,13 +50,13 @@
         @param normLocalM the local matrix for the normal coordinates
     */
     SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal,
-                         const SkLightingShader::Lights* lights,
+                         const sk_sp<SkLights> lights,
                          const SkVector& invNormRotation,
                          const SkMatrix* diffLocalM, const SkMatrix* normLocalM)
         : INHERITED(diffLocalM)
         , fDiffuseMap(diffuse)
         , fNormalMap(normal)
-        , fLights(SkRef(lights))
+        , fLights(std::move(lights))
         , fInvNormRotation(invNormRotation) {
 
         if (normLocalM) {
@@ -108,13 +108,13 @@
     bool computeNormTotalInverse(const ContextRec& rec, SkMatrix* normTotalInverse) const;
 
 private:
-    SkBitmap  fDiffuseMap;
-    SkBitmap  fNormalMap;
+    SkBitmap        fDiffuseMap;
+    SkBitmap        fNormalMap;
 
-    SkAutoTUnref<const SkLightingShader::Lights>   fLights;
+    sk_sp<SkLights> fLights;
 
-    SkMatrix  fNormLocalMatrix;
-    SkVector  fInvNormRotation;
+    SkMatrix        fNormLocalMatrix;
+    SkVector        fInvNormRotation;
 
     friend class SkLightingShader;
 
@@ -140,7 +140,7 @@
 public:
     LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& diffMatrix,
                const SkMatrix& normMatrix, const GrTextureParams& diffParams,
-               const GrTextureParams& normParams, const SkLightingShader::Lights* lights,
+               const GrTextureParams& normParams, sk_sp<SkLights> lights,
                const SkVector& invNormRotation)
         : fDiffDeviceTransform(kLocal_GrCoordSet, diffMatrix, diffuse, diffParams.filterMode())
         , fNormDeviceTransform(kLocal_GrCoordSet, normMatrix, normal, normParams.filterMode())
@@ -155,7 +155,7 @@
         // fuse all ambient lights into a single one
         fAmbientColor.set(0.0f, 0.0f, 0.0f);
         for (int i = 0; i < lights->numLights(); ++i) {
-            if (SkLight::kAmbient_LightType == lights->light(i).type()) {
+            if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) {
                 fAmbientColor += lights->light(i).color();
             } else {
                 // TODO: handle more than one of these
@@ -519,9 +519,9 @@
             SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f);
             // This is all done in linear unpremul color space (each component 0..255.0f though)
             for (int l = 0; l < lightShader.fLights->numLights(); ++l) {
-                const SkLight& light = lightShader.fLights->light(l);
+                const SkLights::Light& light = lightShader.fLights->light(l);
 
-                if (SkLight::kAmbient_LightType == light.type()) {
+                if (SkLights::Light::kAmbient_LightType == light.type()) {
                     accum += light.color().makeScale(255.0f);
                 } else {
                     SkScalar NdotL = xformedNorm.dot(light.dir());
@@ -583,7 +583,7 @@
 
     int numLights = buf.readInt();
 
-    SkLightingShader::Lights::Builder builder;
+    SkLights::Builder builder;
 
     for (int l = 0; l < numLights; ++l) {
         bool isAmbient = buf.readBool();
@@ -594,24 +594,24 @@
         }
 
         if (isAmbient) {
-            builder.add(SkLight(color));
+            builder.add(SkLights::Light(color));
         } else {
             SkVector3 dir;
             if (!buf.readScalarArray(&dir.fX, 3)) {
                 return nullptr;
             }
-            builder.add(SkLight(color, dir));
+            builder.add(SkLights::Light(color, dir));
         }
     }
 
-    SkAutoTUnref<const SkLightingShader::Lights> lights(builder.finish());
+    sk_sp<SkLights> lights(builder.finish());
 
     SkVector invNormRotation = {1,0};
     if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) {
         invNormRotation = buf.readPoint();
     }
 
-    return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, lights, invNormRotation,
+    return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), invNormRotation,
                                             &diffLocalM, &normLocalM);
 }
 
@@ -629,9 +629,9 @@
 
     buf.writeInt(fLights->numLights());
     for (int l = 0; l < fLights->numLights(); ++l) {
-        const SkLight& light = fLights->light(l);
+        const SkLights::Light& light = fLights->light(l);
 
-        bool isAmbient = SkLight::kAmbient_LightType == light.type();
+        bool isAmbient = SkLights::Light::kAmbient_LightType == light.type();
 
         buf.writeBool(isAmbient);
         buf.writeScalarArray(&light.color().fX, 3);
@@ -706,7 +706,7 @@
 }
 
 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap& normal,
-                                       const Lights* lights,
+                                       sk_sp<SkLights> lights,
                                        const SkVector& invNormRotation,
                                        const SkMatrix* diffLocalM, const SkMatrix* normLocalM) {
     if (diffuse.isNull() || bitmap_is_too_big(diffuse) ||
@@ -718,8 +718,8 @@
 
     SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1));
 
-    return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, lights, invNormRotation, diffLocalM,
-                                            normLocalM);
+    return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights),
+                                            invNormRotation, diffLocalM, normLocalM);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/core/SkLightingShader.h b/src/core/SkLightingShader.h
index ffbcbe9..f25b303 100644
--- a/src/core/SkLightingShader.h
+++ b/src/core/SkLightingShader.h
@@ -8,68 +8,25 @@
 #ifndef SkLightingShader_DEFINED
 #define SkLightingShader_DEFINED
 
-#include "SkFlattenable.h"
-#include "SkLight.h"
+#include "SkLights.h"
 #include "SkShader.h"
-#include "SkTDArray.h"
 
 class SkBitmap;
 class SkMatrix;
 
 class SK_API SkLightingShader {
 public:
-    class Lights  : public SkRefCnt {
-    public:
-        class Builder {
-        public:
-            Builder(const SkLight lights[], int numLights)
-                : fLights(new Lights(lights, numLights)) {}
-
-            Builder() : fLights(new Lights) {}
-
-            // TODO: limit the number of lights here or just ignore those
-            // above some maximum?
-            void add(const SkLight& light) {
-                if (fLights) {
-                    *fLights->fLights.push() = light;
-                }
-            }
-
-            const Lights* finish() {
-                return fLights.release();
-            }
-
-        private:
-            SkAutoTUnref<Lights> fLights;
-        };
-
-        int numLights() const {
-            return fLights.count();
-        }
-
-        const SkLight& light(int index) const {
-            return fLights[index];
-        }
-
-    private:
-        Lights() {}
-        Lights(const SkLight lights[], int numLights) : fLights(lights, numLights) {}
-
-        SkTDArray<SkLight> fLights;
-
-        typedef SkRefCnt INHERITED;
-    };
-
-    /** Returns a shader that lights the diffuse and normal maps with a single light.
+    /** Returns a shader that lights the diffuse and normal maps with a set of lights.
 
         It returns a shader with a reference count of 1.
         The caller should decrement the shader's reference count when done with the shader.
         It is an error for count to be < 2.
         @param  diffuse     the diffuse bitmap
         @param  normal      the normal map
-        @param  light       the light applied to the normal map
-        @param  ambient     the linear (unpremul) ambient light color. Range is 0..1/channel.
-        @param  localMatrix the matrix mapping the textures to the dest rect
+        @param  lights       the lights applied to the normal map
+        @param  invNormRotation rotation applied to the normal map's normals
+        @param  diffLocalMatrix the local matrix for the diffuse texture
+        @param  normLocalMatrix the local matrix for the normal map
 
         nullptr will be returned if:
             either 'diffuse' or 'normal' are empty
@@ -89,7 +46,7 @@
         (127, 127, 0).
     */
     static sk_sp<SkShader> Make(const SkBitmap& diffuse, const SkBitmap& normal,
-                                const Lights* lights, const SkVector& invNormRotation,
+                                sk_sp<SkLights> lights, const SkVector& invNormRotation,
                                 const SkMatrix* diffLocalMatrix, const SkMatrix* normLocalMatrix);
 
     SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()