aboutsummaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorGravatar Robert Phillips <robertphillips@google.com>2018-07-17 12:30:40 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-07-17 17:24:50 +0000
commita8cdbd7431b6a27d28db1bc80d7557cedf7e66d0 (patch)
tree3d866605d2236b31da70535f2a4263c55b01acae /include
parent9e0d7e4072e43495a3907bb2bac7824e8e60c368 (diff)
Restore SkLightingShader and associated classes
This reverts https://skia-review.googlesource.com/c/skia/+/31140 (Remove SkLightingShader and associated classes) and updates the classes to ToT Change-Id: I3b1df1704cca8907aa00f081a7e93339b65ad4fa Reviewed-on: https://skia-review.googlesource.com/141545 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Robert Phillips <robertphillips@google.com>
Diffstat (limited to 'include')
-rw-r--r--include/core/SkFlattenable.h2
-rw-r--r--include/core/SkLights.h195
2 files changed, 196 insertions, 1 deletions
diff --git a/include/core/SkFlattenable.h b/include/core/SkFlattenable.h
index 1bee00a403..af3d5870a8 100644
--- a/include/core/SkFlattenable.h
+++ b/include/core/SkFlattenable.h
@@ -37,7 +37,7 @@ public:
kSkShaderBase_Type,
kSkUnused_Type, // used to be SkUnitMapper
kSkUnused_Type2,
- kSkUnused_Type3, // used to be SkNormalSource
+ kSkNormalSource_Type,
};
typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&);
diff --git a/include/core/SkLights.h b/include/core/SkLights.h
new file mode 100644
index 0000000000..c4262a0fc0
--- /dev/null
+++ b/include/core/SkLights.h
@@ -0,0 +1,195 @@
+
+/*
+ * 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 SkLights_DEFINED
+#define SkLights_DEFINED
+
+#include "SkPoint3.h"
+#include "SkRefCnt.h"
+#include "../private/SkTArray.h"
+
+class SkColorSpaceXformer;
+class SkReadBuffer;
+class SkWriteBuffer;
+
+/** \class SkLights
+ SkLights encapsulates a set of directional, point and ambient lights for use with the
+ SkLightingShader.
+*/
+class SK_API SkLights : public SkRefCnt {
+public:
+ class Light {
+ public:
+ enum LightType {
+ kDirectional_LightType,
+ kPoint_LightType
+ };
+
+ Light(const Light& other)
+ : fType(other.fType)
+ , fColor(other.fColor)
+ , fDirOrPos(other.fDirOrPos)
+ , fIntensity(other.fIntensity) {}
+
+ Light(Light&& other)
+ : fType(other.fType)
+ , fColor(other.fColor)
+ , fDirOrPos(other.fDirOrPos)
+ , fIntensity(other.fIntensity) {}
+
+ static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir) {
+ Light light(kDirectional_LightType, color, dir, 0.0f);
+ if (!light.fDirOrPos.normalize()) {
+ light.fDirOrPos.set(0.0f, 0.0f, 1.0f);
+ }
+ return light;
+ }
+
+ static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkScalar intensity) {
+ return Light(kPoint_LightType, color, pos, intensity);
+ }
+
+ LightType type() const { return fType; }
+ const SkColor3f& color() const { return fColor; }
+ const SkVector3& dir() const {
+ SkASSERT(kDirectional_LightType == fType);
+ return fDirOrPos;
+ }
+ const SkPoint3& pos() const {
+ SkASSERT(kPoint_LightType == fType);
+ return fDirOrPos;
+ }
+ SkScalar intensity() const {
+ SkASSERT(kPoint_LightType == fType);
+ return fIntensity;
+ }
+
+ Light& operator=(const Light& other) {
+ if (this == &other) {
+ return *this;
+ }
+
+ fType = other.fType;
+ fColor = other.fColor;
+ fDirOrPos = other.fDirOrPos;
+ fIntensity = other.fIntensity;
+ return *this;
+ }
+
+ bool operator==(const Light& other) {
+ return (fType == other.fType) &&
+ (fColor == other.fColor) &&
+ (fDirOrPos == other.fDirOrPos) &&
+ (fIntensity == other.fIntensity);
+ }
+
+ bool operator!=(const Light& other) { return !(this->operator==(other)); }
+
+ private:
+ friend class SkLights;
+
+ Light(LightType type, const SkColor3f& color, const SkVector3& dirOrPos,
+ SkScalar intensity)
+ : fType(type)
+ , fColor(color)
+ , fDirOrPos(dirOrPos)
+ , fIntensity(intensity) {}
+
+ LightType fType;
+ SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel.
+
+ SkVector3 fDirOrPos; // For directional lights, holds the direction towards the
+ // light (+Z is out of the screen).
+ // If degenerate, it will be replaced with (0, 0, 1).
+ // For point lights, holds location of point light
+
+ SkScalar fIntensity; // For point lights, dictates the light intensity.
+ // Simply a multiplier to the final light output value.
+ };
+
+ class Builder {
+ public:
+ Builder() : fLights(new SkLights) {}
+
+ void add(const Light& light) {
+ if (fLights) {
+ fLights->fLights.push_back(light);
+ }
+ }
+
+ void add(Light&& light) {
+ if (fLights) {
+ fLights->fLights.push_back(std::move(light));
+ }
+ }
+
+ void setAmbientLightColor(const SkColor3f& color) {
+ if (fLights) {
+ fLights->fAmbientLightColor = color;
+ }
+ }
+
+ sk_sp<SkLights> finish() {
+ return std::move(fLights);
+ }
+
+ private:
+ sk_sp<SkLights> fLights;
+ };
+
+ /** Returns number of lights not including the ambient light.
+
+ @return number of lights not including the ambient light
+ */
+ int numLights() const { return fLights.count(); }
+
+ /** Returns the index-th light.
+
+ @param index the index of the desired light
+ @return the index-th light
+ */
+ const Light& light(int index) const { return fLights[index]; }
+
+ /** Returns the ambient light.
+
+ @return the ambient light
+ */
+ const SkColor3f& ambientLightColor() const {
+ return fAmbientLightColor;
+ }
+
+ /**
+ * Recreate an SkLights object that was serialized into a buffer.
+ *
+ * @param SkReadBuffer Serialized blob data.
+ * @return A new SkLights representing the serialized data, or NULL if the buffer is
+ * invalid.
+ */
+ static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);
+
+ /**
+ * Serialize to a buffer.
+ *
+ * @param buffer the write buffer to write out to
+ */
+ void flatten(SkWriteBuffer& buf) const;
+
+private:
+ friend class SkLightingShaderImpl;
+
+ SkLights() : fAmbientLightColor(SkColor3f::Make(0.0f, 0.0f, 0.0f)) {}
+
+ sk_sp<SkLights> makeColorSpace(SkColorSpaceXformer* xformer) const;
+
+ SkTArray<Light> fLights;
+ SkColor3f fAmbientLightColor;
+
+ typedef SkRefCnt INHERITED;
+};
+
+#endif