Home | History | Annotate | Download | only in graphics
      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 
     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 
     40 Picture::Picture(sk_sp<SkPicture>&& src) {
     41     mPicture = std::move(src);
     42     mWidth = 0;
     43     mHeight = 0;
     44 }
     45 
     46 Canvas* Picture::beginRecording(int width, int height) {
     47     mPicture.reset(NULL);
     48     mRecorder.reset(new SkPictureRecorder);
     49     mWidth = width;
     50     mHeight = height;
     51     SkCanvas* canvas = mRecorder->beginRecording(SkIntToScalar(width), SkIntToScalar(height));
     52     return Canvas::create_canvas(canvas);
     53 }
     54 
     55 void Picture::endRecording() {
     56     if (NULL != mRecorder.get()) {
     57         mPicture = mRecorder->finishRecordingAsPicture();
     58         mRecorder.reset(NULL);
     59     }
     60 }
     61 
     62 int Picture::width() const {
     63     return mWidth;
     64 }
     65 
     66 int Picture::height() const {
     67     return mHeight;
     68 }
     69 
     70 Picture* Picture::CreateFromStream(SkStream* stream) {
     71     Picture* newPict = new Picture;
     72 
     73     sk_sp<SkPicture> skPicture = SkPicture::MakeFromStream(stream);
     74     if (NULL != skPicture) {
     75         newPict->mPicture = skPicture;
     76 
     77         const SkIRect cullRect = skPicture->cullRect().roundOut();
     78         newPict->mWidth = cullRect.width();
     79         newPict->mHeight = cullRect.height();
     80     }
     81 
     82     return newPict;
     83 }
     84 
     85 void Picture::serialize(SkWStream* stream) const {
     86     if (NULL != mRecorder.get()) {
     87         this->makePartialCopy()->serialize(stream);
     88     } else if (NULL != mPicture.get()) {
     89         mPicture->serialize(stream);
     90     } else {
     91         // serialize "empty" picture
     92         SkPictureRecorder recorder;
     93         recorder.beginRecording(0, 0);
     94         recorder.finishRecordingAsPicture()->serialize(stream);
     95     }
     96 }
     97 
     98 void Picture::draw(Canvas* canvas) {
     99     if (NULL != mRecorder.get()) {
    100         this->endRecording();
    101         SkASSERT(NULL != mPicture.get());
    102     }
    103     if (NULL != mPicture.get()) {
    104         mPicture->playback(canvas->asSkCanvas());
    105     }
    106 }
    107 
    108 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 
    118 }; // namespace android
    119