SKPBench.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2014 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. #include "bench/SKPBench.h"
  8. #include "include/core/SkMultiPictureDraw.h"
  9. #include "include/core/SkSurface.h"
  10. #include "tools/flags/CommandLineFlags.h"
  11. #include "include/gpu/GrContext.h"
  12. #include "src/gpu/GrContextPriv.h"
  13. // These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
  14. static DEFINE_int(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
  15. static DEFINE_int(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
  16. static DEFINE_int(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
  17. static DEFINE_int(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
  18. SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
  19. bool useMultiPictureDraw, bool doLooping)
  20. : fPic(SkRef(pic))
  21. , fClip(clip)
  22. , fScale(scale)
  23. , fName(name)
  24. , fUseMultiPictureDraw(useMultiPictureDraw)
  25. , fDoLooping(doLooping) {
  26. fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
  27. if (useMultiPictureDraw) {
  28. fUniqueName.append("_mpd");
  29. }
  30. }
  31. SKPBench::~SKPBench() {
  32. for (int i = 0; i < fSurfaces.count(); ++i) {
  33. fSurfaces[i]->unref();
  34. }
  35. }
  36. const char* SKPBench::onGetName() {
  37. return fName.c_str();
  38. }
  39. const char* SKPBench::onGetUniqueName() {
  40. return fUniqueName.c_str();
  41. }
  42. void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
  43. SkIRect bounds = canvas->getDeviceClipBounds();
  44. SkAssertResult(!bounds.isEmpty());
  45. const bool gpu = canvas->getGrContext() != nullptr;
  46. int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
  47. tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
  48. tileW = SkTMin(tileW, bounds.width());
  49. tileH = SkTMin(tileH, bounds.height());
  50. int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
  51. int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
  52. fSurfaces.reserve(xTiles * yTiles);
  53. fTileRects.setReserve(xTiles * yTiles);
  54. SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
  55. for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
  56. for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
  57. const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
  58. *fTileRects.append() = tileRect;
  59. fSurfaces.emplace_back(canvas->makeSurface(ii));
  60. // Never want the contents of a tile to include stuff the parent
  61. // canvas clips out
  62. SkRect clip = SkRect::Make(bounds);
  63. clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
  64. fSurfaces.back()->getCanvas()->clipRect(clip);
  65. fSurfaces.back()->getCanvas()->setMatrix(canvas->getTotalMatrix());
  66. fSurfaces.back()->getCanvas()->scale(fScale, fScale);
  67. }
  68. }
  69. }
  70. void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
  71. // Draw the last set of tiles into the master canvas in case we're
  72. // saving the images
  73. for (int i = 0; i < fTileRects.count(); ++i) {
  74. sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
  75. canvas->drawImage(image,
  76. SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
  77. }
  78. fSurfaces.reset();
  79. fTileRects.rewind();
  80. }
  81. bool SKPBench::isSuitableFor(Backend backend) {
  82. return backend != kNonRendering_Backend;
  83. }
  84. SkIPoint SKPBench::onGetSize() {
  85. return SkIPoint::Make(fClip.width(), fClip.height());
  86. }
  87. void SKPBench::onDraw(int loops, SkCanvas* canvas) {
  88. SkASSERT(fDoLooping || 1 == loops);
  89. while (1) {
  90. if (fUseMultiPictureDraw) {
  91. this->drawMPDPicture();
  92. } else {
  93. this->drawPicture();
  94. }
  95. if (0 == --loops) {
  96. break;
  97. }
  98. // Ensure the GrContext doesn't combine ops across draw loops.
  99. if (GrContext* context = canvas->getGrContext()) {
  100. context->flush();
  101. }
  102. }
  103. }
  104. void SKPBench::drawMPDPicture() {
  105. SkMultiPictureDraw mpd;
  106. for (int j = 0; j < fTileRects.count(); ++j) {
  107. SkMatrix trans;
  108. trans.setTranslate(-fTileRects[j].fLeft/fScale,
  109. -fTileRects[j].fTop/fScale);
  110. mpd.add(fSurfaces[j]->getCanvas(), fPic.get(), &trans);
  111. }
  112. // We flush after each picture to more closely model how Chrome rasterizes tiles.
  113. mpd.draw(/*flush = */ true);
  114. }
  115. void SKPBench::drawPicture() {
  116. for (int j = 0; j < fTileRects.count(); ++j) {
  117. const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
  118. -fTileRects[j].fTop / fScale);
  119. fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
  120. }
  121. for (int j = 0; j < fTileRects.count(); ++j) {
  122. fSurfaces[j]->getCanvas()->flush();
  123. }
  124. }
  125. #include "src/gpu/GrGpu.h"
  126. static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
  127. SkTArray<SkString>* keys, SkTArray<double>* values) {
  128. context->priv().resetGpuStats();
  129. canvas->drawPicture(picture);
  130. canvas->flush();
  131. context->priv().dumpGpuStatsKeyValuePairs(keys, values);
  132. context->priv().dumpCacheStatsKeyValuePairs(keys, values);
  133. }
  134. void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
  135. // we do a special single draw and then dump the key / value pairs
  136. GrContext* context = canvas->getGrContext();
  137. if (!context) {
  138. return;
  139. }
  140. // TODO refactor this out if we want to test other subclasses of skpbench
  141. context->flush();
  142. context->freeGpuResources();
  143. context->resetContext();
  144. context->priv().getGpu()->resetShaderCacheForTesting();
  145. draw_pic_for_stats(canvas, context, fPic.get(), keys, values);
  146. }