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 } |
| 44 |
| 45 if (isPoint) { |
32 SkScalar intensity = 0.0f; | 46 SkScalar intensity = 0.0f; |
33 if (isPoint) { | 47 intensity = buf.readScalar(); |
34 intensity = buf.readScalar(); | 48 Light light = Light::MakePoint(color, dirOrPos, intensity); |
35 } | 49 light.setShadowMap(depthMap); |
36 | 50 builder.add(light); |
37 sk_sp<SkImage> depthMap; | 51 } else { |
38 bool hasShadowMap = buf.readBool(); | 52 Light light = Light::MakeDirectional(color, dirOrPos); |
39 if (hasShadowMap) { | 53 light.setShadowMap(depthMap); |
40 if (!(depthMap = buf.readImage())) { | 54 builder.add(light); |
41 return nullptr; | |
42 } | |
43 } | |
44 | |
45 if (isPoint) { | |
46 Light light = Light::MakePoint(color, dirOrPos, intensity); | |
47 light.setShadowMap(depthMap); | |
48 builder.add(light); | |
49 } else { | |
50 Light light = Light::MakeDirectional(color, dirOrPos); | |
51 light.setShadowMap(depthMap); | |
52 builder.add(light); | |
53 } | |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 return builder.finish(); | 58 return builder.finish(); |
58 } | 59 } |
59 | 60 |
60 void SkLights::flatten(SkWriteBuffer& buf) const { | 61 void SkLights::flatten(SkWriteBuffer& buf) const { |
| 62 buf.writeScalarArray(&this->ambientLightColor().fX, 3); |
61 | 63 |
62 buf.writeInt(this->numLights()); | 64 buf.writeInt(this->numLights()); |
63 for (int l = 0; l < this->numLights(); ++l) { | 65 for (int l = 0; l < this->numLights(); ++l) { |
64 const Light& light = this->light(l); | 66 const Light& light = this->light(l); |
65 | 67 |
66 bool isAmbient = Light::kAmbient_LightType == light.type(); | |
67 bool isPoint = Light::kPoint_LightType == light.type(); | 68 bool isPoint = Light::kPoint_LightType == light.type(); |
68 | 69 |
69 buf.writeBool(isAmbient); | |
70 buf.writeBool(isPoint); | 70 buf.writeBool(isPoint); |
71 buf.writeScalarArray(&light.color().fX, 3); | 71 buf.writeScalarArray(&light.color().fX, 3); |
72 if (!isAmbient) { | 72 buf.writeScalarArray(&light.dir().fX, 3); |
73 if (isPoint) { | 73 bool hasShadowMap = light.getShadowMap() != nullptr; |
74 buf.writeScalarArray(&light.pos().fX, 3); | 74 buf.writeBool(hasShadowMap); |
75 buf.writeScalar(light.intensity()); | 75 if (hasShadowMap) { |
76 } else { | 76 buf.writeImage(light.getShadowMap()); |
77 buf.writeScalarArray(&light.dir().fX, 3); | 77 } |
78 } | 78 if (isPoint) { |
79 bool hasShadowMap = light.getShadowMap() != nullptr; | 79 buf.writeScalar(light.intensity()); |
80 buf.writeBool(hasShadowMap); | |
81 if (hasShadowMap) { | |
82 buf.writeImage(light.getShadowMap()); | |
83 } | |
84 } | 80 } |
85 } | 81 } |
86 } | 82 } |
OLD | NEW |