blob: 8cf75b60e4fa260756b2acd0560c09632583e05e [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
joshualitt81793412015-07-08 12:54:04 -070022 virtual GrBackendObject onGetTextureHandle(BackendHandleAccess) {
reedfa5e68e2015-06-29 07:37:01 -070023 return 0;
24 }
25
joshualitt81793412015-07-08 12:54:04 -070026 virtual bool onGetRenderTargetHandle(GrBackendObject*, BackendHandleAccess) {
27 return false;
28 }
29
reed@google.com9ea5a3b2012-07-30 21:03:46 +000030 /**
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.comc9062042012-07-30 18:06:00 +000036 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037
reede8f30622016-03-23 18:59:25 -070038 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039
reed@google.com9ea5a3b2012-07-30 21:03:46 +000040 /**
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
bsalomoneaaaf0b2015-01-23 08:08:04 -080044 * is changed after this called (e.g. it is drawn to via its canvas).
reed@google.com9ea5a3b2012-07-30 21:03:46 +000045 */
Robert Phillipsac6b1fa2017-03-20 08:38:50 -040046 virtual sk_sp<SkImage> onNewImageSnapshot() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
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 Daniel51316782017-08-02 15:10:09 +000083 virtual GrSemaphoresSubmitted onFlush(int numSemaphores,
84 GrBackendSemaphore signalSemaphores[]) {
85 return GrSemaphoresSubmitted::kNo;
Greg Danielc64ee462017-06-15 16:59:49 -040086 }
Greg Daniela5cb7812017-06-16 09:45:32 -040087
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 Danielc64ee462017-06-15 16:59:49 -040093 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
94 return false;
95 }
ericrkf7b8b8a2016-02-24 14:49:51 -080096
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