Home | History | Annotate | Download | only in image
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkImage_Base.h"
      9 #include "SkImagePriv.h"
     10 #include "SkPicture.h"
     11 
     12 class SkImage_Picture : public SkImage_Base {
     13 public:
     14     SkImage_Picture(SkPicture*);
     15     virtual ~SkImage_Picture();
     16 
     17     virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
     18 
     19 private:
     20     SkPicture*  fPicture;
     21 
     22     typedef SkImage_Base INHERITED;
     23 };
     24 
     25 ///////////////////////////////////////////////////////////////////////////////
     26 
     27 SkImage_Picture::SkImage_Picture(SkPicture* pict) : INHERITED(pict->width(), pict->height()) {
     28     pict->endRecording();
     29     pict->ref();
     30     fPicture = pict;
     31 }
     32 
     33 SkImage_Picture::~SkImage_Picture() {
     34     fPicture->unref();
     35 }
     36 
     37 void SkImage_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
     38                              const SkPaint* paint) {
     39     SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
     40 }
     41 
     42 SkImage* SkNewImageFromPicture(const SkPicture* srcPicture) {
     43     /**
     44      *  We want to snapshot the playback status of the picture, w/o affecting
     45      *  its ability to continue recording (if needed).
     46      *
     47      *  Optimally this will shared as much data/buffers as it can with
     48      *  srcPicture, and srcPicture will perform a copy-on-write as needed if it
     49      *  needs to mutate them later on.
     50      */
     51     SkAutoTUnref<SkPicture> playback(SkNEW_ARGS(SkPicture, (*srcPicture)));
     52 
     53     return SkNEW_ARGS(SkImage_Picture, (playback));
     54 }
     55