SkDrawable.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2014 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/SkCanvas.h"
  8. #include "include/core/SkDrawable.h"
  9. #include <atomic>
  10. static int32_t next_generation_id() {
  11. static std::atomic<int32_t> nextID{1};
  12. int32_t id;
  13. do {
  14. id = nextID++;
  15. } while (id == 0);
  16. return id;
  17. }
  18. SkDrawable::SkDrawable() : fGenerationID(0) {}
  19. static void draw_bbox(SkCanvas* canvas, const SkRect& r) {
  20. SkPaint paint;
  21. paint.setStyle(SkPaint::kStroke_Style);
  22. paint.setColor(0xFFFF7088);
  23. canvas->drawRect(r, paint);
  24. canvas->drawLine(r.left(), r.top(), r.right(), r.bottom(), paint);
  25. canvas->drawLine(r.left(), r.bottom(), r.right(), r.top(), paint);
  26. }
  27. void SkDrawable::draw(SkCanvas* canvas, const SkMatrix* matrix) {
  28. SkAutoCanvasRestore acr(canvas, true);
  29. if (matrix) {
  30. canvas->concat(*matrix);
  31. }
  32. this->onDraw(canvas);
  33. if (false) {
  34. draw_bbox(canvas, this->getBounds());
  35. }
  36. }
  37. void SkDrawable::draw(SkCanvas* canvas, SkScalar x, SkScalar y) {
  38. SkMatrix matrix = SkMatrix::MakeTrans(x, y);
  39. this->draw(canvas, &matrix);
  40. }
  41. SkPicture* SkDrawable::newPictureSnapshot() {
  42. return this->onNewPictureSnapshot();
  43. }
  44. uint32_t SkDrawable::getGenerationID() {
  45. if (0 == fGenerationID) {
  46. fGenerationID = next_generation_id();
  47. }
  48. return fGenerationID;
  49. }
  50. SkRect SkDrawable::getBounds() {
  51. return this->onGetBounds();
  52. }
  53. void SkDrawable::notifyDrawingChanged() {
  54. fGenerationID = 0;
  55. }
  56. /////////////////////////////////////////////////////////////////////////////////////////
  57. #include "include/core/SkPictureRecorder.h"
  58. SkPicture* SkDrawable::onNewPictureSnapshot() {
  59. SkPictureRecorder recorder;
  60. const SkRect bounds = this->getBounds();
  61. SkCanvas* canvas = recorder.beginRecording(bounds, nullptr, 0);
  62. this->draw(canvas);
  63. if (false) {
  64. draw_bbox(canvas, bounds);
  65. }
  66. return recorder.finishRecordingAsPicture().release();
  67. }