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 | #ifndef SkSurface_Base_DEFINED |
| 9 | #define SkSurface_Base_DEFINED |
| 10 | |
reed | 29c857d | 2014-09-21 10:25:07 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 12 | #include "SkSurface.h" |
| 13 | #include "SkSurfacePriv.h" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 14 | |
| 15 | class SkSurface_Base : public SkSurface { |
| 16 | public: |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 17 | SkSurface_Base(int width, int height, const SkSurfaceProps*); |
| 18 | SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 19 | virtual ~SkSurface_Base(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 20 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 21 | virtual GrBackendObject onGetTextureHandle(BackendHandleAccess) { |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 25 | virtual bool onGetRenderTargetHandle(GrBackendObject*, BackendHandleAccess) { |
| 26 | return false; |
| 27 | } |
| 28 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Allocate a canvas that will draw into this surface. We will cache this |
| 31 | * canvas, to return the same object to the caller multiple times. We |
| 32 | * take ownership, and will call unref() on the canvas when we go out of |
| 33 | * scope. |
| 34 | */ |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 35 | virtual SkCanvas* onNewCanvas() = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 36 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 37 | virtual SkSurface* onNewSurface(const SkImageInfo&) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 38 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Allocate an SkImage that represents the current contents of the surface. |
| 41 | * This needs to be able to outlive the surface itself (if need be), and |
| 42 | * must faithfully represent the current contents, even if the surface |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 43 | * is changed after this called (e.g. it is drawn to via its canvas). |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 44 | */ |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 45 | virtual SkImage* onNewImageSnapshot(Budgeted) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 46 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Default implementation: |
| 49 | * |
| 50 | * image = this->newImageSnapshot(); |
| 51 | * if (image) { |
| 52 | * image->draw(canvas, ...); |
| 53 | * image->unref(); |
| 54 | * } |
| 55 | */ |
| 56 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); |
| 57 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 58 | /** |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 59 | * Called as a performance hint when the Surface is allowed to make it's contents |
| 60 | * undefined. |
| 61 | */ |
| 62 | virtual void onDiscard() {} |
| 63 | |
| 64 | /** |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 65 | * If the surface is about to change, we call this so that our subclass |
| 66 | * can optionally fork their backend (copy-on-write) in case it was |
| 67 | * being shared with the cachedImage. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 68 | */ |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 69 | virtual void onCopyOnWrite(ContentChangeMode) = 0; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 70 | |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Signal the surface to remind its backing store that it's mutable again. |
| 73 | * Called only when we _didn't_ copy-on-write; we assume the copies start mutable. |
| 74 | */ |
| 75 | virtual void onRestoreBackingMutability() {} |
| 76 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 77 | inline SkCanvas* getCachedCanvas(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 78 | inline SkImage* getCachedImage(Budgeted); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 79 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | bool hasCachedImage() const { return fCachedImage != nullptr; } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 81 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 82 | // called by SkSurface to compute a new genID |
| 83 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 85 | private: |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 86 | SkCanvas* fCachedCanvas; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 87 | SkImage* fCachedImage; |
| 88 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 89 | void aboutToDraw(ContentChangeMode mode); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 90 | |
| 91 | // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw |
| 92 | // would trigger a copy-on-write. |
| 93 | bool outstandingImageSnapshot() const; |
| 94 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 95 | friend class SkCanvas; |
| 96 | friend class SkSurface; |
| 97 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 98 | typedef SkSurface INHERITED; |
| 99 | }; |
| 100 | |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 101 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | if (nullptr == fCachedCanvas) { |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 103 | fCachedCanvas = this->onNewCanvas(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 104 | if (fCachedCanvas) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 105 | fCachedCanvas->setSurfaceBase(this); |
| 106 | } |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 107 | } |
| 108 | return fCachedCanvas; |
| 109 | } |
| 110 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 111 | SkImage* SkSurface_Base::getCachedImage(Budgeted budgeted) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | if (nullptr == fCachedImage) { |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 113 | fCachedImage = this->onNewImageSnapshot(budgeted); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 114 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 115 | } |
| 116 | return fCachedImage; |
| 117 | } |
| 118 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 119 | #endif |