bitmapcopy.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "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/SkFont.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkImageInfo.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkTypeface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "tools/ToolUtils.h"
  22. #include <string.h>
  23. namespace {
  24. static const char* color_type_name(SkColorType colorType) {
  25. switch (colorType) {
  26. case kUnknown_SkColorType: return "unknown";
  27. case kAlpha_8_SkColorType: return "A8";
  28. case kRGB_565_SkColorType: return "565";
  29. case kARGB_4444_SkColorType: return "4444";
  30. case kRGBA_8888_SkColorType: return "8888";
  31. case kRGB_888x_SkColorType: return "888x";
  32. case kBGRA_8888_SkColorType: return "8888";
  33. case kRGBA_1010102_SkColorType: return "1010102";
  34. case kRGB_101010x_SkColorType: return "101010x";
  35. case kGray_8_SkColorType: return "G8";
  36. case kRGBA_F16Norm_SkColorType: return "F16Norm";
  37. case kRGBA_F16_SkColorType: return "F16";
  38. case kRGBA_F32_SkColorType: return "F32";
  39. }
  40. return "";
  41. }
  42. constexpr SkColorType gColorTypes[] = {
  43. kRGB_565_SkColorType,
  44. kARGB_4444_SkColorType,
  45. kN32_SkColorType,
  46. };
  47. #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
  48. static void draw_checks(SkCanvas* canvas, int width, int height) {
  49. SkPaint paint;
  50. paint.setColor(SK_ColorRED);
  51. canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
  52. paint.setColor(SK_ColorGREEN);
  53. canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
  54. paint);
  55. paint.setColor(SK_ColorBLUE);
  56. canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
  57. paint);
  58. paint.setColor(SK_ColorYELLOW);
  59. canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
  60. SkIntToScalar(height) }, paint);
  61. }
  62. class BitmapCopyGM : public skiagm::GM {
  63. SkBitmap fDst[NUM_CONFIGS];
  64. void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
  65. SkString onShortName() override { return SkString("bitmapcopy"); }
  66. SkISize onISize() override { return {540, 330}; }
  67. void onDraw(SkCanvas* canvas) override {
  68. SkPaint paint;
  69. SkScalar horizMargin = 10;
  70. SkScalar vertMargin = 10;
  71. SkBitmap src;
  72. src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
  73. SkCanvas canvasTmp(src);
  74. draw_checks(&canvasTmp, 40, 40);
  75. for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
  76. ToolUtils::copy_to(&fDst[i], gColorTypes[i], src);
  77. }
  78. canvas->clear(0xFFDDDDDD);
  79. paint.setAntiAlias(true);
  80. SkFont font(ToolUtils::create_portable_typeface());
  81. SkScalar width = SkIntToScalar(40);
  82. SkScalar height = SkIntToScalar(40);
  83. if (font.getSpacing() > height) {
  84. height = font.getSpacing();
  85. }
  86. for (unsigned i = 0; i < NUM_CONFIGS; i++) {
  87. const char* name = color_type_name(src.colorType());
  88. SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
  89. if (textWidth > width) {
  90. width = textWidth;
  91. }
  92. }
  93. SkScalar horizOffset = width + horizMargin;
  94. SkScalar vertOffset = height + vertMargin;
  95. canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
  96. for (unsigned i = 0; i < NUM_CONFIGS; i++) {
  97. canvas->save();
  98. // Draw destination config name
  99. const char* name = color_type_name(fDst[i].colorType());
  100. SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
  101. SkScalar x = (width - textWidth) / SkScalar(2);
  102. SkScalar y = font.getSpacing() / SkScalar(2);
  103. canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, x, y, font, paint);
  104. // Draw destination bitmap
  105. canvas->translate(0, vertOffset);
  106. x = (width - 40) / SkScalar(2);
  107. canvas->drawBitmap(fDst[i], x, 0, &paint);
  108. canvas->restore();
  109. canvas->translate(horizOffset, 0);
  110. }
  111. }
  112. };
  113. } // namespace
  114. //////////////////////////////////////////////////////////////////////////////
  115. DEF_GM( return new BitmapCopyGM; )