SkMiniRecorder.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2015 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 SkMiniRecorder_DEFINED
  8. #define SkMiniRecorder_DEFINED
  9. #include "include/core/SkScalar.h"
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkNoncopyable.h"
  12. #include "src/core/SkRecords.h"
  13. class SkCanvas;
  14. // Records small pictures, but only a limited subset of the canvas API, and may fail.
  15. class SkMiniRecorder : SkNoncopyable {
  16. public:
  17. SkMiniRecorder();
  18. ~SkMiniRecorder();
  19. // Try to record an op. Returns false on failure.
  20. bool drawPath(const SkPath&, const SkPaint&);
  21. bool drawRect(const SkRect&, const SkPaint&);
  22. bool drawTextBlob(const SkTextBlob*, SkScalar x, SkScalar y, const SkPaint&);
  23. // Detach anything we've recorded as a picture, resetting this SkMiniRecorder.
  24. // If cull is nullptr we'll calculate it.
  25. sk_sp<SkPicture> detachAsPicture(const SkRect* cull);
  26. // Flush anything we've recorded to the canvas, resetting this SkMiniRecorder.
  27. // This is logically the same as but rather more efficient than:
  28. // sk_sp<SkPicture> pic(this->detachAsPicture(nullptr));
  29. // pic->playback(canvas);
  30. void flushAndReset(SkCanvas*);
  31. private:
  32. enum class State {
  33. kEmpty,
  34. kDrawPath,
  35. kDrawRect,
  36. kDrawTextBlob,
  37. };
  38. State fState;
  39. template <size_t A, size_t B>
  40. struct Max { static const size_t val = A > B ? A : B; };
  41. static const size_t kInlineStorage =
  42. Max<sizeof(SkRecords::DrawPath),
  43. Max<sizeof(SkRecords::DrawRect),
  44. sizeof(SkRecords::DrawTextBlob)>::val>::val;
  45. SkAlignedSStorage<kInlineStorage> fBuffer;
  46. };
  47. #endif//SkMiniRecorder_DEFINED