C++11 override should now be supported by all of {bots,Chrome,Android,Mozilla}
[platform/upstream/libSkiaSharp.git] / src / core / SkPictureRecorder.cpp
1 /*
2  * Copyright 2014 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 #include "SkData.h"
9 #include "SkDrawable.h"
10 #include "SkLayerInfo.h"
11 #include "SkPictureRecorder.h"
12 #include "SkRecord.h"
13 #include "SkRecordDraw.h"
14 #include "SkRecorder.h"
15 #include "SkRecordOpts.h"
16 #include "SkTypes.h"
17
18 SkPictureRecorder::SkPictureRecorder() {}
19
20 SkPictureRecorder::~SkPictureRecorder() {}
21
22 SkCanvas* SkPictureRecorder::beginRecording(const SkRect& cullRect,
23                                             SkBBHFactory* bbhFactory /* = NULL */,
24                                             uint32_t recordFlags /* = 0 */) {
25     fCullRect = cullRect;
26     fFlags = recordFlags;
27
28     if (bbhFactory) {
29         fBBH.reset((*bbhFactory)(cullRect));
30         SkASSERT(fBBH.get());
31     }
32
33     fRecord.reset(SkNEW(SkRecord));
34     fRecorder.reset(SkNEW_ARGS(SkRecorder, (fRecord.get(), cullRect)));
35     return this->getRecordingCanvas();
36 }
37
38 SkCanvas* SkPictureRecorder::getRecordingCanvas() {
39     return fRecorder.get();
40 }
41
42 SkPicture* SkPictureRecorder::endRecordingAsPicture() {
43     // TODO: delay as much of this work until just before first playback?
44     SkRecordOptimize(fRecord);
45
46     SkAutoTUnref<SkLayerInfo> saveLayerData;
47
48     if (fBBH && (fFlags & kComputeSaveLayerInfo_RecordFlag)) {
49         SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
50
51         saveLayerData.reset(SkNEW_ARGS(SkLayerInfo, (key)));
52     }
53
54     SkDrawableList* drawableList = fRecorder->getDrawableList();
55     SkPicture::SnapshotArray* pictList = drawableList ? drawableList->newDrawableSnapshot() : NULL;
56
57     if (fBBH.get()) {
58         if (saveLayerData) {
59             SkRecordComputeLayers(fCullRect, *fRecord, pictList, fBBH.get(), saveLayerData);
60         } else {
61             SkRecordFillBounds(fCullRect, *fRecord, fBBH.get());
62         }
63         SkRect bbhBound = fBBH->getRootBound();
64         SkASSERT((bbhBound.isEmpty() || fCullRect.contains(bbhBound))
65             || (bbhBound.isEmpty() && fCullRect.isEmpty()));
66         fCullRect = bbhBound;
67     }
68
69     SkPicture* pict = SkNEW_ARGS(SkPicture, (fCullRect, fRecord, pictList, fBBH));
70
71     if (saveLayerData) {
72         pict->EXPERIMENTAL_addAccelData(saveLayerData);
73     }
74
75     // release our refs now, so only the picture will be the owner.
76     fRecorder.reset(NULL);
77     fRecord.reset(NULL);
78     fBBH.reset(NULL);
79
80     return pict;
81 }
82
83 void SkPictureRecorder::partialReplay(SkCanvas* canvas) const {
84     if (NULL == canvas) {
85         return;
86     }
87
88     int drawableCount = 0;
89     SkDrawable* const* drawables = NULL;
90     SkDrawableList* drawableList = fRecorder->getDrawableList();
91     if (drawableList) {
92         drawableCount = drawableList->count();
93         drawables = drawableList->begin();
94     }
95     SkRecordDraw(*fRecord, canvas, NULL, drawables, drawableCount, NULL/*bbh*/, NULL/*callback*/);
96 }
97
98 ///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 class SkRecordedDrawable : public SkDrawable {
101     SkAutoTUnref<SkRecord>          fRecord;
102     SkAutoTUnref<SkBBoxHierarchy>   fBBH;
103     SkAutoTDelete<SkDrawableList>   fDrawableList;
104     const SkRect                    fBounds;
105     const bool                      fDoSaveLayerInfo;
106
107 public:
108     SkRecordedDrawable(SkRecord* record, SkBBoxHierarchy* bbh, SkDrawableList* drawableList,
109                        const SkRect& bounds, bool doSaveLayerInfo)
110         : fRecord(SkRef(record))
111         , fBBH(SkSafeRef(bbh))
112         , fDrawableList(drawableList)   // we take ownership
113         , fBounds(bounds)
114         , fDoSaveLayerInfo(doSaveLayerInfo)
115     {}
116
117 protected:
118     SkRect onGetBounds() override { return fBounds; }
119
120     void onDraw(SkCanvas* canvas) override {
121         SkDrawable* const* drawables = NULL;
122         int drawableCount = 0;
123         if (fDrawableList) {
124             drawables = fDrawableList->begin();
125             drawableCount = fDrawableList->count();
126         }
127         SkRecordDraw(*fRecord, canvas, NULL, drawables, drawableCount, fBBH, NULL/*callback*/);
128     }
129
130     SkPicture* onNewPictureSnapshot() override {
131         SkPicture::SnapshotArray* pictList = NULL;
132         if (fDrawableList) {
133             // TODO: should we plumb-down the BBHFactory and recordFlags from our host
134             //       PictureRecorder?
135             pictList = fDrawableList->newDrawableSnapshot();
136         }
137
138         SkAutoTUnref<SkLayerInfo> saveLayerData;
139
140         if (fBBH && fDoSaveLayerInfo) {
141             SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
142
143             saveLayerData.reset(SkNEW_ARGS(SkLayerInfo, (key)));
144
145             SkBBoxHierarchy* bbh = NULL;    // we've already computed fBBH (received in constructor)
146             // TODO: update saveLayer info computation to reuse the already computed
147             // bounds in 'fBBH'
148             SkRecordComputeLayers(fBounds, *fRecord, pictList, bbh, saveLayerData);
149         }
150
151         SkPicture* pict = SkNEW_ARGS(SkPicture, (fBounds, fRecord, pictList, fBBH));
152
153         if (saveLayerData) {
154             pict->EXPERIMENTAL_addAccelData(saveLayerData);
155         }
156         return pict;
157     }
158 };
159
160 SkDrawable* SkPictureRecorder::endRecordingAsDrawable() {
161     // TODO: delay as much of this work until just before first playback?
162     SkRecordOptimize(fRecord);
163
164     if (fBBH.get()) {
165         SkRecordFillBounds(fCullRect, *fRecord, fBBH.get());
166     }
167
168     SkDrawable* drawable = SkNEW_ARGS(SkRecordedDrawable,
169                                         (fRecord, fBBH, fRecorder->detachDrawableList(),
170                                          fCullRect,
171                                          SkToBool(fFlags & kComputeSaveLayerInfo_RecordFlag)));
172
173     // release our refs now, so only the drawable will be the owner.
174     fRecorder.reset(NULL);
175     fRecord.reset(NULL);
176     fBBH.reset(NULL);
177
178     return drawable;
179 }