skia2/tests/DrawOpAtlasTest.cpp
Aditya Kushwah a080f9d6e9 Label intermediate texture.
Label temprory offscreen textures for draws. In this CL, we will
label texture for a part of blur from SkGpuBlurUtils..

Bug: chromium:1164111
Change-Id: Ibfe1c16efa57b6a134a763bc918411108e286704
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/549057
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2022-06-14 18:13:54 +00:00

319 lines
12 KiB
C++

/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkBitmap.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkFont.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSize.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/core/SkIPoint16.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrDeferredUpload.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrDrawOpAtlas.h"
#include "src/gpu/ganesh/GrDrawingManager.h"
#include "src/gpu/ganesh/GrMemoryPool.h"
#include "src/gpu/ganesh/GrOnFlushResourceProvider.h"
#include "src/gpu/ganesh/GrOpFlushState.h"
#include "src/gpu/ganesh/GrTextureProxy.h"
#include "src/gpu/ganesh/GrXferProcessor.h"
#include "src/gpu/ganesh/ops/AtlasTextOp.h"
#include "src/gpu/ganesh/ops/GrDrawOp.h"
#include "src/gpu/ganesh/ops/GrOp.h"
#include "src/gpu/ganesh/text/GrAtlasManager.h"
#include "tests/Test.h"
#include "tools/gpu/GrContextFactory.h"
#include <memory>
#include <utility>
using MaskFormat = skgpu::MaskFormat;
class GrResourceProvider;
static const int kNumPlots = 2;
static const int kPlotSize = 32;
static const int kAtlasSize = kNumPlots * kPlotSize;
int GrDrawOpAtlas::numAllocated_TestingOnly() const {
int count = 0;
for (uint32_t i = 0; i < this->maxPages(); ++i) {
if (fViews[i].proxy()->isInstantiated()) {
++count;
}
}
return count;
}
void GrAtlasManager::setMaxPages_TestingOnly(uint32_t maxPages) {
for (int i = 0; i < skgpu::kMaskFormatCount; i++) {
if (fAtlases[i]) {
fAtlases[i]->setMaxPages_TestingOnly(maxPages);
}
}
}
void GrDrawOpAtlas::setMaxPages_TestingOnly(uint32_t maxPages) {
SkASSERT(!fNumActivePages);
fMaxPages = maxPages;
}
class AssertOnEvict : public skgpu::PlotEvictionCallback {
public:
void evict(skgpu::PlotLocator) override {
SkASSERT(0); // The unit test shouldn't exercise this code path
}
};
static void check(skiatest::Reporter* r, GrDrawOpAtlas* atlas,
uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced) {
REPORTER_ASSERT(r, expectedActive == atlas->numActivePages());
REPORTER_ASSERT(r, expectedMax == atlas->maxPages());
REPORTER_ASSERT(r, expectedAlloced == atlas->numAllocated_TestingOnly());
}
class TestingUploadTarget : public GrDeferredUploadTarget {
public:
TestingUploadTarget() { }
const skgpu::TokenTracker* tokenTracker() final { return &fTokenTracker; }
skgpu::TokenTracker* writeableTokenTracker() { return &fTokenTracker; }
skgpu::DrawToken addInlineUpload(GrDeferredTextureUploadFn&&) final {
SkASSERT(0); // this test shouldn't invoke this code path
return fTokenTracker.nextDrawToken();
}
skgpu::DrawToken addASAPUpload(GrDeferredTextureUploadFn&& upload) final {
return fTokenTracker.nextTokenToFlush();
}
void issueDrawToken() { fTokenTracker.issueDrawToken(); }
void flushToken() { fTokenTracker.flushToken(); }
private:
skgpu::TokenTracker fTokenTracker;
using INHERITED = GrDeferredUploadTarget;
};
static bool fill_plot(GrDrawOpAtlas* atlas,
GrResourceProvider* resourceProvider,
GrDeferredUploadTarget* target,
skgpu::AtlasLocator* atlasLocator,
int alpha) {
SkImageInfo ii = SkImageInfo::MakeA8(kPlotSize, kPlotSize);
SkBitmap data;
data.allocPixels(ii);
data.eraseARGB(alpha, 0, 0, 0);
GrDrawOpAtlas::ErrorCode code;
code = atlas->addToAtlas(resourceProvider, target, kPlotSize, kPlotSize,
data.getAddr(0, 0), atlasLocator);
return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
}
// This is a basic DrawOpAtlas test. It simply verifies that multitexture atlases correctly
// add and remove pages. Note that this is simulating flush-time behavior.
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas, reporter, ctxInfo) {
auto context = ctxInfo.directContext();
auto proxyProvider = context->priv().proxyProvider();
auto resourceProvider = context->priv().resourceProvider();
auto drawingManager = context->priv().drawingManager();
const GrCaps* caps = context->priv().caps();
GrOnFlushResourceProvider onFlushResourceProvider(drawingManager);
TestingUploadTarget uploadTarget;
GrColorType atlasColorType = GrColorType::kAlpha_8;
GrBackendFormat format = caps->getDefaultBackendFormat(atlasColorType,
GrRenderable::kNo);
AssertOnEvict evictor;
skgpu::AtlasGenerationCounter counter;
std::unique_ptr<GrDrawOpAtlas> atlas = GrDrawOpAtlas::Make(
proxyProvider,
format,
GrColorTypeToSkColorType(atlasColorType),
GrColorTypeBytesPerPixel(atlasColorType),
kAtlasSize, kAtlasSize,
kAtlasSize/kNumPlots, kAtlasSize/kNumPlots,
&counter,
GrDrawOpAtlas::AllowMultitexturing::kYes,
&evictor,
/*label=*/"BasicDrawOpAtlasTest");
check(reporter, atlas.get(), 0, 4, 0);
// Fill up the first level
skgpu::AtlasLocator atlasLocators[kNumPlots * kNumPlots];
for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
bool result = fill_plot(
atlas.get(), resourceProvider, &uploadTarget, &atlasLocators[i], i * 32);
REPORTER_ASSERT(reporter, result);
check(reporter, atlas.get(), 1, 4, 1);
}
atlas->instantiate(&onFlushResourceProvider);
check(reporter, atlas.get(), 1, 4, 1);
// Force allocation of a second level
skgpu::AtlasLocator atlasLocator;
bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &atlasLocator, 4 * 32);
REPORTER_ASSERT(reporter, result);
check(reporter, atlas.get(), 2, 4, 2);
// Simulate a lot of draws using only the first plot. The last texture should be compacted.
for (int i = 0; i < 512; ++i) {
atlas->setLastUseToken(atlasLocators[0], uploadTarget.tokenTracker()->nextDrawToken());
uploadTarget.issueDrawToken();
uploadTarget.flushToken();
atlas->compact(uploadTarget.tokenTracker()->nextTokenToFlush());
}
check(reporter, atlas.get(), 1, 4, 1);
}
#if SK_GPU_V1
#include "src/gpu/ganesh/v1/SurfaceDrawContext_v1.h"
// This test verifies that the AtlasTextOp::onPrepare method correctly handles a failure
// when allocating an atlas page.
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrAtlasTextOpPreparation, reporter, ctxInfo) {
auto dContext = ctxInfo.directContext();
auto gpu = dContext->priv().getGpu();
auto resourceProvider = dContext->priv().resourceProvider();
auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
SkBackingFit::kApprox, {32, 32},
SkSurfaceProps(),
/*label=*/"AtlasTextOpPreparation");
SkPaint paint;
paint.setColor(SK_ColorRED);
SkFont font;
font.setEdging(SkFont::Edging::kAlias);
const char* text = "a";
SkMatrixProvider matrixProvider(SkMatrix::I());
GrOp::Owner op = skgpu::v1::AtlasTextOp::CreateOpTestingOnly(sdc.get(), paint,
font, matrixProvider,
text, 16, 16);
if (!op) {
return;
}
auto atlasTextOp = (skgpu::v1::AtlasTextOp*)op.get();
atlasTextOp->finalize(*dContext->priv().caps(), nullptr, GrClampType::kAuto);
TestingUploadTarget uploadTarget;
GrOpFlushState flushState(gpu, resourceProvider, uploadTarget.writeableTokenTracker());
GrSurfaceProxyView surfaceView = sdc->writeSurfaceView();
GrOpFlushState::OpArgs opArgs(op.get(),
surfaceView,
false /*usesMSAASurface*/,
nullptr,
GrDstProxyView(),
GrXferBarrierFlags::kNone,
GrLoadOp::kLoad);
// Modify the atlas manager so it can't allocate any pages. This will force a failure
// in the preparation of the text op
auto atlasManager = dContext->priv().getAtlasManager();
unsigned int numProxies;
atlasManager->getViews(MaskFormat::kA8, &numProxies);
atlasManager->setMaxPages_TestingOnly(0);
flushState.setOpArgs(&opArgs);
op->prepare(&flushState);
flushState.setOpArgs(nullptr);
}
#endif // SK_GPU_V1
void test_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
MaskFormat maskFormat, SkISize expectedDimensions,
SkISize expectedPlotDimensions) {
GrDrawOpAtlasConfig config(maxTextureSize, maxBytes);
REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
}
DEF_GPUTEST(GrDrawOpAtlasConfig_Basic, reporter, options) {
// 1/4 MB
test_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kARGB,
{ 256, 256 }, { 256, 256 });
test_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kA8,
{ 512, 512 }, { 256, 256 });
// 1/2 MB
test_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kARGB,
{ 512, 256 }, { 256, 256 });
test_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kA8,
{ 1024, 512 }, { 256, 256 });
// 1 MB
test_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kARGB,
{ 512, 512 }, { 256, 256 });
test_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kA8,
{ 1024, 1024 }, { 256, 256 });
// 2 MB
test_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kARGB,
{ 1024, 512 }, { 256, 256 });
test_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kA8,
{ 2048, 1024 }, { 512, 256 });
// 4 MB
test_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kARGB,
{ 1024, 1024 }, { 256, 256 });
test_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kA8,
{ 2048, 2048 }, { 512, 512 });
// 8 MB
test_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kARGB,
{ 2048, 1024 }, { 256, 256 });
test_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kA8,
{ 2048, 2048 }, { 512, 512 });
// 16 MB (should be same as 8 MB)
test_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kARGB,
{ 2048, 1024 }, { 256, 256 });
test_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kA8,
{ 2048, 2048 }, { 512, 512 });
// 4MB, restricted texture size
test_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kARGB,
{ 1024, 1024 }, { 256, 256 });
test_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kA8,
{ 1024, 1024 }, { 256, 256 });
// 3 MB (should be same as 2 MB)
test_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kARGB,
{ 1024, 512 }, { 256, 256 });
test_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kA8,
{ 2048, 1024 }, { 512, 256 });
// minimum size
test_atlas_config(reporter, 65536, 0, MaskFormat::kARGB,
{ 256, 256 }, { 256, 256 });
test_atlas_config(reporter, 65536, 0, MaskFormat::kA8,
{ 512, 512 }, { 256, 256 });
}