OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
10 #include "SkBitmapSource.h" | 10 #include "SkBitmapSource.h" |
11 | 11 |
12 // This GM exercises the SkBitmapSource ImageFilter class. | 12 // This GM exercises the SkBitmapSource ImageFilter class. |
13 | 13 |
14 class BitmapSourceGM : public skiagm::GM { | 14 class BitmapSourceGM : public skiagm::GM { |
15 public: | 15 public: |
16 BitmapSourceGM() { | 16 BitmapSourceGM() { |
17 } | 17 } |
18 | 18 |
19 protected: | 19 protected: |
20 SkString onShortName() override { | 20 SkString onShortName() override { |
21 return SkString("bitmapsource"); | 21 return SkString("bitmapsource"); |
22 } | 22 } |
23 | 23 |
24 void makeBitmap() { | |
25 fBitmap.allocN32Pixels(100, 100); | |
26 SkCanvas canvas(fBitmap); | |
27 canvas.clear(0x00000000); | |
28 SkPaint paint; | |
29 paint.setAntiAlias(true); | |
30 sk_tool_utils::set_portable_typeface(&paint); | |
31 paint.setColor(0xFFFFFFFF); | |
32 paint.setTextSize(SkIntToScalar(96)); | |
33 const char* str = "e"; | |
34 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70),
paint); | |
35 } | |
36 | |
37 SkISize onISize() override { return SkISize::Make(500, 150); } | 24 SkISize onISize() override { return SkISize::Make(500, 150); } |
38 | 25 |
39 void onOnceBeforeDraw() override { | 26 void onOnceBeforeDraw() override { |
40 this->makeBitmap(); | 27 fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20,
70, 96, "e"); |
41 } | 28 } |
42 | 29 |
43 static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkIma
geFilter* filter) { | 30 static void FillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkIma
geFilter* filter) { |
44 SkPaint paint; | 31 SkPaint paint; |
45 paint.setImageFilter(filter); | 32 paint.setImageFilter(filter); |
46 canvas->save(); | 33 canvas->save(); |
47 canvas->clipRect(clipRect); | 34 canvas->clipRect(clipRect); |
48 canvas->drawPaint(paint); | 35 canvas->drawPaint(paint); |
49 canvas->restore(); | 36 canvas->restore(); |
50 } | 37 } |
51 | 38 |
52 void onDraw(SkCanvas* canvas) override { | 39 void onDraw(SkCanvas* canvas) override { |
53 canvas->clear(SK_ColorBLACK); | 40 canvas->clear(SK_ColorBLACK); |
54 { | 41 { |
55 SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); | 42 SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
56 SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); | 43 SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); |
57 SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); | 44 SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); |
58 SkRect bounds; | 45 SkRect bounds; |
59 fBitmap.getBounds(&bounds); | 46 fBitmap.getBounds(&bounds); |
60 SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBit
map)); | 47 SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBit
map)); |
61 SkAutoTUnref<SkImageFilter> bitmapSourceSrcRect(SkBitmapSource::Crea
te(fBitmap, srcRect, srcRect)); | 48 SkAutoTUnref<SkImageFilter> bitmapSourceSrcRect(SkBitmapSource::Crea
te(fBitmap, srcRect, srcRect)); |
62 SkAutoTUnref<SkImageFilter> bitmapSourceSrcRectDstRect(SkBitmapSourc
e::Create(fBitmap, srcRect, dstRect)); | 49 SkAutoTUnref<SkImageFilter> bitmapSourceSrcRectDstRect(SkBitmapSourc
e::Create(fBitmap, srcRect, dstRect)); |
63 SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(SkBitmapSource::
Create(fBitmap, bounds, dstRect)); | 50 SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(SkBitmapSource::
Create(fBitmap, bounds, dstRect)); |
64 | 51 |
65 // Draw an unscaled bitmap. | 52 // Draw an unscaled bitmap. |
66 fillRectFiltered(canvas, clipRect, bitmapSource); | 53 FillRectFiltered(canvas, clipRect, bitmapSource); |
67 canvas->translate(SkIntToScalar(100), 0); | 54 canvas->translate(SkIntToScalar(100), 0); |
68 | 55 |
69 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect)
. | 56 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect)
. |
70 fillRectFiltered(canvas, clipRect, bitmapSourceSrcRect); | 57 FillRectFiltered(canvas, clipRect, bitmapSourceSrcRect); |
71 canvas->translate(SkIntToScalar(100), 0); | 58 canvas->translate(SkIntToScalar(100), 0); |
72 | 59 |
73 // Draw a subset of the bitmap scaled to a destination rect (srcRect
-> dstRect). | 60 // Draw a subset of the bitmap scaled to a destination rect (srcRect
-> dstRect). |
74 fillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect); | 61 FillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect); |
75 canvas->translate(SkIntToScalar(100), 0); | 62 canvas->translate(SkIntToScalar(100), 0); |
76 | 63 |
77 // Draw the entire bitmap scaled to a destination rect (bounds -> ds
tRect). | 64 // Draw the entire bitmap scaled to a destination rect (bounds -> ds
tRect). |
78 fillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly); | 65 FillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly); |
79 canvas->translate(SkIntToScalar(100), 0); | 66 canvas->translate(SkIntToScalar(100), 0); |
80 } | 67 } |
81 } | 68 } |
82 | 69 |
83 private: | 70 private: |
84 SkBitmap fBitmap; | 71 SkBitmap fBitmap; |
85 typedef GM INHERITED; | 72 typedef GM INHERITED; |
86 }; | 73 }; |
87 | 74 |
88 /////////////////////////////////////////////////////////////////////////////// | 75 /////////////////////////////////////////////////////////////////////////////// |
89 | 76 |
90 DEF_GM( return new BitmapSourceGM; ) | 77 DEF_GM( return new BitmapSourceGM; ) |
OLD | NEW |