Picture.cpp revision d40e89258e0baa903f8fa9599dde12ed995b40c0
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 // 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 52void Picture::endRecording() { 53 if (NULL != mRecorder.get()) { 54 mPicture.reset(mRecorder->endRecording()); 55 mRecorder.reset(NULL); 56 } 57} 58 59int 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 68int 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 77Picture* 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 89void 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 SkPictureRecorder recorder; 97 recorder.beginRecording(0, 0); 98 SkAutoTUnref<SkPicture> empty(recorder.endRecording()); 99 empty->serialize(stream); 100 } 101} 102 103void Picture::draw(Canvas* canvas) { 104 if (NULL != mRecorder.get()) { 105 this->endRecording(); 106 SkASSERT(NULL != mPicture.get()); 107 } 108 if (NULL != mPicture.get()) { 109 mPicture.get()->playback(canvas->getSkCanvas()); 110 } 111} 112 113SkPicture* Picture::makePartialCopy() const { 114 SkASSERT(NULL != mRecorder.get()); 115 116 SkPictureRecorder reRecorder; 117 118 SkCanvas* canvas = reRecorder.beginRecording(mWidth, mHeight, NULL, 0); 119 mRecorder->partialReplay(canvas); 120 return reRecorder.endRecording(); 121} 122 123}; // namespace android 124