| Index: tests/GrSurfaceTest.cpp
|
| diff --git a/tests/GrSurfaceTest.cpp b/tests/GrSurfaceTest.cpp
|
| index 685bf4187461f352c6bdca227f0e78326393ff6e..68d067d75566de5d61a639cfc9a5d3eb47472960 100644
|
| --- a/tests/GrSurfaceTest.cpp
|
| +++ b/tests/GrSurfaceTest.cpp
|
| @@ -14,6 +14,10 @@
|
| #include "GrRenderTarget.h"
|
| #include "GrTexture.h"
|
| #include "GrSurfacePriv.h"
|
| +#include "SkCanvas.h"
|
| +#include "SkImage.h"
|
| +#include "SkSurface.h"
|
| +#include "SkUtils.h"
|
| #include "Test.h"
|
|
|
| // Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
|
| @@ -67,4 +71,92 @@ DEF_GPUTEST(GrSurface, reporter, factory) {
|
| }
|
| }
|
|
|
| +static SkImage* make_gpu_image(GrContext* ctx, int w, int h, SkColor color) {
|
| + const SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
|
| + SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info));
|
| + if (!surface) {
|
| + return NULL;
|
| + }
|
| + surface->getCanvas()->drawColor(color);
|
| + surface->getCanvas()->flush();
|
| + return surface->newImageSnapshot();
|
| +}
|
| +
|
| +static void test_tex_image(skiatest::Reporter* reporter, GrContext* ctx, GrBackendObject nativeTex,
|
| + int w, int h, bool testCopy, SkPMColor expected) {
|
| + GrBackendTextureDesc desc;
|
| + desc.fConfig = kSkia8888_GrPixelConfig;
|
| + // need to be a rendertarget for now...
|
| + desc.fFlags = kRenderTarget_GrBackendTextureFlag; //kNone_GrBackendTextureFlag;
|
| + desc.fWidth = w;
|
| + desc.fHeight = h;
|
| + desc.fSampleCnt = 0;
|
| + desc.fTextureHandle = nativeTex;
|
| + SkAutoTUnref<SkImage> image(testCopy ?
|
| + SkImage::NewFromTextureCopy(ctx, desc) :
|
| + SkImage::NewFromTexture(ctx, desc));
|
| + REPORTER_ASSERT(reporter, image.get() != NULL);
|
| + if (image) {
|
| + const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
|
| + SkPMColor pixel;
|
| + REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0, 0));
|
| + REPORTER_ASSERT(reporter, pixel == expected);
|
| + }
|
| +}
|
| +
|
| +DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) {
|
| + GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
|
| + if (!ctx) {
|
| + REPORTER_ASSERT(reporter, false);
|
| + return;
|
| + }
|
| +
|
| + const int w = 10;
|
| + const int h = 10;
|
| + const SkColor srcColor = SK_ColorRED;
|
| + SkPMColor expected = SkPreMultiplyColor(srcColor);
|
| + SkAutoTUnref<SkImage> srcImg(make_gpu_image(ctx, w, h, srcColor));
|
| + if (!srcImg) {
|
| + REPORTER_ASSERT(reporter, false);
|
| + return;
|
| + }
|
| +
|
| + GrBackendObject srcTex = srcImg->getTexture()->getTextureHandle();
|
| + test_tex_image(reporter, ctx, srcTex, w, h, false, expected);
|
| + test_tex_image(reporter, ctx, srcTex, w, h, true, expected);
|
| +}
|
| +
|
| +DEF_GPUTEST(SkImage_NewFromTexture2, reporter, factory) {
|
| + GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
|
| + if (!ctx) {
|
| + REPORTER_ASSERT(reporter, false);
|
| + return;
|
| + }
|
| + GrTextureProvider* provider = ctx->textureProvider();
|
| +
|
| + const int w = 10;
|
| + const int h = 10;
|
| + SkPMColor storage[w * h];
|
| + SkPMColor expected = SkPreMultiplyColor(SK_ColorRED);
|
| + sk_memset32(storage, expected, 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;
|
| + }
|
| +
|
| + GrBackendObject srcTex = tex->getTextureHandle();
|
| + test_tex_image(reporter, ctx, srcTex, w, h, false, expected);
|
| + test_tex_image(reporter, ctx, srcTex, w, h, true, expected);
|
| +}
|
| +
|
| #endif
|
|
|