• 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 "sk_tool_utils.h"
9 #include "SkBitmapSource.h"
10 #include "SkOffsetImageFilter.h"
11 #include "gm.h"
12 
13 #define WIDTH 600
14 #define HEIGHT 100
15 #define MARGIN 12
16 
17 namespace skiagm {
18 
19 class OffsetImageFilterGM : public GM {
20 public:
OffsetImageFilterGM()21     OffsetImageFilterGM() : fInitialized(false) {
22         this->setBGColor(0xFF000000);
23     }
24 
25 protected:
onShortName()26     virtual SkString onShortName() {
27         return SkString("offsetimagefilter");
28     }
29 
make_bitmap()30     void make_bitmap() {
31         fBitmap.allocN32Pixels(80, 80);
32         SkCanvas canvas(fBitmap);
33         canvas.clear(0x00000000);
34         SkPaint paint;
35         paint.setAntiAlias(true);
36         sk_tool_utils::set_portable_typeface(&paint);
37         paint.setColor(0xD000D000);
38         paint.setTextSize(SkIntToScalar(96));
39         const char* str = "e";
40         canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
41     }
42 
onISize()43     virtual SkISize onISize() {
44         return SkISize::Make(WIDTH, HEIGHT);
45     }
46 
drawClippedBitmap(SkCanvas * canvas,const SkBitmap & bitmap,const SkPaint & paint,SkScalar scale,const SkIRect & cropRect)47     void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint, SkScalar scale, const SkIRect& cropRect) {
48         canvas->save();
49         SkRect clipRect = SkRect::MakeWH(
50             SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
51         canvas->clipRect(clipRect);
52         canvas->scale(scale, scale);
53         canvas->drawBitmap(bitmap, 0, 0, &paint);
54         canvas->restore();
55         SkPaint strokePaint;
56         strokePaint.setStyle(SkPaint::kStroke_Style);
57         strokePaint.setColor(SK_ColorRED);
58 
59         // Draw a boundary rect around the intersection of the clip rect
60         // and crop rect.
61         SkMatrix scaleMatrix;
62         scaleMatrix.setScale(scale, scale);
63         SkRect cropRectFloat;
64         scaleMatrix.mapRect(&cropRectFloat, SkRect::Make(cropRect));
65         if (clipRect.intersect(cropRectFloat)) {
66             canvas->drawRect(clipRect, strokePaint);
67         }
68     }
69 
onDraw(SkCanvas * canvas)70     virtual void onDraw(SkCanvas* canvas) {
71         if (!fInitialized) {
72             make_bitmap();
73 
74             fCheckerboard.allocN32Pixels(80, 80);
75             SkCanvas checkerboardCanvas(fCheckerboard);
76             sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
77 
78             fInitialized = true;
79         }
80         canvas->clear(SK_ColorBLACK);
81         SkPaint paint;
82 
83         for (int i = 0; i < 4; i++) {
84             SkBitmap* bitmap = (i & 0x01) ? &fCheckerboard : &fBitmap;
85             SkIRect cropRect = SkIRect::MakeXYWH(i * 12,
86                                                  i * 8,
87                                                  bitmap->width() - i * 8,
88                                                  bitmap->height() - i * 12);
89             SkImageFilter::CropRect rect(SkRect::Make(cropRect));
90             SkAutoTUnref<SkImageFilter> tileInput(SkBitmapSource::Create(*bitmap));
91             SkScalar dx = SkIntToScalar(i*5);
92             SkScalar dy = SkIntToScalar(i*10);
93             SkAutoTUnref<SkImageFilter> filter(
94                 SkOffsetImageFilter::Create(dx, dy, tileInput, &rect));
95             paint.setImageFilter(filter);
96             drawClippedBitmap(canvas, *bitmap, paint, SK_Scalar1, cropRect);
97             canvas->translate(SkIntToScalar(bitmap->width() + MARGIN), 0);
98         }
99 
100         SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100);
101         SkImageFilter::CropRect rect(SkRect::Make(cropRect));
102         SkAutoTUnref<SkImageFilter> filter(
103             SkOffsetImageFilter::Create(SkIntToScalar(-5), SkIntToScalar(-10), NULL, &rect));
104         paint.setImageFilter(filter);
105         drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(2), cropRect);
106     }
107 private:
108     typedef GM INHERITED;
109     SkBitmap fBitmap, fCheckerboard;
110     bool fInitialized;
111 };
112 
113 //////////////////////////////////////////////////////////////////////////////
114 
MyFactory(void *)115 static GM* MyFactory(void*) { return new OffsetImageFilterGM; }
116 static GMRegistry reg(MyFactory);
117 
118 }
119