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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "src/core/SkImagePriv.h" |
| 14 | #include "src/core/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); |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 24 | virtual bool onReplaceBackendTexture(const GrBackendTexture&, |
| 25 | GrSurfaceOrigin, |
| 26 | TextureReleaseProc, |
| 27 | ReleaseContext); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Allocate a canvas that will draw into this surface. We will cache this |
| 30 | * canvas, to return the same object to the caller multiple times. We |
| 31 | * take ownership, and will call unref() on the canvas when we go out of |
| 32 | * scope. |
| 33 | */ |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 34 | virtual SkCanvas* onNewCanvas() = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 36 | virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 37 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Allocate an SkImage that represents the current contents of the surface. |
| 40 | * This needs to be able to outlive the surface itself (if need be), and |
| 41 | * must faithfully represent the current contents, even if the surface |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 42 | * 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] | 43 | * |
| 44 | * If a subset is specified, the the impl must make a copy, rather than try to wait |
| 45 | * on copy-on-write. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 46 | */ |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 47 | virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 48 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 49 | virtual void onWritePixels(const SkPixmap&, int x, int y) = 0; |
| 50 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 51 | /** |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 52 | * Default implementation does a rescale/read and then calls the callback. |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 53 | */ |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 54 | virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&, |
| 55 | const SkIRect& srcRect, |
| 56 | RescaleGamma, |
| 57 | SkFilterQuality, |
| 58 | ReadPixelsCallback, |
| 59 | ReadPixelsContext); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Default implementation does a rescale/read/yuv conversion and then calls the callback. |
| 62 | */ |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 63 | virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 64 | sk_sp<SkColorSpace> dstColorSpace, |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 65 | const SkIRect& srcRect, |
| 66 | const SkISize& dstSize, |
| 67 | RescaleGamma, |
| 68 | SkFilterQuality, |
| 69 | ReadPixelsCallback, |
| 70 | ReadPixelsContext); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 71 | |
| 72 | /** |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 73 | * Default implementation: |
| 74 | * |
| 75 | * image = this->newImageSnapshot(); |
| 76 | * if (image) { |
| 77 | * image->draw(canvas, ...); |
| 78 | * image->unref(); |
| 79 | * } |
| 80 | */ |
| 81 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); |
| 82 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 83 | /** |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 84 | * Called as a performance hint when the Surface is allowed to make it's contents |
| 85 | * undefined. |
| 86 | */ |
| 87 | virtual void onDiscard() {} |
| 88 | |
| 89 | /** |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 90 | * If the surface is about to change, we call this so that our subclass |
| 91 | * can optionally fork their backend (copy-on-write) in case it was |
| 92 | * being shared with the cachedImage. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 93 | */ |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 94 | virtual void onCopyOnWrite(ContentChangeMode) = 0; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 95 | |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Signal the surface to remind its backing store that it's mutable again. |
| 98 | * Called only when we _didn't_ copy-on-write; we assume the copies start mutable. |
| 99 | */ |
| 100 | virtual void onRestoreBackingMutability() {} |
| 101 | |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 102 | /** |
| 103 | * 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] | 104 | * Inserts the requested number of semaphores for the gpu to signal when work is complete on the |
| 105 | * gpu and inits the array of GrBackendSemaphores with the signaled semaphores. |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 106 | */ |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 107 | virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&) { |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 108 | return GrSemaphoresSubmitted::kNo; |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 109 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Caused the current backend 3D API to wait on the passed in semaphores before executing new |
| 113 | * commands on the gpu. Any previously submitting commands will not be blocked by these |
| 114 | * semaphores. |
| 115 | */ |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 116 | virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) { |
| 117 | return false; |
| 118 | } |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 119 | |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 120 | virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; } |
Robert Phillips | 9907e6e | 2019-06-25 14:47:04 -0400 | [diff] [blame] | 121 | virtual bool onIsCompatible(const SkSurfaceCharacterization&) const { return false; } |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 122 | virtual bool onDraw(const SkDeferredDisplayList*) { return false; } |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 123 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 124 | inline SkCanvas* getCachedCanvas(); |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 125 | inline sk_sp<SkImage> refCachedImage(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 126 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | bool hasCachedImage() const { return fCachedImage != nullptr; } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 128 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 129 | // called by SkSurface to compute a new genID |
| 130 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 131 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 132 | private: |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 133 | std::unique_ptr<SkCanvas> fCachedCanvas; |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 134 | sk_sp<SkImage> fCachedImage; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 135 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 136 | void aboutToDraw(ContentChangeMode mode); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 137 | |
| 138 | // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw |
| 139 | // would trigger a copy-on-write. |
| 140 | bool outstandingImageSnapshot() const; |
| 141 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 142 | friend class SkCanvas; |
| 143 | friend class SkSurface; |
| 144 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 145 | typedef SkSurface INHERITED; |
| 146 | }; |
| 147 | |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 148 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | if (nullptr == fCachedCanvas) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 150 | fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 151 | if (fCachedCanvas) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 152 | fCachedCanvas->setSurfaceBase(this); |
| 153 | } |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 154 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 155 | return fCachedCanvas.get(); |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 158 | sk_sp<SkImage> SkSurface_Base::refCachedImage() { |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 159 | if (fCachedImage) { |
| 160 | return fCachedImage; |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 161 | } |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 162 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 163 | fCachedImage = this->onNewImageSnapshot(); |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 164 | |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 165 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 166 | return fCachedImage; |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 169 | #endif |