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 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 22 | virtual GrBackendTexture onGetBackendTexture(BackendHandleAccess); |
| 23 | virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess); |
| 24 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Allocate a canvas that will draw into this surface. We will cache this |
| 27 | * canvas, to return the same object to the caller multiple times. We |
| 28 | * take ownership, and will call unref() on the canvas when we go out of |
| 29 | * scope. |
| 30 | */ |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 31 | virtual SkCanvas* onNewCanvas() = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 33 | virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 34 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Allocate an SkImage that represents the current contents of the surface. |
| 37 | * This needs to be able to outlive the surface itself (if need be), and |
| 38 | * must faithfully represent the current contents, even if the surface |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 39 | * is changed after this called (e.g. it is drawn to via its canvas). |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 40 | * |
| 41 | * If a subset is specified, the the impl must make a copy, rather than try to wait |
| 42 | * on copy-on-write. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 43 | */ |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 44 | virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 45 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 46 | virtual void onWritePixels(const SkPixmap&, int x, int y) = 0; |
| 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. |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 80 | * Inserts the requested number of semaphores for the gpu to signal when work is complete on the |
| 81 | * gpu and inits the array of GrBackendSemaphores with the signaled semaphores. |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 82 | */ |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 83 | virtual GrSemaphoresSubmitted onFlush(int numSemaphores, |
| 84 | GrBackendSemaphore signalSemaphores[]) { |
| 85 | return GrSemaphoresSubmitted::kNo; |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 86 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Caused the current backend 3D API to wait on the passed in semaphores before executing new |
| 90 | * commands on the gpu. Any previously submitting commands will not be blocked by these |
| 91 | * semaphores. |
| 92 | */ |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 93 | virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) { |
| 94 | return false; |
| 95 | } |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 96 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 97 | virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; } |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 98 | virtual bool onDraw(const SkDeferredDisplayList*) { return false; } |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 99 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 100 | inline SkCanvas* getCachedCanvas(); |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 101 | inline sk_sp<SkImage> refCachedImage(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 102 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | bool hasCachedImage() const { return fCachedImage != nullptr; } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 104 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 105 | // called by SkSurface to compute a new genID |
| 106 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 107 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 108 | private: |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 109 | std::unique_ptr<SkCanvas> fCachedCanvas; |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 110 | sk_sp<SkImage> fCachedImage; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 112 | void aboutToDraw(ContentChangeMode mode); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 113 | |
| 114 | // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw |
| 115 | // would trigger a copy-on-write. |
| 116 | bool outstandingImageSnapshot() const; |
| 117 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 118 | friend class SkCanvas; |
| 119 | friend class SkSurface; |
| 120 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 121 | typedef SkSurface INHERITED; |
| 122 | }; |
| 123 | |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 124 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | if (nullptr == fCachedCanvas) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 126 | fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 127 | if (fCachedCanvas) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 128 | fCachedCanvas->setSurfaceBase(this); |
| 129 | } |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 130 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 131 | return fCachedCanvas.get(); |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 134 | sk_sp<SkImage> SkSurface_Base::refCachedImage() { |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 135 | if (fCachedImage) { |
| 136 | return fCachedImage; |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 137 | } |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 138 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 139 | fCachedImage = this->onNewImageSnapshot(); |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 140 | |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 141 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 142 | return fCachedImage; |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 143 | } |
| 144 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 145 | #endif |