blob: b9c4890c66e989d3035c46c9acd709067bc26c65 [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
11#include "SkSurface.h"
12
13class SkSurface_Base : public SkSurface {
14public:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000015 SkSurface_Base(int width, int height);
16 virtual ~SkSurface_Base();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000017
reed@google.com9ea5a3b2012-07-30 21:03:46 +000018 /**
19 * Allocate a canvas that will draw into this surface. We will cache this
20 * canvas, to return the same object to the caller multiple times. We
21 * take ownership, and will call unref() on the canvas when we go out of
22 * scope.
23 */
reed@google.comc9062042012-07-30 18:06:00 +000024 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000025
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000026 virtual SkSurface* onNewSurface(const SkImage::Info&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000027
reed@google.com9ea5a3b2012-07-30 21:03:46 +000028 /**
29 * Allocate an SkImage that represents the current contents of the surface.
30 * This needs to be able to outlive the surface itself (if need be), and
31 * must faithfully represent the current contents, even if the surface
32 * is chaged after this calle (e.g. it is drawn to via its canvas).
33 */
reed@google.comc9062042012-07-30 18:06:00 +000034 virtual SkImage* onNewImageShapshot() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
reed@google.comc9062042012-07-30 18:06:00 +000036 /**
37 * Default implementation:
38 *
39 * image = this->newImageSnapshot();
40 * if (image) {
41 * image->draw(canvas, ...);
42 * image->unref();
43 * }
44 */
45 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
46
reed@google.com9ea5a3b2012-07-30 21:03:46 +000047 /**
reed@google.com97af1a62012-08-28 12:19:02 +000048 * If the surface is about to change, we call this so that our subclass
49 * can optionally fork their backend (copy-on-write) in case it was
50 * being shared with the cachedImage.
51 *
52 * The default implementation does nothing.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000053 */
robertphillips@google.com97b6b072012-10-31 14:48:39 +000054 virtual void onCopyOnWrite(SkImage* cachedImage, SkCanvas*) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000055
56 inline SkCanvas* getCachedCanvas();
57 inline SkImage* getCachedImage();
58
59 // called by SkSurface to compute a new genID
60 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061
reed@google.comc9062042012-07-30 18:06:00 +000062private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000063 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000064 SkImage* fCachedImage;
65
66 void aboutToDraw(SkCanvas*);
67 friend class SkCanvas;
68 friend class SkSurface;
69
70 inline void installIntoCanvasForDirtyNotification();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
reed@google.comc9062042012-07-30 18:06:00 +000072 typedef SkSurface INHERITED;
73};
74
75#endif