Picture.cpp revision 25bdb88d844a442579debcece2341b285a58dc7b
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Picture.h"
18
19#include "SkCanvas.h"
20#include "SkStream.h"
21
22namespace android {
23
24Picture::Picture(const Picture* src) {
25    if (NULL != src) {
26        mWidth = src->width();
27        mHeight = src->height();
28        if (NULL != src->mPicture.get()) {
29            mPicture.reset(SkRef(src->mPicture.get()));
30        } if (NULL != src->mRecorder.get()) {
31            mPicture.reset(src->makePartialCopy());
32        }
33    } else {
34        mWidth = 0;
35        mHeight = 0;
36    }
37}
38
39SkCanvas* Picture::beginRecording(int width, int height) {
40    mPicture.reset(NULL);
41    mRecorder.reset(new SkPictureRecorder);
42    mWidth = width;
43    mHeight = height;
44    return mRecorder->beginRecording(width, height, NULL, 0);
45}
46
47void Picture::endRecording() {
48    if (NULL != mRecorder.get()) {
49        mPicture.reset(mRecorder->endRecording());
50        mRecorder.reset(NULL);
51    }
52}
53
54int Picture::width() const {
55    if (NULL != mPicture.get()) {
56        SkASSERT(mPicture->width() == mWidth);
57        SkASSERT(mPicture->height() == mHeight);
58    }
59
60    return mWidth;
61}
62
63int Picture::height() const {
64    if (NULL != mPicture.get()) {
65        SkASSERT(mPicture->width() == mWidth);
66        SkASSERT(mPicture->height() == mHeight);
67    }
68
69    return mHeight;
70}
71
72Picture* Picture::CreateFromStream(SkStream* stream) {
73    Picture* newPict = new Picture;
74
75    newPict->mPicture.reset(SkPicture::CreateFromStream(stream));
76    if (NULL != newPict->mPicture.get()) {
77        newPict->mWidth = newPict->mPicture->width();
78        newPict->mHeight = newPict->mPicture->height();
79    }
80
81    return newPict;
82}
83
84void Picture::serialize(SkWStream* stream) const {
85    if (NULL != mRecorder.get()) {
86        SkAutoTDelete<SkPicture> tempPict(this->makePartialCopy());
87        tempPict->serialize(stream);
88    } else if (NULL != mPicture.get()) {
89        mPicture->serialize(stream);
90    } else {
91        SkPictureRecorder recorder;
92        recorder.beginRecording(0, 0);
93        SkAutoTUnref<SkPicture> empty(recorder.endRecording());
94        empty->serialize(stream);
95    }
96}
97
98void Picture::draw(SkCanvas* canvas) {
99    if (NULL != mRecorder.get()) {
100        this->endRecording();
101        SkASSERT(NULL != mPicture.get());
102    }
103    if (NULL != mPicture.get()) {
104        // TODO: remove this const_cast once pictures are immutable
105        const_cast<SkPicture*>(mPicture.get())->draw(canvas);
106    }
107}
108
109SkPicture* Picture::makePartialCopy() const {
110    SkASSERT(NULL != mRecorder.get());
111
112    SkPictureRecorder reRecorder;
113
114    SkCanvas* canvas = reRecorder.beginRecording(mWidth, mHeight, NULL, 0);
115    mRecorder->partialReplay(canvas);
116    return reRecorder.endRecording();
117}
118
119}; // namespace android
120