overdrawcolorfilter.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2016 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/SkColorFilter.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkString.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/effects/SkOverdrawColorFilter.h"
  19. static inline void set_bitmap(SkBitmap* bitmap, uint8_t alpha) {
  20. for (int y = 0; y < bitmap->height(); y++) {
  21. for (int x = 0; x < bitmap->width(); x++) {
  22. uint8_t* addr = bitmap->getAddr8(x, y);
  23. *addr = alpha;
  24. }
  25. }
  26. bitmap->notifyPixelsChanged();
  27. }
  28. class OverdrawColorFilter : public skiagm::GM {
  29. SkString onShortName() override { return SkString("overdrawcolorfilter"); }
  30. SkISize onISize() override { return {200, 400}; }
  31. void onDraw(SkCanvas* canvas) override {
  32. static const SkPMColor colors[SkOverdrawColorFilter::kNumColors] = {
  33. 0x80800000, 0x80008000, 0x80000080, 0x80808000, 0x80008080, 0x80800080,
  34. };
  35. SkPaint paint;
  36. sk_sp<SkColorFilter> colorFilter = SkOverdrawColorFilter::Make(colors);
  37. paint.setColorFilter(colorFilter);
  38. SkImageInfo info = SkImageInfo::MakeA8(100, 100);
  39. SkBitmap bitmap;
  40. bitmap.allocPixels(info);
  41. set_bitmap(&bitmap, 0);
  42. canvas->drawBitmap(bitmap, 0, 0, &paint);
  43. set_bitmap(&bitmap, 1);
  44. canvas->drawBitmap(bitmap, 0, 100, &paint);
  45. set_bitmap(&bitmap, 2);
  46. canvas->drawBitmap(bitmap, 0, 200, &paint);
  47. set_bitmap(&bitmap, 3);
  48. canvas->drawBitmap(bitmap, 0, 300, &paint);
  49. set_bitmap(&bitmap, 4);
  50. canvas->drawBitmap(bitmap, 100, 0, &paint);
  51. set_bitmap(&bitmap, 5);
  52. canvas->drawBitmap(bitmap, 100, 100, &paint);
  53. set_bitmap(&bitmap, 6);
  54. canvas->drawBitmap(bitmap, 100, 200, &paint);
  55. }
  56. };
  57. DEF_GM(return new OverdrawColorFilter;)