1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2013 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include <set> 9cb93a386Sopenharmony_ci#include "include/core/SkSurface.h" 10cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h" 11cb93a386Sopenharmony_ci#include "src/core/SkAutoPixmapStorage.h" 12cb93a386Sopenharmony_ci#include "src/core/SkCanvasPriv.h" 13cb93a386Sopenharmony_ci#include "src/core/SkCompressedDataUtils.h" 14cb93a386Sopenharmony_ci#include "src/gpu/GrBackendUtils.h" 15cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h" 16cb93a386Sopenharmony_ci#include "src/gpu/GrGpu.h" 17cb93a386Sopenharmony_ci#include "src/gpu/GrImageInfo.h" 18cb93a386Sopenharmony_ci#include "src/gpu/GrProxyProvider.h" 19cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTarget.h" 20cb93a386Sopenharmony_ci#include "src/gpu/GrResourceProvider.h" 21cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h" 22cb93a386Sopenharmony_ci#include "src/gpu/SkGr.h" 23cb93a386Sopenharmony_ci#include "src/gpu/SurfaceContext.h" 24cb93a386Sopenharmony_ci#include "tests/Test.h" 25cb93a386Sopenharmony_ci#include "tests/TestUtils.h" 26cb93a386Sopenharmony_ci#include "tools/gpu/BackendTextureImageFactory.h" 27cb93a386Sopenharmony_ci#include "tools/gpu/ManagedBackendTexture.h" 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture 30cb93a386Sopenharmony_ci// and render targets to GrSurface all work as expected. 31cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) { 32cb93a386Sopenharmony_ci auto context = ctxInfo.directContext(); 33cb93a386Sopenharmony_ci auto resourceProvider = context->priv().resourceProvider(); 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ci static constexpr SkISize kDesc = {256, 256}; 36cb93a386Sopenharmony_ci auto format = context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, 37cb93a386Sopenharmony_ci GrRenderable::kYes); 38cb93a386Sopenharmony_ci sk_sp<GrSurface> texRT1 = 39cb93a386Sopenharmony_ci resourceProvider->createTexture(kDesc, format, GrTextureType::k2D, GrRenderable::kYes, 40cb93a386Sopenharmony_ci 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget()); 43cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture()); 44cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == 45cb93a386Sopenharmony_ci texRT1->asTexture()); 46cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, texRT1->asRenderTarget() == 47cb93a386Sopenharmony_ci static_cast<GrSurface*>(texRT1->asTexture())); 48cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == 49cb93a386Sopenharmony_ci static_cast<GrSurface*>(texRT1->asTexture())); 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_ci sk_sp<GrTexture> tex1 = 52cb93a386Sopenharmony_ci resourceProvider->createTexture(kDesc, format, GrTextureType::k2D, GrRenderable::kNo, 1, 53cb93a386Sopenharmony_ci GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); 54cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget()); 55cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture()); 56cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture()); 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_ci GrBackendTexture backendTex = context->createBackendTexture( 59cb93a386Sopenharmony_ci 256, 256, kRGBA_8888_SkColorType, 60cb93a386Sopenharmony_ci SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); 61cb93a386Sopenharmony_ci 62cb93a386Sopenharmony_ci sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture( 63cb93a386Sopenharmony_ci backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo); 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget()); 66cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture()); 67cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == 68cb93a386Sopenharmony_ci texRT2->asTexture()); 69cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, texRT2->asRenderTarget() == 70cb93a386Sopenharmony_ci static_cast<GrSurface*>(texRT2->asTexture())); 71cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == 72cb93a386Sopenharmony_ci static_cast<GrSurface*>(texRT2->asTexture())); 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci context->deleteBackendTexture(backendTex); 75cb93a386Sopenharmony_ci} 76cb93a386Sopenharmony_ci 77cb93a386Sopenharmony_ci// This test checks that the isFormatTexturable and isFormatRenderable are 78cb93a386Sopenharmony_ci// consistent with createTexture's result. 79cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) { 80cb93a386Sopenharmony_ci auto context = ctxInfo.directContext(); 81cb93a386Sopenharmony_ci GrProxyProvider* proxyProvider = context->priv().proxyProvider(); 82cb93a386Sopenharmony_ci GrResourceProvider* resourceProvider = context->priv().resourceProvider(); 83cb93a386Sopenharmony_ci const GrCaps* caps = context->priv().caps(); 84cb93a386Sopenharmony_ci 85cb93a386Sopenharmony_ci // TODO: Should only need format here but need to determine compression type from format 86cb93a386Sopenharmony_ci // without config. 87cb93a386Sopenharmony_ci auto createTexture = [](SkISize dimensions, GrColorType colorType, 88cb93a386Sopenharmony_ci const GrBackendFormat& format, GrRenderable renderable, 89cb93a386Sopenharmony_ci GrResourceProvider* rp) -> sk_sp<GrTexture> { 90cb93a386Sopenharmony_ci SkImage::CompressionType compression = GrBackendFormatToCompressionType(format); 91cb93a386Sopenharmony_ci if (compression != SkImage::CompressionType::kNone) { 92cb93a386Sopenharmony_ci if (renderable == GrRenderable::kYes) { 93cb93a386Sopenharmony_ci return nullptr; 94cb93a386Sopenharmony_ci } 95cb93a386Sopenharmony_ci auto size = SkCompressedDataSize(compression, dimensions, nullptr, false); 96cb93a386Sopenharmony_ci auto data = SkData::MakeUninitialized(size); 97cb93a386Sopenharmony_ci SkColor4f color = {0, 0, 0, 0}; 98cb93a386Sopenharmony_ci GrFillInCompressedData(compression, dimensions, GrMipmapped::kNo, 99cb93a386Sopenharmony_ci (char*)data->writable_data(), color); 100cb93a386Sopenharmony_ci return rp->createCompressedTexture(dimensions, format, SkBudgeted::kNo, 101cb93a386Sopenharmony_ci GrMipmapped::kNo, GrProtected::kNo, data.get()); 102cb93a386Sopenharmony_ci } else { 103cb93a386Sopenharmony_ci return rp->createTexture(dimensions, format, GrTextureType::k2D, renderable, 1, 104cb93a386Sopenharmony_ci GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); 105cb93a386Sopenharmony_ci } 106cb93a386Sopenharmony_ci }; 107cb93a386Sopenharmony_ci 108cb93a386Sopenharmony_ci static constexpr SkISize kDims = {64, 64}; 109cb93a386Sopenharmony_ci 110cb93a386Sopenharmony_ci const std::vector<GrCaps::TestFormatColorTypeCombination>& combos = 111cb93a386Sopenharmony_ci caps->getTestingCombinations(); 112cb93a386Sopenharmony_ci 113cb93a386Sopenharmony_ci for (const GrCaps::TestFormatColorTypeCombination& combo : combos) { 114cb93a386Sopenharmony_ci 115cb93a386Sopenharmony_ci SkASSERT(combo.fColorType != GrColorType::kUnknown); 116cb93a386Sopenharmony_ci SkASSERT(combo.fFormat.isValid()); 117cb93a386Sopenharmony_ci 118cb93a386Sopenharmony_ci // Right now Vulkan has two backend formats that support ABGR_4444 (R4G4B4A4 and B4G4R4A4). 119cb93a386Sopenharmony_ci // Until we can create textures directly from the backend format this yields some 120cb93a386Sopenharmony_ci // ambiguity in what is actually supported and which textures can be created. 121cb93a386Sopenharmony_ci if (ctxInfo.backend() == kVulkan_GrBackend && combo.fColorType == GrColorType::kABGR_4444) { 122cb93a386Sopenharmony_ci continue; 123cb93a386Sopenharmony_ci } 124cb93a386Sopenharmony_ci 125cb93a386Sopenharmony_ci // Check if 'isFormatTexturable' agrees with 'createTexture' and that the mipmap 126cb93a386Sopenharmony_ci // support check is working 127cb93a386Sopenharmony_ci { 128cb93a386Sopenharmony_ci bool isCompressed = caps->isFormatCompressed(combo.fFormat); 129cb93a386Sopenharmony_ci bool isTexturable = caps->isFormatTexturable(combo.fFormat, GrTextureType::k2D); 130cb93a386Sopenharmony_ci 131cb93a386Sopenharmony_ci sk_sp<GrSurface> tex = createTexture(kDims, combo.fColorType, combo.fFormat, 132cb93a386Sopenharmony_ci GrRenderable::kNo, resourceProvider); 133cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkToBool(tex) == isTexturable, 134cb93a386Sopenharmony_ci "ct:%s format:%s, tex:%d, isTexturable:%d", 135cb93a386Sopenharmony_ci GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), 136cb93a386Sopenharmony_ci SkToBool(tex), isTexturable); 137cb93a386Sopenharmony_ci 138cb93a386Sopenharmony_ci // Check that the lack of mipmap support blocks the creation of mipmapped 139cb93a386Sopenharmony_ci // proxies 140cb93a386Sopenharmony_ci bool expectedMipMapability = isTexturable && caps->mipmapSupport() && !isCompressed; 141cb93a386Sopenharmony_ci 142cb93a386Sopenharmony_ci sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( 143cb93a386Sopenharmony_ci combo.fFormat, kDims, GrRenderable::kNo, 1, GrMipmapped::kYes, 144cb93a386Sopenharmony_ci SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo); 145cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == expectedMipMapability, 146cb93a386Sopenharmony_ci "ct:%s format:%s, tex:%d, expectedMipMapability:%d", 147cb93a386Sopenharmony_ci GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), 148cb93a386Sopenharmony_ci SkToBool(proxy.get()), expectedMipMapability); 149cb93a386Sopenharmony_ci } 150cb93a386Sopenharmony_ci 151cb93a386Sopenharmony_ci // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' (w/o MSAA) 152cb93a386Sopenharmony_ci { 153cb93a386Sopenharmony_ci bool isRenderable = caps->isFormatRenderable(combo.fFormat, 1); 154cb93a386Sopenharmony_ci 155cb93a386Sopenharmony_ci sk_sp<GrSurface> tex = resourceProvider->createTexture( 156cb93a386Sopenharmony_ci kDims, combo.fFormat, GrTextureType::k2D, GrRenderable::kYes, 1, 157cb93a386Sopenharmony_ci GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); 158cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, 159cb93a386Sopenharmony_ci "ct:%s format:%s, tex:%d, isRenderable:%d", 160cb93a386Sopenharmony_ci GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), 161cb93a386Sopenharmony_ci SkToBool(tex), isRenderable); 162cb93a386Sopenharmony_ci } 163cb93a386Sopenharmony_ci 164cb93a386Sopenharmony_ci // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' w/ MSAA 165cb93a386Sopenharmony_ci { 166cb93a386Sopenharmony_ci bool isRenderable = caps->isFormatRenderable(combo.fFormat, 2); 167cb93a386Sopenharmony_ci 168cb93a386Sopenharmony_ci sk_sp<GrSurface> tex = resourceProvider->createTexture( 169cb93a386Sopenharmony_ci kDims, combo.fFormat, GrTextureType::k2D, GrRenderable::kYes, 2, 170cb93a386Sopenharmony_ci GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); 171cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, 172cb93a386Sopenharmony_ci "ct:%s format:%s, tex:%d, isRenderable:%d", 173cb93a386Sopenharmony_ci GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), 174cb93a386Sopenharmony_ci SkToBool(tex), isRenderable); 175cb93a386Sopenharmony_ci } 176cb93a386Sopenharmony_ci } 177cb93a386Sopenharmony_ci} 178cb93a386Sopenharmony_ci 179cb93a386Sopenharmony_ci#include "src/gpu/GrDrawingManager.h" 180cb93a386Sopenharmony_ci#include "src/gpu/GrSurfaceProxy.h" 181cb93a386Sopenharmony_ci 182cb93a386Sopenharmony_ci// For each context, set it to always clear the textures and then run through all the 183cb93a386Sopenharmony_ci// supported formats checking that the textures are actually cleared 184cb93a386Sopenharmony_ciDEF_GPUTEST(InitialTextureClear, reporter, baseOptions) { 185cb93a386Sopenharmony_ci GrContextOptions options = baseOptions; 186cb93a386Sopenharmony_ci options.fClearAllTextures = true; 187cb93a386Sopenharmony_ci 188cb93a386Sopenharmony_ci static constexpr int kSize = 100; 189cb93a386Sopenharmony_ci static constexpr SkColor kClearColor = 0xABABABAB; 190cb93a386Sopenharmony_ci 191cb93a386Sopenharmony_ci const SkImageInfo imageInfo = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, 192cb93a386Sopenharmony_ci kPremul_SkAlphaType); 193cb93a386Sopenharmony_ci 194cb93a386Sopenharmony_ci SkAutoPixmapStorage readback; 195cb93a386Sopenharmony_ci readback.alloc(imageInfo); 196cb93a386Sopenharmony_ci 197cb93a386Sopenharmony_ci SkISize desc; 198cb93a386Sopenharmony_ci desc.fWidth = desc.fHeight = kSize; 199cb93a386Sopenharmony_ci 200cb93a386Sopenharmony_ci for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) { 201cb93a386Sopenharmony_ci sk_gpu_test::GrContextFactory factory(options); 202cb93a386Sopenharmony_ci auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct); 203cb93a386Sopenharmony_ci if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) { 204cb93a386Sopenharmony_ci continue; 205cb93a386Sopenharmony_ci } 206cb93a386Sopenharmony_ci auto dContext = factory.get(contextType); 207cb93a386Sopenharmony_ci if (!dContext) { 208cb93a386Sopenharmony_ci continue; 209cb93a386Sopenharmony_ci } 210cb93a386Sopenharmony_ci 211cb93a386Sopenharmony_ci GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); 212cb93a386Sopenharmony_ci const GrCaps* caps = dContext->priv().caps(); 213cb93a386Sopenharmony_ci 214cb93a386Sopenharmony_ci const std::vector<GrCaps::TestFormatColorTypeCombination>& combos = 215cb93a386Sopenharmony_ci caps->getTestingCombinations(); 216cb93a386Sopenharmony_ci 217cb93a386Sopenharmony_ci for (const GrCaps::TestFormatColorTypeCombination& combo : combos) { 218cb93a386Sopenharmony_ci 219cb93a386Sopenharmony_ci SkASSERT(combo.fColorType != GrColorType::kUnknown); 220cb93a386Sopenharmony_ci SkASSERT(combo.fFormat.isValid()); 221cb93a386Sopenharmony_ci 222cb93a386Sopenharmony_ci if (!caps->isFormatTexturable(combo.fFormat, GrTextureType::k2D)) { 223cb93a386Sopenharmony_ci continue; 224cb93a386Sopenharmony_ci } 225cb93a386Sopenharmony_ci 226cb93a386Sopenharmony_ci auto checkColor = [reporter](const GrCaps::TestFormatColorTypeCombination& combo, 227cb93a386Sopenharmony_ci uint32_t readColor) { 228cb93a386Sopenharmony_ci // We expect that if there is no alpha in the src color type and we read it to a 229cb93a386Sopenharmony_ci // color type with alpha that we will get one for alpha rather than zero. We used to 230cb93a386Sopenharmony_ci // require this but the Intel Iris 6100 on Win 10 test bot doesn't put one in the 231cb93a386Sopenharmony_ci // alpha channel when reading back from GL_RG16 or GL_RG16F. So now we allow either. 232cb93a386Sopenharmony_ci uint32_t channels = GrColorTypeChannelFlags(combo.fColorType); 233cb93a386Sopenharmony_ci bool allowAlphaOne = !(channels & kAlpha_SkColorChannelFlag); 234cb93a386Sopenharmony_ci if (allowAlphaOne) { 235cb93a386Sopenharmony_ci if (readColor != 0x00000000 && readColor != 0xFF000000) { 236cb93a386Sopenharmony_ci ERRORF(reporter, 237cb93a386Sopenharmony_ci "Failed on ct %s format %s 0x%08x is not 0x00000000 or 0xFF000000", 238cb93a386Sopenharmony_ci GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), 239cb93a386Sopenharmony_ci readColor); 240cb93a386Sopenharmony_ci return false; 241cb93a386Sopenharmony_ci } 242cb93a386Sopenharmony_ci } else { 243cb93a386Sopenharmony_ci if (readColor) { 244cb93a386Sopenharmony_ci ERRORF(reporter, "Failed on ct %s format %s 0x%08x != 0x00000000", 245cb93a386Sopenharmony_ci GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), 246cb93a386Sopenharmony_ci readColor); 247cb93a386Sopenharmony_ci return false; 248cb93a386Sopenharmony_ci } 249cb93a386Sopenharmony_ci } 250cb93a386Sopenharmony_ci return true; 251cb93a386Sopenharmony_ci }; 252cb93a386Sopenharmony_ci 253cb93a386Sopenharmony_ci for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { 254cb93a386Sopenharmony_ci if (renderable == GrRenderable::kYes && 255cb93a386Sopenharmony_ci !caps->isFormatAsColorTypeRenderable(combo.fColorType, combo.fFormat)) { 256cb93a386Sopenharmony_ci continue; 257cb93a386Sopenharmony_ci } 258cb93a386Sopenharmony_ci 259cb93a386Sopenharmony_ci for (auto fit : {SkBackingFit::kApprox, SkBackingFit::kExact}) { 260cb93a386Sopenharmony_ci 261cb93a386Sopenharmony_ci // Does directly allocating a texture clear it? 262cb93a386Sopenharmony_ci { 263cb93a386Sopenharmony_ci auto proxy = proxyProvider->testingOnly_createInstantiatedProxy( 264cb93a386Sopenharmony_ci {kSize, kSize}, combo.fFormat, renderable, 1, fit, SkBudgeted::kYes, 265cb93a386Sopenharmony_ci GrProtected::kNo); 266cb93a386Sopenharmony_ci if (proxy) { 267cb93a386Sopenharmony_ci GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat, 268cb93a386Sopenharmony_ci combo.fColorType); 269cb93a386Sopenharmony_ci GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, 270cb93a386Sopenharmony_ci swizzle); 271cb93a386Sopenharmony_ci GrColorInfo info(combo.fColorType, kPremul_SkAlphaType, nullptr); 272cb93a386Sopenharmony_ci auto texCtx = dContext->priv().makeSC(std::move(view), info); 273cb93a386Sopenharmony_ci 274cb93a386Sopenharmony_ci readback.erase(kClearColor); 275cb93a386Sopenharmony_ci if (texCtx->readPixels(dContext, readback, {0, 0})) { 276cb93a386Sopenharmony_ci for (int i = 0; i < kSize * kSize; ++i) { 277cb93a386Sopenharmony_ci if (!checkColor(combo, readback.addr32()[i])) { 278cb93a386Sopenharmony_ci break; 279cb93a386Sopenharmony_ci } 280cb93a386Sopenharmony_ci } 281cb93a386Sopenharmony_ci } 282cb93a386Sopenharmony_ci } 283cb93a386Sopenharmony_ci 284cb93a386Sopenharmony_ci dContext->priv().getResourceCache()->purgeUnlockedResources(); 285cb93a386Sopenharmony_ci } 286cb93a386Sopenharmony_ci 287cb93a386Sopenharmony_ci // Try creating the texture as a deferred proxy. 288cb93a386Sopenharmony_ci { 289cb93a386Sopenharmony_ci GrImageInfo info(combo.fColorType, 290cb93a386Sopenharmony_ci GrColorTypeHasAlpha(combo.fColorType) 291cb93a386Sopenharmony_ci ? kPremul_SkAlphaType 292cb93a386Sopenharmony_ci : kOpaque_SkAlphaType, 293cb93a386Sopenharmony_ci nullptr, 294cb93a386Sopenharmony_ci {desc.fHeight, desc.fHeight}); 295cb93a386Sopenharmony_ci 296cb93a386Sopenharmony_ci auto sc = dContext->priv().makeSC(info, 297cb93a386Sopenharmony_ci combo.fFormat, 298cb93a386Sopenharmony_ci fit, 299cb93a386Sopenharmony_ci kTopLeft_GrSurfaceOrigin, 300cb93a386Sopenharmony_ci renderable); 301cb93a386Sopenharmony_ci if (!sc) { 302cb93a386Sopenharmony_ci continue; 303cb93a386Sopenharmony_ci } 304cb93a386Sopenharmony_ci 305cb93a386Sopenharmony_ci readback.erase(kClearColor); 306cb93a386Sopenharmony_ci if (sc->readPixels(dContext, readback, {0, 0})) { 307cb93a386Sopenharmony_ci for (int i = 0; i < kSize * kSize; ++i) { 308cb93a386Sopenharmony_ci if (!checkColor(combo, readback.addr32()[i])) { 309cb93a386Sopenharmony_ci break; 310cb93a386Sopenharmony_ci } 311cb93a386Sopenharmony_ci } 312cb93a386Sopenharmony_ci } 313cb93a386Sopenharmony_ci dContext->priv().getResourceCache()->purgeUnlockedResources(); 314cb93a386Sopenharmony_ci } 315cb93a386Sopenharmony_ci } 316cb93a386Sopenharmony_ci } 317cb93a386Sopenharmony_ci } 318cb93a386Sopenharmony_ci } 319cb93a386Sopenharmony_ci} 320cb93a386Sopenharmony_ci 321cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) { 322cb93a386Sopenharmony_ci auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) { 323cb93a386Sopenharmony_ci for (int y = 0; y < p->height(); ++y) { 324cb93a386Sopenharmony_ci for (int x = 0; x < p->width(); ++x) { 325cb93a386Sopenharmony_ci *p->writable_addr32(x, y) = f(x, y); 326cb93a386Sopenharmony_ci } 327cb93a386Sopenharmony_ci } 328cb93a386Sopenharmony_ci }; 329cb93a386Sopenharmony_ci 330cb93a386Sopenharmony_ci auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) { 331cb93a386Sopenharmony_ci SkASSERT(p1.info() == p2.info()); 332cb93a386Sopenharmony_ci for (int y = 0; y < p1.height(); ++y) { 333cb93a386Sopenharmony_ci for (int x = 0; x < p1.width(); ++x) { 334cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y)); 335cb93a386Sopenharmony_ci if (p1.getColor(x, y) != p2.getColor(x, y)) { 336cb93a386Sopenharmony_ci return; 337cb93a386Sopenharmony_ci } 338cb93a386Sopenharmony_ci } 339cb93a386Sopenharmony_ci } 340cb93a386Sopenharmony_ci }; 341cb93a386Sopenharmony_ci 342cb93a386Sopenharmony_ci static constexpr int kSize = 100; 343cb93a386Sopenharmony_ci SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); 344cb93a386Sopenharmony_ci SkAutoPixmapStorage srcPixmap; 345cb93a386Sopenharmony_ci srcPixmap.alloc(ii); 346cb93a386Sopenharmony_ci fillPixels(&srcPixmap, 347cb93a386Sopenharmony_ci [](int x, int y) { 348cb93a386Sopenharmony_ci return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF); 349cb93a386Sopenharmony_ci }); 350cb93a386Sopenharmony_ci 351cb93a386Sopenharmony_ci auto dContext = context_info.directContext(); 352cb93a386Sopenharmony_ci GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); 353cb93a386Sopenharmony_ci 354cb93a386Sopenharmony_ci // We test both kRW in addition to kRead mostly to ensure that the calls are structured such 355cb93a386Sopenharmony_ci // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with 356cb93a386Sopenharmony_ci // kRead for the right reason. 357cb93a386Sopenharmony_ci for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) { 358cb93a386Sopenharmony_ci auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithData( 359cb93a386Sopenharmony_ci dContext, srcPixmap, kTopLeft_GrSurfaceOrigin, GrRenderable::kNo, GrProtected::kNo); 360cb93a386Sopenharmony_ci if (!mbet) { 361cb93a386Sopenharmony_ci ERRORF(reporter, "Could not make texture."); 362cb93a386Sopenharmony_ci return; 363cb93a386Sopenharmony_ci } 364cb93a386Sopenharmony_ci auto proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership, 365cb93a386Sopenharmony_ci GrWrapCacheable::kNo, ioType, 366cb93a386Sopenharmony_ci mbet->refCountedCallback()); 367cb93a386Sopenharmony_ci GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), 368cb93a386Sopenharmony_ci GrColorType::kRGBA_8888); 369cb93a386Sopenharmony_ci GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle); 370cb93a386Sopenharmony_ci auto surfContext = dContext->priv().makeSC(std::move(view), ii.colorInfo()); 371cb93a386Sopenharmony_ci // Read pixels should work with a read-only texture. 372cb93a386Sopenharmony_ci { 373cb93a386Sopenharmony_ci SkAutoPixmapStorage read; 374cb93a386Sopenharmony_ci read.alloc(srcPixmap.info()); 375cb93a386Sopenharmony_ci auto readResult = surfContext->readPixels(dContext, read, {0, 0}); 376cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, readResult); 377cb93a386Sopenharmony_ci if (readResult) { 378cb93a386Sopenharmony_ci comparePixels(srcPixmap, read, reporter); 379cb93a386Sopenharmony_ci } 380cb93a386Sopenharmony_ci } 381cb93a386Sopenharmony_ci 382cb93a386Sopenharmony_ci // Write pixels should not work with a read-only texture. 383cb93a386Sopenharmony_ci SkAutoPixmapStorage write; 384cb93a386Sopenharmony_ci write.alloc(srcPixmap.info()); 385cb93a386Sopenharmony_ci fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); }); 386cb93a386Sopenharmony_ci auto writeResult = surfContext->writePixels(dContext, write, {0, 0}); 387cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType)); 388cb93a386Sopenharmony_ci // Try the low level write. 389cb93a386Sopenharmony_ci dContext->flushAndSubmit(); 390cb93a386Sopenharmony_ci auto gpuWriteResult = dContext->priv().getGpu()->writePixels( 391cb93a386Sopenharmony_ci proxy->peekTexture(), 392cb93a386Sopenharmony_ci SkIRect::MakeWH(kSize, kSize), 393cb93a386Sopenharmony_ci GrColorType::kRGBA_8888, 394cb93a386Sopenharmony_ci GrColorType::kRGBA_8888, 395cb93a386Sopenharmony_ci write.addr32(), 396cb93a386Sopenharmony_ci kSize*GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888)); 397cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType)); 398cb93a386Sopenharmony_ci 399cb93a386Sopenharmony_ci SkBitmap copySrcBitmap; 400cb93a386Sopenharmony_ci copySrcBitmap.installPixels(write); 401cb93a386Sopenharmony_ci copySrcBitmap.setImmutable(); 402cb93a386Sopenharmony_ci 403cb93a386Sopenharmony_ci auto copySrc = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, copySrcBitmap)); 404cb93a386Sopenharmony_ci 405cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, copySrc); 406cb93a386Sopenharmony_ci auto copyResult = surfContext->testCopy(copySrc.refProxy()); 407cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType)); 408cb93a386Sopenharmony_ci // Try the low level copy. 409cb93a386Sopenharmony_ci dContext->flushAndSubmit(); 410cb93a386Sopenharmony_ci auto gpuCopyResult = dContext->priv().getGpu()->copySurface( 411cb93a386Sopenharmony_ci proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize), 412cb93a386Sopenharmony_ci {0, 0}); 413cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType)); 414cb93a386Sopenharmony_ci 415cb93a386Sopenharmony_ci // Mip regen should not work with a read only texture. 416cb93a386Sopenharmony_ci if (dContext->priv().caps()->mipmapSupport()) { 417cb93a386Sopenharmony_ci mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext, 418cb93a386Sopenharmony_ci kSize, 419cb93a386Sopenharmony_ci kSize, 420cb93a386Sopenharmony_ci kRGBA_8888_SkColorType, 421cb93a386Sopenharmony_ci GrMipmapped::kYes, 422cb93a386Sopenharmony_ci GrRenderable::kNo, 423cb93a386Sopenharmony_ci GrProtected::kNo); 424cb93a386Sopenharmony_ci proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership, 425cb93a386Sopenharmony_ci GrWrapCacheable::kNo, ioType, 426cb93a386Sopenharmony_ci mbet->refCountedCallback()); 427cb93a386Sopenharmony_ci dContext->flushAndSubmit(); 428cb93a386Sopenharmony_ci proxy->peekTexture()->markMipmapsDirty(); // avoids assert in GrGpu. 429cb93a386Sopenharmony_ci auto regenResult = 430cb93a386Sopenharmony_ci dContext->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture()); 431cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType)); 432cb93a386Sopenharmony_ci } 433cb93a386Sopenharmony_ci } 434cb93a386Sopenharmony_ci} 435