blob: 67d330f9bbdae0a1be54907114769e8a53006ab5 [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).
reed@google.com9ea5a3b2012-07-30 21:03:46 +000040 */
Robert Phillipsac6b1fa2017-03-20 08:38:50 -040041 virtual sk_sp<SkImage> onNewImageSnapshot() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042
Mike Reed4c790bd2018-02-08 14:10:40 -050043 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
44
reed@google.comc9062042012-07-30 18:06:00 +000045 /**
46 * Default implementation:
47 *
48 * image = this->newImageSnapshot();
49 * if (image) {
50 * image->draw(canvas, ...);
51 * image->unref();
52 * }
53 */
54 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
55
reed@google.com9ea5a3b2012-07-30 21:03:46 +000056 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000057 * Called as a performance hint when the Surface is allowed to make it's contents
58 * undefined.
59 */
60 virtual void onDiscard() {}
61
62 /**
reed@google.com97af1a62012-08-28 12:19:02 +000063 * If the surface is about to change, we call this so that our subclass
64 * can optionally fork their backend (copy-on-write) in case it was
65 * being shared with the cachedImage.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000066 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000067 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000068
reed26e0e582015-07-29 11:44:52 -070069 /**
70 * Signal the surface to remind its backing store that it's mutable again.
71 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
72 */
73 virtual void onRestoreBackingMutability() {}
74
ericrkf7b8b8a2016-02-24 14:49:51 -080075 /**
76 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
Greg Daniela5cb7812017-06-16 09:45:32 -040077 * Inserts the requested number of semaphores for the gpu to signal when work is complete on the
78 * gpu and inits the array of GrBackendSemaphores with the signaled semaphores.
ericrkf7b8b8a2016-02-24 14:49:51 -080079 */
Greg Daniel51316782017-08-02 15:10:09 +000080 virtual GrSemaphoresSubmitted onFlush(int numSemaphores,
81 GrBackendSemaphore signalSemaphores[]) {
82 return GrSemaphoresSubmitted::kNo;
Greg Danielc64ee462017-06-15 16:59:49 -040083 }
Greg Daniela5cb7812017-06-16 09:45:32 -040084
85 /**
86 * Caused the current backend 3D API to wait on the passed in semaphores before executing new
87 * commands on the gpu. Any previously submitting commands will not be blocked by these
88 * semaphores.
89 */
Greg Danielc64ee462017-06-15 16:59:49 -040090 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
91 return false;
92 }
ericrkf7b8b8a2016-02-24 14:49:51 -080093
Robert Phillipsad8a43f2017-08-30 12:06:35 -040094 virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050095 virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
Robert Phillipsad8a43f2017-08-30 12:06:35 -040096
reed@google.com97af1a62012-08-28 12:19:02 +000097 inline SkCanvas* getCachedCanvas();
Robert Phillipsac6b1fa2017-03-20 08:38:50 -040098 inline sk_sp<SkImage> refCachedImage();
reed@google.com97af1a62012-08-28 12:19:02 +000099
halcanary96fcdcc2015-08-27 07:41:13 -0700100 bool hasCachedImage() const { return fCachedImage != nullptr; }
reed26e0e582015-07-29 11:44:52 -0700101
reed@google.com97af1a62012-08-28 12:19:02 +0000102 // called by SkSurface to compute a new genID
103 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104
reed@google.comc9062042012-07-30 18:06:00 +0000105private:
Mike Reed5df49342016-11-12 08:06:55 -0600106 std::unique_ptr<SkCanvas> fCachedCanvas;
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500107 sk_sp<SkImage> fCachedImage;
reed@google.com97af1a62012-08-28 12:19:02 +0000108
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000109 void aboutToDraw(ContentChangeMode mode);
reedc83a2972015-07-16 07:40:45 -0700110
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.com97af1a62012-08-28 12:19:02 +0000115 friend class SkCanvas;
116 friend class SkSurface;
117
reed@google.comc9062042012-07-30 18:06:00 +0000118 typedef SkSurface INHERITED;
119};
120
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000121SkCanvas* SkSurface_Base::getCachedCanvas() {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (nullptr == fCachedCanvas) {
Mike Reed5df49342016-11-12 08:06:55 -0600123 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
bsalomon49f085d2014-09-05 13:34:00 -0700124 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000125 fCachedCanvas->setSurfaceBase(this);
126 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000127 }
Mike Reed5df49342016-11-12 08:06:55 -0600128 return fCachedCanvas.get();
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000129}
130
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400131sk_sp<SkImage> SkSurface_Base::refCachedImage() {
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500132 if (fCachedImage) {
133 return fCachedImage;
bsalomonf47b9a32016-02-22 11:02:58 -0800134 }
Mike Reed85ff8482016-12-29 09:36:20 -0500135
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400136 fCachedImage = this->onNewImageSnapshot();
Mike Reed85ff8482016-12-29 09:36:20 -0500137
bsalomonf47b9a32016-02-22 11:02:58 -0800138 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500139 return fCachedImage;
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000140}
141
reed@google.comc9062042012-07-30 18:06:00 +0000142#endif