aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/lattice.cpp
blob: b2809105bc555bbb631ebc60f74c1e405d6f6e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkSurface.h"
#include "sk_tool_utils.h"

static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop,
                                     int padRight, int padBottom) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom);
    return sk_tool_utils::makeSurface(root, info);
}

static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop,
                                 int padRight, int padBottom) {
    const int kCap = 28;
    const int kMid = 8;
    const int kSize = 2*kCap + 3*kMid;

    auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
    SkCanvas* canvas = surface->getCanvas();
    canvas->translate((float) padLeft, (float) padTop);

    SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
    const SkScalar strokeWidth = SkIntToScalar(6);
    const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;

    xDivs[0] = kCap + padLeft;
    yDivs[0] = kCap + padTop;
    xDivs[1] = kCap + kMid + padLeft;
    yDivs[1] = kCap + kMid + padTop;
    xDivs[2] = kCap + 2 * kMid + padLeft;
    yDivs[2] = kCap + 2 * kMid + padTop;
    xDivs[3] = kCap + 3 * kMid + padLeft;
    yDivs[3] = kCap + 3 * kMid + padTop;

    SkPaint paint;
    paint.setAntiAlias(true);

    paint.setColor(0xFFFFFF00);
    canvas->drawRoundRect(r, radius, radius, paint);

    r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
    paint.setColor(0x8800FF00);
    canvas->drawRect(r, paint);
    r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
    paint.setColor(0x880000FF);
    canvas->drawRect(r, paint);
    r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
    paint.setColor(0x88FF00FF);
    canvas->drawRect(r, paint);

    r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid));
    paint.setColor(0x8800FF00);
    canvas->drawRect(r, paint);
    r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
    paint.setColor(0x880000FF);
    canvas->drawRect(r, paint);
    r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
    paint.setColor(0x88FF00FF);
    canvas->drawRect(r, paint);

    return surface->makeImageSnapshot();
}

static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
    bm->allocPixels(info);
    image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
}

/**
 *  This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
 *  than nine patches.
 */
class LatticeGM : public skiagm::GM {
public:
    LatticeGM() {}

protected:
    SkString onShortName() override {
        return SkString("lattice");
    }

    SkISize onISize() override {
        return SkISize::Make(800, 800);
    }

    void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom) {
        canvas->save();

        int xDivs[5];
        int yDivs[5];
        xDivs[0] = padLeft;
        yDivs[0] = padTop;

        SkBitmap bitmap;
        sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
                                          padRight, padBottom);
        image_to_bitmap(image.get(), &bitmap);

        const SkSize size[] = {
            {  50,  50, }, // shrink in both axes
            {  50, 200, }, // shrink in X
            { 200,  50, }, // shrink in Y
            { 200, 200, },
        };

        canvas->drawImage(image, 10, 10, nullptr);

        SkScalar x = SkIntToScalar(100);
        SkScalar y = SkIntToScalar(100);

        SkCanvas::Lattice lattice;
        lattice.fXCount = 4;
        lattice.fXDivs = xDivs + 1;
        lattice.fYCount = 4;
        lattice.fYDivs = yDivs + 1;
        lattice.fRectTypes = nullptr;
        lattice.fColors = nullptr;

        SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
                                           image->width() - padRight, image->height() - padBottom);
        lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ?
                nullptr : &bounds;

        for (int iy = 0; iy < 2; ++iy) {
            for (int ix = 0; ix < 2; ++ix) {
                int i = ix * 2 + iy;
                SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
                                            size[i].width(), size[i].height());
                canvas->drawBitmapLattice(bitmap, lattice, r);
            }
        }

        // Provide hints about 3 solid color rects. These colors match
        // what was already in the bitmap.
        int fixedColorX[3] = {2, 4, 1};
        int fixedColorY[3] = {1, 1, 2};
        SkColor fixedColor[3] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
        const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType,
                                                   kUnpremul_SkAlphaType);
        for (int rectNum = 0; rectNum < 3; rectNum++) {
            int srcX = xDivs[fixedColorX[rectNum]-1];
            int srcY = yDivs[fixedColorY[rectNum]-1];
            image->readPixels(info, &fixedColor[rectNum], 4, srcX, srcY);
        }

        // Include the degenerate first div.  While normally the first patch is "scalable",
        // this will mean that the first non-degenerate patch is "fixed".
        lattice.fXCount = 5;
        lattice.fXDivs = xDivs;
        lattice.fYCount = 5;
        lattice.fYDivs = yDivs;

        // Let's skip a few rects.
        SkCanvas::Lattice::RectType flags[36];
        sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::RectType));
        flags[4] = SkCanvas::Lattice::kTransparent;
        flags[9] = SkCanvas::Lattice::kTransparent;
        flags[12] = SkCanvas::Lattice::kTransparent;
        flags[19] = SkCanvas::Lattice::kTransparent;
        for (int rectNum = 0; rectNum < 3; rectNum++) {
            flags[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
                   = SkCanvas::Lattice::kFixedColor;
        }
        lattice.fRectTypes = flags;

        SkColor colors[36];
        sk_bzero(colors, 36 * sizeof(SkColor));
        for (int rectNum = 0; rectNum < 3; rectNum++) {
            colors[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
                   = fixedColor[rectNum];
        }

        lattice.fColors = colors;

        canvas->translate(400, 0);
        for (int iy = 0; iy < 2; ++iy) {
            for (int ix = 0; ix < 2; ++ix) {
                int i = ix * 2 + iy;
                SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
                                            size[i].width(), size[i].height());
                canvas->drawImageLattice(image.get(), lattice, r);
            }
        }

        canvas->restore();
    }

    void onDraw(SkCanvas* canvas) override {
        this->onDrawHelper(canvas, 0, 0, 0, 0);
        canvas->translate(0.0f, 400.0f);
        this->onDrawHelper(canvas, 3, 7, 4, 11);
    }

private:
    typedef skiagm::GM INHERITED;
};
DEF_GM( return new LatticeGM; )


// LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
class LatticeGM2 : public skiagm::GM {
public:
    LatticeGM2() {}
    SkString onShortName() override {
        return SkString("lattice2");
    }

    SkISize onISize() override {
        return SkISize::Make(800, 800);
    }

    sk_sp<SkImage> makeImage(SkCanvas* root, int padLeft, int padTop, int padRight, int padBottom) {
        const int kSize = 80;
        auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
        SkCanvas* canvas = surface->getCanvas();;
        SkPaint paint;
        paint.setAntiAlias(false);
        SkRect r;

        //first line
        r.setXYWH(0, 0, 4, 1);  //4x1 green rect
        paint.setColor(0xFF00FF00);
        canvas->drawRect(r, paint);

        r.setXYWH(4, 0, 1, 1); //1x1 blue pixel -> draws as rectangle
        paint.setColor(0xFF0000FF);
        canvas->drawRect(r, paint);

        r.setXYWH(5, 0, kSize-5, 1); //the rest of the line is red
        paint.setColor(0xFFFF0000);
        canvas->drawRect(r, paint);


        //second line -> draws as fixed color rectangles
        r.setXYWH(0, 1, 4, 1);  //4x1 red rect
        paint.setColor(0xFFFF0000);
        canvas->drawRect(r, paint);

        r.setXYWH(4, 1, 1, 1); //1x1 blue pixel with alpha
        paint.setColor(0x880000FF);
        canvas->drawRect(r, paint);

        r.setXYWH(5, 1, kSize-5, 1); //the rest of the line is green
        paint.setColor(0xFF00FF00);
        canvas->drawRect(r, paint);


        //third line - does not draw, because it is transparent
        r.setXYWH(0, 2, 4, kSize-2);  //4x78 green rect
        paint.setColor(0xFF00FF00);
        canvas->drawRect(r, paint);

        r.setXYWH(4, 2, 1, kSize-2); //1x78 red pixel with alpha
        paint.setColor(0x88FF0000);
        canvas->drawRect(r, paint);

        r.setXYWH(5, 2, kSize-5, kSize-2); //the rest of the image is blue
        paint.setColor(0xFF0000FF);
        canvas->drawRect(r, paint);

        return surface->makeImageSnapshot();
    }

    void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom,
                      SkPaint& paint) {
        int xDivs[2] = {4, 5};
        int yDivs[2] = {1, 2};

        canvas->save();

        sk_sp<SkImage> image = makeImage(canvas, padLeft, padTop, padRight, padBottom);

        canvas->drawImage(image, 10, 10, nullptr);

        SkCanvas::Lattice lattice;
        lattice.fXCount = 2;
        lattice.fXDivs = xDivs;
        lattice.fYCount = 2;
        lattice.fYDivs = yDivs;
        lattice.fBounds = nullptr;

        SkCanvas::Lattice::RectType flags[9];
        sk_bzero(flags, 9 * sizeof(SkCanvas::Lattice::RectType));
        flags[3] = SkCanvas::Lattice::kFixedColor;
        flags[4] = SkCanvas::Lattice::kFixedColor;
        flags[5] = SkCanvas::Lattice::kFixedColor;

        flags[6] = SkCanvas::Lattice::kTransparent;
        flags[7] = SkCanvas::Lattice::kTransparent;
        flags[8] = SkCanvas::Lattice::kTransparent;
        lattice.fRectTypes = flags;

        SkColor colors[9] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK,
                             0xFFFF0000, 0x880000FF, 0xFF00FF00,
                             SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
        lattice.fColors = colors;
        paint.setColor(0xFFFFFFFF);
        canvas->drawImageLattice(image.get(), lattice,
                                 SkRect::MakeXYWH(100, 100, 200, 200), &paint);

        //draw the same content with alpha
        canvas->translate(400, 0);
        paint.setColor(0x80000FFF);
        canvas->drawImageLattice(image.get(), lattice,
                                 SkRect::MakeXYWH(100, 100, 200, 200), &paint);

        canvas->restore();
    }

    void onDraw(SkCanvas* canvas) override {

        //draw a rectangle in the background with transparent pixels
        SkPaint paint;
        paint.setColor(0x7F123456);
        paint.setBlendMode(SkBlendMode::kSrc);
        canvas->drawRect( SkRect::MakeXYWH(300, 0, 300, 800), paint);

        //draw image lattice with kSrcOver blending
        paint.setBlendMode(SkBlendMode::kSrcOver);
        this->onDrawHelper(canvas, 0, 0, 0, 0, paint);

        //draw image lattice with kSrcATop blending
        canvas->translate(0.0f, 400.0f);
        paint.setBlendMode(SkBlendMode::kSrcATop);
        this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
    }

private:
    typedef skiagm::GM INHERITED;
};
DEF_GM( return new LatticeGM2; )