blob: dab5648125d7d5e2126358d3a33c8eb9e006cfd4 [file] [log] [blame]
Mike Kleina8ceb772019-05-20 12:45:50 +00001/*
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 Canaryfc894d42019-06-04 09:51:54 -040013#include "include/private/SkNoncopyable.h"
Mike Kleina8ceb772019-05-20 12:45:50 +000014#include "src/core/SkRecords.h"
15class SkCanvas;
16
17// Records small pictures, but only a limited subset of the canvas API, and may fail.
18class SkMiniRecorder : SkNoncopyable {
19public:
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
38private:
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 Klein339f0ee2020-11-09 12:21:53 -060055 alignas(void*) alignas(double) char fBuffer[kInlineStorage];
Mike Kleina8ceb772019-05-20 12:45:50 +000056};
57
58#endif//SkMiniRecorder_DEFINED