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