140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org/*
240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org * Copyright 2013 Google Inc.
340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org *
440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org * found in the LICENSE file.
640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org */
740eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org#include "gm.h"
940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org#include "SkAlphaThresholdFilter.h"
1040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org#include "SkRandom.h"
1140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
1240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org#define WIDTH 500
1340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org#define HEIGHT 500
1440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
1540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgnamespace skiagm {
1640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
1740eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgclass ImageAlphaThresholdGM : public GM {
1840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgpublic:
1940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    ImageAlphaThresholdGM() {
2040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        this->setBGColor(0xFFFFFFFF);
2140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    }
2240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
2340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgprotected:
2440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    virtual uint32_t onGetFlags() const SK_OVERRIDE {
25a19818a146ecb966b16dc06ebdbd318fde214128bsalomon@google.com        return this->INHERITED::onGetFlags() |
26a19818a146ecb966b16dc06ebdbd318fde214128bsalomon@google.com               GM::kSkipTiled_Flag |
27a19818a146ecb966b16dc06ebdbd318fde214128bsalomon@google.com               GM::kSkipPicture_Flag |
288f7a108bb1f85ecc06717bbdc1b6a2bca6098db2skia.committer@gmail.com               GM::kSkipPipe_Flag |
29a19818a146ecb966b16dc06ebdbd318fde214128bsalomon@google.com               GM::kSkipPipeCrossProcess_Flag;
3040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    }
3140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
3240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    virtual SkString onShortName() SK_OVERRIDE {
3340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        return SkString("imagealphathreshold");
3440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    }
3540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
3640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    virtual SkISize onISize() SK_OVERRIDE {
37f539318f0d3dba743ec1886d5d9df0fb1be628a1tfarina        return SkISize::Make(WIDTH, HEIGHT);
3840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    }
3940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
4040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
4140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        SkIRect rects[2];
4240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
4340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
4440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        SkRegion region;
4540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        region.setRects(rects, 2);
4640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
4740eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        SkMatrix matrix;
4840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        matrix.reset();
4940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
509109e188c77abfd2832767530d33fdec35290a84commit-bot@chromium.org        matrix.postScale(.8f, .8f);
5140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
5240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->concat(matrix);
5340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
5440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        SkPaint paint;
5540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        paint.setImageFilter(
5640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org            SkAlphaThresholdFilter::Create(region, 0.2f, 0.7f))->unref();
5740eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->saveLayer(NULL, &paint);
5840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        paint.setAntiAlias(true);
5940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
6040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        SkPaint rect_paint;
6140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        rect_paint.setColor(0xFF0000FF);
6240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2),
6340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org                         rect_paint);
6440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        rect_paint.setColor(0xBFFF0000);
6540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2),
6640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org                         rect_paint);
6740eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        rect_paint.setColor(0x3F00FF00);
6840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2),
6940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org                         rect_paint);
7040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        rect_paint.setColor(0x00000000);
7140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2),
7240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org                         rect_paint);
7340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
7440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org        canvas->restore();
7540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    }
7640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
7740eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgprivate:
7840eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org    typedef GM INHERITED;
7940eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org};
8040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
8140eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
8240eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
8340eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgstatic GM* MyFactory(void*) { return new ImageAlphaThresholdGM; }
8440eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.orgstatic GMRegistry reg(MyFactory);
8540eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org
8640eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org}
87