piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | #if SK_SUPPORT_GPU |
| 9 | #include "GrContextFactory.h" |
| 10 | #endif |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkImage.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkSurface.h" |
| 15 | |
| 16 | #include "Test.h" |
| 17 | |
| 18 | void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) { |
| 19 | bm1.lockPixels(); |
| 20 | bm2.lockPixels(); |
| 21 | |
| 22 | REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize()); |
| 23 | REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize())); |
| 24 | |
| 25 | bm2.unlockPixels(); |
| 26 | bm1.unlockPixels(); |
| 27 | } |
| 28 | |
| 29 | void runShaderTest(skiatest::Reporter* reporter, SkSurface* source, SkSurface* destination, SkImageInfo& info) { |
| 30 | SkCanvas* rasterCanvas = source->getCanvas(); |
| 31 | rasterCanvas->drawColor(0xFFDEDEDE, SkXfermode::kSrc_Mode); |
| 32 | |
| 33 | SkAutoTUnref<SkImage> rasterImage(source->newImageSnapshot()); |
| 34 | SkAutoTUnref<SkShader> rasterShader(rasterImage->newShader( |
| 35 | SkShader::kRepeat_TileMode, |
| 36 | SkShader::kRepeat_TileMode)); |
| 37 | |
| 38 | SkPaint paint; |
| 39 | paint.setShader(rasterShader); |
| 40 | SkCanvas* canvasDest = destination->getCanvas(); |
| 41 | canvasDest->clear(SK_ColorTRANSPARENT); |
| 42 | canvasDest->drawPaint(paint); |
| 43 | |
| 44 | SkIRect rect = SkIRect::MakeXYWH(0, 0, 5, 5); |
| 45 | |
| 46 | SkBitmap bmOrig; |
| 47 | rasterCanvas->readPixels(rect, &bmOrig); |
| 48 | |
| 49 | SkBitmap bm; |
| 50 | canvasDest->readPixels(rect, &bm); |
| 51 | |
| 52 | testBitmapEquality(reporter, bmOrig, bm); |
| 53 | } |
| 54 | |
| 55 | DEF_TEST(ImageNewShader, reporter) { |
| 56 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 57 | |
| 58 | SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info)); |
| 59 | SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info)); |
| 60 | |
| 61 | runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); |
| 62 | } |
| 63 | |
| 64 | #if SK_SUPPORT_GPU |
| 65 | |
| 66 | void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) { |
| 67 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 68 | |
| 69 | SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info)); |
| 70 | SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info)); |
| 71 | |
| 72 | runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); |
| 73 | } |
| 74 | |
| 75 | void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) { |
| 76 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 77 | |
| 78 | SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info)); |
| 79 | SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info)); |
| 80 | |
| 81 | runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); |
| 82 | } |
| 83 | |
| 84 | void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) { |
| 85 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 86 | |
| 87 | SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info)); |
| 88 | SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info)); |
| 89 | |
| 90 | runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); |
| 91 | } |
| 92 | |
| 93 | DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) { |
| 94 | GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
| 95 | |
| 96 | // GPU -> GPU |
| 97 | gpuToGpu(reporter, context); |
| 98 | |
| 99 | // GPU -> RASTER |
| 100 | gpuToRaster(reporter, context); |
| 101 | |
| 102 | |
| 103 | // RASTER -> GPU |
| 104 | rasterToGpu(reporter, context); |
| 105 | } |
| 106 | |
| 107 | #endif |