SkLights.cpp revision c1a50e1b735bd717eed1fc72a8f093a1b399cb07
1
2/*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkLights.h"
10#include "SkReadBuffer.h"
11
12sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) {
13    Builder builder;
14
15    SkColor3f ambColor;
16    if (!buf.readScalarArray(&ambColor.fX, 3)) {
17        return nullptr;
18    }
19
20    builder.setAmbientLightColor(ambColor);
21
22    int numLights = buf.readInt();
23
24    for (int l = 0; l < numLights; ++l) {
25        bool isPoint = buf.readBool();
26
27        SkColor3f color;
28        if (!buf.readScalarArray(&color.fX, 3)) {
29            return nullptr;
30        }
31
32        SkVector3 dirOrPos;
33        if (!buf.readScalarArray(&dirOrPos.fX, 3)) {
34            return nullptr;
35        }
36
37        sk_sp<SkImage> depthMap;
38        bool hasShadowMap = buf.readBool();
39        if (hasShadowMap) {
40            if (!(depthMap = buf.readImage())) {
41                return nullptr;
42            }
43        }
44
45        bool isRadial = buf.readBool();
46        if (isPoint) {
47            SkScalar intensity;
48            intensity = buf.readScalar();
49            Light light = Light::MakePoint(color, dirOrPos, intensity, isRadial);
50            light.setShadowMap(depthMap);
51            builder.add(light);
52        } else {
53            Light light = Light::MakeDirectional(color, dirOrPos, isRadial);
54            light.setShadowMap(depthMap);
55            builder.add(light);
56        }
57    }
58
59    return builder.finish();
60}
61
62void SkLights::flatten(SkWriteBuffer& buf) const {
63    buf.writeScalarArray(&this->ambientLightColor().fX, 3);
64
65    buf.writeInt(this->numLights());
66    for (int l = 0; l < this->numLights(); ++l) {
67        const Light& light = this->light(l);
68
69        bool isPoint = Light::kPoint_LightType == light.type();
70
71        buf.writeBool(isPoint);
72        buf.writeScalarArray(&light.color().fX, 3);
73        buf.writeScalarArray(&light.dir().fX, 3);
74
75        bool hasShadowMap = light.getShadowMap() != nullptr;
76        buf.writeBool(hasShadowMap);
77
78        bool isRadial = light.isRadial();
79        buf.writeBool(isRadial);
80
81        if (hasShadowMap) {
82            buf.writeImage(light.getShadowMap());
83        }
84        if (isPoint) {
85            buf.writeScalar(light.intensity());
86        }
87    }
88}
89