2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkSurface.h"
12 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
13 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
14 auto surface(rootCanvas->makeSurface(info));
16 surface = SkSurface::MakeRaster(info);
20 paint.setAntiAlias(true);
21 paint.setColor(color);
22 surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
23 return surface->makeImageSnapshot();
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);
30 rotate.setRotate(45.0f);
31 sk_sp<SkShader> redImageShader = redImage->makeShader(&translate);
32 sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate);
34 // Rotate about the origin will happen first.
36 paint.setShader(redLocalMatrixShader);
37 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
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);
43 // Translate will happen first.
44 paint.setShader(blueLocalMatrixShader);
45 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
47 canvas->translate(100.0f, 0.0f);
49 // Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100).
50 SkShader::TileMode mode[2];
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);