1 /* 2 * Copyright 2016 Google Inc. 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 8 #include "bench/Benchmark.h" 9 #include "include/core/SkBitmap.h" 10 #include "include/core/SkPictureRecorder.h" 11 #include "modules/skottie/include/Skottie.h" 12 #include "tools/Resources.h" 13 14 class DecodeBench : public Benchmark { 15 protected: DecodeBench(const char * name,const char * source)16 DecodeBench(const char* name, const char* source) 17 : fName(SkStringPrintf("decode_%s", name)) 18 , fSource(source) 19 {} 20 isSuitableFor(Backend backend)21 bool isSuitableFor(Backend backend) final { 22 return backend == kNonRendering_Backend; 23 } 24 onGetName()25 const char* onGetName() final { return fName.c_str(); } 26 onDelayedSetup()27 void onDelayedSetup() override { 28 fData = GetResourceAsData(fSource); 29 SkASSERT(fData); 30 } 31 32 protected: 33 sk_sp<SkData> fData; 34 35 private: 36 const SkString fName; 37 const char* fSource; 38 }; 39 40 class BitmapDecodeBench final : public DecodeBench { 41 public: BitmapDecodeBench(const char * name,const char * source)42 BitmapDecodeBench(const char* name, const char* source) 43 : INHERITED(name, source) 44 {} 45 onDraw(int loops,SkCanvas *)46 void onDraw(int loops, SkCanvas*) override { 47 while (loops-- > 0) { 48 SkBitmap bm; 49 SkAssertResult(DecodeDataToBitmap(fData, &bm)); 50 } 51 } 52 53 private: 54 using INHERITED = DecodeBench; 55 }; 56 57 58 class SkottieDecodeBench final : public DecodeBench { 59 public: SkottieDecodeBench(const char * name,const char * source)60 SkottieDecodeBench(const char* name, const char* source) 61 : INHERITED(name, source) 62 {} 63 onDraw(int loops,SkCanvas *)64 void onDraw(int loops, SkCanvas*) override { 65 while (loops-- > 0) { 66 const auto anim = skottie::Animation::Make(reinterpret_cast<const char*>(fData->data()), 67 fData->size()); 68 } 69 } 70 71 private: 72 using INHERITED = DecodeBench; 73 }; 74 75 class SkottiePictureDecodeBench final : public DecodeBench { 76 public: SkottiePictureDecodeBench(const char * name,const char * source)77 SkottiePictureDecodeBench(const char* name, const char* source) 78 : INHERITED(name, source) 79 {} 80 onDraw(int loops,SkCanvas *)81 void onDraw(int loops, SkCanvas*) override { 82 while (loops-- > 0) { 83 const auto anim = skottie::Animation::Make(reinterpret_cast<const char*>(fData->data()), 84 fData->size()); 85 SkPictureRecorder recorder; 86 anim->seek(0); 87 anim->render(recorder.beginRecording(anim->size().width(), anim->size().height())); 88 89 const auto pic = recorder.finishRecordingAsPicture(); 90 } 91 } 92 93 private: 94 using INHERITED = DecodeBench; 95 }; 96 97 DEF_BENCH(return new SkottieDecodeBench("skottie_large", // 426593 98 "skottie/skottie-text-scale-to-fit-minmax.json")); 99 DEF_BENCH(return new SkottieDecodeBench("skottie_medium", // 10947 100 "skottie/skottie-sphere-effect.json")); 101 DEF_BENCH(return new SkottieDecodeBench("skottie_small", // 1112 102 "skottie/skottie_sample_multiframe.json")); 103 // Created from PhoneHub assets SVG source, with https://lottiefiles.com/svg-to-lottie 104 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_connecting.json", // 216x216 105 "skottie/skottie-phonehub-connecting.json")); 106 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_generic_error.json", // 216x217 107 "skottie/skottie-phonehub-generic-error.json")); 108 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_onboard.json", // 217x217 109 "skottie/skottie-phonehub-onboard.json")); 110 // Created from PhoneHub assets SVG source, with https://jakearchibald.github.io/svgomg/ and then 111 // https://lottiefiles.com/svg-to-lottie 112 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_connecting.json", 113 "skottie/skottie-phonehub-svgo-connecting.json")); 114 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_generic_error.json", 115 "skottie/skottie-phonehub-svgo-generic-error.json")); 116 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_onboard.json", 117 "skottie/skottie-phonehub-svgo-onboard.json")); 118 // Created from PhoneHub assets SVG source: 119 // 1. Manually edited to have no masks (but look the same as the original) 120 // 2. https://jakearchibald.github.io/svgomg/ 121 // 3. https://lottiefiles.com/svg-to-lottie 122 // Note: The Generic Error asset is excluded here because it has no masks in the first place. 123 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_no_masks_connecting.json", 124 "skottie/skottie-phonehub-svgo-no-masks-connecting.json")); 125 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_no_masks_onboard.json", 126 "skottie/skottie-phonehub-svgo-no-masks-onboard.json")); 127 // Created from PhoneHub assets SVG source: 128 // 1. Manually edited to use only the most basic functionality of SVG (but look the same as the 129 // original) 130 // 2. https://jakearchibald.github.io/svgomg/ 131 // 3. https://lottiefiles.com/svg-to-lottie 132 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_no_frills_connecting.json", 133 "skottie/skottie-phonehub-svgo-no-frills-connecting.json")); 134 DEF_BENCH(return new SkottieDecodeBench( 135 "skottie_phonehub_svgo_no_frills_generic_error.json", 136 "skottie/skottie-phonehub-svgo-no-frills-generic-error.json")); 137 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_no_frills_onboard.json", 138 "skottie/skottie-phonehub-svgo-no-frills-onboard.json")); 139 // All of the above PhoneHub benchmarks, with https://skia-review.googlesource.com/c/skia/+/141265 140 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_connecting_min.json", 141 "skottie/skottie-phonehub-connecting_min.json")); 142 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_generic_error_min.json", 143 "skottie/skottie-phonehub-generic-error_min.json")); 144 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_onboard_min.json", 145 "skottie/skottie-phonehub-onboard_min.json")); 146 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_connecting_min.json", 147 "skottie/skottie-phonehub-svgo-connecting_min.json")); 148 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_generic_error_min.json", 149 "skottie/skottie-phonehub-svgo-generic-error_min.json")); 150 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_onboard_min.json", 151 "skottie/skottie-phonehub-svgo-onboard_min.json")); 152 DEF_BENCH(return new SkottieDecodeBench( 153 "skottie_phonehub_svgo_no_masks_connecting_min.json", 154 "skottie/skottie-phonehub-svgo-no-masks-connecting_min.json")); 155 DEF_BENCH(return new SkottieDecodeBench("skottie_phonehub_svgo_no_masks_onboard_min.json", 156 "skottie/skottie-phonehub-svgo-no-masks-onboard_min.json")); 157 DEF_BENCH(return new SkottieDecodeBench( 158 "skottie_phonehub_svgo_no_frills_connecting_min.json", 159 "skottie/skottie-phonehub-svgo-no-frills-connecting_min.json")); 160 DEF_BENCH(return new SkottieDecodeBench( 161 "skottie_phonehub_svgo_no_frills_generic_error_min.json", 162 "skottie/skottie-phonehub-svgo-no-frills-generic-error_min.json")); 163 DEF_BENCH( 164 return new SkottieDecodeBench("skottie_phonehub_svgo_no_frills_onboard_min.json", 165 "skottie/skottie-phonehub-svgo-no-frills-onboard_min.json")); 166 167 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_large", 168 "skottie/skottie-text-scale-to-fit-minmax.json")); 169 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_medium", 170 "skottie/skottie-sphere-effect.json")); 171 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_small", 172 "skottie/skottie_sample_multiframe.json")); 173 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_connecting.json", 174 "skottie/skottie-phonehub-connecting.json")); 175 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_generic_error.json", 176 "skottie/skottie-phonehub-generic-error.json")); 177 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_onboard.json", 178 "skottie/skottie-phonehub-onboard.json")); 179 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_svgo_connecting.json", 180 "skottie/skottie-phonehub-svgo-connecting.json")); 181 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_svgo_generic_error.json", 182 "skottie/skottie-phonehub-svgo-generic-error.json")); 183 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_svgo_onboard.json", 184 "skottie/skottie-phonehub-svgo-onboard.json")); 185 DEF_BENCH(return new SkottiePictureDecodeBench( 186 "skottiepic_phonehub_svgo_no_masks_connecting.json", 187 "skottie/skottie-phonehub-svgo-no-masks-connecting.json")); 188 DEF_BENCH(return new SkottiePictureDecodeBench( 189 "skottiepic_phonehub_svgo_no_masks_onboard.json", 190 "skottie/skottie-phonehub-svgo-no-masks-onboard.json")); 191 DEF_BENCH(return new SkottiePictureDecodeBench( 192 "skottiepic_phonehub_svgo_no_frills_connecting.json", 193 "skottie/skottie-phonehub-svgo-no-frills-connecting.json")); 194 DEF_BENCH(return new SkottiePictureDecodeBench( 195 "skottiepic_phonehub_svgo_no_frills_generic_error.json", 196 "skottie/skottie-phonehub-svgo-no-frills-generic-error.json")); 197 DEF_BENCH(return new SkottiePictureDecodeBench( 198 "skottiepic_phonehub_svgo_no_frills_onboard.json", 199 "skottie/skottie-phonehub-svgo-no-frills-onboard.json")); 200 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_connecting_min.json", 201 "skottie/skottie-phonehub-connecting_min.json")); 202 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_generic_error_min.json", 203 "skottie/skottie-phonehub-generic-error_min.json")); 204 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_onboard_min.json", 205 "skottie/skottie-phonehub-onboard_min.json")); 206 DEF_BENCH( 207 return new SkottiePictureDecodeBench("skottiepic_phonehub_svgo_connecting_min.json", 208 "skottie/skottie-phonehub-svgo-connecting_min.json")); 209 DEF_BENCH(return new SkottiePictureDecodeBench( 210 "skottiepic_phonehub_svgo_generic_error_min.json", 211 "skottie/skottie-phonehub-svgo-generic-error_min.json")); 212 DEF_BENCH(return new SkottiePictureDecodeBench("skottiepic_phonehub_svgo_onboard_min.json", 213 "skottie/skottie-phonehub-svgo-onboard_min.json")); 214 DEF_BENCH(return new SkottiePictureDecodeBench( 215 "skottiepic_phonehub_svgo_no_masks_connecting_min.json", 216 "skottie/skottie-phonehub-svgo-no-masks-connecting_min.json")); 217 DEF_BENCH(return new SkottiePictureDecodeBench( 218 "skottiepic_phonehub_svgo_no_masks_onboard_min.json", 219 "skottie/skottie-phonehub-svgo-no-masks-onboard_min.json")); 220 DEF_BENCH(return new SkottiePictureDecodeBench( 221 "skottiepic_phonehub_svgo_no_frills_connecting_min.json", 222 "skottie/skottie-phonehub-svgo-no-frills-connecting_min.json")); 223 DEF_BENCH(return new SkottiePictureDecodeBench( 224 "skottiepic_phonehub_svgo_no_frills_generic_error_min.json", 225 "skottie/skottie-phonehub-svgo-no-frills-generic-error_min.json")); 226 DEF_BENCH(return new SkottiePictureDecodeBench( 227 "skottiepic_phonehub_svgo_no_frills_onboard_min.json", 228 "skottie/skottie-phonehub-svgo-no-frills-onboard_min.json")); 229 230 DEF_BENCH(return new BitmapDecodeBench("png_large" /*1600x1600*/, "images/mandrill_1600.png")); 231 DEF_BENCH(return new BitmapDecodeBench("png_medium" /* 512x 512*/, "images/mandrill_512.png")); 232 DEF_BENCH(return new BitmapDecodeBench("png_small" /* 32x 32*/, "images/mandrill_32.png")); 233 DEF_BENCH(return new BitmapDecodeBench("png_phonehub_connecting" , "images/Connecting.png")); 234 DEF_BENCH(return new BitmapDecodeBench("png_phonehub_generic_error", "images/Generic_Error.png")); 235 DEF_BENCH(return new BitmapDecodeBench("png_phonehub_onboard" , "images/Onboard.png")); 236