OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include "SkSurface.h" | 9 #include "SkSurface.h" |
10 | 10 |
11 static SkSurface* make_surface(SkCanvas* root, int N) { | 11 static SkSurface* make_surface(SkCanvas* root, int N) { |
12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); | 12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); |
13 SkSurface* surface = root->newSurface(info); | 13 SkSurface* surface = root->newSurface(info); |
14 if (!surface) { | 14 if (!surface) { |
15 surface = SkSurface::NewRaster(info); | 15 surface = SkSurface::NewRaster(info); |
16 } | 16 } |
17 return surface; | 17 return surface; |
18 } | 18 } |
19 | 19 |
20 static SkImage* make_image(SkCanvas* root, SkIRect* center) { | 20 static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) { |
21 const int kFixed = 28; | 21 const int kFixed = 28; |
22 const int kStretchy = 8; | 22 const int kStretchy = 8; |
23 const int kSize = 2*kFixed + kStretchy; | 23 const int kSize = 2*kFixed + kStretchy; |
24 | 24 |
25 SkAutoTUnref<SkSurface> surface(make_surface(root, kSize)); | 25 SkAutoTUnref<SkSurface> surface(make_surface(root, kSize)); |
26 SkCanvas* canvas = surface->getCanvas(); | 26 SkCanvas* canvas = surface->getCanvas(); |
27 | 27 |
28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); | 28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
29 const SkScalar strokeWidth = SkIntToScalar(6); | 29 const SkScalar strokeWidth = SkIntToScalar(6); |
30 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; | 30 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
31 | 31 |
32 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); | 32 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
33 | 33 |
34 SkPaint paint; | 34 SkPaint paint; |
35 paint.setAntiAlias(true); | 35 paint.setAntiAlias(true); |
36 | 36 |
37 paint.setColor(0xFFFF0000); | 37 paint.setColor(0xFFFF0000); |
38 canvas->drawRoundRect(r, radius, radius, paint); | 38 canvas->drawRoundRect(r, radius, radius, paint); |
39 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(
kSize)); | 39 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(
kSize)); |
40 paint.setColor(0x8800FF00); | 40 paint.setColor(0x8800FF00); |
41 canvas->drawRect(r, paint); | 41 canvas->drawRect(r, paint); |
42 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStr
etchy)); | 42 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStr
etchy)); |
43 paint.setColor(0x880000FF); | 43 paint.setColor(0x880000FF); |
44 canvas->drawRect(r, paint); | 44 canvas->drawRect(r, paint); |
45 | 45 |
46 return surface->newImageSnapshot(); | 46 return surface->makeImageSnapshot(); |
47 } | 47 } |
48 | 48 |
49 static void image_to_bitmap(const SkImage* image, SkBitmap* bm) { | 49 static void image_to_bitmap(const SkImage* image, SkBitmap* bm) { |
50 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height(
)); | 50 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height(
)); |
51 bm->allocPixels(info); | 51 bm->allocPixels(info); |
52 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0); | 52 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0); |
53 } | 53 } |
54 | 54 |
55 class NinePatchStretchGM : public skiagm::GM { | 55 class NinePatchStretchGM : public skiagm::GM { |
56 public: | 56 public: |
57 SkAutoTUnref<SkImage> fImage; | 57 sk_sp<SkImage> fImage; |
58 SkBitmap fBitmap; | 58 SkBitmap fBitmap; |
59 SkIRect fCenter; | 59 SkIRect fCenter; |
60 | 60 |
61 NinePatchStretchGM() {} | 61 NinePatchStretchGM() {} |
62 | 62 |
63 protected: | 63 protected: |
64 SkString onShortName() override { | 64 SkString onShortName() override { |
65 return SkString("ninepatch-stretch"); | 65 return SkString("ninepatch-stretch"); |
66 } | 66 } |
67 | 67 |
68 SkISize onISize() override { | 68 SkISize onISize() override { |
69 return SkISize::Make(760, 800); | 69 return SkISize::Make(760, 800); |
70 } | 70 } |
71 | 71 |
72 void onDraw(SkCanvas* canvas) override { | 72 void onDraw(SkCanvas* canvas) override { |
73 if (nullptr == fBitmap.pixelRef()) { | 73 if (nullptr == fBitmap.pixelRef()) { |
74 fImage.reset(make_image(canvas, &fCenter)); | 74 fImage = make_image(canvas, &fCenter); |
75 image_to_bitmap(fImage, &fBitmap); | 75 image_to_bitmap(fImage.get(), &fBitmap); |
76 } | 76 } |
77 | 77 |
78 // amount of bm that should not be stretched (unless we have to) | 78 // amount of bm that should not be stretched (unless we have to) |
79 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width()); | 79 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width()); |
80 | 80 |
81 const SkTSize<SkScalar> size[] = { | 81 const SkTSize<SkScalar> size[] = { |
82 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes | 82 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes |
83 { fixed * 4 / 5, fixed * 4 }, // shrink in X | 83 { fixed * 4 / 5, fixed * 4 }, // shrink in X |
84 { fixed * 4, fixed * 4 / 5 }, // shrink in Y | 84 { fixed * 4, fixed * 4 / 5 }, // shrink in Y |
85 { fixed * 4, fixed * 4 } | 85 { fixed * 4, fixed * 4 } |
86 }; | 86 }; |
87 | 87 |
88 canvas->drawBitmap(fBitmap, 10, 10, nullptr); | 88 canvas->drawBitmap(fBitmap, 10, 10, nullptr); |
89 | 89 |
90 SkScalar x = SkIntToScalar(100); | 90 SkScalar x = SkIntToScalar(100); |
91 SkScalar y = SkIntToScalar(100); | 91 SkScalar y = SkIntToScalar(100); |
92 | 92 |
93 SkPaint paint; | 93 SkPaint paint; |
94 for (int filter = 0; filter < 2; filter++) { | 94 for (int filter = 0; filter < 2; filter++) { |
95 paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_Sk
FilterQuality); | 95 paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_Sk
FilterQuality); |
96 canvas->translate(0, filter * SkIntToScalar(400)); | 96 canvas->translate(0, filter * SkIntToScalar(400)); |
97 for (int iy = 0; iy < 2; ++iy) { | 97 for (int iy = 0; iy < 2; ++iy) { |
98 for (int ix = 0; ix < 2; ++ix) { | 98 for (int ix = 0; ix < 2; ++ix) { |
99 int i = ix * 2 + iy; | 99 int i = ix * 2 + iy; |
100 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, | 100 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, |
101 size[i].width(), size[i].height(
)); | 101 size[i].width(), size[i].height(
)); |
102 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint); | 102 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint); |
103 canvas->drawImageNine(fImage, fCenter, r.makeOffset(360, 0),
&paint); | 103 canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(36
0, 0), &paint); |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 private: | 109 private: |
110 typedef skiagm::GM INHERITED; | 110 typedef skiagm::GM INHERITED; |
111 }; | 111 }; |
112 DEF_GM( return new NinePatchStretchGM; ) | 112 DEF_GM( return new NinePatchStretchGM; ) |
113 | 113 |
OLD | NEW |