blob: 0e3cd955f2c9339d6b9848aeaa24cf51397e1f23 [file] [log] [blame]
reed@google.comc9062042012-07-30 18:06:00 +00001/*
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
reed29c857d2014-09-21 10:25:07 -070011#include "SkCanvas.h"
bsalomonf47b9a32016-02-22 11:02:58 -080012#include "SkImagePriv.h"
reed4a8126e2014-09-22 07:29:03 -070013#include "SkSurface.h"
14#include "SkSurfacePriv.h"
reed@google.comc9062042012-07-30 18:06:00 +000015
16class SkSurface_Base : public SkSurface {
17public:
reed4a8126e2014-09-22 07:29:03 -070018 SkSurface_Base(int width, int height, const SkSurfaceProps*);
19 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
reed@google.com9ea5a3b2012-07-30 21:03:46 +000020 virtual ~SkSurface_Base();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000021
Robert Phillips8caf85f2018-04-05 09:30:38 -040022 virtual GrBackendTexture onGetBackendTexture(BackendHandleAccess);
23 virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess);
24
reed@google.com9ea5a3b2012-07-30 21:03:46 +000025 /**
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.comc9062042012-07-30 18:06:00 +000031 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032
reede8f30622016-03-23 18:59:25 -070033 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034
reed@google.com9ea5a3b2012-07-30 21:03:46 +000035 /**
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
bsalomoneaaaf0b2015-01-23 08:08:04 -080039 * is changed after this called (e.g. it is drawn to via its canvas).
Mike Reed114bde82018-11-21 09:12:09 -050040 *
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.com9ea5a3b2012-07-30 21:03:46 +000043 */
Mike Reed114bde82018-11-21 09:12:09 -050044 virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045
Mike Reed4c790bd2018-02-08 14:10:40 -050046 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
47
reed@google.comc9062042012-07-30 18:06:00 +000048 /**
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.com9ea5a3b2012-07-30 21:03:46 +000059 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000060 * 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.com97af1a62012-08-28 12:19:02 +000066 * 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.com9ea5a3b2012-07-30 21:03:46 +000069 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000070 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000071
reed26e0e582015-07-29 11:44:52 -070072 /**
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
ericrkf7b8b8a2016-02-24 14:49:51 -080078 /**
79 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
Greg Daniela5cb7812017-06-16 09:45:32 -040080 * 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.
ericrkf7b8b8a2016-02-24 14:49:51 -080082 */
Greg Danielb9990e42019-04-10 16:28:52 -040083 virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, GrFlushFlags flags,
Greg Danielbae71212019-03-01 15:24:35 -050084 int numSemaphores,
Greg Daniel51316782017-08-02 15:10:09 +000085 GrBackendSemaphore signalSemaphores[]) {
86 return GrSemaphoresSubmitted::kNo;
Greg Danielc64ee462017-06-15 16:59:49 -040087 }
Greg Daniela5cb7812017-06-16 09:45:32 -040088
89 /**
90 * Caused the current backend 3D API to wait on the passed in semaphores before executing new
91 * commands on the gpu. Any previously submitting commands will not be blocked by these
92 * semaphores.
93 */
Greg Danielc64ee462017-06-15 16:59:49 -040094 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
95 return false;
96 }
ericrkf7b8b8a2016-02-24 14:49:51 -080097
Robert Phillipsad8a43f2017-08-30 12:06:35 -040098 virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050099 virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400100
reed@google.com97af1a62012-08-28 12:19:02 +0000101 inline SkCanvas* getCachedCanvas();
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400102 inline sk_sp<SkImage> refCachedImage();
reed@google.com97af1a62012-08-28 12:19:02 +0000103
halcanary96fcdcc2015-08-27 07:41:13 -0700104 bool hasCachedImage() const { return fCachedImage != nullptr; }
reed26e0e582015-07-29 11:44:52 -0700105
reed@google.com97af1a62012-08-28 12:19:02 +0000106 // called by SkSurface to compute a new genID
107 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000108
reed@google.comc9062042012-07-30 18:06:00 +0000109private:
Mike Reed5df49342016-11-12 08:06:55 -0600110 std::unique_ptr<SkCanvas> fCachedCanvas;
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500111 sk_sp<SkImage> fCachedImage;
reed@google.com97af1a62012-08-28 12:19:02 +0000112
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000113 void aboutToDraw(ContentChangeMode mode);
reedc83a2972015-07-16 07:40:45 -0700114
115 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
116 // would trigger a copy-on-write.
117 bool outstandingImageSnapshot() const;
118
reed@google.com97af1a62012-08-28 12:19:02 +0000119 friend class SkCanvas;
120 friend class SkSurface;
121
reed@google.comc9062042012-07-30 18:06:00 +0000122 typedef SkSurface INHERITED;
123};
124
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000125SkCanvas* SkSurface_Base::getCachedCanvas() {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 if (nullptr == fCachedCanvas) {
Mike Reed5df49342016-11-12 08:06:55 -0600127 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
bsalomon49f085d2014-09-05 13:34:00 -0700128 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000129 fCachedCanvas->setSurfaceBase(this);
130 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000131 }
Mike Reed5df49342016-11-12 08:06:55 -0600132 return fCachedCanvas.get();
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000133}
134
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400135sk_sp<SkImage> SkSurface_Base::refCachedImage() {
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500136 if (fCachedImage) {
137 return fCachedImage;
bsalomonf47b9a32016-02-22 11:02:58 -0800138 }
Mike Reed85ff8482016-12-29 09:36:20 -0500139
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400140 fCachedImage = this->onNewImageSnapshot();
Mike Reed85ff8482016-12-29 09:36:20 -0500141
bsalomonf47b9a32016-02-22 11:02:58 -0800142 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500143 return fCachedImage;
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000144}
145
reed@google.comc9062042012-07-30 18:06:00 +0000146#endif