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 #include "SkStream.h"
19
20 #include <memory>
21 #include <hwui/Canvas.h>
22
23 namespace android {
24
Picture(const Picture * src)25 Picture::Picture(const Picture* src) {
26 if (NULL != src) {
27 mWidth = src->width();
28 mHeight = src->height();
29 if (NULL != src->mPicture.get()) {
30 mPicture = src->mPicture;
31 } else if (NULL != src->mRecorder.get()) {
32 mPicture = src->makePartialCopy();
33 }
34 } else {
35 mWidth = 0;
36 mHeight = 0;
37 }
38 }
39
beginRecording(int width,int height)40 Canvas* Picture::beginRecording(int width, int height) {
41 mPicture.reset(NULL);
42 mRecorder.reset(new SkPictureRecorder);
43 mWidth = width;
44 mHeight = height;
45 SkCanvas* canvas = mRecorder->beginRecording(SkIntToScalar(width), SkIntToScalar(height));
46 return Canvas::create_canvas(canvas);
47 }
48
endRecording()49 void Picture::endRecording() {
50 if (NULL != mRecorder.get()) {
51 mPicture = mRecorder->finishRecordingAsPicture();
52 mRecorder.reset(NULL);
53 }
54 }
55
width() const56 int Picture::width() const {
57 return mWidth;
58 }
59
height() const60 int Picture::height() const {
61 return mHeight;
62 }
63
CreateFromStream(SkStream * stream)64 Picture* Picture::CreateFromStream(SkStream* stream) {
65 Picture* newPict = new Picture;
66
67 sk_sp<SkPicture> skPicture = SkPicture::MakeFromStream(stream);
68 if (NULL != skPicture) {
69 newPict->mPicture = skPicture;
70
71 const SkIRect cullRect = skPicture->cullRect().roundOut();
72 newPict->mWidth = cullRect.width();
73 newPict->mHeight = cullRect.height();
74 }
75
76 return newPict;
77 }
78
serialize(SkWStream * stream) const79 void Picture::serialize(SkWStream* stream) const {
80 if (NULL != mRecorder.get()) {
81 this->makePartialCopy()->serialize(stream);
82 } else if (NULL != mPicture.get()) {
83 mPicture->serialize(stream);
84 } else {
85 // serialize "empty" picture
86 SkPictureRecorder recorder;
87 recorder.beginRecording(0, 0);
88 recorder.finishRecordingAsPicture()->serialize(stream);
89 }
90 }
91
draw(Canvas * canvas)92 void Picture::draw(Canvas* canvas) {
93 if (NULL != mRecorder.get()) {
94 this->endRecording();
95 SkASSERT(NULL != mPicture.get());
96 }
97 if (NULL != mPicture.get()) {
98 mPicture->playback(canvas->asSkCanvas());
99 }
100 }
101
makePartialCopy() const102 sk_sp<SkPicture> Picture::makePartialCopy() const {
103 SkASSERT(NULL != mRecorder.get());
104
105 SkPictureRecorder reRecorder;
106
107 SkCanvas* canvas = reRecorder.beginRecording(mWidth, mHeight, NULL, 0);
108 mRecorder->partialReplay(canvas);
109 return reRecorder.finishRecordingAsPicture();
110 }
111
112 }; // namespace android
113