DrawOpAtlasTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "include/core/SkBitmap.h"
  8. #include "include/core/SkColor.h"
  9. #include "include/core/SkColorSpace.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkImageInfo.h"
  12. #include "include/core/SkMatrix.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/gpu/GrBackendSurface.h"
  19. #include "include/gpu/GrContext.h"
  20. #include "include/private/GrTypesPriv.h"
  21. #include "src/core/SkIPoint16.h"
  22. #include "src/gpu/GrCaps.h"
  23. #include "src/gpu/GrContextPriv.h"
  24. #include "src/gpu/GrDeferredUpload.h"
  25. #include "src/gpu/GrDrawOpAtlas.h"
  26. #include "src/gpu/GrDrawingManager.h"
  27. #include "src/gpu/GrMemoryPool.h"
  28. #include "src/gpu/GrOnFlushResourceProvider.h"
  29. #include "src/gpu/GrOpFlushState.h"
  30. #include "src/gpu/GrRenderTargetContext.h"
  31. #include "src/gpu/GrTextureProxy.h"
  32. #include "src/gpu/GrXferProcessor.h"
  33. #include "src/gpu/ops/GrDrawOp.h"
  34. #include "src/gpu/ops/GrOp.h"
  35. #include "src/gpu/text/GrAtlasManager.h"
  36. #include "src/gpu/text/GrTextContext.h"
  37. #include "tests/Test.h"
  38. #include "tools/gpu/GrContextFactory.h"
  39. #include <memory>
  40. #include <utility>
  41. class GrResourceProvider;
  42. static const int kNumPlots = 2;
  43. static const int kPlotSize = 32;
  44. static const int kAtlasSize = kNumPlots * kPlotSize;
  45. int GrDrawOpAtlas::numAllocated_TestingOnly() const {
  46. int count = 0;
  47. for (uint32_t i = 0; i < this->maxPages(); ++i) {
  48. if (fProxies[i]->isInstantiated()) {
  49. ++count;
  50. }
  51. }
  52. return count;
  53. }
  54. void GrAtlasManager::setMaxPages_TestingOnly(uint32_t maxPages) {
  55. for (int i = 0; i < kMaskFormatCount; i++) {
  56. if (fAtlases[i]) {
  57. fAtlases[i]->setMaxPages_TestingOnly(maxPages);
  58. }
  59. }
  60. }
  61. void GrDrawOpAtlas::setMaxPages_TestingOnly(uint32_t maxPages) {
  62. SkASSERT(!fNumActivePages);
  63. fMaxPages = maxPages;
  64. }
  65. void EvictionFunc(GrDrawOpAtlas::AtlasID atlasID, void*) {
  66. SkASSERT(0); // The unit test shouldn't exercise this code path
  67. }
  68. static void check(skiatest::Reporter* r, GrDrawOpAtlas* atlas,
  69. uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced) {
  70. REPORTER_ASSERT(r, expectedActive == atlas->numActivePages());
  71. REPORTER_ASSERT(r, expectedMax == atlas->maxPages());
  72. REPORTER_ASSERT(r, expectedAlloced == atlas->numAllocated_TestingOnly());
  73. }
  74. class TestingUploadTarget : public GrDeferredUploadTarget {
  75. public:
  76. TestingUploadTarget() { }
  77. const GrTokenTracker* tokenTracker() final { return &fTokenTracker; }
  78. GrTokenTracker* writeableTokenTracker() { return &fTokenTracker; }
  79. GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final {
  80. SkASSERT(0); // this test shouldn't invoke this code path
  81. return fTokenTracker.nextDrawToken();
  82. }
  83. virtual GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&& upload) final {
  84. return fTokenTracker.nextTokenToFlush();
  85. }
  86. void issueDrawToken() { fTokenTracker.issueDrawToken(); }
  87. void flushToken() { fTokenTracker.flushToken(); }
  88. private:
  89. GrTokenTracker fTokenTracker;
  90. typedef GrDeferredUploadTarget INHERITED;
  91. };
  92. static bool fill_plot(GrDrawOpAtlas* atlas,
  93. GrResourceProvider* resourceProvider,
  94. GrDeferredUploadTarget* target,
  95. GrDrawOpAtlas::AtlasID* atlasID,
  96. int alpha) {
  97. SkImageInfo ii = SkImageInfo::MakeA8(kPlotSize, kPlotSize);
  98. SkBitmap data;
  99. data.allocPixels(ii);
  100. data.eraseARGB(alpha, 0, 0, 0);
  101. SkIPoint16 loc;
  102. GrDrawOpAtlas::ErrorCode code;
  103. code = atlas->addToAtlas(resourceProvider, atlasID, target, kPlotSize, kPlotSize,
  104. data.getAddr(0, 0), &loc);
  105. return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
  106. }
  107. // This is a basic DrawOpAtlas test. It simply verifies that multitexture atlases correctly
  108. // add and remove pages. Note that this is simulating flush-time behavior.
  109. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas, reporter, ctxInfo) {
  110. auto context = ctxInfo.grContext();
  111. auto proxyProvider = context->priv().proxyProvider();
  112. auto resourceProvider = context->priv().resourceProvider();
  113. auto drawingManager = context->priv().drawingManager();
  114. GrOnFlushResourceProvider onFlushResourceProvider(drawingManager);
  115. TestingUploadTarget uploadTarget;
  116. GrBackendFormat format =
  117. context->priv().caps()->getBackendFormatFromColorType(GrColorType::kAlpha_8);
  118. std::unique_ptr<GrDrawOpAtlas> atlas = GrDrawOpAtlas::Make(
  119. proxyProvider,
  120. format,
  121. GrColorType::kAlpha_8,
  122. kAtlasSize, kAtlasSize,
  123. kAtlasSize/kNumPlots, kAtlasSize/kNumPlots,
  124. GrDrawOpAtlas::AllowMultitexturing::kYes,
  125. EvictionFunc, nullptr);
  126. check(reporter, atlas.get(), 0, 4, 0);
  127. // Fill up the first level
  128. GrDrawOpAtlas::AtlasID atlasIDs[kNumPlots * kNumPlots];
  129. for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
  130. bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &atlasIDs[i], i*32);
  131. REPORTER_ASSERT(reporter, result);
  132. check(reporter, atlas.get(), 1, 4, 1);
  133. }
  134. atlas->instantiate(&onFlushResourceProvider);
  135. check(reporter, atlas.get(), 1, 4, 1);
  136. // Force allocation of a second level
  137. GrDrawOpAtlas::AtlasID atlasID;
  138. bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &atlasID, 4*32);
  139. REPORTER_ASSERT(reporter, result);
  140. check(reporter, atlas.get(), 2, 4, 2);
  141. // Simulate a lot of draws using only the first plot. The last texture should be compacted.
  142. for (int i = 0; i < 512; ++i) {
  143. atlas->setLastUseToken(atlasIDs[0], uploadTarget.tokenTracker()->nextDrawToken());
  144. uploadTarget.issueDrawToken();
  145. uploadTarget.flushToken();
  146. atlas->compact(uploadTarget.tokenTracker()->nextTokenToFlush());
  147. }
  148. check(reporter, atlas.get(), 1, 4, 1);
  149. }
  150. // This test verifies that the GrAtlasTextOp::onPrepare method correctly handles a failure
  151. // when allocating an atlas page.
  152. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrAtlasTextOpPreparation, reporter, ctxInfo) {
  153. auto context = ctxInfo.grContext();
  154. auto gpu = context->priv().getGpu();
  155. auto resourceProvider = context->priv().resourceProvider();
  156. auto drawingManager = context->priv().drawingManager();
  157. auto textContext = drawingManager->getTextContext();
  158. auto opMemoryPool = context->priv().opMemoryPool();
  159. auto rtc = context->priv().makeDeferredRenderTargetContext(SkBackingFit::kApprox, 32, 32,
  160. GrColorType::kRGBA_8888, nullptr);
  161. SkPaint paint;
  162. paint.setColor(SK_ColorRED);
  163. SkFont font;
  164. font.setEdging(SkFont::Edging::kAlias);
  165. const char* text = "a";
  166. std::unique_ptr<GrDrawOp> op = textContext->createOp_TestingOnly(
  167. context, textContext, rtc.get(), paint, font, SkMatrix::I(), text, 16, 16);
  168. bool hasMixedSampledCoverage = false;
  169. op->finalize(*context->priv().caps(), nullptr, hasMixedSampledCoverage, GrClampType::kAuto);
  170. TestingUploadTarget uploadTarget;
  171. GrOpFlushState flushState(gpu, resourceProvider, uploadTarget.writeableTokenTracker());
  172. GrOpFlushState::OpArgs opArgs = {
  173. op.get(),
  174. rtc->asRenderTargetProxy(),
  175. nullptr,
  176. rtc->asRenderTargetProxy()->outputSwizzle(),
  177. GrXferProcessor::DstProxy(nullptr, SkIPoint::Make(0, 0))
  178. };
  179. // Cripple the atlas manager so it can't allocate any pages. This will force a failure
  180. // in the preparation of the text op
  181. auto atlasManager = context->priv().getAtlasManager();
  182. unsigned int numProxies;
  183. atlasManager->getProxies(kA8_GrMaskFormat, &numProxies);
  184. atlasManager->setMaxPages_TestingOnly(0);
  185. flushState.setOpArgs(&opArgs);
  186. op->prepare(&flushState);
  187. flushState.setOpArgs(nullptr);
  188. opMemoryPool->release(std::move(op));
  189. }
  190. void test_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
  191. GrMaskFormat maskFormat, SkISize expectedDimensions,
  192. SkISize expectedPlotDimensions) {
  193. GrDrawOpAtlasConfig config(maxTextureSize, maxBytes);
  194. REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
  195. REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
  196. }
  197. DEF_GPUTEST(GrDrawOpAtlasConfig_Basic, reporter, options) {
  198. // 1/4 MB
  199. test_atlas_config(reporter, 65536, 256 * 1024, kARGB_GrMaskFormat,
  200. { 256, 256 }, { 256, 256 });
  201. test_atlas_config(reporter, 65536, 256 * 1024, kA8_GrMaskFormat,
  202. { 512, 512 }, { 256, 256 });
  203. // 1/2 MB
  204. test_atlas_config(reporter, 65536, 512 * 1024, kARGB_GrMaskFormat,
  205. { 512, 256 }, { 256, 256 });
  206. test_atlas_config(reporter, 65536, 512 * 1024, kA8_GrMaskFormat,
  207. { 1024, 512 }, { 256, 256 });
  208. // 1 MB
  209. test_atlas_config(reporter, 65536, 1024 * 1024, kARGB_GrMaskFormat,
  210. { 512, 512 }, { 256, 256 });
  211. test_atlas_config(reporter, 65536, 1024 * 1024, kA8_GrMaskFormat,
  212. { 1024, 1024 }, { 256, 256 });
  213. // 2 MB
  214. test_atlas_config(reporter, 65536, 2 * 1024 * 1024, kARGB_GrMaskFormat,
  215. { 1024, 512 }, { 256, 256 });
  216. test_atlas_config(reporter, 65536, 2 * 1024 * 1024, kA8_GrMaskFormat,
  217. { 2048, 1024 }, { 512, 256 });
  218. // 4 MB
  219. test_atlas_config(reporter, 65536, 4 * 1024 * 1024, kARGB_GrMaskFormat,
  220. { 1024, 1024 }, { 256, 256 });
  221. test_atlas_config(reporter, 65536, 4 * 1024 * 1024, kA8_GrMaskFormat,
  222. { 2048, 2048 }, { 512, 512 });
  223. // 8 MB
  224. test_atlas_config(reporter, 65536, 8 * 1024 * 1024, kARGB_GrMaskFormat,
  225. { 2048, 1024 }, { 256, 256 });
  226. test_atlas_config(reporter, 65536, 8 * 1024 * 1024, kA8_GrMaskFormat,
  227. { 2048, 2048 }, { 512, 512 });
  228. // 16 MB (should be same as 8 MB)
  229. test_atlas_config(reporter, 65536, 16 * 1024 * 1024, kARGB_GrMaskFormat,
  230. { 2048, 1024 }, { 256, 256 });
  231. test_atlas_config(reporter, 65536, 16 * 1024 * 1024, kA8_GrMaskFormat,
  232. { 2048, 2048 }, { 512, 512 });
  233. // 4MB, restricted texture size
  234. test_atlas_config(reporter, 1024, 8 * 1024 * 1024, kARGB_GrMaskFormat,
  235. { 1024, 1024 }, { 256, 256 });
  236. test_atlas_config(reporter, 1024, 8 * 1024 * 1024, kA8_GrMaskFormat,
  237. { 1024, 1024 }, { 256, 256 });
  238. // 3 MB (should be same as 2 MB)
  239. test_atlas_config(reporter, 65536, 3 * 1024 * 1024, kARGB_GrMaskFormat,
  240. { 1024, 512 }, { 256, 256 });
  241. test_atlas_config(reporter, 65536, 3 * 1024 * 1024, kA8_GrMaskFormat,
  242. { 2048, 1024 }, { 512, 256 });
  243. // minimum size
  244. test_atlas_config(reporter, 65536, 0, kARGB_GrMaskFormat,
  245. { 256, 256 }, { 256, 256 });
  246. test_atlas_config(reporter, 65536, 0, kA8_GrMaskFormat,
  247. { 512, 512 }, { 256, 256 });
  248. }