aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkLights.cpp
blob: 1073ba0b827227388b91a5038af5a86ff7261b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkColorSpaceXformer.h"
#include "SkLights.h"
#include "SkReadBuffer.h"

sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) {
    Builder builder;

    SkColor3f ambColor;
    if (!buf.readScalarArray(&ambColor.fX, 3)) {
        return nullptr;
    }

    builder.setAmbientLightColor(ambColor);

    int numLights = buf.readInt();

    for (int l = 0; l < numLights; ++l) {
        bool isPoint = buf.readBool();

        SkColor3f color;
        if (!buf.readScalarArray(&color.fX, 3)) {
            return nullptr;
        }

        SkVector3 dirOrPos;
        if (!buf.readScalarArray(&dirOrPos.fX, 3)) {
            return nullptr;
        }

        sk_sp<SkImage> depthMap;
        bool hasShadowMap = buf.readBool();
        if (hasShadowMap) {
            if (!(depthMap = buf.readImage())) {
                return nullptr;
            }
        }

        bool isRadial = buf.readBool();
        if (isPoint) {
            SkScalar intensity;
            intensity = buf.readScalar();
            Light light = Light::MakePoint(color, dirOrPos, intensity, isRadial);
            light.setShadowMap(depthMap);
            builder.add(light);
        } else {
            Light light = Light::MakeDirectional(color, dirOrPos, isRadial);
            light.setShadowMap(depthMap);
            builder.add(light);
        }
    }

    return builder.finish();
}

static SkColor3f xform_color(const SkColor3f& color, SkColorSpaceXformer* xformer) {
    SkColor origColor = SkColorSetARGBInline(0xFF,
                                             SkScalarRoundToInt(color.fX),
                                             SkScalarRoundToInt(color.fY),
                                             SkScalarRoundToInt(color.fZ));
    SkColor xformedColor = xformer->apply(origColor);
    return SkColor3f::Make(SkIntToScalar(SkGetPackedR32(xformedColor)),
                           SkIntToScalar(SkGetPackedG32(xformedColor)),
                           SkIntToScalar(SkGetPackedB32(xformedColor)));
}

sk_sp<SkLights> SkLights::makeColorSpace(SkColorSpaceXformer* xformer) const {
    SkLights::Builder builder;
    for (int i = 0; i < this->numLights(); i++) {
        Light light(fLights[i].type(), xform_color(fLights[i].color(), xformer),
                    fLights[i].fDirOrPos, fLights[i].fIntensity, fLights[i].isRadial());
        builder.add(light);
    }
    builder.setAmbientLightColor(xform_color(fAmbientLightColor, xformer));
    return builder.finish();
}

void SkLights::flatten(SkWriteBuffer& buf) const {
    buf.writeScalarArray(&this->ambientLightColor().fX, 3);

    buf.writeInt(this->numLights());
    for (int l = 0; l < this->numLights(); ++l) {
        const Light& light = this->light(l);

        bool isPoint = Light::kPoint_LightType == light.type();

        buf.writeBool(isPoint);
        buf.writeScalarArray(&light.color().fX, 3);
        buf.writeScalarArray(&light.dir().fX, 3);

        bool hasShadowMap = light.getShadowMap() != nullptr;
        buf.writeBool(hasShadowMap);

        bool isRadial = light.isRadial();
        buf.writeBool(isRadial);

        if (hasShadowMap) {
            buf.writeImage(light.getShadowMap());
        }
        if (isPoint) {
            buf.writeScalar(light.intensity());
        }
    }
}