reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 8 | #ifndef SkDrawable_DEFINED |
| 9 | #define SkDrawable_DEFINED |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 10 | |
| 11 | #include "SkRefCnt.h" |
| 12 | |
| 13 | class SkCanvas; |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 14 | class SkPicture; |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 15 | struct SkRect; |
| 16 | |
| 17 | /** |
reed | 79c77a4 | 2014-11-11 07:54:10 -0800 | [diff] [blame] | 18 | * Base-class for objects that draw into SkCanvas. |
| 19 | * |
| 20 | * The object has a generation ID, which is guaranteed to be unique across all drawables. To |
| 21 | * allow for clients of the drawable that may want to cache the results, the drawable must |
| 22 | * change its generation ID whenever its internal state changes such that it will draw differently. |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 23 | */ |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 24 | class SkDrawable : public SkRefCnt { |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 25 | public: |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 26 | SkDrawable(); |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Draws into the specified content. The drawing sequence will be balanced upon return |
| 30 | * (i.e. the saveLevel() on the canvas will match what it was when draw() was called, |
| 31 | * and the current matrix and clip settings will not be changed. |
| 32 | */ |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame^] | 33 | void draw(SkCanvas*, const SkMatrix* = NULL); |
| 34 | void draw(SkCanvas*, SkScalar x, SkScalar y); |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 35 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 36 | SkPicture* newPictureSnapshot(); |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 37 | |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Return a unique value for this instance. If two calls to this return the same value, |
| 40 | * it is presumed that calling the draw() method will render the same thing as well. |
| 41 | * |
| 42 | * Subclasses that change their state should call notifyDrawingChanged() to ensure that |
| 43 | * a new value will be returned the next time it is called. |
| 44 | */ |
| 45 | uint32_t getGenerationID(); |
| 46 | |
| 47 | /** |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 48 | * Return the (conservative) bounds of what the drawable will draw. If the drawable can |
| 49 | * change what it draws (e.g. animation or in response to some external change), then this |
| 50 | * must return a bounds that is always valid for all possible states. |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 51 | */ |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 52 | SkRect getBounds(); |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 53 | |
reed | 79c77a4 | 2014-11-11 07:54:10 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Calling this invalidates the previous generation ID, and causes a new one to be computed |
| 56 | * the next time getGenerationID() is called. Typically this is called by the object itself, |
| 57 | * in response to its internal state changing. |
| 58 | */ |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 59 | void notifyDrawingChanged(); |
| 60 | |
| 61 | protected: |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 62 | virtual SkRect onGetBounds() = 0; |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 63 | virtual void onDraw(SkCanvas*) = 0; |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Default implementation calls onDraw() with a canvas that records into a picture. Subclasses |
| 67 | * may override if they have a more efficient way to return a picture for the current state |
| 68 | * of their drawable. Note: this picture must draw the same as what would be drawn from |
| 69 | * onDraw(). |
| 70 | */ |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 71 | virtual SkPicture* onNewPictureSnapshot(); |
reed | c536942 | 2014-11-11 04:56:05 -0800 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | int32_t fGenerationID; |
| 75 | }; |
| 76 | |
| 77 | #endif |