DEF_TEST(ImageReadPixels, reporter) { SkAutoTUnref<SkImage> image(create_image()); test_read_pixels(reporter, image); image.reset(create_data_image()); test_read_pixels(reporter, image); RasterDataHolder dataHolder; image.reset(create_rasterproc_image(&dataHolder)); test_read_pixels(reporter, image); image.reset(); REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); image.reset(create_codec_image()); test_read_pixels(reporter, image); }
void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider, GrSurfaceContext* dstContext, const char* testName) { int pixelCnt = dstContext->width() * dstContext->height(); SkAutoTMalloc<uint32_t> pixels(pixelCnt); for (int y = 0; y < dstContext->width(); ++y) { for (int x = 0; x < dstContext->height(); ++x) { pixels.get()[y * dstContext->width() + x] = GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y)); } } GrSurfaceDesc copySrcDesc; copySrcDesc.fWidth = dstContext->width(); copySrcDesc.fHeight = dstContext->height(); copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { copySrcDesc.fFlags = flags; copySrcDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; sk_sp<GrTextureProxy> src = proxyProvider->createTextureProxy(copySrcDesc, SkBudgeted::kYes, pixels.get(), 0); dstContext->copy(src.get()); test_read_pixels(reporter, dstContext, pixels.get(), testName); } }
void test_write_pixels(skiatest::Reporter* reporter, GrSurfaceContext* dstContext, bool expectedToWork, const char* testName) { int pixelCnt = dstContext->width() * dstContext->height(); SkAutoTMalloc<uint32_t> pixels(pixelCnt); for (int y = 0; y < dstContext->width(); ++y) { for (int x = 0; x < dstContext->height(); ++x) { pixels.get()[y * dstContext->width() + x] = GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y)); } } SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType); bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0); if (!write) { if (expectedToWork) { ERRORF(reporter, "%s: Error writing to texture.", testName); } return; } if (write && !expectedToWork) { ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName); return; } test_read_pixels(reporter, dstContext, pixels.get(), testName); }
static void test_copy_surface(skiatest::Reporter* reporter, GrContext* context, GrTexture* externalTexture, uint32_t expectedPixelValues[]) { GrSurfaceDesc copyDesc; copyDesc.fConfig = kRGBA_8888_GrPixelConfig; copyDesc.fWidth = externalTexture->width(); copyDesc.fHeight = externalTexture->height(); copyDesc.fFlags = kRenderTarget_GrSurfaceFlag; SkAutoTUnref<GrTexture> copy(context->textureProvider()->createTexture( copyDesc, SkBudgeted::kYes)); context->copySurface(copy, externalTexture); test_read_pixels(reporter, context, copy, expectedPixelValues); }
static void test_clear(skiatest::Reporter* reporter, GrContext* context, GrTexture* rectangleTexture) { if (rectangleTexture->asRenderTarget()) { sk_sp<GrDrawContext> dc( context->drawContext(sk_ref_sp(rectangleTexture->asRenderTarget()), nullptr)); if (!dc) { ERRORF(reporter, "Could not get GrDrawContext for rectangle texture."); return; } // Clear the whole thing. GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); dc->clear(nullptr, color0, false); int w = rectangleTexture->width(); int h = rectangleTexture->height(); int pixelCnt = w * h; SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different. uint32_t expectedColor0 = 0; uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); expectedBytes0[0] = GrColorUnpackR(color0); expectedBytes0[1] = GrColorUnpackG(color0); expectedBytes0[2] = GrColorUnpackB(color0); expectedBytes0[3] = GrColorUnpackA(color0); for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) { expectedPixels.get()[i] = expectedColor0; } // Clear the the top to a different color. GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); SkIRect rect = SkIRect::MakeWH(w, h/2); dc->clear(&rect, color1, false); uint32_t expectedColor1 = 0; uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); expectedBytes1[0] = GrColorUnpackR(color1); expectedBytes1[1] = GrColorUnpackG(color1); expectedBytes1[2] = GrColorUnpackB(color1); expectedBytes1[3] = GrColorUnpackA(color1); for (int y = 0; y < h/2; ++y) { for (int x = 0; x < w; ++x) { expectedPixels.get()[y * h + x] = expectedColor1; } } test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get()); } }
static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, GrTexture* rectangleTexture) { int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); SkAutoTMalloc<uint32_t> pixels(pixelCnt); for (int y = 0; y < rectangleTexture->width(); ++y) { for (int x = 0; x < rectangleTexture->height(); ++x) { pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(x, y, x + y, x * y); } } bool write = rectangleTexture->writePixels(0, 0, rectangleTexture->width(), rectangleTexture->height(), kRGBA_8888_GrPixelConfig, pixels.get()); if (!write) { ERRORF(reporter, "Error writing to rectangle texture."); } test_read_pixels(reporter, context, rectangleTexture, pixels.get()); }
static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) { if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) { // Clear the whole thing. GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); rtc->clear(nullptr, color0, GrRenderTargetContext::CanClearFullscreen::kNo); int w = rtc->width(); int h = rtc->height(); int pixelCnt = w * h; SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different. uint32_t expectedColor0 = 0; uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); expectedBytes0[0] = GrColorUnpackR(color0); expectedBytes0[1] = GrColorUnpackG(color0); expectedBytes0[2] = GrColorUnpackB(color0); expectedBytes0[3] = GrColorUnpackA(color0); for (int i = 0; i < rtc->width() * rtc->height(); ++i) { expectedPixels.get()[i] = expectedColor0; } // Clear the the top to a different color. GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); SkIRect rect = SkIRect::MakeWH(w, h/2); rtc->clear(&rect, color1, GrRenderTargetContext::CanClearFullscreen::kNo); uint32_t expectedColor1 = 0; uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); expectedBytes1[0] = GrColorUnpackR(color1); expectedBytes1[1] = GrColorUnpackG(color1); expectedBytes1[2] = GrColorUnpackB(color1); expectedBytes1[3] = GrColorUnpackA(color1); for (int y = 0; y < h/2; ++y) { for (int x = 0; x < w; ++x) { expectedPixels.get()[y * h + x] = expectedColor1; } } test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear"); } }
// skbug.com/5932 static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context, sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) { sk_sp<GrRenderTargetContext> rtContext(context->contextPriv().makeDeferredRenderTargetContext( SkBackingFit::kExact, rectProxy->width(), rectProxy->height(), rectProxy->config(), nullptr)); for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kBilerp, GrSamplerState::Filter::kMipMap}) { rtContext->clear(nullptr, 0xDDCCBBAA, GrRenderTargetContext::CanClearFullscreen::kYes); auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter); GrPaint paint; paint.setPorterDuffXPFactory(SkBlendMode::kSrc); paint.addColorFragmentProcessor(std::move(fp)); rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); test_read_pixels(reporter, rtContext.get(), expectedPixelValues, "RectangleTexture-basic-draw"); } }
static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context, GrTexture* rectangleTexture) { int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); SkAutoTMalloc<uint32_t> pixels(pixelCnt); for (int y = 0; y < rectangleTexture->width(); ++y) { for (int x = 0; x < rectangleTexture->height(); ++x) { pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y); } } GrSurfaceDesc copySrcDesc; copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; copySrcDesc.fWidth = rectangleTexture->width(); copySrcDesc.fHeight = rectangleTexture->height(); copySrcDesc.fFlags = kRenderTarget_GrSurfaceFlag; SkAutoTUnref<GrTexture> src(context->textureProvider()->createTexture( copySrcDesc, SkBudgeted::kYes, pixels.get(), 0)); context->copySurface(rectangleTexture, src); test_read_pixels(reporter, context, rectangleTexture, pixels.get()); }
void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context, GrSurfaceProxy* proxy, uint32_t expectedPixelValues[], bool onlyTestRTConfig, const char* testName) { GrSurfaceDesc copyDstDesc; copyDstDesc.fWidth = proxy->width(); copyDstDesc.fHeight = proxy->height(); copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) { continue; } copyDstDesc.fFlags = flags; copyDstDesc.fOrigin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy)); test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName); } }
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EGLImageTest, reporter, context0, glCtx0) { // Try to create a second GL context and then check if the contexts have necessary // extensions to run this test. if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) { return; } GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu()); if (!gpu0->glCaps().glslCaps()->externalTextureSupport()) { return; } SkAutoTDelete<SkGLContext> glCtx1 = glCtx0->createNew(); if (!glCtx1) { return; } GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl()); const GrGLTextureInfo* backendTexture1 = nullptr; GrEGLImage image = GR_EGL_NO_IMAGE; GrGLTextureInfo externalTexture; externalTexture.fID = 0; if (!context1) { cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); return; } if (!glCtx1->gl()->hasExtension("EGL_KHR_image") || !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); return; } ///////////////////////////////// CONTEXT 1 /////////////////////////////////// // Use GL Context 1 to create a texture unknown to GrContext. context1->flush(); GrGpu* gpu1 = context1->getGpu(); static const int kSize = 100; backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>( gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig)); if (!backendTexture1 || !backendTexture1->fID) { ERRORF(reporter, "Error creating texture for EGL Image"); cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); return; } if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) { ERRORF(reporter, "Expected backend texture to be 2D"); cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); return; } // Wrap the texture in an EGLImage image = glCtx1->texture2DToEGLImage(backendTexture1->fID); if (GR_EGL_NO_IMAGE == image) { ERRORF(reporter, "Error creating EGL Image from texture"); cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); return; } // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans // the EGL image. Also, this must be done after creating the EGLImage as the texture // contents may not be preserved when the image is created. SkAutoTMalloc<uint32_t> pixels(kSize * kSize); for (int i = 0; i < kSize*kSize; ++i) { pixels.get()[i] = 0xDDAABBCC; } GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0)); GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID)); GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get())); GR_GL_CALL(glCtx1->gl(), Finish()); // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal // state is invalid. context1->resetContext(); ///////////////////////////////// CONTEXT 0 /////////////////////////////////// // Make a new texture ID in GL Context 0 from the EGL Image glCtx0->makeCurrent(); externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL; externalTexture.fID = glCtx0->eglImageToExternalTexture(image); // Wrap this texture ID in a GrTexture GrBackendTextureDesc externalDesc; externalDesc.fConfig = kRGBA_8888_GrPixelConfig; externalDesc.fWidth = kSize; externalDesc.fHeight = kSize; externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture); SkAutoTUnref<GrTexture> externalTextureObj( context0->textureProvider()->wrapBackendTexture(externalDesc)); if (!externalTextureObj) { ERRORF(reporter, "Error wrapping external texture in GrTexture."); cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); return; } // Should not be able to wrap as a RT externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag; SkAutoTUnref<GrTexture> externalTextureRTObj( context0->textureProvider()->wrapBackendTexture(externalDesc)); if (externalTextureRTObj) { ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT."); } externalDesc.fFlags = kNone_GrBackendTextureFlag; // Should not be able to wrap with a sample count externalDesc.fSampleCnt = 4; SkAutoTUnref<GrTexture> externalTextureMSAAObj( context0->textureProvider()->wrapBackendTexture(externalDesc)); if (externalTextureMSAAObj) { ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA."); } externalDesc.fSampleCnt = 0; test_read_pixels(reporter, context0, externalTextureObj, pixels.get()); test_write_pixels(reporter, context0, externalTextureObj); test_copy_surface(reporter, context0, externalTextureObj, pixels.get()); cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); }
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { GrContext* context = ctxInfo.grContext(); sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); static const int kWidth = 13; static const int kHeight = 13; GrColor pixels[kWidth * kHeight]; for (int y = 0; y < kHeight; ++y) { for (int x = 0; x < kWidth; ++x) { pixels[y * kWidth + x] = y * kWidth + x; } } for (int origin = 0; origin < 2; ++origin) { GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels); if (!rectTexID) { return; } // Let GrContext know that we messed with the GL context directly. context->resetContext(); // Wrap the rectangle texture ID in a GrTexture GrGLTextureInfo rectangleInfo; rectangleInfo.fID = rectTexID; rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; GrBackendTextureDesc rectangleDesc; rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag; rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig; rectangleDesc.fWidth = kWidth; rectangleDesc.fHeight = kHeight; rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo); GrColor refPixels[kWidth * kHeight]; bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin; for (int y = 0; y < kHeight; ++y) { for (int x = 0; x < kWidth; ++x) { int y0 = flipRef ? kHeight - y - 1 : y; refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; } } SkAutoTUnref<GrTexture> rectangleTexture( context->textureProvider()->wrapBackendTexture(rectangleDesc)); if (!rectangleTexture) { ERRORF(reporter, "Error wrapping rectangle texture in GrTexture."); GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); continue; } test_read_pixels(reporter, context, rectangleTexture, refPixels); test_copy_surface_src(reporter, context, rectangleTexture, refPixels); test_copy_surface_dst(reporter, context, rectangleTexture); test_write_pixels(reporter, context, rectangleTexture); test_clear(reporter, context, rectangleTexture); GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); } }
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { GrContext* context = ctxInfo.grContext(); GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); static const int kWidth = 13; static const int kHeight = 13; GrColor pixels[kWidth * kHeight]; for (int y = 0; y < kHeight; ++y) { for (int x = 0; x < kWidth; ++x) { pixels[y * kWidth + x] = y * kWidth + x; } } for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin; GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels); if (!rectTexID) { return; } // Let GrContext know that we messed with the GL context directly. context->resetContext(); // Wrap the rectangle texture ID in a GrTexture GrGLTextureInfo rectangleInfo; rectangleInfo.fID = rectTexID; rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; GrBackendTexture rectangleTex(kWidth, kHeight, kRGBA_8888_GrPixelConfig, rectangleInfo); GrColor refPixels[kWidth * kHeight]; for (int y = 0; y < kHeight; ++y) { for (int x = 0; x < kWidth; ++x) { int y0 = useBLOrigin ? kHeight - y - 1 : y; refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; } } sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(rectangleTex, origin); if (!rectProxy) { ERRORF(reporter, "Error creating proxy for rectangle texture."); GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); continue; } SkASSERT(rectProxy->texPriv().doesNotSupportMipMaps()); SkASSERT(rectProxy->priv().peekTexture()->surfacePriv().doesNotSupportMipMaps()); SkASSERT(rectProxy->texPriv().isClampOnly()); SkASSERT(rectProxy->priv().peekTexture()->surfacePriv().isClampOnly()); test_basic_draw_as_src(reporter, context, rectProxy, refPixels); // Test copy to both a texture and RT test_copy_from_surface(reporter, context, rectProxy.get(), refPixels, false, "RectangleTexture-copy-from"); sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext( std::move(rectProxy)); SkASSERT(rectContext); test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read"); test_copy_to_surface(reporter, context->contextPriv().proxyProvider(), rectContext.get(), "RectangleTexture-copy-to"); test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write"); test_clear(reporter, rectContext.get()); GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); } }
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, context) { SkAutoTUnref<SkImage> image(create_gpu_image(context)); test_read_pixels(reporter, image); }