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 "Canvas.h"
18 #include "Picture.h"
19 
20 #include "SkStream.h"
21 
22 namespace android {
23 
Picture(const Picture * src)24 Picture::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 
beginRecording(int width,int height)39 Canvas* Picture::beginRecording(int width, int height) {
40     mPicture.reset(NULL);
41     mRecorder.reset(new SkPictureRecorder);
42     mWidth = width;
43     mHeight = height;
44     SkCanvas* canvas = mRecorder->beginRecording(width, height, NULL, 0);
45     // the java side will wrap this guy in a Canvas.java, which will call
46     // unref in its finalizer, so we have to ref it here, so that both that
47     // Canvas.java and our picture can both be owners
48     canvas->ref();
49     return Canvas::create_canvas(canvas);
50 }
51 
endRecording()52 void Picture::endRecording() {
53     if (NULL != mRecorder.get()) {
54         mPicture.reset(mRecorder->endRecording());
55         mRecorder.reset(NULL);
56     }
57 }
58 
width() const59 int Picture::width() const {
60     if (NULL != mPicture.get()) {
61         SkASSERT(mPicture->width() == mWidth);
62         SkASSERT(mPicture->height() == mHeight);
63     }
64 
65     return mWidth;
66 }
67 
height() const68 int Picture::height() const {
69     if (NULL != mPicture.get()) {
70         SkASSERT(mPicture->width() == mWidth);
71         SkASSERT(mPicture->height() == mHeight);
72     }
73 
74     return mHeight;
75 }
76 
CreateFromStream(SkStream * stream)77 Picture* Picture::CreateFromStream(SkStream* stream) {
78     Picture* newPict = new Picture;
79 
80     newPict->mPicture.reset(SkPicture::CreateFromStream(stream));
81     if (NULL != newPict->mPicture.get()) {
82         newPict->mWidth = newPict->mPicture->width();
83         newPict->mHeight = newPict->mPicture->height();
84     }
85 
86     return newPict;
87 }
88 
serialize(SkWStream * stream) const89 void Picture::serialize(SkWStream* stream) const {
90     if (NULL != mRecorder.get()) {
91         SkAutoTDelete<SkPicture> tempPict(this->makePartialCopy());
92         tempPict->serialize(stream);
93     } else if (NULL != mPicture.get()) {
94         mPicture->serialize(stream);
95     } else {
96         SkPicture empty;
97         empty.serialize(stream);
98     }
99 }
100 
draw(Canvas * canvas)101 void Picture::draw(Canvas* canvas) {
102     if (NULL != mRecorder.get()) {
103         this->endRecording();
104         SkASSERT(NULL != mPicture.get());
105     }
106     if (NULL != mPicture.get()) {
107         // TODO: remove this const_cast once pictures are immutable
108         const_cast<SkPicture*>(mPicture.get())->draw(canvas->getSkCanvas());
109     }
110 }
111 
makePartialCopy() const112 SkPicture* Picture::makePartialCopy() const {
113     SkASSERT(NULL != mRecorder.get());
114 
115     SkPictureRecorder reRecorder;
116 
117     SkCanvas* canvas = reRecorder.beginRecording(mWidth, mHeight, NULL, 0);
118     mRecorder->partialReplay(canvas);
119     return reRecorder.endRecording();
120 }
121 
122 }; // namespace android
123