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 SkRecorder_DEFINED 9 #define SkRecorder_DEFINED 10 11 #include "include/core/SkCanvasVirtualEnforcer.h" 12 #include "include/core/SkColor.h" 13 #include "include/core/SkM44.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkSamplingOptions.h" 16 #include "include/core/SkScalar.h" 17 #include "include/core/SkTypes.h" 18 #include "include/private/base/SkNoncopyable.h" 19 #include "include/private/base/SkTDArray.h" 20 #include "include/utils/SkNoDrawCanvas.h" 21 #include "src/core/SkBigPicture.h" 22 23 #include <cstddef> 24 #include <memory> 25 #include <utility> 26 27 class SkBlender; 28 class SkData; 29 class SkDrawable; 30 class SkImage; 31 class SkMatrix; 32 class SkMesh; 33 class SkPaint; 34 class SkPath; 35 class SkPicture; 36 class SkRRect; 37 class SkRecord; 38 class SkRegion; 39 class SkShader; 40 class SkSurface; 41 class SkSurfaceProps; 42 class SkTextBlob; 43 class SkVertices; 44 enum class SkBlendMode; 45 enum class SkClipOp; 46 struct SkDrawShadowRec; 47 struct SkImageInfo; 48 struct SkPoint; 49 struct SkRSXform; 50 struct SkRect; 51 52 namespace sktext { 53 class GlyphRunList; 54 namespace gpu { class Slug; } 55 } 56 57 class SkDrawableList : SkNoncopyable { 58 public: SkDrawableList()59 SkDrawableList() {} 60 ~SkDrawableList(); 61 count()62 int count() const { return fArray.size(); } begin()63 SkDrawable* const* begin() const { return fArray.begin(); } end()64 SkDrawable* const* end() const { return fArray.end(); } 65 66 void append(SkDrawable* drawable); 67 68 // Return a new or ref'd array of pictures that were snapped from our drawables. 69 SkBigPicture::SnapshotArray* newDrawableSnapshot(); 70 71 private: 72 SkTDArray<SkDrawable*> fArray; 73 }; 74 75 // SkRecorder provides an SkCanvas interface for recording into an SkRecord. 76 77 class SkRecorder final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> { 78 public: 79 // Does not take ownership of the SkRecord. 80 SkRecorder(SkRecord*, int width, int height); // TODO: remove 81 SkRecorder(SkRecord*, const SkRect& bounds); 82 83 void reset(SkRecord*, const SkRect& bounds); 84 approxBytesUsedBySubPictures()85 size_t approxBytesUsedBySubPictures() const { return fApproxBytesUsedBySubPictures; } 86 getDrawableList()87 SkDrawableList* getDrawableList() const { return fDrawableList.get(); } detachDrawableList()88 std::unique_ptr<SkDrawableList> detachDrawableList() { return std::move(fDrawableList); } 89 90 // Make SkRecorder forget entirely about its SkRecord*; all calls to SkRecorder will fail. 91 void forgetRecord(); 92 93 void willSave() override; 94 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; 95 bool onDoSaveBehind(const SkRect*) override; willRestore()96 void willRestore() override {} 97 void didRestore() override; 98 99 void didConcat44(const SkM44&) override; 100 void didSetM44(const SkM44&) override; 101 void didScale(SkScalar, SkScalar) override; 102 void didTranslate(SkScalar, SkScalar) override; 103 104 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 105 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 106 void onDrawTextBlob(const SkTextBlob* blob, 107 SkScalar x, 108 SkScalar y, 109 const SkPaint& paint) override; 110 void onDrawSlug(const sktext::gpu::Slug* slug, const SkPaint& paint) override; 111 void onDrawGlyphRunList( 112 const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) override; 113 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 114 const SkPoint texCoords[4], SkBlendMode, 115 const SkPaint& paint) override; 116 117 void onDrawPaint(const SkPaint&) override; 118 void onDrawBehind(const SkPaint&) override; 119 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 120 void onDrawRect(const SkRect&, const SkPaint&) override; 121 void onDrawRegion(const SkRegion&, const SkPaint&) override; 122 void onDrawOval(const SkRect&, const SkPaint&) override; 123 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 124 void onDrawRRect(const SkRRect&, const SkPaint&) override; 125 void onDrawPath(const SkPath&, const SkPaint&) override; 126 127 void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&, 128 const SkPaint*) override; 129 void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&, 130 const SkPaint*, SrcRectConstraint) override; 131 void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode, 132 const SkPaint*) override; 133 void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, 134 SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override; 135 136 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override; 137 138 void onDrawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override; 139 140 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override; 141 142 void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle) override; 143 void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle) override; 144 void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle) override; 145 void onClipShader(sk_sp<SkShader>, SkClipOp) override; 146 void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override; 147 void onResetClip() override; 148 149 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 150 151 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 152 153 void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&, 154 SkBlendMode) override; 155 void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], 156 const SkSamplingOptions&, const SkPaint*, 157 SrcRectConstraint) override; 158 159 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override; 160 161 private: 162 template <typename T> 163 T* copy(const T*); 164 165 template <typename T> 166 T* copy(const T[], size_t count); 167 168 template<typename T, typename... Args> 169 void append(Args&&...); 170 171 size_t fApproxBytesUsedBySubPictures; 172 SkRecord* fRecord; 173 std::unique_ptr<SkDrawableList> fDrawableList; 174 }; 175 176 #endif//SkRecorder_DEFINED 177