17b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com/*
27b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com* Copyright 2013 Google Inc.
37b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com*
47b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com* Use of this source code is governed by a BSD-style license that can be
57b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com* found in the LICENSE file.
67b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com*/
77b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
87b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "gm.h"
97b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkBlurMask.h"
107b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkBlurMaskFilter.h"
117b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkCanvas.h"
127b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkColorFilter.h"
137b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkLayerDrawLooper.h"
147b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkPaint.h"
157b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkPath.h"
167b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkPoint.h"
177b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkRect.h"
187b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkRRect.h"
197b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkString.h"
207b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com#include "SkXfermode.h"
217b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
228610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com// This GM mimics a blurred RR seen in the wild.
237b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.comclass BlurRoundRectGM : public skiagm::GM {
247b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.compublic:
258610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    BlurRoundRectGM(int width, int height, int radius)
267b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        : fName("blurroundrect")
278610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    {
288610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
298610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        fRRect.setRectXY(r, SkIntToScalar(radius), SkIntToScalar(radius));
30ef470f185f0dcd94943d0c470ddf8cc109f30325scroggo        fName.appendf("-WH-%ix%i-corner-%i", width, height, radius);
318610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    }
328610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com
338610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    BlurRoundRectGM(int width, int height)
348610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        : fName("blurroundrect") {
35ef470f185f0dcd94943d0c470ddf8cc109f30325scroggo        fName.appendf("-WH-%ix%i-unevenCorners", width,  height);
367b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        SkVector radii[4];
378610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
388610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
398610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
408610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
418610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
427b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        fRRect.setRectRadii(r, radii);
437b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
447b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
457b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    virtual SkString onShortName() SK_OVERRIDE {
467b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        return fName;
477b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
487b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
497b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    virtual SkISize onISize() SK_OVERRIDE {
508610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()),
518610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                             SkScalarCeilToInt(fRRect.rect().height()));
527b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
537b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
547b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
5573cb15351f33459e0c861a96135c634dec77ef9dcommit-bot@chromium.org        SkLayerDrawLooper::Builder looperBuilder;
567b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        {
577b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            SkLayerDrawLooper::LayerInfo info;
588610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com            info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
598610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                              | SkLayerDrawLooper::kColorFilter_Bit;
607b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            info.fColorMode = SkXfermode::kSrc_Mode;
617b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
627b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            info.fPostTranslate = false;
6373cb15351f33459e0c861a96135c634dec77ef9dcommit-bot@chromium.org            SkPaint* paint = looperBuilder.addLayerOnTop(info);
646c1e49af8de1f6dfdc5fb09cd0edfc2e469e945frobertphillips@google.com            SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
65e396455d2d60ddf8e625b5037254f3c09fbcdcf5commit-bot@chromium.org                    kNormal_SkBlurStyle,
666c1e49af8de1f6dfdc5fb09cd0edfc2e469e945frobertphillips@google.com                    SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
677b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com                    SkBlurMaskFilter::kHighQuality_BlurFlag);
687b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            paint->setMaskFilter(maskFilter)->unref();
695fdef98f627697966763b88d770a8a8e45c8197ascroggo@google.com            SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY,
707b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com                    SkXfermode::kSrcIn_Mode);
717b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            paint->setColorFilter(colorFilter)->unref();
725fdef98f627697966763b88d770a8a8e45c8197ascroggo@google.com            paint->setColor(SK_ColorGRAY);
737b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        }
747b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        {
757b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com            SkLayerDrawLooper::LayerInfo info;
7673cb15351f33459e0c861a96135c634dec77ef9dcommit-bot@chromium.org            looperBuilder.addLayerOnTop(info);
777b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        }
787b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        SkPaint paint;
798610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        canvas->drawRect(fRRect.rect(), paint);
807b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
8173cb15351f33459e0c861a96135c634dec77ef9dcommit-bot@chromium.org        paint.setLooper(looperBuilder.detachLooper())->unref();
825fdef98f627697966763b88d770a8a8e45c8197ascroggo@google.com        paint.setColor(SK_ColorCYAN);
837b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com        paint.setAntiAlias(true);
847b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
858610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        canvas->drawRRect(fRRect, paint);
867b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
877b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
887b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.comprivate:
898610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    SkString        fName;
908610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    SkRRect         fRRect;
918610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com
928610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    typedef skiagm::GM INHERITED;
937b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com};
947b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
958610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com// Simpler blurred RR test cases where all the radii are the same.
968610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.comclass SimpleBlurRoundRectGM : public skiagm::GM {
977b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.compublic:
988610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    SimpleBlurRoundRectGM()
998610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        : fName("simpleblurroundrect") {
1007b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
1017b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
1027b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.comprotected:
1038610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    virtual SkString onShortName() SK_OVERRIDE {
1048610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        return fName;
1058610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    }
1068610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com
1078610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    virtual SkISize onISize() SK_OVERRIDE {
1083d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org        return SkISize::Make(950, 950);
1097b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
1107b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
1117b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
1124b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org        canvas->scale(1.5f, 1.5f);
1133d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org        canvas->translate(50,50);
1148610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com
1153d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org        const float blurRadii[] = { 1,5,10,20 };
1163d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org        const int cornerRadii[] = { 1,5,10,20 };
1178610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        const SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1188610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
1198610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com            SkAutoCanvasRestore autoRestore(canvas, true);
1203d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org            canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
1218610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com            for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
1226c1e49af8de1f6dfdc5fb09cd0edfc2e469e945frobertphillips@google.com                SkMaskFilter* filter = SkBlurMaskFilter::Create(
123e396455d2d60ddf8e625b5037254f3c09fbcdcf5commit-bot@chromium.org                    kNormal_SkBlurStyle,
1243d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org                    SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])),
1253d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org                    SkBlurMaskFilter::kHighQuality_BlurFlag);
1268610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                SkPaint paint;
1273d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org                paint.setColor(SK_ColorBLACK);
1288610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                paint.setMaskFilter(filter)->unref();
1298610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com
1308610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                SkRRect rrect;
1318610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), SkIntToScalar(cornerRadii[j]));
1328610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com                canvas->drawRRect(rrect, paint);
1333d8bf232bd2e4903d2ee2bb575637e7555361604commit-bot@chromium.org                canvas->translate(r.width() + SkIntToScalar(50), 0);
1348610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com            }
1358610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com        }
1367b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com    }
1377b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.comprivate:
1388610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    const SkString  fName;
1397b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
1408610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com    typedef         skiagm::GM INHERITED;
1417b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com};
1427b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com
1437b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com// Create one with dimensions/rounded corners based on the skp
1447e6e80a6eba2ce4250466d4d61e91ad8c609a593epoger@google.com//
1457e6e80a6eba2ce4250466d4d61e91ad8c609a593epoger@google.com// TODO(scroggo): Disabled in an attempt to rememdy
1467e6e80a6eba2ce4250466d4d61e91ad8c609a593epoger@google.com// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
1477e6e80a6eba2ce4250466d4d61e91ad8c609a593epoger@google.com// ran wrong number of tests')
1487e6e80a6eba2ce4250466d4d61e91ad8c609a593epoger@google.com//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
1497e6e80a6eba2ce4250466d4d61e91ad8c609a593epoger@google.com
1507b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com// Rounded rect with two opposite corners with large radii, the other two
1517b0565907272bfe6682b09a36f7a8fef85e0f73dscroggo@google.com// small.
1528610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.comDEF_GM(return new BlurRoundRectGM(100, 100);)
1538610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.com
1548610d2cdbdcc0e1cf28f9a1ffea0ebab53a33485scroggo@google.comDEF_GM(return new SimpleBlurRoundRectGM();)
155