SkDrawable.h 6.2 KB

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