SampleBitmapRect.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2011 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 "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColorFilter.h"
  10. #include "include/core/SkColorPriv.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkGraphics.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkRegion.h"
  15. #include "include/core/SkShader.h"
  16. #include "include/core/SkTime.h"
  17. #include "include/core/SkTypeface.h"
  18. #include "include/effects/SkGradientShader.h"
  19. #include "samplecode/Sample.h"
  20. #include "src/utils/SkUTF.h"
  21. #include "include/core/SkStream.h"
  22. #include "src/core/SkOSFile.h"
  23. static constexpr int INT_SIZE = 64;
  24. static constexpr float SCALAR_SIZE = (float)INT_SIZE;
  25. static void make_bitmap(SkBitmap* bitmap) {
  26. bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
  27. SkCanvas canvas(*bitmap);
  28. canvas.drawColor(SK_ColorRED);
  29. SkPaint paint;
  30. paint.setAntiAlias(true);
  31. const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
  32. const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
  33. paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
  34. canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
  35. }
  36. static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
  37. *value += *delta;
  38. if (*value < min) {
  39. *value = min;
  40. *delta = - *delta;
  41. } else if (*value > max) {
  42. *value = max;
  43. *delta = - *delta;
  44. }
  45. }
  46. static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
  47. bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
  48. bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
  49. }
  50. class BitmapRectView : public Sample {
  51. SkPoint fSrcPt = {0, 0};
  52. SkPoint fSrcVec = {0.866025f, 0.5f};
  53. SkRect fSrcLimit = {-SCALAR_SIZE/4, -SCALAR_SIZE/4,
  54. SCALAR_SIZE*5/4, SCALAR_SIZE*5/4};
  55. SkRect fDstR[2] = {{10, 100, 260, 400}, {322.5, 100, 572.5, 400}};
  56. SkBitmap fBitmap;
  57. SkString name() override { return SkString("BitmapRect"); }
  58. void onOnceBeforeDraw() override {
  59. this->setBGColor(SK_ColorGRAY);
  60. make_bitmap(&fBitmap);
  61. }
  62. void onDrawContent(SkCanvas* canvas) override {
  63. SkRect srcR = {fSrcPt.fX - 16, fSrcPt.fY - 16,
  64. fSrcPt.fX + 16, fSrcPt.fY + 16};
  65. SkPaint paint(SkColors::kYellow);
  66. paint.setStyle(SkPaint::kStroke_Style);
  67. canvas->translate(20, 20);
  68. canvas->drawBitmap(fBitmap, 0, 0, &paint);
  69. canvas->drawRect(srcR, paint);
  70. for (int i = 0; i < 2; ++i) {
  71. paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
  72. canvas->drawBitmapRect(fBitmap, srcR, fDstR[i], &paint,
  73. SkCanvas::kStrict_SrcRectConstraint);
  74. canvas->drawRect(fDstR[i], paint);
  75. }
  76. }
  77. bool onAnimate(double nanos) override {
  78. bounce_pt(&fSrcPt, &fSrcVec, fSrcLimit);
  79. return true;
  80. }
  81. };
  82. static constexpr int BIG_H = 120;
  83. static void make_big_bitmap(SkBitmap* bm) {
  84. static const char gText[] =
  85. "We the people, in order to form a more perfect union, establish justice,"
  86. " ensure domestic tranquility, provide for the common defense, promote the"
  87. " general welfare and ensure the blessings of liberty to ourselves and our"
  88. " posterity, do ordain and establish this constitution for the United"
  89. " States of America.";
  90. SkFont font;
  91. font.setSize(SkIntToScalar(BIG_H));
  92. const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), SkTextEncoding::kUTF8));
  93. bm->allocN32Pixels(BIG_W, BIG_H);
  94. bm->eraseColor(SK_ColorWHITE);
  95. SkCanvas canvas(*bm);
  96. canvas.drawSimpleText(gText, strlen(gText), SkTextEncoding::kUTF8, 0, font.getSize()*4/5, font, SkPaint());
  97. }
  98. class BitmapRectView2 : public Sample {
  99. SkBitmap fBitmap;
  100. SkRect fSrcR = {0, 0, 3 * BIG_H, BIG_H};
  101. SkRect fLimitR;
  102. SkScalar fDX = 1;
  103. SkRect fDstR[2] = {{20, 20, 620, 220}, {20, 270, 620, 470}};
  104. SkString name() override { return SkString("BigBitmapRect"); }
  105. void onOnceBeforeDraw() override {
  106. this->setBGColor(SK_ColorGRAY);
  107. make_big_bitmap(&fBitmap);
  108. fLimitR = SkRect::Make(fBitmap.dimensions());
  109. }
  110. void onDrawContent(SkCanvas* canvas) override {
  111. SkPaint paint;
  112. paint.setStyle(SkPaint::kStroke_Style);
  113. paint.setColor(SK_ColorYELLOW);
  114. for (int i = 0; i < 2; ++i) {
  115. paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
  116. canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
  117. SkCanvas::kStrict_SrcRectConstraint);
  118. canvas->drawRect(fDstR[i], paint);
  119. }
  120. }
  121. bool onAnimate(double nanos) override {
  122. SkScalar width = fSrcR.width();
  123. bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
  124. fSrcR.fRight = fSrcR.fLeft + width;
  125. return true;
  126. }
  127. };
  128. DEF_SAMPLE( return new BitmapRectView(); )
  129. DEF_SAMPLE( return new BitmapRectView2(); )