DEF_GPUTEST_FOR_NATIVE_CONTEXT(SkImage_NewFromTexture, reporter, context) { GrTextureProvider* provider = context->textureProvider(); const int w = 10; const int h = 10; SkPMColor storage[w * h]; const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED); sk_memset32(storage, expected0, w * h); GrSurfaceDesc desc; desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget for readpixels(); desc.fOrigin = kDefault_GrSurfaceOrigin; desc.fWidth = w; desc.fHeight = h; desc.fConfig = kSkia8888_GrPixelConfig; desc.fSampleCnt = 0; SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w * 4)); if (!tex) { REPORTER_ASSERT(reporter, false); return; } GrBackendTextureDesc backendDesc; backendDesc.fConfig = kSkia8888_GrPixelConfig; backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag; backendDesc.fWidth = w; backendDesc.fHeight = h; backendDesc.fSampleCnt = 0; backendDesc.fTextureHandle = tex->getTextureHandle(); TextureReleaseChecker releaseChecker; SkAutoTUnref<SkImage> refImg( SkImage::NewFromTexture(context, backendDesc, kPremul_SkAlphaType, TextureReleaseChecker::Release, &releaseChecker)); SkAutoTUnref<SkImage> cpyImg(SkImage::NewFromTextureCopy(context, backendDesc, kPremul_SkAlphaType)); check_image_color(reporter, refImg, expected0); check_image_color(reporter, cpyImg, expected0); // Now lets jam new colors into our "external" texture, and see if the images notice const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE); sk_memset32(storage, expected1, w * h); tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kFlushWrites_PixelOp); // The cpy'd one should still see the old color #if 0 // There is no guarantee that refImg sees the new color. We are free to have made a copy. Our // write pixels call violated the contract with refImg and refImg is now undefined. check_image_color(reporter, refImg, expected1); #endif check_image_color(reporter, cpyImg, expected0); // Now exercise the release proc REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount); refImg.reset(nullptr); // force a release of the image REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount); }
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) { GrContext* context = ctxInfo.grContext(); if (!context->caps()->srgbSupport()) { return; } const int rtS = 16; const int texS = rtS * 2; // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern // maximizes the minimum error across all three conceivable failure modes: // 1) Likely incorrect: // (A + B) / 2 // 2) No input decode, decode output: // linear_to_srgb((A + B) / 2) // 3) Decode input, no output encode: // (srgb_to_linear(A) + srgb_to_linear(B)) / 2 const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f); static const SkPMColor colors[2] = { SkPackARGB32(0xFF, srgb60, srgb60, srgb60), SkPackARGB32(0xFF, 0x00, 0x00, 0x00) }; uint32_t texData[texS * texS]; for (int y = 0; y < texS; ++y) { for (int x = 0; x < texS; ++x) { texData[y * texS + x] = colors[(x + y) % 2]; } } // We can be pretty generous with the error detection, thanks to the choice of input. // The closest likely failure mode is off by > 0.1, so anything that encodes within // 10/255 of optimal is more than good enough for this test. const U8CPU expectedSRGB = sk_float_round2int( linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f); const U8CPU expectedLinear = srgb60 / 2; const U8CPU error = 10; // Create our test texture GrSurfaceDesc desc; desc.fFlags = kNone_GrSurfaceFlags; desc.fConfig = kSRGBA_8888_GrPixelConfig; desc.fWidth = texS; desc.fHeight = texS; GrTextureProvider* texProvider = context->textureProvider(); SkAutoTUnref<GrTexture> texture(texProvider->createTexture(desc, SkBudgeted::kNo, texData, 0)); // Create two render target contexts (L32 and S32) sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); sk_sp<GrRenderTargetContext> l32RenderTargetContext = context->makeRenderTargetContext( SkBackingFit::kExact, rtS, rtS, kRGBA_8888_GrPixelConfig, nullptr); sk_sp<GrRenderTargetContext> s32RenderTargetContext = context->makeRenderTargetContext( SkBackingFit::kExact, rtS, rtS, kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace)); SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS)); GrNoClip noClip; GrPaint paint; paint.setPorterDuffXPFactory(SkBlendMode::kSrc); GrTextureParams mipMapParams(SkShader::kRepeat_TileMode, GrTextureParams::kMipMap_FilterMode); paint.addColorTextureProcessor(texture, nullptr, SkMatrix::MakeScale(0.5f), mipMapParams); // 1) Draw texture to S32 surface (should generate/use sRGB mips) paint.setGammaCorrect(true); s32RenderTargetContext->drawRect(noClip, paint, SkMatrix::I(), rect); read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error, "first render of sRGB"); // 2) Draw texture to L32 surface (should generate/use linear mips) paint.setGammaCorrect(false); l32RenderTargetContext->drawRect(noClip, paint, SkMatrix::I(), rect); read_and_check_pixels(reporter, l32RenderTargetContext->asTexture().get(), expectedLinear, error, "re-render as linear"); // 3) Go back to sRGB paint.setGammaCorrect(true); s32RenderTargetContext->drawRect(noClip, paint, SkMatrix::I(), rect); read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error, "re-render as sRGB"); }