aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/localmatriximageshader.cpp
blob: 2c9fcd2601f1aff2a2931c992d04ff6c32f1bf83 (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
/*
 * Copyright 2015 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 "SkCanvas.h"
#include "SkSurface.h"

static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
    auto surface(rootCanvas->makeSurface(info));
    if (!surface) {
        surface = SkSurface::MakeRaster(info);
    }

    SkPaint paint;
    paint.setAntiAlias(true);
    paint.setColor(color);
    surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
    return surface->makeImageSnapshot();
}

DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) {
    sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED);
    SkMatrix translate = SkMatrix::MakeTrans(100.0f, 0.0f);
    SkMatrix rotate;
    rotate.setRotate(45.0f);
    sk_sp<SkShader> redImageShader = redImage->makeShader(SkShader::TileMode::kClamp_TileMode,
            SkShader::TileMode::kClamp_TileMode, &translate);
    sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate);

    // Rotate about the origin will happen first.
    SkPaint paint;
    paint.setShader(redLocalMatrixShader);
    canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);

    sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE);
    sk_sp<SkShader> blueImageShader = blueImage->makeShader(SkShader::TileMode::kClamp_TileMode,
            SkShader::TileMode::kClamp_TileMode, &rotate);
    sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(translate);

    // Translate will happen first.
    paint.setShader(blueLocalMatrixShader);
    canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);

    canvas->translate(100.0f, 0.0f);

    // Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100).
    SkShader::TileMode mode[2];
    SkMatrix matrix;
    SkImage* image = redLocalMatrixShader->isAImage(&matrix, mode);
    paint.setShader(image->makeShader(mode[0], mode[1], &matrix));
    canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
    image = blueLocalMatrixShader->isAImage(&matrix, mode);
    paint.setShader(image->makeShader(mode[0], mode[1], &matrix));
    canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
}