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