SkPictureRecorder.cpp revision 46616af01b412ea984a516fda1ed8ec08e689f29
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 "SkPictureRecord.h"
9#include "SkPictureRecorder.h"
10#include "SkRecord.h"
11#include "SkRecordDraw.h"
12#include "SkRecorder.h"
13#include "SkTypes.h"
14
15SkPictureRecorder::SkPictureRecorder() {}
16
17SkPictureRecorder::~SkPictureRecorder() {}
18
19SkCanvas* SkPictureRecorder::beginRecording(SkScalar width, SkScalar height,
20                                            SkBBHFactory* bbhFactory /* = NULL */,
21                                            uint32_t recordFlags /* = 0 */) {
22    return EXPERIMENTAL_beginRecording(width, height, bbhFactory);
23}
24
25SkCanvas* SkPictureRecorder::DEPRECATED_beginRecording(SkScalar width, SkScalar height,
26                                                       SkBBHFactory* bbhFactory /* = NULL */,
27                                                       uint32_t recordFlags /* = 0 */) {
28    SkASSERT(!bbhFactory);  // No longer suppported with this backend.
29
30    fCullWidth = width;
31    fCullHeight = height;
32    fPictureRecord.reset(SkNEW_ARGS(SkPictureRecord, (SkISize::Make(width, height), recordFlags)));
33
34    fPictureRecord->beginRecording();
35    return this->getRecordingCanvas();
36}
37
38SkCanvas* SkPictureRecorder::EXPERIMENTAL_beginRecording(SkScalar width, SkScalar height,
39                                                         SkBBHFactory* bbhFactory /* = NULL */) {
40    fCullWidth = width;
41    fCullHeight = height;
42
43    if (bbhFactory) {
44        fBBH.reset((*bbhFactory)(width, height));
45        SkASSERT(fBBH.get());
46    }
47
48    fRecord.reset(SkNEW(SkRecord));
49    fRecorder.reset(SkNEW_ARGS(SkRecorder, (fRecord.get(), width, height)));
50    return this->getRecordingCanvas();
51}
52
53SkCanvas* SkPictureRecorder::getRecordingCanvas() {
54    if (fRecorder.get()) {
55        return fRecorder.get();
56    }
57    return fPictureRecord.get();
58}
59
60SkPicture* SkPictureRecorder::endRecording() {
61    SkPicture* picture = NULL;
62
63    if (fRecord.get()) {
64        picture = SkNEW_ARGS(SkPicture, (fCullWidth, fCullHeight,
65                                         fRecord.detach(), fBBH.get()));
66    }
67
68    if (fPictureRecord.get()) {
69        fPictureRecord->endRecording();
70        const bool deepCopyOps = false;
71        picture = SkNEW_ARGS(SkPicture, (fCullWidth, fCullHeight,
72                                         *fPictureRecord.get(), deepCopyOps));
73    }
74
75    return picture;
76}
77
78void SkPictureRecorder::partialReplay(SkCanvas* canvas) const {
79    if (NULL == canvas) {
80        return;
81    }
82
83    if (fRecord.get()) {
84        SkRecordDraw(*fRecord, canvas, NULL/*bbh*/, NULL/*callback*/);
85    }
86
87    if (fPictureRecord.get()) {
88        const bool deepCopyOps = true;
89        SkPicture picture(fCullWidth, fCullHeight,
90                          *fPictureRecord.get(), deepCopyOps);
91        picture.playback(canvas);
92    }
93}
94