blob: 4da4cfb496866081daa7f343c1d649436a34c27c [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
junov@chromium.org63cd3c62013-04-16 19:56:45 +000011#include "SkCanvas.h"
reed3716fd02014-09-21 09:39:55 -070012#include "SkSurface.h"
13#include "SkSurfacePriv.h"
reed@google.comc9062042012-07-30 18:06:00 +000014
15class SkSurface_Base : public SkSurface {
16public:
reed3716fd02014-09-21 09:39:55 -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
reed@google.com9ea5a3b2012-07-30 21:03:46 +000021 /**
22 * Allocate a canvas that will draw into this surface. We will cache this
23 * canvas, to return the same object to the caller multiple times. We
24 * take ownership, and will call unref() on the canvas when we go out of
25 * scope.
26 */
reed@google.comc9062042012-07-30 18:06:00 +000027 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028
reed@google.com2bd8b812013-11-01 13:46:54 +000029 virtual SkSurface* onNewSurface(const SkImageInfo&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030
reed@google.com9ea5a3b2012-07-30 21:03:46 +000031 /**
32 * Allocate an SkImage that represents the current contents of the surface.
33 * This needs to be able to outlive the surface itself (if need be), and
34 * must faithfully represent the current contents, even if the surface
35 * is chaged after this calle (e.g. it is drawn to via its canvas).
36 */
junov@chromium.org5ee449a2013-04-12 20:20:50 +000037 virtual SkImage* onNewImageSnapshot() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000038
reed@google.comc9062042012-07-30 18:06:00 +000039 /**
40 * Default implementation:
41 *
42 * image = this->newImageSnapshot();
43 * if (image) {
44 * image->draw(canvas, ...);
45 * image->unref();
46 * }
47 */
48 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
49
reed@google.com9ea5a3b2012-07-30 21:03:46 +000050 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000051 * Called as a performance hint when the Surface is allowed to make it's contents
52 * undefined.
53 */
54 virtual void onDiscard() {}
55
56 /**
reed@google.com97af1a62012-08-28 12:19:02 +000057 * If the surface is about to change, we call this so that our subclass
58 * can optionally fork their backend (copy-on-write) in case it was
59 * being shared with the cachedImage.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000060 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000061 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000062
63 inline SkCanvas* getCachedCanvas();
64 inline SkImage* getCachedImage();
65
66 // called by SkSurface to compute a new genID
67 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
reed@google.comc9062042012-07-30 18:06:00 +000069private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000070 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000071 SkImage* fCachedImage;
72
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000073 void aboutToDraw(ContentChangeMode mode);
reed@google.com97af1a62012-08-28 12:19:02 +000074 friend class SkCanvas;
75 friend class SkSurface;
76
reed@google.comc9062042012-07-30 18:06:00 +000077 typedef SkSurface INHERITED;
78};
79
junov@chromium.org6a9bb802013-04-16 19:50:30 +000080SkCanvas* SkSurface_Base::getCachedCanvas() {
81 if (NULL == fCachedCanvas) {
82 fCachedCanvas = this->onNewCanvas();
bsalomon49f085d2014-09-05 13:34:00 -070083 if (fCachedCanvas) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000084 fCachedCanvas->setSurfaceBase(this);
85 }
junov@chromium.org6a9bb802013-04-16 19:50:30 +000086 }
87 return fCachedCanvas;
88}
89
90SkImage* SkSurface_Base::getCachedImage() {
91 if (NULL == fCachedImage) {
92 fCachedImage = this->onNewImageSnapshot();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000093 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
junov@chromium.org6a9bb802013-04-16 19:50:30 +000094 }
95 return fCachedImage;
96}
97
reed@google.comc9062042012-07-30 18:06:00 +000098#endif