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 | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 38 | virtual sk_sp<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 | */ |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 46 | virtual sk_sp<SkImage> onNewImageSnapshot() = 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. |
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 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 97 | inline SkCanvas* getCachedCanvas(); |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 98 | inline sk_sp<SkImage> refCachedImage(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 99 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 100 | bool hasCachedImage() const { return fCachedImage != nullptr; } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 101 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 102 | // called by SkSurface to compute a new genID |
| 103 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 104 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 105 | private: |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 106 | std::unique_ptr<SkCanvas> fCachedCanvas; |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 107 | sk_sp<SkImage> fCachedImage; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 109 | void aboutToDraw(ContentChangeMode mode); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 110 | |
| 111 | // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw |
| 112 | // would trigger a copy-on-write. |
| 113 | bool outstandingImageSnapshot() const; |
| 114 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 115 | friend class SkCanvas; |
| 116 | friend class SkSurface; |
| 117 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 118 | typedef SkSurface INHERITED; |
| 119 | }; |
| 120 | |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 121 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | if (nullptr == fCachedCanvas) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 123 | fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 124 | if (fCachedCanvas) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 125 | fCachedCanvas->setSurfaceBase(this); |
| 126 | } |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 127 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 128 | return fCachedCanvas.get(); |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 131 | sk_sp<SkImage> SkSurface_Base::refCachedImage() { |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 132 | if (fCachedImage) { |
| 133 | return fCachedImage; |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 134 | } |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 135 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 136 | fCachedImage = this->onNewImageSnapshot(); |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 137 | |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 138 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 139 | return fCachedImage; |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 142 | #endif |