reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 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 "SkSurface_Base.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkImagePriv.h" |
| 11 | #include "SkPicture.h" |
| 12 | |
| 13 | /** |
| 14 | * What does it mean to ask for more than one canvas from a picture? |
| 15 | * How do we return an Image and then "continue" recording? |
| 16 | */ |
| 17 | class SkSurface_Picture : public SkSurface_Base { |
| 18 | public: |
| 19 | SkSurface_Picture(int width, int height); |
| 20 | virtual ~SkSurface_Picture(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 21 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 22 | virtual SkCanvas* onNewCanvas() SK_OVERRIDE; |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 23 | virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE; |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 24 | virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 25 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
| 26 | const SkPaint*) SK_OVERRIDE; |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 27 | virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 28 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 29 | private: |
| 30 | SkPicture* fPicture; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 31 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 32 | typedef SkSurface_Base INHERITED; |
| 33 | }; |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | SkSurface_Picture::SkSurface_Picture(int width, int height) : INHERITED(width, height) { |
| 38 | fPicture = NULL; |
| 39 | } |
| 40 | |
| 41 | SkSurface_Picture::~SkSurface_Picture() { |
| 42 | SkSafeUnref(fPicture); |
| 43 | } |
| 44 | |
| 45 | SkCanvas* SkSurface_Picture::onNewCanvas() { |
| 46 | if (!fPicture) { |
| 47 | fPicture = SkNEW(SkPicture); |
| 48 | } |
| 49 | SkCanvas* canvas = fPicture->beginRecording(this->width(), this->height()); |
| 50 | canvas->ref(); // our caller will call unref() |
| 51 | return canvas; |
| 52 | } |
| 53 | |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 54 | SkSurface* SkSurface_Picture::onNewSurface(const SkImage::Info& info) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 55 | return SkSurface::NewPicture(info.fWidth, info.fHeight); |
| 56 | } |
| 57 | |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 58 | SkImage* SkSurface_Picture::onNewImageSnapshot() { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 59 | if (fPicture) { |
| 60 | return SkNewImageFromPicture(fPicture); |
| 61 | } else { |
| 62 | SkImage::Info info; |
| 63 | info.fWidth = info.fHeight = 0; |
| 64 | info.fColorType = SkImage::kPMColor_ColorType; |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 65 | info.fAlphaType = kOpaque_SkAlphaType; |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 66 | return SkImage::NewRasterCopy(info, NULL, 0); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | void SkSurface_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 71 | const SkPaint* paint) { |
| 72 | if (!fPicture) { |
| 73 | return; |
| 74 | } |
| 75 | SkImagePrivDrawPicture(canvas, fPicture, x, y, paint); |
| 76 | } |
| 77 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 78 | void SkSurface_Picture::onCopyOnWrite(ContentChangeMode /*mode*/) { |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 79 | // We always spawn a copy of the recording picture when we |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 80 | // are asked for a snapshot, so we never need to do anything here. |
| 81 | } |
| 82 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | |
| 86 | SkSurface* SkSurface::NewPicture(int width, int height) { |
| 87 | if ((width | height) < 0) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | return SkNEW_ARGS(SkSurface_Picture, (width, height)); |
| 92 | } |