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

Side by Side Diff: gm/lattice.cpp

Issue 2382893002: Add a src rect to drawImageLattice() API (Closed)
Patch Set: Simplify impl Created 4 years, 2 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
« no previous file with comments | « no previous file | include/core/SkCanvas.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 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 sk_sp<SkSurface> make_surface(SkCanvas* root, int N) { 11 static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int pad Top,
12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); 12 int padRight, int padBottom) {
13 SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + pa dTop + padBottom);
13 auto surface = root->makeSurface(info); 14 auto surface = root->makeSurface(info);
14 if (!surface) { 15 if (!surface) {
15 surface = SkSurface::MakeRaster(info); 16 surface = SkSurface::MakeRaster(info);
16 } 17 }
18
17 return surface; 19 return surface;
18 } 20 }
19 21
20 static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs) { 22 static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int pad Left, int padTop,
23 int padRight, int padBottom) {
21 const int kCap = 28; 24 const int kCap = 28;
22 const int kMid = 8; 25 const int kMid = 8;
23 const int kSize = 2*kCap + 3*kMid; 26 const int kSize = 2*kCap + 3*kMid;
24 27
25 auto surface(make_surface(root, kSize)); 28 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom) );
26 SkCanvas* canvas = surface->getCanvas(); 29 SkCanvas* canvas = surface->getCanvas();
30 canvas->translate((float) padLeft, (float) padTop);
27 31
28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); 32 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
29 const SkScalar strokeWidth = SkIntToScalar(6); 33 const SkScalar strokeWidth = SkIntToScalar(6);
30 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2; 34 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
31 35
32 xDivs[0] = yDivs[0] = kCap; 36 xDivs[0] = kCap + padLeft;
33 xDivs[1] = yDivs[1] = kCap + kMid; 37 yDivs[0] = kCap + padTop;
34 xDivs[2] = yDivs[2] = kCap + 2 * kMid; 38 xDivs[1] = kCap + kMid + padLeft;
35 xDivs[3] = yDivs[3] = kCap + 3 * kMid; 39 yDivs[1] = kCap + kMid + padTop;
40 xDivs[2] = kCap + 2 * kMid + padLeft;
41 yDivs[2] = kCap + 2 * kMid + padTop;
42 xDivs[3] = kCap + 3 * kMid + padLeft;
43 yDivs[3] = kCap + 3 * kMid + padTop;
36 44
37 SkPaint paint; 45 SkPaint paint;
38 paint.setAntiAlias(true); 46 paint.setAntiAlias(true);
39 47
40 paint.setColor(0xFFFFFF00); 48 paint.setColor(0xFFFFFF00);
41 canvas->drawRoundRect(r, radius, radius, paint); 49 canvas->drawRoundRect(r, radius, radius, paint);
42 50
43 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize)) ; 51 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize)) ;
44 paint.setColor(0x8800FF00); 52 paint.setColor(0x8800FF00);
45 canvas->drawRect(r, paint); 53 canvas->drawRect(r, paint);
(...skipping 30 matching lines...) Expand all
76 class LatticeGM : public skiagm::GM { 84 class LatticeGM : public skiagm::GM {
77 public: 85 public:
78 LatticeGM() {} 86 LatticeGM() {}
79 87
80 protected: 88 protected:
81 SkString onShortName() override { 89 SkString onShortName() override {
82 return SkString("lattice"); 90 return SkString("lattice");
83 } 91 }
84 92
85 SkISize onISize() override { 93 SkISize onISize() override {
86 return SkISize::Make(800, 400); 94 return SkISize::Make(800, 800);
87 } 95 }
88 96
89 void onDraw(SkCanvas* canvas) override { 97 void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, i nt padBottom) {
98 canvas->save();
99
90 int xDivs[5]; 100 int xDivs[5];
91 int yDivs[5]; 101 int yDivs[5];
92 xDivs[0] = 0; 102 xDivs[0] = padLeft;
93 yDivs[0] = 0; 103 yDivs[0] = padTop;
94 104
95 SkBitmap bitmap; 105 SkBitmap bitmap;
96 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1); 106 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
107 padRight, padBottom);
97 image_to_bitmap(image.get(), &bitmap); 108 image_to_bitmap(image.get(), &bitmap);
98 109
99 const SkTSize<SkScalar> size[] = { 110 const SkTSize<SkScalar> size[] = {
100 { 50, 50, }, // shrink in both axes 111 { 50, 50, }, // shrink in both axes
101 { 50, 200, }, // shrink in X 112 { 50, 200, }, // shrink in X
102 { 200, 50, }, // shrink in Y 113 { 200, 50, }, // shrink in Y
103 { 200, 200, }, 114 { 200, 200, },
104 }; 115 };
105 116
106 canvas->drawImage(image, 10, 10, nullptr); 117 canvas->drawImage(image, 10, 10, nullptr);
107 118
108 SkScalar x = SkIntToScalar(100); 119 SkScalar x = SkIntToScalar(100);
109 SkScalar y = SkIntToScalar(100); 120 SkScalar y = SkIntToScalar(100);
110 121
111 SkCanvas::Lattice lattice; 122 SkCanvas::Lattice lattice;
112 lattice.fXCount = 4; 123 lattice.fXCount = 4;
113 lattice.fXDivs = xDivs + 1; 124 lattice.fXDivs = xDivs + 1;
114 lattice.fYCount = 4; 125 lattice.fYCount = 4;
115 lattice.fYDivs = yDivs + 1; 126 lattice.fYDivs = yDivs + 1;
116 lattice.fFlags = nullptr; 127 lattice.fFlags = nullptr;
117 128
129 SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
130 image->width() - padRight, image->hei ght() - padBottom);
131 lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->heig ht())) ?
132 nullptr : &bounds;
133
118 for (int iy = 0; iy < 2; ++iy) { 134 for (int iy = 0; iy < 2; ++iy) {
119 for (int ix = 0; ix < 2; ++ix) { 135 for (int ix = 0; ix < 2; ++ix) {
120 int i = ix * 2 + iy; 136 int i = ix * 2 + iy;
121 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60, 137 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
122 size[i].width(), size[i].height()); 138 size[i].width(), size[i].height());
123 canvas->drawBitmapLattice(bitmap, lattice, r); 139 canvas->drawBitmapLattice(bitmap, lattice, r);
124 } 140 }
125 } 141 }
126 142
127 // Include the degenerate first div. While normally the first patch is "scalable", 143 // Include the degenerate first div. While normally the first patch is "scalable",
(...skipping 14 matching lines...) Expand all
142 158
143 canvas->translate(400, 0); 159 canvas->translate(400, 0);
144 for (int iy = 0; iy < 2; ++iy) { 160 for (int iy = 0; iy < 2; ++iy) {
145 for (int ix = 0; ix < 2; ++ix) { 161 for (int ix = 0; ix < 2; ++ix) {
146 int i = ix * 2 + iy; 162 int i = ix * 2 + iy;
147 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60, 163 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
148 size[i].width(), size[i].height()); 164 size[i].width(), size[i].height());
149 canvas->drawImageLattice(image.get(), lattice, r); 165 canvas->drawImageLattice(image.get(), lattice, r);
150 } 166 }
151 } 167 }
168
169 canvas->restore();
170 }
171
172 void onDraw(SkCanvas* canvas) override {
173 this->onDrawHelper(canvas, 0, 0, 0, 0);
174 canvas->translate(0.0f, 400.0f);
175 this->onDrawHelper(canvas, 3, 7, 4, 11);
152 } 176 }
153 177
154 private: 178 private:
155 typedef skiagm::GM INHERITED; 179 typedef skiagm::GM INHERITED;
156 }; 180 };
157 DEF_GM( return new LatticeGM; ) 181 DEF_GM( return new LatticeGM; )
OLDNEW
« no previous file with comments | « no previous file | include/core/SkCanvas.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698