blob: 32cfc883da987175842823a3b9549421fd61f7e2 [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
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000027 virtual SkSurface* onNewSurface(const SkImage::Info&) = 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.
52 *
53 * The default implementation does nothing.
reed@google.com9ea5a3b2012-07-30 21:03:46 +000054 */
junov@chromium.orgacea3ef2013-04-16 19:41:09 +000055 virtual void onCopyOnWrite() = 0;
reed@google.com97af1a62012-08-28 12:19:02 +000056
57 inline SkCanvas* getCachedCanvas();
58 inline SkImage* getCachedImage();
59
60 // called by SkSurface to compute a new genID
61 uint32_t newGenerationID();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062
reed@google.comc9062042012-07-30 18:06:00 +000063private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000064 SkCanvas* fCachedCanvas;
reed@google.com97af1a62012-08-28 12:19:02 +000065 SkImage* fCachedImage;
66
junov@chromium.orgacea3ef2013-04-16 19:41:09 +000067 void aboutToDraw();
reed@google.com97af1a62012-08-28 12:19:02 +000068 friend class SkCanvas;
69 friend class SkSurface;
70
71 inline void installIntoCanvasForDirtyNotification();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
reed@google.comc9062042012-07-30 18:06:00 +000073 typedef SkSurface INHERITED;
74};
75
junov@chromium.org6a9bb802013-04-16 19:50:30 +000076SkCanvas* SkSurface_Base::getCachedCanvas() {
77 if (NULL == fCachedCanvas) {
78 fCachedCanvas = this->onNewCanvas();
79 this->installIntoCanvasForDirtyNotification();
80 }
81 return fCachedCanvas;
82}
83
84SkImage* SkSurface_Base::getCachedImage() {
85 if (NULL == fCachedImage) {
86 fCachedImage = this->onNewImageSnapshot();
87 this->installIntoCanvasForDirtyNotification();
88 }
89 return fCachedImage;
90}
91
junov@chromium.org63cd3c62013-04-16 19:56:45 +000092void SkSurface_Base::installIntoCanvasForDirtyNotification() {
93 if (NULL != fCachedCanvas) {
94 fCachedCanvas->setSurfaceBase(this);
95 }
96}
97
reed@google.comc9062042012-07-30 18:06:00 +000098#endif