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 | |
reed | 29c857d | 2014-09-21 10:25:07 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 12 | #include "SkSurface.h" |
| 13 | #include "SkSurfacePriv.h" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 14 | |
| 15 | class SkSurface_Base : public SkSurface { |
| 16 | public: |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 17 | SkSurface_Base(int width, int height, const SkSurfaceProps*); |
| 18 | SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 19 | virtual ~SkSurface_Base(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 20 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Allocate a canvas that will draw into this surface. We will cache this |
| 23 | * canvas, to return the same object to the caller multiple times. We |
| 24 | * take ownership, and will call unref() on the canvas when we go out of |
| 25 | * scope. |
| 26 | */ |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 27 | virtual SkCanvas* onNewCanvas() = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 28 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 29 | virtual SkSurface* onNewSurface(const SkImageInfo&) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 30 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Allocate an SkImage that represents the current contents of the surface. |
| 33 | * This needs to be able to outlive the surface itself (if need be), and |
| 34 | * must faithfully represent the current contents, even if the surface |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 35 | * is changed after this called (e.g. it is drawn to via its canvas). |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 36 | */ |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 37 | virtual SkImage* onNewImageSnapshot(Budgeted) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 38 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Default implementation: |
| 41 | * |
| 42 | * image = this->newImageSnapshot(); |
| 43 | * if (image) { |
| 44 | * image->draw(canvas, ...); |
| 45 | * image->unref(); |
| 46 | * } |
| 47 | */ |
| 48 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); |
| 49 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 50 | /** |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 51 | * Called as a performance hint when the Surface is allowed to make it's contents |
| 52 | * undefined. |
| 53 | */ |
| 54 | virtual void onDiscard() {} |
| 55 | |
| 56 | /** |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 57 | * If the surface is about to change, we call this so that our subclass |
| 58 | * can optionally fork their backend (copy-on-write) in case it was |
| 59 | * being shared with the cachedImage. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 60 | */ |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 61 | virtual void onCopyOnWrite(ContentChangeMode) = 0; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 62 | |
| 63 | inline SkCanvas* getCachedCanvas(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 64 | inline SkImage* getCachedImage(Budgeted); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 65 | |
| 66 | // called by SkSurface to compute a new genID |
| 67 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 68 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 69 | private: |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 70 | SkCanvas* fCachedCanvas; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 71 | SkImage* fCachedImage; |
| 72 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 73 | void aboutToDraw(ContentChangeMode mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 74 | friend class SkCanvas; |
| 75 | friend class SkSurface; |
| 76 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 77 | typedef SkSurface INHERITED; |
| 78 | }; |
| 79 | |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 80 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
| 81 | if (NULL == fCachedCanvas) { |
| 82 | fCachedCanvas = this->onNewCanvas(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 83 | if (fCachedCanvas) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 84 | fCachedCanvas->setSurfaceBase(this); |
| 85 | } |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 86 | } |
| 87 | return fCachedCanvas; |
| 88 | } |
| 89 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 90 | SkImage* SkSurface_Base::getCachedImage(Budgeted budgeted) { |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 91 | if (NULL == fCachedImage) { |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 92 | fCachedImage = this->onNewImageSnapshot(budgeted); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 93 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 94 | } |
| 95 | return fCachedImage; |
| 96 | } |
| 97 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 98 | #endif |