localmatriximageshader.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFilterQuality.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkMatrix.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkSurface.h"
  19. #include "include/core/SkTileMode.h"
  20. #include "tools/Resources.h"
  21. #include "tools/ToolUtils.h"
  22. static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
  23. SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
  24. auto surface(ToolUtils::makeSurface(rootCanvas, info));
  25. SkPaint paint;
  26. paint.setAntiAlias(true);
  27. paint.setColor(color);
  28. surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
  29. return surface->makeImageSnapshot();
  30. }
  31. DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) {
  32. sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED);
  33. SkMatrix translate = SkMatrix::MakeTrans(100.0f, 0.0f);
  34. SkMatrix rotate;
  35. rotate.setRotate(45.0f);
  36. sk_sp<SkShader> redImageShader = redImage->makeShader(&translate);
  37. sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate);
  38. // Rotate about the origin will happen first.
  39. SkPaint paint;
  40. paint.setShader(redLocalMatrixShader);
  41. canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
  42. sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE);
  43. sk_sp<SkShader> blueImageShader = blueImage->makeShader(&rotate);
  44. sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(translate);
  45. // Translate will happen first.
  46. paint.setShader(blueLocalMatrixShader);
  47. canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
  48. 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. SkTileMode 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. DEF_SIMPLE_GM(localmatriximageshader_filtering, canvas, 256, 256) {
  60. // Test that filtering decisions (eg bicubic for upscale) are made correctly when the scale
  61. // comes from a local matrix shader.
  62. auto image = GetResourceAsImage("images/mandrill_256.png");
  63. SkPaint p;
  64. p.setFilterQuality(kHigh_SkFilterQuality);
  65. SkMatrix m = SkMatrix::MakeScale(2.0f);
  66. p.setShader(image->makeShader()->makeWithLocalMatrix(m));
  67. canvas->drawRect(SkRect::MakeXYWH(0, 0, 256, 256), p);
  68. }