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