DDLTileHelper.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2018 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 "tools/DDLTileHelper.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkDeferredDisplayListRecorder.h"
  10. #include "include/core/SkPicture.h"
  11. #include "include/core/SkSurface.h"
  12. #include "include/core/SkSurfaceCharacterization.h"
  13. #include "src/core/SkDeferredDisplayListPriv.h"
  14. #include "src/core/SkTaskGroup.h"
  15. #include "src/image/SkImage_Gpu.h"
  16. #include "tools/DDLPromiseImageHelper.h"
  17. DDLTileHelper::TileData::TileData(sk_sp<SkSurface> s, const SkIRect& clip)
  18. : fSurface(std::move(s))
  19. , fClip(clip) {
  20. SkAssertResult(fSurface->characterize(&fCharacterization));
  21. }
  22. void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
  23. const DDLPromiseImageHelper& helper) {
  24. SkASSERT(!fReconstitutedPicture);
  25. // This is bending the DDLRecorder contract! The promise images in the SKP should be
  26. // created by the same recorder used to create the matching DDL.
  27. SkDeferredDisplayListRecorder recorder(fCharacterization);
  28. fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
  29. std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach();
  30. if (ddl->priv().numOpLists()) {
  31. // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
  32. // reinflation of the SKPs contains opLists that means some image subset operation
  33. // created a draw.
  34. fReconstitutedPicture.reset();
  35. }
  36. }
  37. void DDLTileHelper::TileData::createDDL() {
  38. SkASSERT(!fDisplayList);
  39. SkDeferredDisplayListRecorder recorder(fCharacterization);
  40. // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
  41. // Maybe set it up in the ctor?
  42. SkCanvas* subCanvas = recorder.getCanvas();
  43. // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
  44. // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
  45. // one now.
  46. for (int i = 0; i < fPromiseImages.count(); ++i) {
  47. GrContext* newContext = subCanvas->getGrContext();
  48. if (fPromiseImages[i]->isTextureBacked()) {
  49. SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
  50. gpuImage->resetContext(sk_ref_sp(newContext));
  51. }
  52. }
  53. subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
  54. subCanvas->translate(-fClip.fLeft, -fClip.fTop);
  55. // Note: in this use case we only render a picture to the deferred canvas
  56. // but, more generally, clients will use arbitrary draw calls.
  57. if (fReconstitutedPicture) {
  58. subCanvas->drawPicture(fReconstitutedPicture);
  59. }
  60. fDisplayList = recorder.detach();
  61. }
  62. void DDLTileHelper::TileData::draw() {
  63. SkASSERT(fDisplayList);
  64. fSurface->draw(fDisplayList.get());
  65. }
  66. void DDLTileHelper::TileData::compose(SkCanvas* dst) {
  67. sk_sp<SkImage> img = fSurface->makeImageSnapshot();
  68. dst->save();
  69. dst->clipRect(SkRect::Make(fClip));
  70. dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
  71. dst->restore();
  72. }
  73. void DDLTileHelper::TileData::reset() {
  74. // TODO: when DDLs are re-renderable we don't need to do this
  75. fDisplayList = nullptr;
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////////////////////////
  78. DDLTileHelper::DDLTileHelper(SkCanvas* canvas, const SkIRect& viewport, int numDivisions)
  79. : fNumDivisions(numDivisions) {
  80. SkASSERT(fNumDivisions > 0);
  81. fTiles.reserve(fNumDivisions*fNumDivisions);
  82. int xTileSize = viewport.width()/fNumDivisions;
  83. int yTileSize = viewport.height()/fNumDivisions;
  84. // Create the destination tiles
  85. for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
  86. int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
  87. for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
  88. int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
  89. SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
  90. SkASSERT(viewport.contains(clip));
  91. SkImageInfo tileII = SkImageInfo::MakeN32Premul(xSize, ySize);
  92. sk_sp<SkSurface> tileSurface = canvas->makeSurface(tileII);
  93. // TODO: this is here to deal w/ a resource allocator bug (skbug.com/8007). If all
  94. // the DDLs are flushed at the same time (w/o the composition draws) the allocator
  95. // feels free to reuse the backing GrSurfaces!
  96. tileSurface->flush();
  97. fTiles.push_back(TileData(std::move(tileSurface), clip));
  98. }
  99. }
  100. }
  101. void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
  102. const DDLPromiseImageHelper& helper) {
  103. for (int i = 0; i < fTiles.count(); ++i) {
  104. fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
  105. }
  106. }
  107. void DDLTileHelper::createDDLsInParallel() {
  108. #if 1
  109. SkTaskGroup().batch(fTiles.count(), [&](int i) { fTiles[i].createDDL(); });
  110. SkTaskGroup().wait();
  111. #else
  112. // Use this code path to debug w/o threads
  113. for (int i = 0; i < fTiles.count(); ++i) {
  114. fTiles[i].createDDL();
  115. }
  116. #endif
  117. }
  118. void DDLTileHelper::drawAllTilesAndFlush(GrContext* context, bool flush) {
  119. for (int i = 0; i < fTiles.count(); ++i) {
  120. fTiles[i].draw();
  121. }
  122. if (flush) {
  123. context->flush();
  124. }
  125. }
  126. void DDLTileHelper::composeAllTiles(SkCanvas* dstCanvas) {
  127. for (int i = 0; i < fTiles.count(); ++i) {
  128. fTiles[i].compose(dstCanvas);
  129. }
  130. }
  131. void DDLTileHelper::resetAllTiles() {
  132. for (int i = 0; i < fTiles.count(); ++i) {
  133. fTiles[i].reset();
  134. }
  135. }