2014-11-11 12:56:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-02-06 16:36:15 +00:00
|
|
|
#ifndef SkDrawable_DEFINED
|
|
|
|
#define SkDrawable_DEFINED
|
2014-11-11 12:56:05 +00:00
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
|
|
|
class SkCanvas;
|
2014-11-24 22:41:51 +00:00
|
|
|
class SkPicture;
|
2014-11-11 12:56:05 +00:00
|
|
|
struct SkRect;
|
|
|
|
|
|
|
|
/**
|
2014-11-11 15:54:10 +00:00
|
|
|
* Base-class for objects that draw into SkCanvas.
|
|
|
|
*
|
|
|
|
* The object has a generation ID, which is guaranteed to be unique across all drawables. To
|
|
|
|
* allow for clients of the drawable that may want to cache the results, the drawable must
|
|
|
|
* change its generation ID whenever its internal state changes such that it will draw differently.
|
2014-11-11 12:56:05 +00:00
|
|
|
*/
|
2015-02-06 16:36:15 +00:00
|
|
|
class SkDrawable : public SkRefCnt {
|
2014-11-11 12:56:05 +00:00
|
|
|
public:
|
2015-02-06 16:36:15 +00:00
|
|
|
SkDrawable();
|
2014-11-11 12:56:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws into the specified content. The drawing sequence will be balanced upon return
|
|
|
|
* (i.e. the saveLevel() on the canvas will match what it was when draw() was called,
|
|
|
|
* and the current matrix and clip settings will not be changed.
|
|
|
|
*/
|
2015-07-07 17:22:31 +00:00
|
|
|
void draw(SkCanvas*, const SkMatrix* = NULL);
|
|
|
|
void draw(SkCanvas*, SkScalar x, SkScalar y);
|
2014-11-11 12:56:05 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
SkPicture* newPictureSnapshot();
|
2014-11-18 19:08:05 +00:00
|
|
|
|
2014-11-11 12:56:05 +00:00
|
|
|
/**
|
|
|
|
* Return a unique value for this instance. If two calls to this return the same value,
|
|
|
|
* it is presumed that calling the draw() method will render the same thing as well.
|
|
|
|
*
|
|
|
|
* Subclasses that change their state should call notifyDrawingChanged() to ensure that
|
|
|
|
* a new value will be returned the next time it is called.
|
|
|
|
*/
|
|
|
|
uint32_t getGenerationID();
|
|
|
|
|
|
|
|
/**
|
2014-11-18 19:08:05 +00:00
|
|
|
* Return the (conservative) bounds of what the drawable will draw. If the drawable can
|
|
|
|
* change what it draws (e.g. animation or in response to some external change), then this
|
|
|
|
* must return a bounds that is always valid for all possible states.
|
2014-11-11 12:56:05 +00:00
|
|
|
*/
|
2014-11-18 19:08:05 +00:00
|
|
|
SkRect getBounds();
|
2014-11-11 12:56:05 +00:00
|
|
|
|
2014-11-11 15:54:10 +00:00
|
|
|
/**
|
|
|
|
* Calling this invalidates the previous generation ID, and causes a new one to be computed
|
|
|
|
* the next time getGenerationID() is called. Typically this is called by the object itself,
|
|
|
|
* in response to its internal state changing.
|
|
|
|
*/
|
2014-11-11 12:56:05 +00:00
|
|
|
void notifyDrawingChanged();
|
|
|
|
|
|
|
|
protected:
|
2014-11-18 19:08:05 +00:00
|
|
|
virtual SkRect onGetBounds() = 0;
|
2014-11-11 12:56:05 +00:00
|
|
|
virtual void onDraw(SkCanvas*) = 0;
|
2015-02-06 16:36:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default implementation calls onDraw() with a canvas that records into a picture. Subclasses
|
|
|
|
* may override if they have a more efficient way to return a picture for the current state
|
|
|
|
* of their drawable. Note: this picture must draw the same as what would be drawn from
|
|
|
|
* onDraw().
|
|
|
|
*/
|
2014-11-24 22:41:51 +00:00
|
|
|
virtual SkPicture* onNewPictureSnapshot();
|
2014-11-11 12:56:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t fGenerationID;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|