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