• 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 "SkCanvas.h"
10 #include "SkBlurImageFilter.h"
11 #include "SkRSXform.h"
12 #include "SkSurface.h"
13 
make_bm(SkBitmap * bm)14 static void make_bm(SkBitmap* bm) {
15     bm->allocN32Pixels(100, 100);
16     bm->eraseColor(SK_ColorBLUE);
17 
18     SkCanvas canvas(*bm);
19     SkPaint paint;
20     paint.setAntiAlias(true);
21     paint.setColor(SK_ColorRED);
22     canvas.drawCircle(50, 50, 50, paint);
23 }
24 
draw_1_bitmap(SkCanvas * canvas,const SkBitmap & bm,bool doClip,int dx,int dy,sk_sp<SkImageFilter> filter)25 static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
26                          int dx, int dy, sk_sp<SkImageFilter> filter) {
27     SkAutoCanvasRestore acr(canvas, true);
28     SkPaint paint;
29 
30     SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
31                                     SkIntToScalar(dy),
32                                     SkIntToScalar(bm.width()),
33                                     SkIntToScalar(bm.height()));
34 
35     paint.setImageFilter(std::move(filter));
36     clipR.inset(5, 5);
37 
38     canvas->translate(SkIntToScalar(bm.width() + 20), 0);
39 
40     if (doClip) {
41         canvas->save();
42         canvas->clipRect(clipR);
43     }
44     canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint);
45     if (doClip) {
46         canvas->restore();
47     }
48 }
49 
50 /**
51  *  Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
52  */
53 class SpriteBitmapGM : public skiagm::GM {
54 public:
SpriteBitmapGM()55     SpriteBitmapGM() {}
56 
57 protected:
58 
onShortName()59     SkString onShortName() override {
60         return SkString("spritebitmap");
61     }
62 
onISize()63     SkISize onISize() override {
64         return SkISize::Make(640, 480);
65     }
66 
onDraw(SkCanvas * canvas)67     void onDraw(SkCanvas* canvas) override {
68         SkBitmap bm;
69         make_bm(&bm);
70 
71         int dx = 10;
72         int dy = 10;
73 
74         SkScalar sigma = 8;
75         sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
76 
77         draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
78         dy += bm.height() + 20;
79         draw_1_bitmap(canvas, bm, false, dx, dy, filter);
80         dy += bm.height() + 20;
81         draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
82         dy += bm.height() + 20;
83         draw_1_bitmap(canvas, bm, true, dx, dy, filter);
84     }
85 
86 private:
87     typedef GM INHERITED;
88 };
89 DEF_GM( return new SpriteBitmapGM; )
90