Make use of new SkLights class
authorrobertphillips <robertphillips@google.com>
Tue, 31 May 2016 19:08:25 +0000 (12:08 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 31 May 2016 19:08:25 +0000 (12:08 -0700)
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2026763002

Review-Url: https://codereview.chromium.org/2026763002

gm/lightingshader.cpp
gyp/core.gypi
samplecode/SampleLighting.cpp
src/core/SkLight.h [deleted file]
src/core/SkLightingShader.cpp
src/core/SkLightingShader.h

index 54f698a75323cbf2abbd2fb989d15ef97f1f90fb..ae6a24f9f0c3ce895aeae048f022c53ce0504daf 100644 (file)
@@ -46,13 +46,13 @@ public:
     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 @@ private:
     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;
 };
index dfe24323c5ec467cada248e9c3296c3e3c23b5fc..3319e6311c1507179c2ba131473ecce6b54d8998 100644 (file)
         '<(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',
index 5171579f6459d0b5ce9665c4985a143b8f0c8ae9..a27aa9de41b117667e030ff9c6cdd61c13e6de5a 100755 (executable)
 #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 @@ protected:
             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 (file)
index d9eb78d..0000000
+++ /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
index f37f664d9859a537ff1f30a2550c552a90b74680..cb25944a01fc9e4e9dfff5442a965a04c0ffecbf 100644 (file)
@@ -50,13 +50,13 @@ public:
         @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 @@ protected:
     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 @@ class LightingFP : public GrFragmentProcessor {
 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 @@ public:
         // 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 @@ void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y,
             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 @@ sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) {
 
     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 @@ sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) {
         }
 
         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 @@ void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const {
 
     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 @@ static bool bitmap_is_too_big(const SkBitmap& bm) {
 }
 
 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 @@ sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap&
 
     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);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
index ffbcbe9eb4ecae3808b6992118f2b3678677a876..f25b30342a70699506cc03a5ffbc786e1e620207 100644 (file)
@@ -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 @@ public:
         (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()