Picture.cpp revision a0dc40409265fa46c3ceee91ec68d2f45b8f67e8
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 SkPicture empty; 92 empty.serialize(stream); 93 } 94} 95 96void Picture::draw(SkCanvas* canvas) { 97 if (NULL != mRecorder.get()) { 98 this->endRecording(); 99 SkASSERT(NULL != mPicture.get()); 100 } 101 if (NULL != mPicture.get()) { 102 // TODO: remove this const_cast once pictures are immutable 103 const_cast<SkPicture*>(mPicture.get())->draw(canvas); 104 } 105} 106 107SkPicture* Picture::makePartialCopy() const { 108 SkASSERT(NULL != mRecorder.get()); 109 110 SkPictureRecorder reRecorder; 111 112 SkCanvas* canvas = reRecorder.beginRecording(mWidth, mHeight, NULL, 0); 113 mRecorder->partialReplay(canvas); 114 return reRecorder.endRecording(); 115} 116 117}; // namespace android 118