blob: 7164c884e08344fdd081955c6486772c777a0764 [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 */
reede8f30622016-03-23 18:59:25 -070046 virtual sk_sp<SkImage> onNewImageSnapshot(SkBudgeted, ForceCopyMode) = 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.
80 */
81 virtual void onPrepareForExternalIO() {}
82
reed@google.com97af1a62012-08-28 12:19:02 +000083 inline SkCanvas* getCachedCanvas();
reed9ce9d672016-03-17 10:51:11 -070084 inline sk_sp<SkImage> refCachedImage(SkBudgeted, ForceUnique);
reed@google.com97af1a62012-08-28 12:19:02 +000085
halcanary96fcdcc2015-08-27 07:41:13 -070086 bool hasCachedImage() const { return fCachedImage != nullptr; }
reed26e0e582015-07-29 11:44:52 -070087
reed@google.com97af1a62012-08-28 12:19:02 +000088 // called by SkSurface to compute a new genID
89 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@google.comc9062042012-07-30 18:06:00 +000091private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000092 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000093 SkImage* fCachedImage;
94
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000095 void aboutToDraw(ContentChangeMode mode);
reedc83a2972015-07-16 07:40:45 -070096
97 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
98 // would trigger a copy-on-write.
99 bool outstandingImageSnapshot() const;
100
reed@google.com97af1a62012-08-28 12:19:02 +0000101 friend class SkCanvas;
102 friend class SkSurface;
103
reed@google.comc9062042012-07-30 18:06:00 +0000104 typedef SkSurface INHERITED;
105};
106
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000107SkCanvas* SkSurface_Base::getCachedCanvas() {
halcanary96fcdcc2015-08-27 07:41:13 -0700108 if (nullptr == fCachedCanvas) {
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000109 fCachedCanvas = this->onNewCanvas();
bsalomon49f085d2014-09-05 13:34:00 -0700110 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000111 fCachedCanvas->setSurfaceBase(this);
112 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000113 }
114 return fCachedCanvas;
115}
116
reed9ce9d672016-03-17 10:51:11 -0700117sk_sp<SkImage> SkSurface_Base::refCachedImage(SkBudgeted budgeted, ForceUnique unique) {
bsalomonf47b9a32016-02-22 11:02:58 -0800118 SkImage* snap = fCachedImage;
119 if (kYes_ForceUnique == unique && snap && !snap->unique()) {
120 snap = nullptr;
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000121 }
bsalomonf47b9a32016-02-22 11:02:58 -0800122 if (snap) {
reed9ce9d672016-03-17 10:51:11 -0700123 return sk_ref_sp(snap);
bsalomonf47b9a32016-02-22 11:02:58 -0800124 }
125 ForceCopyMode fcm = (kYes_ForceUnique == unique) ? kYes_ForceCopyMode :
126 kNo_ForceCopyMode;
reede8f30622016-03-23 18:59:25 -0700127 snap = this->onNewImageSnapshot(budgeted, fcm).release();
bsalomonf47b9a32016-02-22 11:02:58 -0800128 if (kNo_ForceUnique == unique) {
129 SkASSERT(!fCachedImage);
130 fCachedImage = SkSafeRef(snap);
131 }
132 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
reed9ce9d672016-03-17 10:51:11 -0700133 return sk_sp<SkImage>(snap);
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000134}
135
reed@google.comc9062042012-07-30 18:06:00 +0000136#endif