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