ParagraphBench.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2019 Google LLC.
  2. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  3. #include "bench/Benchmark.h"
  4. #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
  5. #include "modules/skparagraph/include/FontCollection.h"
  6. #include "modules/skparagraph/include/Paragraph.h"
  7. #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
  8. #include "modules/skparagraph/src/ParagraphImpl.h"
  9. #include "tools/Resources.h"
  10. #include <cfloat>
  11. #include "modules/skparagraph/utils/TestFontCollection.h"
  12. #include "include/core/SkPictureRecorder.h"
  13. using namespace skia::textlayout;
  14. namespace {
  15. struct ParagraphBench : public Benchmark {
  16. ParagraphBench(SkScalar width, const char* r, const char* n)
  17. : fResource(r), fName(n), fWidth(width) {}
  18. sk_sp<SkData> fData;
  19. const char* fResource;
  20. const char* fName;
  21. SkScalar fWidth;
  22. const char* onGetName() override { return fName; }
  23. bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
  24. void onDelayedSetup() override { fData = GetResourceAsData(fResource); }
  25. void onDraw(int loops, SkCanvas*) override {
  26. if (!fData) {
  27. return;
  28. }
  29. const char* text = (const char*)fData->data();
  30. auto fontCollection = sk_make_sp<FontCollection>();
  31. fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
  32. ParagraphStyle paragraph_style;
  33. paragraph_style.turnHintingOff();
  34. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  35. builder.addText(text);
  36. auto paragraph = builder.Build();
  37. SkPictureRecorder rec;
  38. SkCanvas* canvas = rec.beginRecording({0,0, 2000,3000});
  39. while (loops-- > 0) {
  40. paragraph->layout(fWidth);
  41. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  42. paragraph->paint(canvas, 0, 0);
  43. paragraph->markDirty();
  44. impl->resetCache();
  45. }
  46. }
  47. };
  48. } // namespace
  49. #define PARAGRAPH_BENCH(X) DEF_BENCH(return new ParagraphBench(50000, "text/" #X ".txt", "paragraph_" #X);)
  50. //PARAGRAPH_BENCH(arabic)
  51. //PARAGRAPH_BENCH(emoji)
  52. PARAGRAPH_BENCH(english)
  53. #undef PARAGRAPH_BENCH
  54. #endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)