blob: fd1e8f624d7c9774689a7643eea22b77fc4d7fb5 [file] [log] [blame]
mtklein9db912c2015-05-19 11:11:26 -07001/*
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"
14class SkCanvas;
15
16// Records small pictures, but only a limited subset of the canvas API, and may fail.
17class SkMiniRecorder : SkNoncopyable {
18public:
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.
Mike Klein26eb16f2017-04-10 09:50:25 -040028 // If cull is nullptr we'll calculate it.
29 sk_sp<SkPicture> detachAsPicture(const SkRect* cull);
mtklein9db912c2015-05-19 11:11:26 -070030
mtkleind41ea1d2015-05-20 10:16:49 -070031 // Flush anything we've recorded to the canvas, resetting this SkMiniRecorder.
32 // This is logically the same as but rather more efficient than:
Mike Klein26eb16f2017-04-10 09:50:25 -040033 // sk_sp<SkPicture> pic(this->detachAsPicture(nullptr));
mtkleind41ea1d2015-05-20 10:16:49 -070034 // pic->playback(canvas);
35 void flushAndReset(SkCanvas*);
36
mtklein9db912c2015-05-19 11:11:26 -070037private:
mtklein64b4c782015-07-01 13:56:53 -070038 enum class State {
39 kEmpty,
mtklein64b4c782015-07-01 13:56:53 -070040 kDrawPath,
41 kDrawRect,
42 kDrawTextBlob,
43 };
mtklein9db912c2015-05-19 11:11:26 -070044
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
mtklein64b4c782015-07-01 13:56:53 -070050 static const size_t kInlineStorage =
mtklein64b4c782015-07-01 13:56:53 -070051 Max<sizeof(SkRecords::DrawPath),
52 Max<sizeof(SkRecords::DrawRect),
reede93393b2016-07-26 12:21:10 -070053 sizeof(SkRecords::DrawTextBlob)>::val>::val;
mtklein9db912c2015-05-19 11:11:26 -070054 SkAlignedSStorage<kInlineStorage> fBuffer;
55};
56
57#endif//SkMiniRecorder_DEFINED