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 "src/core/SkReadBuffer.h" 10 #include "src/shaders/SkLights.h" 11 MakeFromBuffer(SkReadBuffer & buf)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 if (isPoint) { 38 SkScalar intensity; 39 intensity = buf.readScalar(); 40 Light light = Light::MakePoint(color, dirOrPos, intensity); 41 builder.add(light); 42 } else { 43 Light light = Light::MakeDirectional(color, dirOrPos); 44 builder.add(light); 45 } 46 } 47 48 return builder.finish(); 49 } 50 flatten(SkWriteBuffer & buf) const51void SkLights::flatten(SkWriteBuffer& buf) const { 52 buf.writeScalarArray(&this->ambientLightColor().fX, 3); 53 54 buf.writeInt(this->numLights()); 55 for (int l = 0; l < this->numLights(); ++l) { 56 const Light& light = this->light(l); 57 58 bool isPoint = Light::kPoint_LightType == light.type(); 59 60 buf.writeBool(isPoint); 61 buf.writeScalarArray(&light.color().fX, 3); 62 buf.writeScalarArray(&light.dir().fX, 3); 63 64 if (isPoint) { 65 buf.writeScalar(light.intensity()); 66 } 67 } 68 } 69