blob: 7bff9a87e9f9f78be8d950ccbdd9364bf0fb8c4c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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.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);
Brian Salomonaad83152019-05-24 10:16:35 -040024 virtual bool onReplaceBackendTexture(const GrBackendTexture&,
25 GrSurfaceOrigin,
26 TextureReleaseProc,
27 ReleaseContext);
reed@google.com9ea5a3b2012-07-30 21:03:46 +000028 /**
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.comc9062042012-07-30 18:06:00 +000034 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
reede8f30622016-03-23 18:59:25 -070036 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037
reed@google.com9ea5a3b2012-07-30 21:03:46 +000038 /**
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
bsalomoneaaaf0b2015-01-23 08:08:04 -080042 * is changed after this called (e.g. it is drawn to via its canvas).
Mike Reed114bde82018-11-21 09:12:09 -050043 *
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.com9ea5a3b2012-07-30 21:03:46 +000046 */
Mike Reed114bde82018-11-21 09:12:09 -050047 virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
Mike Reed4c790bd2018-02-08 14:10:40 -050049 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
50
reed@google.comc9062042012-07-30 18:06:00 +000051 /**
Brian Salomon031b0ba2019-05-23 11:05:26 -040052 * Default implementation does a rescale/read and then calls the callback.
Brian Salomonab32f652019-05-10 14:24:50 -040053 */
Brian Salomon9241a6d2019-10-03 13:26:54 -040054 virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
55 const SkIRect& srcRect,
56 RescaleGamma,
57 SkFilterQuality,
58 ReadPixelsCallback,
59 ReadPixelsContext);
Brian Salomon024bd002019-06-11 11:38:16 -040060 /**
61 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
62 */
Brian Salomon9241a6d2019-10-03 13:26:54 -040063 virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
Brian Salomon024bd002019-06-11 11:38:16 -040064 sk_sp<SkColorSpace> dstColorSpace,
Brian Salomon9241a6d2019-10-03 13:26:54 -040065 const SkIRect& srcRect,
66 const SkISize& dstSize,
67 RescaleGamma,
68 SkFilterQuality,
69 ReadPixelsCallback,
70 ReadPixelsContext);
Brian Salomonab32f652019-05-10 14:24:50 -040071
72 /**
reed@google.comc9062042012-07-30 18:06:00 +000073 * 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.com9ea5a3b2012-07-30 21:03:46 +000083 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000084 * 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.com97af1a62012-08-28 12:19:02 +000090 * 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.com9ea5a3b2012-07-30 21:03:46 +000093 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000094 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000095
reed26e0e582015-07-29 11:44:52 -070096 /**
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
ericrkf7b8b8a2016-02-24 14:49:51 -0800102 /**
103 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
Greg Daniela5cb7812017-06-16 09:45:32 -0400104 * 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.
ericrkf7b8b8a2016-02-24 14:49:51 -0800106 */
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400107 virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&) {
Greg Daniel51316782017-08-02 15:10:09 +0000108 return GrSemaphoresSubmitted::kNo;
Greg Danielc64ee462017-06-15 16:59:49 -0400109 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400110
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 Danielc64ee462017-06-15 16:59:49 -0400116 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
117 return false;
118 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800119
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400120 virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
Robert Phillips9907e6e2019-06-25 14:47:04 -0400121 virtual bool onIsCompatible(const SkSurfaceCharacterization&) const { return false; }
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500122 virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400123
reed@google.com97af1a62012-08-28 12:19:02 +0000124 inline SkCanvas* getCachedCanvas();
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400125 inline sk_sp<SkImage> refCachedImage();
reed@google.com97af1a62012-08-28 12:19:02 +0000126
halcanary96fcdcc2015-08-27 07:41:13 -0700127 bool hasCachedImage() const { return fCachedImage != nullptr; }
reed26e0e582015-07-29 11:44:52 -0700128
reed@google.com97af1a62012-08-28 12:19:02 +0000129 // called by SkSurface to compute a new genID
130 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000131
reed@google.comc9062042012-07-30 18:06:00 +0000132private:
Mike Reed5df49342016-11-12 08:06:55 -0600133 std::unique_ptr<SkCanvas> fCachedCanvas;
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500134 sk_sp<SkImage> fCachedImage;
reed@google.com97af1a62012-08-28 12:19:02 +0000135
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000136 void aboutToDraw(ContentChangeMode mode);
reedc83a2972015-07-16 07:40:45 -0700137
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.com97af1a62012-08-28 12:19:02 +0000142 friend class SkCanvas;
143 friend class SkSurface;
144
reed@google.comc9062042012-07-30 18:06:00 +0000145 typedef SkSurface INHERITED;
146};
147
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000148SkCanvas* SkSurface_Base::getCachedCanvas() {
halcanary96fcdcc2015-08-27 07:41:13 -0700149 if (nullptr == fCachedCanvas) {
Mike Reed5df49342016-11-12 08:06:55 -0600150 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
bsalomon49f085d2014-09-05 13:34:00 -0700151 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000152 fCachedCanvas->setSurfaceBase(this);
153 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000154 }
Mike Reed5df49342016-11-12 08:06:55 -0600155 return fCachedCanvas.get();
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000156}
157
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400158sk_sp<SkImage> SkSurface_Base::refCachedImage() {
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500159 if (fCachedImage) {
160 return fCachedImage;
bsalomonf47b9a32016-02-22 11:02:58 -0800161 }
Mike Reed85ff8482016-12-29 09:36:20 -0500162
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400163 fCachedImage = this->onNewImageSnapshot();
Mike Reed85ff8482016-12-29 09:36:20 -0500164
bsalomonf47b9a32016-02-22 11:02:58 -0800165 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
Robert Phillipsa54ccb22017-01-31 07:40:33 -0500166 return fCachedImage;
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000167}
168
reed@google.comc9062042012-07-30 18:06:00 +0000169#endif