summaryrefslogtreecommitdiffstatsci
path: root/src/skia-shapes.cpp
blob: 7271e3dd68566c0d3a127a6080f5c1e11c157ff4 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright 2014 © Samsung Research America, Silicon Valley
 *
 * Use of this source code is governed by the 3-Clause BSD license
 * specified in the COPYING file included with this source code.
 */
#include <config.h>

#include <SkCanvas.h>
#include <SkShader.h>
#include <SkImageDecoder.h>
#include <SkGradientShader.h>
#include <SkDashPathEffect.h>

#include "forward.h"
#include "caskbench.h"
#include "caskbench_context.h"
#include "skia-shapes.h"

SkPath path;
#ifndef SkToS32
int32_t SkToS32(intmax_t x) { return (int32_t)x; }
#endif

bool gPrintInstCount = false;

SkColor
skiaRandomColor()
{
    unsigned int red, blue, green, alpha;
    red = int( 255 * (double)rnd()/RAND_MAX );
    green = int( 255 * (double)rnd()/RAND_MAX );
    blue = int( 255 * (double)rnd()/RAND_MAX );
    alpha = int( 255 * (double)rnd()/RAND_MAX );
    return SkColorSetARGB(alpha, red, green, blue);
}

void
skiaRandomizePaintColor(caskbench_context_t *ctx)
{
    ctx->skia_paint->setColor(skiaRandomColor());
}

void
sk_set_fill_style(caskbench_context_t *ctx, const shapes_t *shape)
{
    SkShader* shader = NULL;
    switch (shape->fill_type) {
        case CB_FILL_NONE:
            ctx->skia_paint->setStyle(SkPaint::kStroke_Style);
            break;
        case CB_FILL_SOLID:
            if (shape->stroke_width <= 0)
                ctx->skia_paint->setStyle(SkPaint::kFill_Style);
            else
                ctx->skia_paint->setStyle(SkPaint::kStrokeAndFill_Style);
            break;
        case CB_FILL_LINEAR_GRADIENT:
            if(shape->shape_type == CB_SHAPE_STAR || shape->shape_type == CB_SHAPE_CIRCLE)
                shader = skiaCreateLinearGradientShader(shape->y-shape->radius, shape->y + shape->radius);
            else
                shader = skiaCreateLinearGradientShader(shape->y, shape->y + shape->height);
            ctx->skia_paint->setStyle(SkPaint::kFill_Style);
            ctx->skia_paint->setShader(shader);
            break;
        case CB_FILL_RADIAL_GRADIENT:
            shader = skiaCreateRadialGradientShader(shape);
            ctx->skia_paint->setStyle(SkPaint::kFill_Style);
            ctx->skia_paint->setShader(shader);
            break;
        case CB_FILL_IMAGE_PATTERN:
            if (ctx->stock_image_path) {
            shader = skiaCreateBitmapShader(ctx->stock_image_path);
            ctx->skia_paint->setStyle(SkPaint::kFill_Style);
            ctx->skia_paint->setShader(shader);
            }
            break;
#if 0
        case CB_FILL_HERRINGBONE_PATTERN:  /* TODO */
            break;
#endif
        default:
            break;
    }

    if (shader)
        shader->unref();
}


SkShader*
skiaCreateLinearGradientShader(int y1, int y2)
{
    SkColor linearColors[2];
    SkPoint linearPoints[2];

    linearColors[0] = skiaRandomColor();
    linearColors[1] = skiaRandomColor();
    linearPoints[0].fX = SkIntToScalar(0);
    linearPoints[0].fY = SkIntToScalar(y1);
    linearPoints[1].fX = SkIntToScalar(0);
    linearPoints[1].fY = SkIntToScalar(y2);

    return SkGradientShader::CreateLinear(
        linearPoints, linearColors, NULL, NUM_ELEM(linearColors),
        SkShader::kClamp_TileMode);
}

SkShader*
skiaCreateRadialGradientShader(const shapes_t *shape)
{
    double radius = shape->radius;
    SkPoint center;
    SkColor colors[2];

    colors[0] = skiaRandomColor();
    colors[1] = skiaRandomColor();
    if (shape->radius > 0) {
        center.set(shape->x + shape->radius, shape->y + shape->radius);
    } else if (shape->width > shape->height) {
        radius = shape->height/2;
        center.set(
            shape->x + radius + ((double)rnd()*shape->width)/RAND_MAX,
            shape->y + radius
            );
    } else {
        radius = shape->width/2;
        center.set(
            shape->x + radius,
            shape->y + radius + ((double)rnd()*shape->height)/RAND_MAX
            );
    }

    return SkGradientShader::CreateRadial(
        center, radius,
        colors, NULL, NUM_ELEM(colors),
        SkShader::kClamp_TileMode);
}

SkShader*
skiaCreateBitmapShader(const char *image_path)
{
    SkBitmap bm;

    SkImageDecoder::DecodeFile(image_path, &bm);
    return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
                                        SkShader::kClamp_TileMode);
}

void
skiaDrawLine(caskbench_context_t *ctx, shapes_t *args)
{
    path.reset();

    path.moveTo(args->x, args->y);
    path.lineTo(args->x + args->width, args->y + args->height);

    ctx->skia_canvas->drawPath(path, *(ctx->skia_paint));
}

void
skiaDrawQuadraticCurve(caskbench_context_t *ctx, shapes_t *args)
{
    path.reset();

    path.moveTo(args->x, args->y);
    path.rQuadTo(args->dx1, args->dy1,
                 args->width, args->height);

    ctx->skia_canvas->drawPath(path, *(ctx->skia_paint));
}

void
skiaDrawCubicCurve(caskbench_context_t *ctx, shapes_t *args)
{
    path.reset();

    path.moveTo(args->x, args->y);
    path.rCubicTo(args->dx1, args->dy1,
                  args->dx2, args->dy2,
                  args->width, args->height);

    ctx->skia_canvas->drawPath(path, *(ctx->skia_paint));
}

void
skiaDrawCircle(caskbench_context_t *ctx, shapes_t *args)
{
    ctx->skia_canvas->drawCircle(args->x + args->radius,
                                 args->y + args->radius,
                                 args->radius,
                                 *(ctx->skia_paint));
}

void
skiaDrawRectangle(caskbench_context_t *ctx, shapes_t *args)
{
    SkRect r;
    r.set(args->x, args->y,
          args->x + args->width, args->y + args->height);
    ctx->skia_canvas->drawRect(r, *(ctx->skia_paint));
}

void
skiaDrawTriangle(caskbench_context_t *ctx, shapes_t *args)
{
    // Temporarily disable anti-aliasing to work around crash in GlShader
    ctx->skia_paint->setAntiAlias(false);

    path.reset();
    path.moveTo(args->x, args->y + 2*args->radius);
    path.rLineTo(2*args->radius, 0);
    path.rLineTo(-args->radius, -2*args->radius);
    path.close();

    ctx->skia_canvas->drawPath(path, *(ctx->skia_paint));
    ctx->skia_paint->setAntiAlias(true);
}

void
skiaDrawStar(caskbench_context_t *ctx, shapes_t *args)
{
    int px = args->x + 2*args->radius * star_points[0][0]/200.0;
    int py = args->y + 2*args->radius * star_points[0][1]/200.0;
    path.reset();
    path.moveTo(px, py);

    for (int p = 1; p < 10; p++ ) {
        px = args->x + 2*args->radius * star_points[p][0]/200.0;
        py = args->y + 2*args->radius * star_points[p][1]/200.0;
        path.lineTo(px, py);
    }
    path.close();

    ctx->skia_canvas->drawPath(path, *(ctx->skia_paint));
}

void
skiaDrawRoundedRectangle (caskbench_context_t *ctx, shapes_t *args)
{
    SkRect rect;
    rect.set(args->x, args->y, args->x + args->width, args->y + args->height);
    ctx->skia_canvas->drawRoundRect(rect, 4.0, 4.0, *(ctx->skia_paint));
}

SkBitmap
skiaCreateSampleImage(caskbench_context_t *ctx)
{
    int i, x, y;
    SkBitmap bitmap;
    bitmap.setConfig(SkBitmap::kARGB_8888_Config, 160, 40);
    SkImageInfo info = SkImageInfo::Make(160, 40,
                                         kBGRA_8888_SkColorType,
                                         kPremul_SkAlphaType);
    bitmap.allocPixels(info);
    SkBitmapDevice device(bitmap);
    SkCanvas canvas(&device);
    SkPaint paint;
    SkRect r;

    canvas.clear(0);

    x = 5;
    for (i=0; i<16; i++) {
        paint.setARGB(255, 255/(i+1), 255, 16*i);
        x += 10;
        y = (i%2)*10;
        r.set(x, y, x+10, y+10);

        canvas.drawRect(r, paint);
    }
    canvas.flush();
    return bitmap;
}

SkPath
getCurrentSkiaPath()
{
    return path;
}

void (*skiaShapes[CB_SHAPE_END-1])(caskbench_context_t *ctx , shapes_t *args) = {
    skiaDrawCircle,
    skiaDrawRectangle,
    skiaDrawTriangle,
    skiaDrawStar,
    skiaDrawRoundedRectangle
};


void
skiaDrawRandomizedShape(caskbench_context_t *ctx, shapes_t *shape)
{
    // Shape Type
    if (shape->shape_type == CB_SHAPE_RANDOM)
        shape->shape_type = generate_random_shape();

    // Options for fill, gradient and transparency
    if (shape->fill_type == CB_FILL_RANDOM)
        shape->fill_type = generate_random_fill_type();
    sk_set_fill_style(ctx, shape);

    SkColor color;
    if (shape->fill_color != -1) {
        color = SkColorSetARGB (shape->fill_color & 255,
                                (shape->fill_color >> 24) & 255,
                                (shape->fill_color >> 16) & 255,
                                (shape->fill_color >> 8) & 255);
    }
    else {
        color = skiaRandomColor();
    }
    ctx->skia_paint->setColor(color);

    // Draw
    skiaShapes[shape->shape_type-1] (ctx, shape);

    // Stroke styles
    if (shape->stroke_width)
    {
        ctx->skia_paint->setShader (NULL);
        ctx->skia_paint->setStyle(SkPaint::kStroke_Style);
        ctx->skia_paint->setStrokeWidth(shape->stroke_width);
        ctx->skia_paint->setStrokeJoin((SkPaint::Join)(int)(drnd48()*2));
        ctx->skia_paint->setStrokeCap((SkPaint::Cap)(int)(drnd48()*2));
        SkScalar vals[] = { SkIntToScalar((drnd48()*20)), SkIntToScalar((drnd48()*20))  };
        ctx->skia_paint->setPathEffect(NULL);
        ctx->skia_paint->setPathEffect(SkDashPathEffect::Create(vals, 2, 0))->unref();
        SkColor color;
        if (shape->stroke_color != -1) {
            color = SkColorSetRGB (shape->stroke_color >> 24 & 255,
                                  (shape->stroke_color >> 16) & 255,
                                  (shape->stroke_color >> 8) & 255);
        }
        else {
            color = skiaRandomColor();
        }
        ctx->skia_paint->setColor(color);
        skiaShapes[shape->shape_type-1] (ctx, shape);
        ctx->skia_paint->setStrokeWidth(1);
    }

    // Cleanup
    ctx->skia_canvas->flush();
    ctx->skia_paint->setPathEffect(NULL);
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :