blob: 8166c8c43587fccfdb0d220431a5b781c3d030ef [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
reed@google.comc9062042012-07-30 18:06:00 +000026 virtual SkSurface* onNewSurface(const SkImage::Info&, SkColorSpace*) = 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 /**
48 * Returns a the result of onNewCanvas(), but caches it so that only one
49 * canvas never ever be created.
50 */
51 SkCanvas* getCachedCanvas() {
52 if (NULL == fCachedCanvas) {
53 fCachedCanvas = this->onNewCanvas();
54 }
55 return fCachedCanvas;
56 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057
reed@google.comc9062042012-07-30 18:06:00 +000058private:
reed@google.com9ea5a3b2012-07-30 21:03:46 +000059 SkCanvas* fCachedCanvas;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000060
reed@google.comc9062042012-07-30 18:06:00 +000061 typedef SkSurface INHERITED;
62};
63
64#endif
65