subsetshader.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright 2016 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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkShader.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkTileMode.h"
  17. #include "tools/Resources.h"
  18. DEF_SIMPLE_GM_CAN_FAIL(bitmap_subset_shader, canvas, errorMsg, 256, 256) {
  19. canvas->clear(SK_ColorWHITE);
  20. SkBitmap source;
  21. if (!GetResourceAsBitmap("images/color_wheel.png", &source)) {
  22. *errorMsg = "Could not load images/color_wheel.png. "
  23. "Did you forget to set the resourcePath?";
  24. return skiagm::DrawResult::kFail;
  25. }
  26. SkIRect left = SkIRect::MakeWH(source.width()/2, source.height());
  27. SkIRect right = SkIRect::MakeXYWH(source.width()/2, 0,
  28. source.width()/2, source.height());
  29. SkBitmap leftBitmap, rightBitmap;
  30. source.extractSubset(&leftBitmap, left);
  31. source.extractSubset(&rightBitmap, right);
  32. SkMatrix matrix;
  33. matrix.setScale(0.75f, 0.75f);
  34. matrix.preRotate(30.0f);
  35. SkTileMode tm = SkTileMode::kRepeat;
  36. SkPaint paint;
  37. paint.setShader(leftBitmap.makeShader(tm, tm, &matrix));
  38. canvas->drawRect(SkRect::MakeWH(256.0f, 128.0f), paint);
  39. paint.setShader(rightBitmap.makeShader(tm, tm, &matrix));
  40. canvas->drawRect(SkRect::MakeXYWH(0, 128.0f, 256.0f, 128.0f), paint);
  41. return skiagm::DrawResult::kOk;
  42. }