1 /*
2  * Copyright 2015 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 "SkImageFilter.h"
10 #include "SkColorMatrixFilter.h"
11 
12 /**
13  *  Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see
14  *  that we apply the xfermode *after* the image has been created and filtered, and not during
15  *  the creation step (i.e. before it is filtered).
16  *
17  *  see skbug.com/3741
18  */
19 class ImageFilterXfermodeTestGM : public skiagm::GM {
20 protected:
onShortName()21     SkString onShortName() override {
22         return SkString("imagefilters_xfermodes");
23     }
24 
onISize()25     SkISize onISize() override { return SkISize::Make(480, 480); }
26 
doDraw(SkCanvas * canvas,SkXfermode::Mode mode,SkImageFilter * imf)27     void doDraw(SkCanvas* canvas, SkXfermode::Mode mode, SkImageFilter* imf) {
28         SkAutoCanvasRestore acr(canvas, true);
29         canvas->clipRect(SkRect::MakeWH(220, 220));
30 
31         // want to force a layer, so modes like DstIn can combine meaningfully, but the final
32         // image can still be shown against our default (opaque) background. non-opaque GMs
33         // are a lot more trouble to compare/triage.
34         canvas->saveLayer(NULL, NULL);
35         canvas->drawColor(SK_ColorGREEN);
36 
37         SkPaint paint;
38         paint.setAntiAlias(true);
39 
40         SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
41         SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
42 
43         paint.setColor(SK_ColorRED);
44         canvas->drawOval(r0, paint);
45 
46         paint.setColor(0x660000FF);
47         paint.setImageFilter(imf);
48         paint.setXfermodeMode(mode);
49         canvas->drawOval(r1, paint);
50     }
51 
onDraw(SkCanvas * canvas)52     void onDraw(SkCanvas* canvas) override {
53         canvas->translate(10, 10);
54 
55         // just need an imagefilter to trigger the code-path (which creates a tmp layer)
56         SkAutoTUnref<SkImageFilter> imf(SkImageFilter::CreateMatrixFilter(SkMatrix::I(),
57                                                                           kNone_SkFilterQuality));
58 
59         const SkXfermode::Mode modes[] = {
60             SkXfermode::kSrcATop_Mode, SkXfermode::kDstIn_Mode
61         };
62 
63         for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
64             canvas->save();
65             this->doDraw(canvas, modes[i], NULL);
66             canvas->translate(240, 0);
67             this->doDraw(canvas, modes[i], imf);
68             canvas->restore();
69 
70             canvas->translate(0, 240);
71         }
72     }
73 
74 private:
75     typedef GM INHERITED;
76 };
77 DEF_GM( return new ImageFilterXfermodeTestGM; )
78 
79