Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSurface_Base.h" | 8 #include "SkSurface_Base.h" |
| 9 #include "SkImagePriv.h" | 9 #include "SkImagePriv.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 } | 52 } |
| 53 | 53 |
| 54 SkImage* SkSurface_Base::getCachedImage() { | 54 SkImage* SkSurface_Base::getCachedImage() { |
| 55 if (NULL == fCachedImage) { | 55 if (NULL == fCachedImage) { |
| 56 fCachedImage = this->onNewImageSnapshot(); | 56 fCachedImage = this->onNewImageSnapshot(); |
| 57 this->installIntoCanvasForDirtyNotification(); | 57 this->installIntoCanvasForDirtyNotification(); |
| 58 } | 58 } |
| 59 return fCachedImage; | 59 return fCachedImage; |
| 60 } | 60 } |
| 61 | 61 |
| 62 void SkSurface_Base::aboutToDraw(SkCanvas* canvas) { | 62 void SkSurface_Base::aboutToDraw() { |
| 63 this->dirtyGenerationID(); | 63 this->dirtyGenerationID(); |
| 64 | 64 |
|
reed1
2013/04/15 18:56:33
How do we know that fCachedCanvas is always presen
Justin Novosad
2013/04/15 19:08:57
Done.
| |
| 65 if (canvas) { | 65 if (NULL == fCachedCanvas->getSurfaceBase()) { |
| 66 SkASSERT(canvas == fCachedCanvas); | 66 // getSurfaceBase will be non-NULL if there are SkImages |
|
reed1
2013/04/15 18:56:33
Why is this test more robust than just checking fC
Justin Novosad
2013/04/15 19:08:57
Done.
| |
| 67 SkASSERT(canvas->getSurfaceBase() == this); | 67 // referencing the canvas backingstore |
| 68 canvas->setSurfaceBase(NULL); | 68 return; |
| 69 } | 69 } |
| 70 | 70 |
| 71 if (fCachedImage) { | 71 SkASSERT(NULL != fCachedImage); |
| 72 // the surface may need to fork its backend, if its sharing it with | 72 SkASSERT(fCachedCanvas->getSurfaceBase() == this); |
| 73 // the cached image. Note: we only call if there is an outstanding owner | 73 fCachedCanvas->setSurfaceBase(NULL); |
| 74 // on the image (besides us). | |
| 75 if (fCachedImage->getRefCnt() > 1) { | |
| 76 this->onCopyOnWrite(fCachedImage, canvas); | |
| 77 } | |
| 78 | 74 |
| 79 // regardless of copy-on-write, we must drop our cached image now, so | 75 // the surface may need to fork its backend, if its sharing it with |
| 80 // that the next request will get our new contents. | 76 // the cached image. Note: we only call if there is an outstanding owner |
| 81 fCachedImage->unref(); | 77 // on the image (besides us). |
| 82 fCachedImage = NULL; | 78 if (fCachedImage->getRefCnt() > 1) { |
| 79 this->onCopyOnWrite(); | |
| 83 } | 80 } |
| 81 | |
| 82 // regardless of copy-on-write, we must drop our cached image now, so | |
| 83 // that the next request will get our new contents. | |
| 84 fCachedImage->unref(); | |
| 85 fCachedImage = NULL; | |
| 84 } | 86 } |
| 85 | 87 |
| 86 uint32_t SkSurface_Base::newGenerationID() { | 88 uint32_t SkSurface_Base::newGenerationID() { |
| 87 this->installIntoCanvasForDirtyNotification(); | 89 this->installIntoCanvasForDirtyNotification(); |
| 88 | 90 |
| 89 static int32_t gID; | 91 static int32_t gID; |
| 90 return sk_atomic_inc(&gID) + 1; | 92 return sk_atomic_inc(&gID) + 1; |
| 91 } | 93 } |
| 92 | 94 |
| 93 static SkSurface_Base* asSB(SkSurface* surface) { | 95 static SkSurface_Base* asSB(SkSurface* surface) { |
| 94 return static_cast<SkSurface_Base*>(surface); | 96 return static_cast<SkSurface_Base*>(surface); |
| 95 } | 97 } |
| 96 | 98 |
| 97 /////////////////////////////////////////////////////////////////////////////// | 99 /////////////////////////////////////////////////////////////////////////////// |
| 98 | 100 |
| 99 SkSurface::SkSurface(int width, int height) : fWidth(width), fHeight(height) { | 101 SkSurface::SkSurface(int width, int height) : fWidth(width), fHeight(height) { |
| 100 SkASSERT(width >= 0); | 102 SkASSERT(width >= 0); |
| 101 SkASSERT(height >= 0); | 103 SkASSERT(height >= 0); |
| 102 fGenerationID = 0; | 104 fGenerationID = 0; |
| 103 } | 105 } |
| 104 | 106 |
| 105 uint32_t SkSurface::generationID() { | 107 uint32_t SkSurface::generationID() { |
| 106 if (0 == fGenerationID) { | 108 if (0 == fGenerationID) { |
| 107 fGenerationID = asSB(this)->newGenerationID(); | 109 fGenerationID = asSB(this)->newGenerationID(); |
| 108 } | 110 } |
| 109 return fGenerationID; | 111 return fGenerationID; |
| 110 } | 112 } |
| 111 | 113 |
| 112 void SkSurface::notifyContentChanged() { | 114 void SkSurface::notifyContentChanged() { |
| 113 asSB(this)->aboutToDraw(NULL); | 115 asSB(this)->aboutToDraw(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 SkCanvas* SkSurface::getCanvas() { | 118 SkCanvas* SkSurface::getCanvas() { |
| 117 return asSB(this)->getCachedCanvas(); | 119 return asSB(this)->getCachedCanvas(); |
| 118 } | 120 } |
| 119 | 121 |
| 120 SkImage* SkSurface::newImageSnapshot() { | 122 SkImage* SkSurface::newImageSnapshot() { |
| 121 SkImage* image = asSB(this)->getCachedImage(); | 123 SkImage* image = asSB(this)->getCachedImage(); |
| 122 SkSafeRef(image); // the caller will call unref() to balance this | 124 SkSafeRef(image); // the caller will call unref() to balance this |
| 123 return image; | 125 return image; |
| 124 } | 126 } |
| 125 | 127 |
| 126 SkSurface* SkSurface::newSurface(const SkImage::Info& info) { | 128 SkSurface* SkSurface::newSurface(const SkImage::Info& info) { |
| 127 return asSB(this)->onNewSurface(info); | 129 return asSB(this)->onNewSurface(info); |
| 128 } | 130 } |
| 129 | 131 |
| 130 void SkSurface::draw(SkCanvas* canvas, SkScalar x, SkScalar y, | 132 void SkSurface::draw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 131 const SkPaint* paint) { | 133 const SkPaint* paint) { |
| 132 return asSB(this)->onDraw(canvas, x, y, paint); | 134 return asSB(this)->onDraw(canvas, x, y, paint); |
| 133 } | 135 } |
| OLD | NEW |