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 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 8 | #include "SkSurface_Base.h" |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 9 | #include "SkImagePriv.h" |
| 10 | #include "SkCanvas.h" |
| 11 | |
| 12 | /////////////////////////////////////////////////////////////////////////////// |
| 13 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 14 | SkSurface_Base::SkSurface_Base(int width, int height) : INHERITED(width, height) { |
| 15 | fCachedCanvas = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 16 | fCachedImage = NULL; |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 17 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 18 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 19 | SkSurface_Base::~SkSurface_Base() { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 20 | // in case the canvas outsurvives us, we null the callback |
| 21 | if (fCachedCanvas) { |
| 22 | fCachedCanvas->setSurfaceBase(NULL); |
| 23 | } |
| 24 | |
| 25 | SkSafeUnref(fCachedImage); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 26 | SkSafeUnref(fCachedCanvas); |
| 27 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 28 | |
| 29 | void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 30 | const SkPaint* paint) { |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 31 | SkImage* image = this->newImageSnapshot(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 32 | if (image) { |
| 33 | image->draw(canvas, x, y, paint); |
| 34 | image->unref(); |
| 35 | } |
| 36 | } |
| 37 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 38 | void SkSurface_Base::aboutToDraw(ContentChangeMode mode) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 39 | this->dirtyGenerationID(); |
| 40 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 41 | if (NULL != fCachedCanvas) { |
| 42 | SkASSERT(fCachedCanvas->getSurfaceBase() == this || \ |
| 43 | NULL == fCachedCanvas->getSurfaceBase()); |
| 44 | fCachedCanvas->setSurfaceBase(NULL); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 45 | } |
| 46 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 47 | if (NULL != fCachedImage) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 48 | // the surface may need to fork its backend, if its sharing it with |
| 49 | // the cached image. Note: we only call if there is an outstanding owner |
| 50 | // on the image (besides us). |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 51 | if (!fCachedImage->unique()) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 52 | this->onCopyOnWrite(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // regardless of copy-on-write, we must drop our cached image now, so |
| 56 | // that the next request will get our new contents. |
| 57 | fCachedImage->unref(); |
| 58 | fCachedImage = NULL; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | uint32_t SkSurface_Base::newGenerationID() { |
| 63 | this->installIntoCanvasForDirtyNotification(); |
| 64 | |
| 65 | static int32_t gID; |
| 66 | return sk_atomic_inc(&gID) + 1; |
| 67 | } |
| 68 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 69 | static SkSurface_Base* asSB(SkSurface* surface) { |
| 70 | return static_cast<SkSurface_Base*>(surface); |
| 71 | } |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | SkSurface::SkSurface(int width, int height) : fWidth(width), fHeight(height) { |
| 76 | SkASSERT(width >= 0); |
| 77 | SkASSERT(height >= 0); |
| 78 | fGenerationID = 0; |
| 79 | } |
| 80 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 81 | uint32_t SkSurface::generationID() { |
| 82 | if (0 == fGenerationID) { |
| 83 | fGenerationID = asSB(this)->newGenerationID(); |
| 84 | } |
| 85 | return fGenerationID; |
| 86 | } |
| 87 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 88 | void SkSurface::notifyContentWillChange(ContentChangeMode mode) { |
| 89 | asSB(this)->aboutToDraw(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 92 | SkCanvas* SkSurface::getCanvas() { |
| 93 | return asSB(this)->getCachedCanvas(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 96 | SkImage* SkSurface::newImageSnapshot() { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 97 | SkImage* image = asSB(this)->getCachedImage(); |
| 98 | SkSafeRef(image); // the caller will call unref() to balance this |
| 99 | return image; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 100 | } |
| 101 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 102 | SkSurface* SkSurface::newSurface(const SkImageInfo& info) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 103 | return asSB(this)->onNewSurface(info); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void SkSurface::draw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 107 | const SkPaint* paint) { |
| 108 | return asSB(this)->onDraw(canvas, x, y, paint); |
| 109 | } |