blob: 7658409ad764eb78cdcb330d5ea13897d986c83f [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"
reed4a8126e2014-09-22 07:29:03 -070012#include "SkSurface.h"
13#include "SkSurfacePriv.h"
reed@google.comc9062042012-07-30 18:06:00 +000014
15class SkSurface_Base : public SkSurface {
16public:
reed4a8126e2014-09-22 07:29:03 -070017 SkSurface_Base(int width, int height, const SkSurfaceProps*);
18 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
reed@google.com9ea5a3b2012-07-30 21:03:46 +000019 virtual ~SkSurface_Base();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020
joshualitt81793412015-07-08 12:54:04 -070021 virtual GrBackendObject onGetTextureHandle(BackendHandleAccess) {
reedfa5e68e2015-06-29 07:37:01 -070022 return 0;
23 }
24
joshualitt81793412015-07-08 12:54:04 -070025 virtual bool onGetRenderTargetHandle(GrBackendObject*, BackendHandleAccess) {
26 return false;
27 }
28
reed@google.com9ea5a3b2012-07-30 21:03:46 +000029 /**
30 * Allocate a canvas that will draw into this surface. We will cache this
31 * canvas, to return the same object to the caller multiple times. We
32 * take ownership, and will call unref() on the canvas when we go out of
33 * scope.
34 */
reed@google.comc9062042012-07-30 18:06:00 +000035 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036
reed@google.com2bd8b812013-11-01 13:46:54 +000037 virtual SkSurface* onNewSurface(const SkImageInfo&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000038
reed@google.com9ea5a3b2012-07-30 21:03:46 +000039 /**
40 * Allocate an SkImage that represents the current contents of the surface.
41 * This needs to be able to outlive the surface itself (if need be), and
42 * must faithfully represent the current contents, even if the surface
bsalomoneaaaf0b2015-01-23 08:08:04 -080043 * is changed after this called (e.g. it is drawn to via its canvas).
reed@google.com9ea5a3b2012-07-30 21:03:46 +000044 */
bsalomoneaaaf0b2015-01-23 08:08:04 -080045 virtual SkImage* onNewImageSnapshot(Budgeted) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
reed@google.comc9062042012-07-30 18:06:00 +000047 /**
48 * Default implementation:
49 *
50 * image = this->newImageSnapshot();
51 * if (image) {
52 * image->draw(canvas, ...);
53 * image->unref();
54 * }
55 */
56 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
57
reed@google.com9ea5a3b2012-07-30 21:03:46 +000058 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000059 * Called as a performance hint when the Surface is allowed to make it's contents
60 * undefined.
61 */
62 virtual void onDiscard() {}
63
64 /**
reed@google.com97af1a62012-08-28 12:19:02 +000065 * If the surface is about to change, we call this so that our subclass
66 * can optionally fork their backend (copy-on-write) in case it was
67 * being shared with the cachedImage.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000068 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000069 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000070
reed26e0e582015-07-29 11:44:52 -070071 /**
72 * Signal the surface to remind its backing store that it's mutable again.
73 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
74 */
75 virtual void onRestoreBackingMutability() {}
76
reed@google.com97af1a62012-08-28 12:19:02 +000077 inline SkCanvas* getCachedCanvas();
bsalomoneaaaf0b2015-01-23 08:08:04 -080078 inline SkImage* getCachedImage(Budgeted);
reed@google.com97af1a62012-08-28 12:19:02 +000079
halcanary96fcdcc2015-08-27 07:41:13 -070080 bool hasCachedImage() const { return fCachedImage != nullptr; }
reed26e0e582015-07-29 11:44:52 -070081
reed@google.com97af1a62012-08-28 12:19:02 +000082 // called by SkSurface to compute a new genID
83 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084
reed@google.comc9062042012-07-30 18:06:00 +000085private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000086 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000087 SkImage* fCachedImage;
88
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000089 void aboutToDraw(ContentChangeMode mode);
reedc83a2972015-07-16 07:40:45 -070090
91 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
92 // would trigger a copy-on-write.
93 bool outstandingImageSnapshot() const;
94
reed@google.com97af1a62012-08-28 12:19:02 +000095 friend class SkCanvas;
96 friend class SkSurface;
97
reed@google.comc9062042012-07-30 18:06:00 +000098 typedef SkSurface INHERITED;
99};
100
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000101SkCanvas* SkSurface_Base::getCachedCanvas() {
halcanary96fcdcc2015-08-27 07:41:13 -0700102 if (nullptr == fCachedCanvas) {
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000103 fCachedCanvas = this->onNewCanvas();
bsalomon49f085d2014-09-05 13:34:00 -0700104 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000105 fCachedCanvas->setSurfaceBase(this);
106 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000107 }
108 return fCachedCanvas;
109}
110
bsalomoneaaaf0b2015-01-23 08:08:04 -0800111SkImage* SkSurface_Base::getCachedImage(Budgeted budgeted) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 if (nullptr == fCachedImage) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800113 fCachedImage = this->onNewImageSnapshot(budgeted);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000114 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
junov@chromium.org6a9bb802013-04-16 19:50:30 +0000115 }
116 return fCachedImage;
117}
118
reed@google.comc9062042012-07-30 18:06:00 +0000119#endif