blob: cbae5bc5c4a0f82909395f77193a515ee847b5ac [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"
junov@chromium.org63cd3c62013-04-16 19:56:45 +000012#include "SkCanvas.h"
reed@google.comc9062042012-07-30 18:06:00 +000013
14class SkSurface_Base : public SkSurface {
15public:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000016 SkSurface_Base(int width, int height);
17 virtual ~SkSurface_Base();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000018
reed@google.com9ea5a3b2012-07-30 21:03:46 +000019 /**
20 * Allocate a canvas that will draw into this surface. We will cache this
21 * canvas, to return the same object to the caller multiple times. We
22 * take ownership, and will call unref() on the canvas when we go out of
23 * scope.
24 */
reed@google.comc9062042012-07-30 18:06:00 +000025 virtual SkCanvas* onNewCanvas() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000026
reed@google.com2bd8b812013-11-01 13:46:54 +000027 virtual SkSurface* onNewSurface(const SkImageInfo&) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028
reed@google.com9ea5a3b2012-07-30 21:03:46 +000029 /**
30 * Allocate an SkImage that represents the current contents of the surface.
31 * This needs to be able to outlive the surface itself (if need be), and
32 * must faithfully represent the current contents, even if the surface
33 * is chaged after this calle (e.g. it is drawn to via its canvas).
34 */
junov@chromium.org5ee449a2013-04-12 20:20:50 +000035 virtual SkImage* onNewImageSnapshot() = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036
reed@google.comc9062042012-07-30 18:06:00 +000037 /**
38 * Default implementation:
39 *
40 * image = this->newImageSnapshot();
41 * if (image) {
42 * image->draw(canvas, ...);
43 * image->unref();
44 * }
45 */
46 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
47
reed@google.com9ea5a3b2012-07-30 21:03:46 +000048 /**
reed@google.com97af1a62012-08-28 12:19:02 +000049 * If the surface is about to change, we call this so that our subclass
50 * can optionally fork their backend (copy-on-write) in case it was
51 * being shared with the cachedImage.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000052 */
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000053 virtual void onCopyOnWrite(ContentChangeMode) = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000054
55 inline SkCanvas* getCachedCanvas();
56 inline SkImage* getCachedImage();
57
58 // called by SkSurface to compute a new genID
59 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000060
reed@google.comc9062042012-07-30 18:06:00 +000061private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000062 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000063 SkImage* fCachedImage;
64
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000065 void aboutToDraw(ContentChangeMode mode);
reed@google.com97af1a62012-08-28 12:19:02 +000066 friend class SkCanvas;
67 friend class SkSurface;
68
69 inline void installIntoCanvasForDirtyNotification();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
reed@google.comc9062042012-07-30 18:06:00 +000071 typedef SkSurface INHERITED;
72};
73
junov@chromium.org6a9bb802013-04-16 19:50:30 +000074SkCanvas* SkSurface_Base::getCachedCanvas() {
75 if (NULL == fCachedCanvas) {
76 fCachedCanvas = this->onNewCanvas();
77 this->installIntoCanvasForDirtyNotification();
78 }
79 return fCachedCanvas;
80}
81
82SkImage* SkSurface_Base::getCachedImage() {
83 if (NULL == fCachedImage) {
84 fCachedImage = this->onNewImageSnapshot();
85 this->installIntoCanvasForDirtyNotification();
86 }
87 return fCachedImage;
88}
89
junov@chromium.org63cd3c62013-04-16 19:56:45 +000090void SkSurface_Base::installIntoCanvasForDirtyNotification() {
91 if (NULL != fCachedCanvas) {
92 fCachedCanvas->setSurfaceBase(this);
93 }
94}
95
reed@google.comc9062042012-07-30 18:06:00 +000096#endif