• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "Resources.h"
10 
11 #include "SkImageDecoder.h"
12 #include "SkPixelXorXfermode.h"
13 #include "SkStream.h"
14 
15 class PixelXorXfermodeGM : public skiagm::GM {
16 public:
PixelXorXfermodeGM()17     PixelXorXfermodeGM() { }
18 
19 protected:
onShortName()20     SkString onShortName() override {
21         return SkString("pixelxorxfermode");
22     }
23 
onISize()24     SkISize onISize() override { return SkISize::Make(512, 512); }
25 
onOnceBeforeDraw()26     void onOnceBeforeDraw() override {
27         SkImageDecoder* codec = nullptr;
28         SkString resourcePath = GetResourcePath("mandrill_512.png");
29         SkFILEStream stream(resourcePath.c_str());
30         if (stream.isValid()) {
31             codec = SkImageDecoder::Factory(&stream);
32         }
33         if (codec) {
34             stream.rewind();
35             codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
36             delete codec;
37         } else {
38             fBM.allocN32Pixels(1, 1);
39             fBM.eraseARGB(255, 255, 0 , 0); // red == bad
40         }
41     }
42 
onDraw(SkCanvas * canvas)43     void onDraw(SkCanvas* canvas) override {
44         canvas->drawBitmap(fBM, 0, 0);
45 
46         SkRect r = SkRect::MakeIWH(256, 256);
47 
48         // Negate the red channel of the dst (via the ancillary color) but leave
49         // the green & blue channels alone
50         SkPaint p1;
51         p1.setColor(SK_ColorBLACK); // noop
52         p1.setXfermode(SkPixelXorXfermode::Create(0x7FFF0000))->unref();
53 
54         canvas->drawRect(r, p1);
55 
56         r.offsetTo(256.0f, 0.0f);
57 
58         // Negate the dst color via the src color
59         SkPaint p2;
60         p2.setColor(SK_ColorWHITE);
61         p2.setXfermode(SkPixelXorXfermode::Create(SK_ColorBLACK))->unref(); // noop
62 
63         canvas->drawRect(r, p2);
64 
65         r.offsetTo(0.0f, 256.0f);
66 
67         // Just return the original color
68         SkPaint p3;
69         p3.setColor(SK_ColorBLACK); // noop
70         p3.setXfermode(SkPixelXorXfermode::Create(SK_ColorBLACK))->unref(); // noop
71 
72         canvas->drawRect(r, p3);
73 
74         r.offsetTo(256.0f, 256.0f);
75 
76         // Negate the red & green channels (via the ancillary color) but leave
77         // the blue channel alone
78         SkPaint p4;
79         p4.setColor(SK_ColorBLACK); // noop
80         p4.setXfermode(SkPixelXorXfermode::Create(SK_ColorYELLOW))->unref();
81 
82         canvas->drawRect(r, p4);
83     }
84 
85 private:
86     SkBitmap fBM;
87 
88     typedef GM INHERITED;
89 };
90 
91 //////////////////////////////////////////////////////////////////////////////
92 
93 DEF_GM(return new PixelXorXfermodeGM;)
94