Exemplo n.º 1
0
    void onDraw(SkCanvas* canvas) override {
        SkAutoTUnref<SkColorFilter> cf(
            SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
        SkImageFilter::CropRect cropRect(
            SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
            SkImageFilter::CropRect::kHasAll_CropEdge);

        sk_sp<SkImage> gradientCircle(MakeGradientCircle(64, 64));
        SkBitmap checkerboard;
        MakeCheckerboard(&checkerboard);

        SkAutoTUnref<SkImageFilter> gradientCircleSource(
            SkImageSource::Create(gradientCircle.get()));
        SkAutoTUnref<SkImageFilter> noopCropped(
            SkOffsetImageFilter::Create(0, 0, nullptr, &cropRect));
        SkScalar sk255 = SkIntToScalar(255);
        SkScalar matrix[20] = { 1, 0, 0, 0, 0,
                                0, 1, 0, 0, sk255,
                                0, 0, 1, 0, 0,
                                0, 0, 0, 0, sk255 };
        SkAutoTUnref<SkColorFilter> cfAlphaTrans(SkColorMatrixFilter::Create(matrix));

        SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
        SkScalar MARGIN = SkIntToScalar(12);

        SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
        SkScalar kd = SkIntToScalar(2);
        SkScalar surfaceScale = SkIntToScalar(1);
        SkIRect bounds;
        r.roundOut(&bounds);

        SkPaint paint;
        canvas->translate(MARGIN, MARGIN);
        for (int outset = -15; outset <= 20; outset += 5) {
            canvas->save();
            SkRect rect = cropRect.rect();
            rect.outset(SkIntToScalar(outset),
                        SkIntToScalar(outset));
            SkImageFilter::CropRect bigRect(rect, SkImageFilter::CropRect::kHasAll_CropEdge);

            Draw(canvas, checkerboard, rect, SkColorFilterImageFilter::Create(
                cfAlphaTrans, noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect, SkBlurImageFilter::Create(
                0.3f, 0.3f, noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect, SkBlurImageFilter::Create(
                8.0f, 8.0f, noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect, SkDilateImageFilter::Create(
                2, 2, noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect, SkErodeImageFilter::Create(
                2, 2, noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect, SkDropShadowImageFilter::Create(
                SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(3), SkIntToScalar(3),
                SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
                noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect, SkDisplacementMapEffect::Create(
                SkDisplacementMapEffect::kR_ChannelSelectorType,
                SkDisplacementMapEffect::kR_ChannelSelectorType,
                SkIntToScalar(12),
                gradientCircleSource.get(),
                noopCropped.get(),
                &bigRect));

            Draw(canvas, checkerboard, rect, SkOffsetImageFilter::Create(
                SkIntToScalar(-8), SkIntToScalar(16), noopCropped.get(), &bigRect));

            Draw(canvas, checkerboard, rect,
                SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, SK_ColorWHITE,
                surfaceScale, kd, noopCropped.get(), &bigRect));

            canvas->restore();
            canvas->translate(0, SkIntToScalar(80));
        }
    }
Exemplo n.º 2
0
    void onDraw(SkCanvas* canvas) override {
        SkImageFilter::CropRect cropRect(
            SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
            SkImageFilter::CropRect::kHasAll_CropEdge);

        sk_sp<SkImage> gradientCircle(MakeGradientCircle(64, 64));
        SkBitmap checkerboard;
        MakeCheckerboard(&checkerboard);

        sk_sp<SkImageFilter> gradientCircleSource(SkImageSource::Make(std::move(gradientCircle)));
        sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect));
        // This color matrix saturates the green component but only partly increases the opacity.
        // For the opaque checkerboard, the opacity boost doesn't matter but it does impact the
        // area outside the checkerboard.
        SkScalar matrix[20] = { 1, 0, 0, 0, 0,
                                0, 1, 0, 0, 255,
                                0, 0, 1, 0, 0,
                                0, 0, 0, 1, 32 };
        sk_sp<SkColorFilter> cfAlphaTrans(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));

        SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
        SkScalar MARGIN = SkIntToScalar(12);

        SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
        SkScalar kd = SkIntToScalar(2);
        SkScalar surfaceScale = SkIntToScalar(1);
        SkIRect bounds;
        r.roundOut(&bounds);

        SkPaint paint;
        canvas->translate(MARGIN, MARGIN);
        for (int outset = -15; outset <= 20; outset += 5) {
            canvas->save();
            SkRect rect = cropRect.rect();
            rect.outset(SkIntToScalar(outset),
                        SkIntToScalar(outset));
            SkImageFilter::CropRect bigRect(rect, SkImageFilter::CropRect::kHasAll_CropEdge);

            Draw(canvas, checkerboard, rect, SkColorFilterImageFilter::Make(cfAlphaTrans,
                                                                            noopCropped,
                                                                            &bigRect));

            Draw(canvas, checkerboard, rect, SkBlurImageFilter::Make(0.3f, 0.3f,
                                                                     noopCropped,
                                                                     &bigRect));

            Draw(canvas, checkerboard, rect, SkBlurImageFilter::Make(8.0f, 8.0f,
                                                                     noopCropped,
                                                                     &bigRect));

            Draw(canvas, checkerboard, rect, SkDilateImageFilter::Make(2, 2,
                                                                       noopCropped,
                                                                       &bigRect));

            Draw(canvas, checkerboard, rect, SkErodeImageFilter::Make(2, 2,
                                                                      noopCropped,
                                                                      &bigRect));

            Draw(canvas, checkerboard, rect,
                 SkDropShadowImageFilter::Make(
                                    SkIntToScalar(10),
                                    SkIntToScalar(10),
                                    SkIntToScalar(3),
                                    SkIntToScalar(3),
                                    SK_ColorBLUE,
                                    SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
                                    noopCropped,
                                    &bigRect));

            Draw(canvas, checkerboard, rect,
                 SkDisplacementMapEffect::Make(SkDisplacementMapEffect::kR_ChannelSelectorType,
                                               SkDisplacementMapEffect::kR_ChannelSelectorType,
                                               SkIntToScalar(12),
                                               gradientCircleSource,
                                               noopCropped,
                                               &bigRect));

            Draw(canvas, checkerboard, rect,
                 SkOffsetImageFilter::Make(SkIntToScalar(-8), SkIntToScalar(16),
                                           noopCropped,
                                           &bigRect));

            Draw(canvas, checkerboard, rect,
                 SkLightingImageFilter::MakePointLitDiffuse(pointLocation,
                                                            SK_ColorWHITE,
                                                            surfaceScale,
                                                            kd,
                                                            noopCropped,
                                                            &bigRect));

            canvas->restore();
            canvas->translate(0, SkIntToScalar(80));
        }
    }