blob: 914eccc2e38a5c9b26b8f0224d051049ac64dea6 [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.
28 SkPicture* detachAsPicture(const SkRect& cull);
29
mtkleind41ea1d2015-05-20 10:16:49 -070030 // Flush anything we've recorded to the canvas, resetting this SkMiniRecorder.
31 // This is logically the same as but rather more efficient than:
32 // SkAutoTUnref<SkPicture> pic(this->detachAsPicture(SkRect::MakeEmpty()));
33 // pic->playback(canvas);
34 void flushAndReset(SkCanvas*);
35
mtklein9db912c2015-05-19 11:11:26 -070036private:
37 enum class State { kEmpty, kDrawPath, kDrawRect, kDrawTextBlob };
38
39 State fState;
40
41 template <size_t A, size_t B>
42 struct Max { static const size_t val = A > B ? A : B; };
43
44 static const size_t kInlineStorage = Max<sizeof(SkRecords::DrawPath),
45 Max<sizeof(SkRecords::DrawRect),
46 sizeof(SkRecords::DrawTextBlob)>::val>::val;
47 SkAlignedSStorage<kInlineStorage> fBuffer;
48};
49
50#endif//SkMiniRecorder_DEFINED