Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2011 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 #include "Test.h"
      9 #include "TestClassDef.h"
     10 #include "SkCanvas.h"
     11 #include "SkShader.h"
     12 #include "SkRectShaderImageFilter.h"
     13 #include "SkGradientShader.h"
     14 
     15 DEF_TEST(ShaderImageFilter, reporter) {
     16     int w = 10, h = 10;
     17     SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); // Make small 10x10 gradient
     18 
     19     SkBitmap filterResult, shaderResult;
     20 
     21     filterResult.setConfig(SkBitmap::kARGB_8888_Config, w, h);
     22     filterResult.allocPixels();
     23     SkCanvas canvasFilter(filterResult);
     24     canvasFilter.clear(0x00000000);
     25 
     26     shaderResult.setConfig(SkBitmap::kARGB_8888_Config, w, h);
     27     shaderResult.allocPixels();
     28     SkCanvas canvasShader(shaderResult);
     29     canvasShader.clear(0x00000000);
     30 
     31     SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
     32     SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
     33     SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
     34     SkScalar radius = SkIntToScalar(5);
     35 
     36     // Test using the image filter
     37     {
     38         SkShader* s = SkGradientShader::CreateRadial(
     39             center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode);
     40         SkPaint paint;
     41         SkImageFilter::CropRect cr(r);
     42         paint.setImageFilter(SkRectShaderImageFilter::Create(s, &cr))->unref();
     43         canvasFilter.drawRect(r, paint);
     44         s->unref();
     45     }
     46 
     47     // Test using the shader directly
     48     {
     49         SkShader* s = SkGradientShader::CreateRadial(
     50             center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode);
     51         SkPaint paint;
     52         paint.setShader(s)->unref();
     53         canvasShader.drawRect(r, paint);
     54     }
     55 
     56     // Assert that both paths yielded the same result
     57     for (int y = 0; y < r.height(); ++y) {
     58         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
     59         const SkPMColor* shaderPtr = shaderResult.getAddr32(0, y);
     60         for (int x = 0; x < r.width(); ++x, ++filterPtr, ++shaderPtr) {
     61             REPORTER_ASSERT(reporter, *filterPtr == *shaderPtr);
     62         }
     63     }
     64 }
     65