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