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