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:
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;
};
'<(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',
#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();
}
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);
+++ /dev/null
-/*
- * 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
@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) {
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;
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())
// 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
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());
int numLights = buf.readInt();
- SkLightingShader::Lights::Builder builder;
+ SkLights::Builder builder;
for (int l = 0; l < numLights; ++l) {
bool isAmbient = buf.readBool();
}
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);
}
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);
}
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) ||
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);
}
///////////////////////////////////////////////////////////////////////////////
#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
(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()