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; |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 23 | virtual SkSurface* onNewSurface(const SkImageInfo&) 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; |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 28 | virtual void onDiscard() SK_OVERRIDE; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 29 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 30 | private: |
| 31 | SkPicture* fPicture; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 33 | typedef SkSurface_Base INHERITED; |
| 34 | }; |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | SkSurface_Picture::SkSurface_Picture(int width, int height) : INHERITED(width, height) { |
| 39 | fPicture = NULL; |
| 40 | } |
| 41 | |
| 42 | SkSurface_Picture::~SkSurface_Picture() { |
| 43 | SkSafeUnref(fPicture); |
| 44 | } |
| 45 | |
| 46 | SkCanvas* SkSurface_Picture::onNewCanvas() { |
| 47 | if (!fPicture) { |
| 48 | fPicture = SkNEW(SkPicture); |
| 49 | } |
| 50 | SkCanvas* canvas = fPicture->beginRecording(this->width(), this->height()); |
| 51 | canvas->ref(); // our caller will call unref() |
| 52 | return canvas; |
| 53 | } |
| 54 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 55 | SkSurface* SkSurface_Picture::onNewSurface(const SkImageInfo& info) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 56 | return SkSurface::NewPicture(info.fWidth, info.fHeight); |
| 57 | } |
| 58 | |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 59 | SkImage* SkSurface_Picture::onNewImageSnapshot() { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 60 | if (fPicture) { |
| 61 | return SkNewImageFromPicture(fPicture); |
| 62 | } else { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 63 | SkImageInfo info; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 64 | info.fWidth = info.fHeight = 0; |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame^] | 65 | info.fColorType = kN32_SkColorType; |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 66 | info.fAlphaType = kOpaque_SkAlphaType; |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 67 | return SkImage::NewRasterCopy(info, NULL, 0); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | void SkSurface_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 72 | const SkPaint* paint) { |
| 73 | if (!fPicture) { |
| 74 | return; |
| 75 | } |
| 76 | SkImagePrivDrawPicture(canvas, fPicture, x, y, paint); |
| 77 | } |
| 78 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 79 | void SkSurface_Picture::onCopyOnWrite(ContentChangeMode mode) { |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 80 | // We always spawn a copy of the recording picture when we |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 81 | // are asked for a snapshot, so we never need to do anything here. |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 82 | if (kDiscard_ContentChangeMode == mode) { |
| 83 | this->SkSurface_Picture::onDiscard(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void SkSurface_Picture::onDiscard() { |
| 88 | if (NULL != fPicture) { |
| 89 | fPicture->beginRecording(this->width(), this->height()); |
| 90 | } |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | |
| 96 | SkSurface* SkSurface::NewPicture(int width, int height) { |
| 97 | if ((width | height) < 0) { |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | return SkNEW_ARGS(SkSurface_Picture, (width, height)); |
| 102 | } |