| reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | class SkSurface_Base : public SkSurface { |
| 14 | public: |
| reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame^] | 15 | SkSurface_Base(int width, int height); |
| 16 | virtual ~SkSurface_Base(); |
| 17 | |
| 18 | /** |
| 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.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 24 | virtual SkCanvas* onNewCanvas() = 0; |
| reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame^] | 25 | |
| reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 26 | virtual SkSurface* onNewSurface(const SkImage::Info&, SkColorSpace*) = 0; |
| reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame^] | 27 | |
| 28 | /** |
| 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.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 34 | virtual SkImage* onNewImageShapshot() = 0; |
| 35 | |
| 36 | /** |
| 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.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame^] | 47 | /** |
| 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 | } |
| 57 | |
| reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 58 | private: |
| reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame^] | 59 | SkCanvas* fCachedCanvas; |
| 60 | |
| reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 61 | typedef SkSurface INHERITED; |
| 62 | }; |
| 63 | |
| 64 | #endif |
| 65 | |