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 validate();
35 } else {
36 mWidth = 0;
37 mHeight = 0;
38 }
39 }
40
beginRecording(int width,int height)41 Canvas* Picture::beginRecording(int width, int height) {
42 mPicture.reset(NULL);
43 mRecorder.reset(new SkPictureRecorder);
44 mWidth = width;
45 mHeight = height;
46 SkCanvas* canvas = mRecorder->beginRecording(SkIntToScalar(width), SkIntToScalar(height));
47 return Canvas::create_canvas(canvas, Canvas::XformToSRGB::kDefer);
48 }
49
endRecording()50 void Picture::endRecording() {
51 if (NULL != mRecorder.get()) {
52 mPicture = mRecorder->finishRecordingAsPicture();
53 validate();
54 mRecorder.reset(NULL);
55 }
56 }
57
width() const58 int Picture::width() const {
59 validate();
60 return mWidth;
61 }
62
height() const63 int Picture::height() const {
64 validate();
65 return mHeight;
66 }
67
CreateFromStream(SkStream * stream)68 Picture* Picture::CreateFromStream(SkStream* stream) {
69 Picture* newPict = new Picture;
70
71 sk_sp<SkPicture> skPicture = SkPicture::MakeFromStream(stream);
72 if (NULL != skPicture) {
73 newPict->mPicture = skPicture;
74
75 const SkIRect cullRect = skPicture->cullRect().roundOut();
76 newPict->mWidth = cullRect.width();
77 newPict->mHeight = cullRect.height();
78 }
79
80 return newPict;
81 }
82
serialize(SkWStream * stream) const83 void Picture::serialize(SkWStream* stream) const {
84 if (NULL != mRecorder.get()) {
85 this->makePartialCopy()->serialize(stream);
86 } else if (NULL != mPicture.get()) {
87 validate();
88 mPicture->serialize(stream);
89 } else {
90 // serialize "empty" picture
91 SkPictureRecorder recorder;
92 recorder.beginRecording(0, 0);
93 recorder.finishRecordingAsPicture()->serialize(stream);
94 }
95 }
96
draw(Canvas * canvas)97 void Picture::draw(Canvas* canvas) {
98 if (NULL != mRecorder.get()) {
99 this->endRecording();
100 SkASSERT(NULL != mPicture.get());
101 }
102 validate();
103 if (NULL != mPicture.get()) {
104 mPicture->playback(canvas->asSkCanvas());
105 }
106 }
107
makePartialCopy() const108 sk_sp<SkPicture> Picture::makePartialCopy() const {
109 SkASSERT(NULL != mRecorder.get());
110
111 SkPictureRecorder reRecorder;
112
113 SkCanvas* canvas = reRecorder.beginRecording(mWidth, mHeight, NULL, 0);
114 mRecorder->partialReplay(canvas);
115 return reRecorder.finishRecordingAsPicture();
116 }
117
validate() const118 void Picture::validate() const {
119 #ifdef SK_DEBUG
120 if (NULL != mPicture.get()) {
121 SkRect cullRect = mPicture->cullRect();
122 SkRect myRect = SkRect::MakeWH(SkIntToScalar(mWidth), SkIntToScalar(mHeight));
123 SkASSERT(cullRect == myRect);
124 }
125 #endif
126 }
127
128 }; // namespace android
129