• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 "gm.h"
9 #include "SkAlphaThresholdFilter.h"
10 #include "SkRandom.h"
11 
12 #define WIDTH 500
13 #define HEIGHT 500
14 
15 namespace skiagm {
16 
17 class ImageAlphaThresholdGM : public GM {
18 public:
ImageAlphaThresholdGM()19     ImageAlphaThresholdGM() {
20         this->setBGColor(0xFFFFFFFF);
21     }
22 
23 protected:
onGetFlags() const24     virtual uint32_t onGetFlags() const SK_OVERRIDE {
25         return this->INHERITED::onGetFlags() |
26                GM::kSkipTiled_Flag |
27                GM::kSkipPicture_Flag |
28                GM::kSkipPipe_Flag |
29                GM::kSkipPipeCrossProcess_Flag;
30     }
31 
onShortName()32     virtual SkString onShortName() SK_OVERRIDE {
33         return SkString("imagealphathreshold");
34     }
35 
onISize()36     virtual SkISize onISize() SK_OVERRIDE {
37         return SkISize::Make(WIDTH, HEIGHT);
38     }
39 
onDraw(SkCanvas * canvas)40     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
41         SkIRect rects[2];
42         rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
43         rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
44         SkRegion region;
45         region.setRects(rects, 2);
46 
47         SkMatrix matrix;
48         matrix.reset();
49         matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
50         matrix.postScale(.8f, .8f);
51 
52         canvas->concat(matrix);
53 
54         SkPaint paint;
55         paint.setImageFilter(
56             SkAlphaThresholdFilter::Create(region, 0.2f, 0.7f))->unref();
57         canvas->saveLayer(NULL, &paint);
58         paint.setAntiAlias(true);
59 
60         SkPaint rect_paint;
61         rect_paint.setColor(0xFF0000FF);
62         canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2),
63                          rect_paint);
64         rect_paint.setColor(0xBFFF0000);
65         canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2),
66                          rect_paint);
67         rect_paint.setColor(0x3F00FF00);
68         canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2),
69                          rect_paint);
70         rect_paint.setColor(0x00000000);
71         canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2),
72                          rect_paint);
73 
74         canvas->restore();
75     }
76 
77 private:
78     typedef GM INHERITED;
79 };
80 
81 //////////////////////////////////////////////////////////////////////////////
82 
MyFactory(void *)83 static GM* MyFactory(void*) { return new ImageAlphaThresholdGM; }
84 static GMRegistry reg(MyFactory);
85 
86 }
87