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