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 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 12 | //SK_DEFINE_INST_COUNT(SkSurface) |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 13 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 14 | /////////////////////////////////////////////////////////////////////////////// |
| 15 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 16 | void SkSurface_Base::installIntoCanvasForDirtyNotification() { |
| 17 | if (fCachedCanvas) { |
| 18 | fCachedCanvas->setSurfaceBase(this); |
| 19 | } |
| 20 | } |
| 21 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 22 | SkSurface_Base::SkSurface_Base(int width, int height) : INHERITED(width, height) { |
| 23 | fCachedCanvas = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 24 | fCachedImage = NULL; |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 25 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 26 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 27 | SkSurface_Base::~SkSurface_Base() { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 28 | // in case the canvas outsurvives us, we null the callback |
| 29 | if (fCachedCanvas) { |
| 30 | fCachedCanvas->setSurfaceBase(NULL); |
| 31 | } |
| 32 | |
| 33 | SkSafeUnref(fCachedImage); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 34 | SkSafeUnref(fCachedCanvas); |
| 35 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 36 | |
| 37 | void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 38 | const SkPaint* paint) { |
| 39 | SkImage* image = this->newImageShapshot(); |
| 40 | if (image) { |
| 41 | image->draw(canvas, x, y, paint); |
| 42 | image->unref(); |
| 43 | } |
| 44 | } |
| 45 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 46 | void SkSurface_Base::onCopyOnWrite(SkImage*, SkCanvas*) {} |
| 47 | |
| 48 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
| 49 | if (NULL == fCachedCanvas) { |
| 50 | fCachedCanvas = this->onNewCanvas(); |
| 51 | this->installIntoCanvasForDirtyNotification(); |
| 52 | } |
| 53 | return fCachedCanvas; |
| 54 | } |
| 55 | |
| 56 | SkImage* SkSurface_Base::getCachedImage() { |
| 57 | if (NULL == fCachedImage) { |
| 58 | fCachedImage = this->onNewImageShapshot(); |
| 59 | this->installIntoCanvasForDirtyNotification(); |
| 60 | } |
| 61 | return fCachedImage; |
| 62 | } |
| 63 | |
| 64 | void SkSurface_Base::aboutToDraw(SkCanvas* canvas) { |
| 65 | this->dirtyGenerationID(); |
| 66 | |
| 67 | if (canvas) { |
| 68 | SkASSERT(canvas == fCachedCanvas); |
| 69 | SkASSERT(canvas->getSurfaceBase() == this); |
| 70 | canvas->setSurfaceBase(NULL); |
| 71 | } |
| 72 | |
| 73 | if (fCachedImage) { |
| 74 | // the surface may need to fork its backend, if its sharing it with |
| 75 | // the cached image. Note: we only call if there is an outstanding owner |
| 76 | // on the image (besides us). |
| 77 | if (fCachedImage->getRefCnt() > 1) { |
| 78 | this->onCopyOnWrite(fCachedImage, canvas); |
| 79 | } |
| 80 | |
| 81 | // regardless of copy-on-write, we must drop our cached image now, so |
| 82 | // that the next request will get our new contents. |
| 83 | fCachedImage->unref(); |
| 84 | fCachedImage = NULL; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | uint32_t SkSurface_Base::newGenerationID() { |
| 89 | this->installIntoCanvasForDirtyNotification(); |
| 90 | |
| 91 | static int32_t gID; |
| 92 | return sk_atomic_inc(&gID) + 1; |
| 93 | } |
| 94 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 95 | static SkSurface_Base* asSB(SkSurface* surface) { |
| 96 | return static_cast<SkSurface_Base*>(surface); |
| 97 | } |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | SkSurface::SkSurface(int width, int height) : fWidth(width), fHeight(height) { |
| 102 | SkASSERT(width >= 0); |
| 103 | SkASSERT(height >= 0); |
| 104 | fGenerationID = 0; |
| 105 | } |
| 106 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 107 | uint32_t SkSurface::generationID() { |
| 108 | if (0 == fGenerationID) { |
| 109 | fGenerationID = asSB(this)->newGenerationID(); |
| 110 | } |
| 111 | return fGenerationID; |
| 112 | } |
| 113 | |
| 114 | void SkSurface::notifyContentChanged() { |
| 115 | asSB(this)->aboutToDraw(NULL); |
| 116 | } |
| 117 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 118 | SkCanvas* SkSurface::getCanvas() { |
| 119 | return asSB(this)->getCachedCanvas(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 120 | } |
| 121 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 122 | SkImage* SkSurface::newImageShapshot() { |
| 123 | SkImage* image = asSB(this)->getCachedImage(); |
| 124 | SkSafeRef(image); // the caller will call unref() to balance this |
| 125 | return image; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 126 | } |
| 127 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame^] | 128 | SkSurface* SkSurface::newSurface(const SkImage::Info& info, SkColorSpace* cs) { |
| 129 | return asSB(this)->onNewSurface(info, cs); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void SkSurface::draw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 133 | const SkPaint* paint) { |
| 134 | return asSB(this)->onDraw(canvas, x, y, paint); |
| 135 | } |
| 136 | |