SkPictureRecorder.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef SkPictureRecorder_DEFINED
  8. #define SkPictureRecorder_DEFINED
  9. #include "include/core/SkBBHFactory.h"
  10. #include "include/core/SkPicture.h"
  11. #include "include/core/SkRefCnt.h"
  12. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  13. namespace android {
  14. class Picture;
  15. };
  16. #endif
  17. class GrContext;
  18. class SkCanvas;
  19. class SkDrawable;
  20. class SkMiniRecorder;
  21. class SkPictureRecord;
  22. class SkRecord;
  23. class SkRecorder;
  24. class SK_API SkPictureRecorder {
  25. public:
  26. SkPictureRecorder();
  27. ~SkPictureRecorder();
  28. enum RecordFlags {
  29. // If you call drawPicture() or drawDrawable() on the recording canvas, this flag forces
  30. // that object to playback its contents immediately rather than reffing the object.
  31. kPlaybackDrawPicture_RecordFlag = 1 << 0,
  32. };
  33. enum FinishFlags {
  34. };
  35. /** Returns the canvas that records the drawing commands.
  36. @param bounds the cull rect used when recording this picture. Any drawing the falls outside
  37. of this rect is undefined, and may be drawn or it may not.
  38. @param bbhFactory factory to create desired acceleration structure
  39. @param recordFlags optional flags that control recording.
  40. @return the canvas.
  41. */
  42. SkCanvas* beginRecording(const SkRect& bounds,
  43. SkBBHFactory* bbhFactory = nullptr,
  44. uint32_t recordFlags = 0);
  45. SkCanvas* beginRecording(SkScalar width, SkScalar height,
  46. SkBBHFactory* bbhFactory = nullptr,
  47. uint32_t recordFlags = 0) {
  48. return this->beginRecording(SkRect::MakeWH(width, height), bbhFactory, recordFlags);
  49. }
  50. /** Returns the recording canvas if one is active, or NULL if recording is
  51. not active. This does not alter the refcnt on the canvas (if present).
  52. */
  53. SkCanvas* getRecordingCanvas();
  54. /**
  55. * Signal that the caller is done recording. This invalidates the canvas returned by
  56. * beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
  57. * must call unref() when they are done using it.
  58. *
  59. * The returned picture is immutable. If during recording drawables were added to the canvas,
  60. * these will have been "drawn" into a recording canvas, so that this resulting picture will
  61. * reflect their current state, but will not contain a live reference to the drawables
  62. * themselves.
  63. */
  64. sk_sp<SkPicture> finishRecordingAsPicture(uint32_t endFlags = 0);
  65. /**
  66. * Signal that the caller is done recording, and update the cull rect to use for bounding
  67. * box hierarchy (BBH) generation. The behavior is the same as calling
  68. * finishRecordingAsPicture(), except that this method updates the cull rect initially passed
  69. * into beginRecording.
  70. * @param cullRect the new culling rectangle to use as the overall bound for BBH generation
  71. * and subsequent culling operations.
  72. * @return the picture containing the recorded content.
  73. */
  74. sk_sp<SkPicture> finishRecordingAsPictureWithCull(const SkRect& cullRect,
  75. uint32_t endFlags = 0);
  76. /**
  77. * Signal that the caller is done recording. This invalidates the canvas returned by
  78. * beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
  79. * must call unref() when they are done using it.
  80. *
  81. * Unlike finishRecordingAsPicture(), which returns an immutable picture, the returned drawable
  82. * may contain live references to other drawables (if they were added to the recording canvas)
  83. * and therefore this drawable will reflect the current state of those nested drawables anytime
  84. * it is drawn or a new picture is snapped from it (by calling drawable->newPictureSnapshot()).
  85. */
  86. sk_sp<SkDrawable> finishRecordingAsDrawable(uint32_t endFlags = 0);
  87. private:
  88. void reset();
  89. /** Replay the current (partially recorded) operation stream into
  90. canvas. This call doesn't close the current recording.
  91. */
  92. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  93. friend class android::Picture;
  94. #endif
  95. friend class SkPictureRecorderReplayTester; // for unit testing
  96. void partialReplay(SkCanvas* canvas) const;
  97. bool fActivelyRecording;
  98. uint32_t fFlags;
  99. SkRect fCullRect;
  100. sk_sp<SkBBoxHierarchy> fBBH;
  101. std::unique_ptr<SkRecorder> fRecorder;
  102. sk_sp<SkRecord> fRecord;
  103. std::unique_ptr<SkMiniRecorder> fMiniRecorder;
  104. SkPictureRecorder(SkPictureRecorder&&) = delete;
  105. SkPictureRecorder& operator=(SkPictureRecorder&&) = delete;
  106. };
  107. #endif