Picture.cpp revision 8872b38ef403cc2c44aca07d392f5e9426fd7f54
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 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 39Canvas* 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 return Canvas::create_canvas(canvas); 46} 47 48void Picture::endRecording() { 49 if (NULL != mRecorder.get()) { 50 mPicture.reset(mRecorder->endRecording()); 51 mRecorder.reset(NULL); 52 } 53} 54 55int Picture::width() const { 56 if (NULL != mPicture.get()) { 57 SkASSERT(mPicture->width() == mWidth); 58 SkASSERT(mPicture->height() == mHeight); 59 } 60 61 return mWidth; 62} 63 64int Picture::height() const { 65 if (NULL != mPicture.get()) { 66 SkASSERT(mPicture->width() == mWidth); 67 SkASSERT(mPicture->height() == mHeight); 68 } 69 70 return mHeight; 71} 72 73Picture* Picture::CreateFromStream(SkStream* stream) { 74 Picture* newPict = new Picture; 75 76 newPict->mPicture.reset(SkPicture::CreateFromStream(stream)); 77 if (NULL != newPict->mPicture.get()) { 78 newPict->mWidth = newPict->mPicture->width(); 79 newPict->mHeight = newPict->mPicture->height(); 80 } 81 82 return newPict; 83} 84 85void Picture::serialize(SkWStream* stream) const { 86 if (NULL != mRecorder.get()) { 87 SkAutoTDelete<SkPicture> tempPict(this->makePartialCopy()); 88 tempPict->serialize(stream); 89 } else if (NULL != mPicture.get()) { 90 mPicture->serialize(stream); 91 } else { 92 SkPicture empty; 93 empty.serialize(stream); 94 } 95} 96 97void Picture::draw(Canvas* canvas) { 98 if (NULL != mRecorder.get()) { 99 this->endRecording(); 100 SkASSERT(NULL != mPicture.get()); 101 } 102 if (NULL != mPicture.get()) { 103 // TODO: remove this const_cast once pictures are immutable 104 const_cast<SkPicture*>(mPicture.get())->draw(canvas->getSkCanvas()); 105 } 106} 107 108SkPicture* 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.endRecording(); 116} 117 118}; // namespace android 119