1 /*
2 * Copyright 2015 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 "SkCanvas.h"
10 #include "SkSurface.h"
11
make_image(SkCanvas * rootCanvas,SkColor color)12 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
13 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
14 auto surface(rootCanvas->makeSurface(info));
15 if (!surface) {
16 surface = SkSurface::MakeRaster(info);
17 }
18
19 SkPaint paint;
20 paint.setAntiAlias(true);
21 paint.setColor(color);
22 surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
23 return surface->makeImageSnapshot();
24 }
25
26 DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) {
27 sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED);
28 SkMatrix translate = SkMatrix::MakeTrans(100.0f, 0.0f);
29 SkMatrix rotate;
30 rotate.setRotate(45.0f);
31 sk_sp<SkShader> redImageShader = redImage->makeShader(&translate);
32 sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate);
33
34 // Rotate about the origin will happen first.
35 SkPaint paint;
36 paint.setShader(redLocalMatrixShader);
37 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
38
39 sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE);
40 sk_sp<SkShader> blueImageShader = blueImage->makeShader(&rotate);
41 sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(translate);
42
43 // Translate will happen first.
44 paint.setShader(blueLocalMatrixShader);
45 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
46
47 canvas->translate(100.0f, 0.0f);
48
49 // Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100).
50 SkShader::TileMode mode[2];
51 SkMatrix matrix;
52 SkImage* image = redLocalMatrixShader->isAImage(&matrix, mode);
53 paint.setShader(image->makeShader(mode[0], mode[1], &matrix));
54 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
55 image = blueLocalMatrixShader->isAImage(&matrix, mode);
56 paint.setShader(image->makeShader(mode[0], mode[1], &matrix));
57 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
58 }
59