| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkTypes.h" |
| 10 | #include "include/utils/SkRandom.h" |
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "modules/particles/include/SkParticleEffect.h" |
| 12 | #include "modules/particles/include/SkParticleSerialization.h" |
| Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 13 | #include "modules/skresources/include/SkResources.h" |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 14 | |
| 15 | #include <string> |
| 16 | |
| 17 | #include <emscripten.h> |
| 18 | #include <emscripten/bind.h> |
| 19 | |
| 20 | using namespace emscripten; |
| 21 | |
| Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | class ParticleAssetProvider : public skresources::ResourceProvider { |
| 25 | public: |
| 26 | ~ParticleAssetProvider() override = default; |
| 27 | |
| 28 | // Tried using a map, but that gave strange errors like |
| 29 | // https://emscripten.org/docs/porting/guidelines/function_pointer_issues.html |
| 30 | // Not entirely sure why, but perhaps the iterator in the map was |
| 31 | // confusing enscripten. |
| 32 | using AssetVec = std::vector<std::pair<SkString, sk_sp<SkData>>>; |
| 33 | |
| 34 | static sk_sp<ParticleAssetProvider> Make(AssetVec assets) { |
| 35 | if (assets.empty()) { |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
| 39 | return sk_sp<ParticleAssetProvider>(new ParticleAssetProvider(std::move(assets))); |
| 40 | } |
| 41 | |
| 42 | sk_sp<skresources::ImageAsset> loadImageAsset(const char[] /* path */, |
| 43 | const char name[], |
| 44 | const char[] /* id */) const override { |
| 45 | // For CK we ignore paths & IDs, and identify images based solely on name. |
| 46 | if (auto data = this->findAsset(name)) { |
| 47 | return skresources::MultiFrameImageAsset::Make(std::move(data)); |
| 48 | } |
| 49 | |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | sk_sp<SkData> loadFont(const char name[], const char[] /* url */) const override { |
| 54 | // Same as images paths, we ignore font URLs. |
| 55 | return this->findAsset(name); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | explicit ParticleAssetProvider(AssetVec assets) : fAssets(std::move(assets)) {} |
| 60 | |
| 61 | sk_sp<SkData> findAsset(const char name[]) const { |
| 62 | for (const auto& asset : fAssets) { |
| 63 | if (asset.first.equals(name)) { |
| 64 | return asset.second; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | SkDebugf("Could not find %s\n", name); |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | const AssetVec fAssets; |
| 73 | }; |
| 74 | |
| 75 | } |
| 76 | |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 77 | EMSCRIPTEN_BINDINGS(Particles) { |
| 78 | class_<SkParticleEffect>("SkParticleEffect") |
| 79 | .smart_ptr<sk_sp<SkParticleEffect>>("sk_sp<SkParticleEffect>") |
| 80 | .function("draw", &SkParticleEffect::draw, allow_raw_pointers()) |
| 81 | .function("start", select_overload<void (double, bool)>(&SkParticleEffect::start)) |
| Brian Osman | 4beafa6 | 2019-10-15 13:35:04 -0400 | [diff] [blame] | 82 | .function("update", select_overload<void (double)>(&SkParticleEffect::update)) |
| 83 | .function("setPosition", select_overload<void (SkPoint)>(&SkParticleEffect::setPosition)) |
| 84 | .function("setRate", select_overload<void (float)>(&SkParticleEffect::setRate)); |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 85 | |
| Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 86 | function("_MakeParticles", optional_override([](std::string json, |
| 87 | size_t assetCount, |
| 88 | uintptr_t /* char** */ nptr, |
| 89 | uintptr_t /* uint8_t** */ dptr, |
| 90 | uintptr_t /* size_t* */ sptr) |
| 91 | ->sk_sp<SkParticleEffect> { |
| 92 | // See the comment in canvaskit_bindings.cpp about the use of uintptr_t |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 93 | static bool didInit = false; |
| 94 | if (!didInit) { |
| Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 95 | SkParticleEffect::RegisterParticleTypes(); |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 96 | didInit = true; |
| 97 | } |
| Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 98 | |
| 99 | const auto assetNames = reinterpret_cast<char** >(nptr); |
| 100 | const auto assetDatas = reinterpret_cast<uint8_t**>(dptr); |
| 101 | const auto assetSizes = reinterpret_cast<size_t* >(sptr); |
| 102 | |
| 103 | ParticleAssetProvider::AssetVec assets; |
| 104 | assets.reserve(assetCount); |
| 105 | |
| 106 | for (size_t i = 0; i < assetCount; i++) { |
| 107 | auto name = SkString(assetNames[i]); |
| 108 | auto bytes = SkData::MakeFromMalloc(assetDatas[i], assetSizes[i]); |
| 109 | assets.push_back(std::make_pair(std::move(name), std::move(bytes))); |
| 110 | } |
| 111 | |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 112 | SkRandom r; |
| 113 | sk_sp<SkParticleEffectParams> params(new SkParticleEffectParams()); |
| 114 | skjson::DOM dom(json.c_str(), json.length()); |
| 115 | SkFromJsonVisitor fromJson(dom.root()); |
| 116 | params->visitFields(&fromJson); |
| Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 117 | params->prepare(skresources::DataURIResourceProviderProxy::Make( |
| 118 | ParticleAssetProvider::Make(std::move(assets))).get()); |
| 119 | return sk_sp<SkParticleEffect>(new SkParticleEffect(std::move(params), r)); |
| Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 120 | })); |
| 121 | constant("particles", true); |
| 122 | |
| 123 | } |