• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #ifndef SkDrawable_DEFINED
9 #define SkDrawable_DEFINED
10 
11 #include "include/core/SkFlattenable.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkScalar.h"
14 
15 class GrBackendDrawableInfo;
16 class SkCanvas;
17 class SkMatrix;
18 class SkPicture;
19 enum class GrBackendApi : unsigned;
20 struct SkRect;
21 
22 /**
23  *  Base-class for objects that draw into SkCanvas.
24  *
25  *  The object has a generation ID, which is guaranteed to be unique across all drawables. To
26  *  allow for clients of the drawable that may want to cache the results, the drawable must
27  *  change its generation ID whenever its internal state changes such that it will draw differently.
28  */
29 class SK_API SkDrawable : public SkFlattenable {
30 public:
31     /**
32      *  Draws into the specified content. The drawing sequence will be balanced upon return
33      *  (i.e. the saveLevel() on the canvas will match what it was when draw() was called,
34      *  and the current matrix and clip settings will not be changed.
35      */
36     void draw(SkCanvas*, const SkMatrix* = nullptr);
37     void draw(SkCanvas*, SkScalar x, SkScalar y);
38 
39     /**
40      *  When using the GPU backend it is possible for a drawable to execute using the underlying 3D
41      *  API rather than the SkCanvas API. It does so by creating a GpuDrawHandler. The GPU backend
42      *  is deferred so the handler will be given access to the 3D API at the correct point in the
43      *  drawing stream as the GPU backend flushes. Since the drawable may mutate, each time it is
44      *  drawn to a GPU-backed canvas a new handler is snapped, representing the drawable's state at
45      *  the time of the snap.
46      *
47      *  When the GPU backend flushes to the 3D API it will call the draw method on the
48      *  GpuDrawHandler. At this time the drawable may add commands to the stream of GPU commands for
49      *  the unerlying 3D API. The draw function takes a GrBackendDrawableInfo which contains
50      *  information about the current state of 3D API which the caller must respect. See
51      *  GrBackendDrawableInfo for more specific details on what information is sent and the
52      *  requirements for different 3D APIs.
53      *
54      *  Additionaly there may be a slight delay from when the drawable adds its commands to when
55      *  those commands are actually submitted to the GPU. Thus the drawable or GpuDrawHandler is
56      *  required to keep any resources that are used by its added commands alive and valid until
57      *  those commands are submitted to the GPU. The GpuDrawHandler will be kept alive and then
58      *  deleted once the commands are submitted to the GPU. The dtor of the GpuDrawHandler is the
59      *  signal to the drawable that the commands have all been submitted. Different 3D APIs may have
60      *  additional requirements for certain resources which require waiting for the GPU to finish
61      *  all work on those resources before reusing or deleting them. In this case, the drawable can
62      *  use the dtor call of the GpuDrawHandler to add a fence to the GPU to track when the GPU work
63      *  has completed.
64      *
65      *  Currently this is only supported for the GPU Vulkan backend.
66      */
67 
68     class GpuDrawHandler {
69     public:
~GpuDrawHandler()70         virtual ~GpuDrawHandler() {}
71 
draw(const GrBackendDrawableInfo &)72         virtual void draw(const GrBackendDrawableInfo&) {}
73     };
74 
75     /**
76      * Snaps off a GpuDrawHandler to represent the state of the SkDrawable at the time the snap is
77      * called. This is used for executing GPU backend specific draws intermixed with normal Skia GPU
78      * draws. The GPU API, which will be used for the draw, as well as the full matrix, device clip
79      * bounds and imageInfo of the target buffer are passed in as inputs.
80      */
snapGpuDrawHandler(GrBackendApi backendApi,const SkMatrix & matrix,const SkIRect & clipBounds,const SkImageInfo & bufferInfo)81     std::unique_ptr<GpuDrawHandler> snapGpuDrawHandler(GrBackendApi backendApi,
82                                                        const SkMatrix& matrix,
83                                                        const SkIRect& clipBounds,
84                                                        const SkImageInfo& bufferInfo) {
85         return this->onSnapGpuDrawHandler(backendApi, matrix, clipBounds, bufferInfo);
86     }
87 
88     SkPicture* newPictureSnapshot();
89 
90     /**
91      *  Return a unique value for this instance. If two calls to this return the same value,
92      *  it is presumed that calling the draw() method will render the same thing as well.
93      *
94      *  Subclasses that change their state should call notifyDrawingChanged() to ensure that
95      *  a new value will be returned the next time it is called.
96      */
97     uint32_t getGenerationID();
98 
99     /**
100      *  Return the (conservative) bounds of what the drawable will draw. If the drawable can
101      *  change what it draws (e.g. animation or in response to some external change), then this
102      *  must return a bounds that is always valid for all possible states.
103      */
104     SkRect getBounds();
105 
106     /**
107      *  Return approximately how many bytes would be freed if this drawable is destroyed.
108      *  The base implementation returns 0 to indicate that this is unknown.
109      */
110     size_t approximateBytesUsed();
111 
112     /**
113      *  Calling this invalidates the previous generation ID, and causes a new one to be computed
114      *  the next time getGenerationID() is called. Typically this is called by the object itself,
115      *  in response to its internal state changing.
116      */
117     void notifyDrawingChanged();
118 
GetFlattenableType()119     static SkFlattenable::Type GetFlattenableType() {
120         return kSkDrawable_Type;
121     }
122 
getFlattenableType()123     SkFlattenable::Type getFlattenableType() const override {
124         return kSkDrawable_Type;
125     }
126 
127     static sk_sp<SkDrawable> Deserialize(const void* data, size_t size,
128                                           const SkDeserialProcs* procs = nullptr) {
129         return sk_sp<SkDrawable>(static_cast<SkDrawable*>(
130                                   SkFlattenable::Deserialize(
131                                   kSkDrawable_Type, data, size, procs).release()));
132     }
133 
getFactory()134     Factory getFactory() const override { return nullptr; }
getTypeName()135     const char* getTypeName() const override { return nullptr; }
136 
137 protected:
138     SkDrawable();
139 
140     virtual SkRect onGetBounds() = 0;
141     virtual size_t onApproximateBytesUsed();
142     virtual void onDraw(SkCanvas*) = 0;
143 
onSnapGpuDrawHandler(GrBackendApi,const SkMatrix &,const SkIRect &,const SkImageInfo &)144     virtual std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi, const SkMatrix&,
145                                                                  const SkIRect& /*clipBounds*/,
146                                                                  const SkImageInfo&) {
147         return nullptr;
148     }
149 
150     // TODO: Delete this once Android gets updated to take the clipBounds version above.
onSnapGpuDrawHandler(GrBackendApi,const SkMatrix &)151     virtual std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi, const SkMatrix&) {
152         return nullptr;
153     }
154 
155     /**
156      *  Default implementation calls onDraw() with a canvas that records into a picture. Subclasses
157      *  may override if they have a more efficient way to return a picture for the current state
158      *  of their drawable. Note: this picture must draw the same as what would be drawn from
159      *  onDraw().
160      */
161     virtual SkPicture* onNewPictureSnapshot();
162 
163 private:
164     int32_t fGenerationID;
165 };
166 
167 #endif
168