verylargebitmap.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2012 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/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkColorSpace.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPicture.h"
  15. #include "include/core/SkPictureRecorder.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkScalar.h"
  20. #include "include/core/SkShader.h"
  21. #include "include/core/SkSize.h"
  22. #include "include/core/SkString.h"
  23. #include "include/core/SkSurface.h"
  24. #include "include/core/SkTileMode.h"
  25. #include "include/effects/SkGradientShader.h"
  26. static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
  27. const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
  28. const SkScalar radius = 40;
  29. SkPaint paint;
  30. paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
  31. SkTileMode::kMirror));
  32. paint.setBlendMode(SkBlendMode::kSrc);
  33. canvas->drawPaint(paint);
  34. }
  35. static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
  36. auto surface(SkSurface::MakeRasterN32Premul(width, height));
  37. draw(surface->getCanvas(), width, height, colors);
  38. return surface->makeImageSnapshot();
  39. }
  40. static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
  41. SkPictureRecorder recorder;
  42. draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
  43. return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
  44. {width, height}, nullptr, nullptr,
  45. SkImage::BitDepth::kU8,
  46. SkColorSpace::MakeSRGB());
  47. }
  48. typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
  49. static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
  50. ImageMakerProc proc) {
  51. sk_sp<SkImage> image(proc(width, height, colors));
  52. SkPaint borderPaint;
  53. borderPaint.setStyle(SkPaint::kStroke_Style);
  54. SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
  55. canvas->save();
  56. canvas->clipRect(dstRect);
  57. canvas->drawImage(image, 0, 0, nullptr);
  58. canvas->restore();
  59. canvas->drawRect(dstRect, borderPaint);
  60. dstRect.offset(SkIntToScalar(150), 0);
  61. int hw = width / 2;
  62. int hh = height / 2;
  63. SkIRect subset = SkIRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
  64. canvas->drawImageRect(image, subset, dstRect, nullptr);
  65. canvas->drawRect(dstRect, borderPaint);
  66. dstRect.offset(SkIntToScalar(150), 0);
  67. canvas->drawImageRect(image, dstRect, nullptr);
  68. canvas->drawRect(dstRect, borderPaint);
  69. }
  70. class VeryLargeBitmapGM : public skiagm::GM {
  71. ImageMakerProc fProc;
  72. const char* fName;
  73. public:
  74. VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
  75. private:
  76. SkString onShortName() override { return SkString(fName); }
  77. SkISize onISize() override { return {500, 600}; }
  78. void onDraw(SkCanvas* canvas) override {
  79. int veryBig = 65*1024; // 64K < size
  80. int big = 33*1024; // 32K < size < 64K
  81. // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
  82. int medium = 5*1024;
  83. int small = 150;
  84. SkColor colors[2];
  85. canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
  86. colors[0] = SK_ColorRED;
  87. colors[1] = SK_ColorGREEN;
  88. show_image(canvas, small, small, colors, fProc);
  89. canvas->translate(0, SkIntToScalar(150));
  90. colors[0] = SK_ColorBLUE;
  91. colors[1] = SK_ColorMAGENTA;
  92. show_image(canvas, big, small, colors, fProc);
  93. canvas->translate(0, SkIntToScalar(150));
  94. colors[0] = SK_ColorMAGENTA;
  95. colors[1] = SK_ColorYELLOW;
  96. show_image(canvas, medium, medium, colors, fProc);
  97. canvas->translate(0, SkIntToScalar(150));
  98. colors[0] = SK_ColorGREEN;
  99. colors[1] = SK_ColorYELLOW;
  100. // This used to be big enough that we didn't draw on CPU, but now we do.
  101. show_image(canvas, veryBig, small, colors, fProc);
  102. }
  103. };
  104. DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
  105. DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )