Skia
2D Graphics Library
SkDrawable.h
Go to the documentation of this file.
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 
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/base/SkAPI.h"
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <memory>
19 
21 class SkCanvas;
22 class SkMatrix;
23 class SkPicture;
24 enum class GrBackendApi : unsigned int;
25 struct SkDeserialProcs;
26 struct SkIRect;
27 struct SkImageInfo;
28 struct SkRect;
29 
37 class SK_API SkDrawable : public SkFlattenable {
38 public:
44  void draw(SkCanvas*, const SkMatrix* = nullptr);
46 
77  public:
78  virtual ~GpuDrawHandler() {}
79 
80  virtual void draw(const GrBackendDrawableInfo&) {}
81  };
82 
89  std::unique_ptr<GpuDrawHandler> snapGpuDrawHandler(GrBackendApi backendApi,
90  const SkMatrix& matrix,
91  const SkIRect& clipBounds,
92  const SkImageInfo& bufferInfo) {
93  return this->onSnapGpuDrawHandler(backendApi, matrix, clipBounds, bufferInfo);
94  }
95 
100 
108  uint32_t getGenerationID();
109 
116 
122 
129 
131  return kSkDrawable_Type;
132  }
133 
135  return kSkDrawable_Type;
136  }
137 
138  static sk_sp<SkDrawable> Deserialize(const void* data, size_t size,
139  const SkDeserialProcs* procs = nullptr) {
140  return sk_sp<SkDrawable>(static_cast<SkDrawable*>(
142  kSkDrawable_Type, data, size, procs).release()));
143  }
144 
145  Factory getFactory() const override { return nullptr; }
146  const char* getTypeName() const override { return nullptr; }
147 
148 protected:
150 
151  virtual SkRect onGetBounds() = 0;
152  virtual size_t onApproximateBytesUsed();
153  virtual void onDraw(SkCanvas*) = 0;
154 
155  virtual std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi, const SkMatrix&,
156  const SkIRect& /*clipBounds*/,
157  const SkImageInfo&) {
158  return nullptr;
159  }
160 
161  // TODO: Delete this once Android gets updated to take the clipBounds version above.
162  virtual std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi, const SkMatrix&) {
163  return nullptr;
164  }
165 
173 
174 private:
175  int32_t fGenerationID;
176 };
177 
178 #endif
GrBackendApi
Possible 3D APIs that may be used by Ganesh.
Definition: GrTypes.h:96
float SkScalar
Definition: SkScalar.h:14
Definition: GrBackendDrawableInfo.h:15
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
When using the GPU backend it is possible for a drawable to execute using the underlying 3D API rathe...
Definition: SkDrawable.h:76
virtual ~GpuDrawHandler()
Definition: SkDrawable.h:78
virtual void draw(const GrBackendDrawableInfo &)
Definition: SkDrawable.h:80
Base-class for objects that draw into SkCanvas.
Definition: SkDrawable.h:37
sk_sp< SkPicture > makePictureSnapshot()
Returns an SkPicture with the contents of this SkDrawable.
std::unique_ptr< GpuDrawHandler > snapGpuDrawHandler(GrBackendApi backendApi, const SkMatrix &matrix, const SkIRect &clipBounds, const SkImageInfo &bufferInfo)
Snaps off a GpuDrawHandler to represent the state of the SkDrawable at the time the snap is called.
Definition: SkDrawable.h:89
void notifyDrawingChanged()
Calling this invalidates the previous generation ID, and causes a new one to be computed the next tim...
uint32_t getGenerationID()
Return a unique value for this instance.
SkFlattenable::Type getFlattenableType() const override
Definition: SkDrawable.h:134
size_t approximateBytesUsed()
Return approximately how many bytes would be freed if this drawable is destroyed.
virtual SkRect onGetBounds()=0
void draw(SkCanvas *, SkScalar x, SkScalar y)
virtual std::unique_ptr< GpuDrawHandler > onSnapGpuDrawHandler(GrBackendApi, const SkMatrix &, const SkIRect &, const SkImageInfo &)
Definition: SkDrawable.h:155
static SkFlattenable::Type GetFlattenableType()
Definition: SkDrawable.h:130
SkRect getBounds()
Return the (conservative) bounds of what the drawable will draw.
virtual std::unique_ptr< GpuDrawHandler > onSnapGpuDrawHandler(GrBackendApi, const SkMatrix &)
Definition: SkDrawable.h:162
virtual void onDraw(SkCanvas *)=0
static sk_sp< SkDrawable > Deserialize(const void *data, size_t size, const SkDeserialProcs *procs=nullptr)
Definition: SkDrawable.h:138
const char * getTypeName() const override
Returns the name of the object's class.
Definition: SkDrawable.h:146
virtual size_t onApproximateBytesUsed()
void draw(SkCanvas *, const SkMatrix *=nullptr)
Draws into the specified content.
Factory getFactory() const override
Implement this to return a factory function pointer that can be called to recreate your class given a...
Definition: SkDrawable.h:145
virtual sk_sp< SkPicture > onMakePictureSnapshot()
Default implementation calls onDraw() with a canvas that records into a picture.
SkFlattenable is the base class for objects that need to be flattened into a data stream for either t...
Definition: SkFlattenable.h:28
static sk_sp< SkFlattenable > Deserialize(Type, const void *data, size_t length, const SkDeserialProcs *procs=nullptr)
Type
Definition: SkFlattenable.h:30
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkPicture records drawing commands made to SkCanvas.
Definition: SkPicture.h:44
Definition: SkSerialProcs.h:97
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:32
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582