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" |
junov@chromium.org | 63cd3c6 | 2013-04-16 19:56:45 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 13 | |
| 14 | class SkSurface_Base : public SkSurface { |
| 15 | public: |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 16 | SkSurface_Base(int width, int height); |
| 17 | virtual ~SkSurface_Base(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 18 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 19 | /** |
| 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.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 25 | virtual SkCanvas* onNewCanvas() = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 26 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame^] | 27 | virtual SkSurface* onNewSurface(const SkImageInfo&) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 28 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 29 | /** |
| 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.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 35 | virtual SkImage* onNewImageSnapshot() = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 36 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 37 | /** |
| 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.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 48 | /** |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 49 | * 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. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 52 | */ |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 53 | virtual void onCopyOnWrite(ContentChangeMode) = 0; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 54 | |
| 55 | inline SkCanvas* getCachedCanvas(); |
| 56 | inline SkImage* getCachedImage(); |
| 57 | |
| 58 | // called by SkSurface to compute a new genID |
| 59 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 60 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 61 | private: |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 62 | SkCanvas* fCachedCanvas; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 63 | SkImage* fCachedImage; |
| 64 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 65 | void aboutToDraw(ContentChangeMode mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 66 | friend class SkCanvas; |
| 67 | friend class SkSurface; |
| 68 | |
| 69 | inline void installIntoCanvasForDirtyNotification(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 70 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 71 | typedef SkSurface INHERITED; |
| 72 | }; |
| 73 | |
junov@chromium.org | 6a9bb80 | 2013-04-16 19:50:30 +0000 | [diff] [blame] | 74 | SkCanvas* SkSurface_Base::getCachedCanvas() { |
| 75 | if (NULL == fCachedCanvas) { |
| 76 | fCachedCanvas = this->onNewCanvas(); |
| 77 | this->installIntoCanvasForDirtyNotification(); |
| 78 | } |
| 79 | return fCachedCanvas; |
| 80 | } |
| 81 | |
| 82 | SkImage* SkSurface_Base::getCachedImage() { |
| 83 | if (NULL == fCachedImage) { |
| 84 | fCachedImage = this->onNewImageSnapshot(); |
| 85 | this->installIntoCanvasForDirtyNotification(); |
| 86 | } |
| 87 | return fCachedImage; |
| 88 | } |
| 89 | |
junov@chromium.org | 63cd3c6 | 2013-04-16 19:56:45 +0000 | [diff] [blame] | 90 | void SkSurface_Base::installIntoCanvasForDirtyNotification() { |
| 91 | if (NULL != fCachedCanvas) { |
| 92 | fCachedCanvas->setSurfaceBase(this); |
| 93 | } |
| 94 | } |
| 95 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 96 | #endif |