sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "Test.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkRectShaderImageFilter.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | |
| 14 | static void test_asShaderMode(skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 3c8fb51 | 2013-02-21 17:10:44 +0000 | [diff] [blame] | 15 | int w = 10, h = 10; |
| 16 | SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); // Make small 10x10 gradient |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 17 | |
| 18 | SkBitmap filterResult, shaderResult; |
| 19 | |
senorblanco@chromium.org | 3c8fb51 | 2013-02-21 17:10:44 +0000 | [diff] [blame] | 20 | filterResult.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 21 | filterResult.allocPixels(); |
| 22 | SkCanvas canvasFilter(filterResult); |
| 23 | canvasFilter.clear(0x00000000); |
| 24 | |
senorblanco@chromium.org | 3c8fb51 | 2013-02-21 17:10:44 +0000 | [diff] [blame] | 25 | shaderResult.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 26 | shaderResult.allocPixels(); |
| 27 | SkCanvas canvasShader(shaderResult); |
| 28 | canvasShader.clear(0x00000000); |
| 29 | |
| 30 | SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5)); |
| 31 | SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN}; |
| 32 | SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1}; |
| 33 | SkScalar radius = SkIntToScalar(5); |
| 34 | |
| 35 | // Test using the image filter |
| 36 | { |
| 37 | SkShader* s = SkGradientShader::CreateRadial( |
| 38 | center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode); |
| 39 | SkPaint paint; |
| 40 | paint.setImageFilter(SkRectShaderImageFilter::Create(s, r))->unref(); |
| 41 | canvasFilter.drawRect(r, paint); |
| 42 | s->unref(); |
| 43 | } |
| 44 | |
| 45 | // Test using the shader directly |
| 46 | { |
| 47 | SkShader* s = SkGradientShader::CreateRadial( |
| 48 | center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode); |
| 49 | SkPaint paint; |
| 50 | paint.setShader(s)->unref(); |
| 51 | canvasShader.drawRect(r, paint); |
| 52 | } |
| 53 | |
| 54 | // Assert that both paths yielded the same result |
| 55 | for (int y = 0; y < r.height(); ++y) { |
| 56 | const SkPMColor* filterPtr = filterResult.getAddr32(0, y); |
| 57 | const SkPMColor* shaderPtr = shaderResult.getAddr32(0, y); |
| 58 | for (int x = 0; x < r.width(); ++x, ++filterPtr, ++shaderPtr) { |
| 59 | REPORTER_ASSERT(reporter, *filterPtr == *shaderPtr); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #include "TestClassDef.h" |
| 65 | DEFINE_TESTCLASS("ShaderImageFilter", ShaderImageFilterTestClass, test_asShaderMode) |