overdrawcanvas.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2019 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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkColorFilter.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkImage.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkOverdrawCanvas.h"
  16. #include "include/core/SkPaint.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkSurface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "include/effects/SkOverdrawColorFilter.h"
  22. #define WIDTH 500
  23. #define HEIGHT 500
  24. static const uint32_t kOverdrawColors[6] = {
  25. 0x00000000, 0x5f00005f, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f,
  26. };
  27. DEF_SIMPLE_GM_BG(overdraw_canvas, canvas, WIDTH, HEIGHT, SK_ColorWHITE) {
  28. // Set up the overdraw canvas.
  29. SkImageInfo offscreenInfo = SkImageInfo::MakeA8(WIDTH, HEIGHT);
  30. sk_sp<SkSurface> offscreen = SkSurface::MakeRaster(offscreenInfo);
  31. auto c = offscreen->getCanvas();
  32. SkOverdrawCanvas overdrawCanvas(c);
  33. overdrawCanvas.drawRect(SkRect::MakeLTRB(10, 10, 200, 200), SkPaint());
  34. overdrawCanvas.drawRect(SkRect::MakeLTRB(20, 20, 190, 190), SkPaint());
  35. overdrawCanvas.drawRect(SkRect::MakeLTRB(30, 30, 180, 180), SkPaint());
  36. overdrawCanvas.drawRect(SkRect::MakeLTRB(40, 40, 170, 170), SkPaint());
  37. overdrawCanvas.drawRect(SkRect::MakeLTRB(50, 50, 160, 160), SkPaint());
  38. overdrawCanvas.drawRect(SkRect::MakeLTRB(60, 60, 150, 150), SkPaint());
  39. char text[] = "Ae_p";
  40. overdrawCanvas.drawSimpleText(text, 4, SkTextEncoding::kUTF8, 300, 300, SkFont(), SkPaint());
  41. sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
  42. // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
  43. SkPaint paint;
  44. paint.setColorFilter(SkOverdrawColorFilter::Make(kOverdrawColors));
  45. canvas->drawImage(counts.get(), 0.0f, 0.0f, &paint);
  46. canvas->drawString("This is some text:", 180, 300, SkFont(), SkPaint());
  47. }