bitmapshader.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2013 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/SkImageInfo.h"
  12. #include "include/core/SkMatrix.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkShader.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTileMode.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/gpu/GrContext.h"
  21. #include "src/gpu/GrCaps.h"
  22. #include "src/gpu/GrContextPriv.h"
  23. namespace skiagm {
  24. static void draw_bm(SkBitmap* bm) {
  25. SkPaint bluePaint;
  26. bluePaint.setColor(SK_ColorBLUE);
  27. bm->allocN32Pixels(20, 20);
  28. bm->eraseColor(SK_ColorRED);
  29. SkCanvas canvas(*bm);
  30. canvas.drawCircle(10, 10, 5, bluePaint);
  31. }
  32. static void draw_mask(SkBitmap* bm) {
  33. SkPaint circlePaint;
  34. circlePaint.setColor(SK_ColorBLACK);
  35. bm->allocPixels(SkImageInfo::MakeA8(20, 20));
  36. bm->eraseColor(SK_ColorTRANSPARENT);
  37. SkCanvas canvas(*bm);
  38. canvas.drawCircle(10, 10, 10, circlePaint);
  39. }
  40. class BitmapShaderGM : public GM {
  41. protected:
  42. void onOnceBeforeDraw() override {
  43. this->setBGColor(SK_ColorGRAY);
  44. draw_bm(&fBitmap);
  45. draw_mask(&fMask);
  46. }
  47. SkString onShortName() override {
  48. return SkString("bitmapshaders");
  49. }
  50. SkISize onISize() override {
  51. return SkISize::Make(150, 100);
  52. }
  53. void onDraw(SkCanvas* canvas) override {
  54. SkPaint paint;
  55. for (int i = 0; i < 2; i++) {
  56. SkMatrix s;
  57. s.reset();
  58. if (1 == i) {
  59. s.setScale(1.5f, 1.5f);
  60. s.postTranslate(2, 2);
  61. }
  62. canvas->save();
  63. paint.setShader(fBitmap.makeShader(&s));
  64. // draw the shader with a bitmap mask
  65. canvas->drawBitmap(fMask, 0, 0, &paint);
  66. // no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
  67. canvas->drawBitmap(fMask, 30, 0, &paint);
  68. canvas->translate(0, 25);
  69. canvas->drawCircle(10, 10, 10, paint);
  70. canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
  71. canvas->translate(0, 25);
  72. // clear the shader, colorized by a solid color with a bitmap mask
  73. paint.setShader(nullptr);
  74. paint.setColor(SK_ColorGREEN);
  75. canvas->drawBitmap(fMask, 0, 0, &paint);
  76. canvas->drawBitmap(fMask, 30, 0, &paint);
  77. canvas->translate(0, 25);
  78. paint.setShader(fMask.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &s));
  79. paint.setColor(SK_ColorRED);
  80. // draw the mask using the shader and a color
  81. canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
  82. canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
  83. canvas->restore();
  84. canvas->translate(60, 0);
  85. }
  86. }
  87. private:
  88. SkBitmap fBitmap;
  89. SkBitmap fMask;
  90. typedef GM INHERITED;
  91. };
  92. DEF_SIMPLE_GM(hugebitmapshader, canvas, 100, 100) {
  93. SkPaint paint;
  94. SkBitmap bitmap;
  95. // The huge height will exceed GL_MAX_TEXTURE_SIZE. We test that the GL backend will at least
  96. // draw something with a default paint instead of drawing nothing.
  97. //
  98. // (See https://skia-review.googlesource.com/c/skia/+/73200)
  99. int bitmapW = 1;
  100. int bitmapH = 60000;
  101. if (auto* ctx = canvas->getGrContext()) {
  102. bitmapH = ctx->priv().caps()->maxTextureSize() + 1;
  103. }
  104. bitmap.setInfo(SkImageInfo::MakeA8(bitmapW, bitmapH), bitmapW);
  105. uint8_t* pixels = new uint8_t[bitmapH];
  106. for(int i = 0; i < bitmapH; ++i) {
  107. pixels[i] = i & 0xff;
  108. }
  109. bitmap.setPixels(pixels);
  110. paint.setShader(bitmap.makeShader(SkTileMode::kMirror, SkTileMode::kMirror));
  111. paint.setColor(SK_ColorRED);
  112. paint.setAntiAlias(true);
  113. canvas->drawCircle(50, 50, 50, paint);
  114. delete [] pixels;
  115. }
  116. //////////////////////////////////////////////////////////////////////////////
  117. DEF_GM( return new BitmapShaderGM; )
  118. }