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