Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: gm/ninepatchmultistretch.cpp

Issue 1992283002: Add drawBitmapLattice() API (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add implementation for SkRecorder and SkPictureRecord Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "SkSurface.h"
10
11 static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
13 auto surface = root->makeSurface(info);
14 if (!surface) {
15 surface = SkSurface::MakeRaster(info);
16 }
17 return surface;
18 }
19
20 static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs) {
21 const int kCap = 28;
22 const int kMid = 8;
23 const int kSize = 2*kCap + 3*kMid;
24
25 auto surface(make_surface(root, kSize));
26 SkCanvas* canvas = surface->getCanvas();
27
28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
29 const SkScalar strokeWidth = SkIntToScalar(6);
30 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
31
32 xDivs[0] = yDivs[0] = kCap;
33 xDivs[1] = yDivs[1] = kCap + kMid;
34 xDivs[2] = yDivs[2] = kCap + 2 * kMid;
35 xDivs[3] = yDivs[3] = kCap + 3 * kMid;
36
37 SkPaint paint;
38 paint.setAntiAlias(true);
39
40 paint.setColor(0xFFFFFF00);
41 canvas->drawRoundRect(r, radius, radius, paint);
42
43 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize)) ;
44 paint.setColor(0x8800FF00);
45 canvas->drawRect(r, paint);
46 r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar( kSize));
47 paint.setColor(0x880000FF);
48 canvas->drawRect(r, paint);
49 r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScala r(kSize));
50 paint.setColor(0x88FF00FF);
51 canvas->drawRect(r, paint);
52
53 r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid)) ;
54 paint.setColor(0x8800FF00);
55 canvas->drawRect(r, paint);
56 r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar (kMid));
57 paint.setColor(0x880000FF);
58 canvas->drawRect(r, paint);
59 r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScal ar(kMid));
60 paint.setColor(0x88FF00FF);
61 canvas->drawRect(r, paint);
62
63 return surface->makeImageSnapshot();
64 }
65
66 static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
67 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height( ));
68 bm->allocPixels(info);
69 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
70 }
71
72 /**
73 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
74 * than nine patches.
75 */
76 class NinePatchMultiStretchGM : public skiagm::GM {
77 public:
78 NinePatchMultiStretchGM() {}
79
80 protected:
81 SkString onShortName() override {
82 return SkString("ninepatch-multistretch");
83 }
84
85 SkISize onISize() override {
86 return SkISize::Make(800, 400);
87 }
88
89 void onDraw(SkCanvas* canvas) override {
90 int xDivs[5];
91 int yDivs[5];
92 xDivs[0] = 0;
93 yDivs[0] = 0;
94
95 if (nullptr == fBitmap.pixelRef()) {
96 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1);
97 image_to_bitmap(image.get(), &fBitmap);
98 }
99
100 const SkTSize<SkScalar> size[] = {
101 { 50, 50, }, // shrink in both axes
102 { 50, 200, }, // shrink in X
103 { 200, 50, }, // shrink in Y
104 { 200, 200, },
105 };
106
107 canvas->drawBitmap(fBitmap, 10, 10, nullptr);
108
109 SkScalar x = SkIntToScalar(100);
110 SkScalar y = SkIntToScalar(100);
111
112 SkCanvas::NinePatchDivs divs;
113 divs.fXCount = 4;
114 divs.fXDivs = xDivs + 1;
115 divs.fYCount = 4;
116 divs.fYDivs = yDivs + 1;
117
118 for (int iy = 0; iy < 2; ++iy) {
119 for (int ix = 0; ix < 2; ++ix) {
120 int i = ix * 2 + iy;
121 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
122 size[i].width(), size[i].height());
123 canvas->drawBitmapNine(fBitmap, divs, r);
124 }
125 }
126
127 // Include the degenerate first div. While normally the first patch is "scalable",
128 // this will mean that the first non-degenerate patch is "fixed".
129 divs.fXCount = 5;
130 divs.fXDivs = xDivs;
131 divs.fYCount = 5;
132 divs.fYDivs = yDivs;
133
134 canvas->translate(400, 0);
135 for (int iy = 0; iy < 2; ++iy) {
136 for (int ix = 0; ix < 2; ++ix) {
137 int i = ix * 2 + iy;
138 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
139 size[i].width(), size[i].height());
140 canvas->drawBitmapNine(fBitmap, divs, r);
141 }
142 }
143 }
144
145 private:
146 SkBitmap fBitmap;
147
148 typedef skiagm::GM INHERITED;
149 };
150 DEF_GM( return new NinePatchMultiStretchGM; )
OLDNEW
« no previous file with comments | « bench/NinePatchBench.cpp ('k') | include/core/SkCanvas.h » ('j') | include/core/SkCanvas.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698