blob: 3f1301ee34c1d7aa1afdfcf5a3cce0ffbda7a75b [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
reedfa5e68e2015-06-29 07:37:01 -070021 virtual GrBackendObject onGetTextureHandle(TextureHandleAccess) {
22 return 0;
23 }
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
reed@google.com2bd8b812013-11-01 13:46:54 +000033 virtual 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 */
bsalomoneaaaf0b2015-01-23 08:08:04 -080041 virtual SkImage* onNewImageSnapshot(Budgeted) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042
reed@google.comc9062042012-07-30 18:06:00 +000043 /**
44 * Default implementation:
45 *
46 * image = this->newImageSnapshot();
47 * if (image) {
48 * image->draw(canvas, ...);
49 * image->unref();
50 * }
51 */
52 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
53
reed@google.com9ea5a3b2012-07-30 21:03:46 +000054 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000055 * Called as a performance hint when the Surface is allowed to make it's contents
56 * undefined.
57 */
58 virtual void onDiscard() {}
59
60 /**
reed@google.com97af1a62012-08-28 12:19:02 +000061 * If the surface is about to change, we call this so that our subclass
62 * can optionally fork their backend (copy-on-write) in case it was
63 * being shared with the cachedImage.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000064 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000065 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000066
67 inline SkCanvas* getCachedCanvas();
bsalomoneaaaf0b2015-01-23 08:08:04 -080068 inline SkImage* getCachedImage(Budgeted);
reed@google.com97af1a62012-08-28 12:19:02 +000069
70 // called by SkSurface to compute a new genID
71 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
reed@google.comc9062042012-07-30 18:06:00 +000073private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000074 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000075 SkImage* fCachedImage;
76
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000077 void aboutToDraw(ContentChangeMode mode);
reed@google.com97af1a62012-08-28 12:19:02 +000078 friend class SkCanvas;
79 friend class SkSurface;
80
reed@google.comc9062042012-07-30 18:06:00 +000081 typedef SkSurface INHERITED;
82};
83
junov@chromium.org6a9bb802013-04-16 19:50:30 +000084SkCanvas* SkSurface_Base::getCachedCanvas() {
85 if (NULL == fCachedCanvas) {
86 fCachedCanvas = this->onNewCanvas();
bsalomon49f085d2014-09-05 13:34:00 -070087 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000088 fCachedCanvas->setSurfaceBase(this);
89 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +000090 }
91 return fCachedCanvas;
92}
93
bsalomoneaaaf0b2015-01-23 08:08:04 -080094SkImage* SkSurface_Base::getCachedImage(Budgeted budgeted) {
junov@chromium.org6a9bb802013-04-16 19:50:30 +000095 if (NULL == fCachedImage) {
bsalomoneaaaf0b2015-01-23 08:08:04 -080096 fCachedImage = this->onNewImageSnapshot(budgeted);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000097 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
junov@chromium.org6a9bb802013-04-16 19:50:30 +000098 }
99 return fCachedImage;
100}
101
reed@google.comc9062042012-07-30 18:06:00 +0000102#endif