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 "SkColorSpaceXformer.h" 10 #include "SkLights.h" 11 #include "SkReadBuffer.h" 12 13 sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) { 14 Builder builder; 15 16 SkColor3f ambColor; 17 if (!buf.readScalarArray(&ambColor.fX, 3)) { 18 return nullptr; 19 } 20 21 builder.setAmbientLightColor(ambColor); 22 23 int numLights = buf.readInt(); 24 25 for (int l = 0; l < numLights; ++l) { 26 bool isPoint = buf.readBool(); 27 28 SkColor3f color; 29 if (!buf.readScalarArray(&color.fX, 3)) { 30 return nullptr; 31 } 32 33 SkVector3 dirOrPos; 34 if (!buf.readScalarArray(&dirOrPos.fX, 3)) { 35 return nullptr; 36 } 37 38 if (isPoint) { 39 SkScalar intensity; 40 intensity = buf.readScalar(); 41 Light light = Light::MakePoint(color, dirOrPos, intensity); 42 builder.add(light); 43 } else { 44 Light light = Light::MakeDirectional(color, dirOrPos); 45 builder.add(light); 46 } 47 } 48 49 return builder.finish(); 50 } 51 52 static SkColor3f xform_color(const SkColor3f& color, SkColorSpaceXformer* xformer) { 53 SkColor origColor = SkColorSetARGB(0xFF, 54 SkScalarRoundToInt(color.fX), 55 SkScalarRoundToInt(color.fY), 56 SkScalarRoundToInt(color.fZ)); 57 SkColor xformedColor = xformer->apply(origColor); 58 return SkColor3f::Make(SkIntToScalar(SkGetPackedR32(xformedColor)), 59 SkIntToScalar(SkGetPackedG32(xformedColor)), 60 SkIntToScalar(SkGetPackedB32(xformedColor))); 61 } 62 63 sk_sp<SkLights> SkLights::makeColorSpace(SkColorSpaceXformer* xformer) const { 64 SkLights::Builder builder; 65 for (int i = 0; i < this->numLights(); i++) { 66 Light light(fLights[i].type(), xform_color(fLights[i].color(), xformer), 67 fLights[i].fDirOrPos, fLights[i].fIntensity); 68 builder.add(light); 69 } 70 builder.setAmbientLightColor(xform_color(fAmbientLightColor, xformer)); 71 return builder.finish(); 72 } 73 74 void SkLights::flatten(SkWriteBuffer& buf) const { 75 buf.writeScalarArray(&this->ambientLightColor().fX, 3); 76 77 buf.writeInt(this->numLights()); 78 for (int l = 0; l < this->numLights(); ++l) { 79 const Light& light = this->light(l); 80 81 bool isPoint = Light::kPoint_LightType == light.type(); 82 83 buf.writeBool(isPoint); 84 buf.writeScalarArray(&light.color().fX, 3); 85 buf.writeScalarArray(&light.dir().fX, 3); 86 87 if (isPoint) { 88 buf.writeScalar(light.intensity()); 89 } 90 } 91 } 92