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(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 17 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 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; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 25 | |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 26 | virtual SkSurface* onNewSurface(const SkImage::Info&) = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 27 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 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; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 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 | /** |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 48 | * If the surface is about to change, we call this so that our subclass |
| 49 | * can optionally fork their backend (copy-on-write) in case it was |
| 50 | * being shared with the cachedImage. |
| 51 | * |
| 52 | * The default implementation does nothing. |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 53 | */ |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 54 | virtual void onCopyOnWrite(SkImage* cachedImage, SkCanvas*) = 0; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 55 | |
| 56 | inline SkCanvas* getCachedCanvas(); |
| 57 | inline SkImage* getCachedImage(); |
| 58 | |
| 59 | // called by SkSurface to compute a new genID |
| 60 | uint32_t newGenerationID(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 61 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 62 | private: |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 63 | SkCanvas* fCachedCanvas; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 64 | SkImage* fCachedImage; |
| 65 | |
| 66 | void aboutToDraw(SkCanvas*); |
| 67 | friend class SkCanvas; |
| 68 | friend class SkSurface; |
| 69 | |
| 70 | inline void installIntoCanvasForDirtyNotification(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 71 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 72 | typedef SkSurface INHERITED; |
| 73 | }; |
| 74 | |
| 75 | #endif |