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