1
2/*
3 * Copyright 2011 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkDrawLooper_DEFINED
11#define SkDrawLooper_DEFINED
12
13#include "include/core/SkBlurTypes.h"
14#include "include/core/SkColor.h"
15#include "include/core/SkFlattenable.h"
16#include "include/core/SkPoint.h"
17#include <functional> // std::function
18
19class SkArenaAlloc;
20class SkCanvas;
21class SkMatrix;
22class SkPaint;
23struct SkRect;
24
25/** \class SkDrawLooper
26 DEPRECATED: No longer supported in Skia.
27*/
28class SK_API SkDrawLooper : public SkFlattenable {
29public:
30 /**
31 * Holds state during a draw. Users call next() until it returns false.
32 *
33 * Subclasses of SkDrawLooper should create a subclass of this object to
34 * hold state specific to their subclass.
35 */
36 class SK_API Context {
37 public:
38 Context() {}
39 virtual ~Context() {}
40
41 struct Info {
42 SkVector fTranslate;
43 bool fApplyPostCTM;
44
45 void applyToCTM(SkMatrix* ctm) const;
46 void applyToCanvas(SkCanvas*) const;
47 };
48
49 /**
50 * Called in a loop on objects returned by SkDrawLooper::createContext().
51 * Each time true is returned, the object is drawn (possibly with a modified
52 * canvas and/or paint). When false is finally returned, drawing for the object
53 * stops.
54 *
55 * On each call, the paint will be in its original state, but the
56 * canvas will be as it was following the previous call to next() or
57 * createContext().
58 *
59 * The implementation must ensure that, when next() finally returns
60 * false, the canvas has been restored to the state it was
61 * initially, before createContext() was first called.
62 */
63 virtual bool next(Info*, SkPaint*) = 0;
64
65 private:
66 Context(const Context&) = delete;
67 Context& operator=(const Context&) = delete;
68 };
69
70 /**
71 * Called right before something is being drawn. Returns a Context
72 * whose next() method should be called until it returns false.
73 */
74 virtual Context* makeContext(SkArenaAlloc*) const = 0;
75
76 /**
77 * The fast bounds functions are used to enable the paint to be culled early
78 * in the drawing pipeline. If a subclass can support this feature it must
79 * return true for the canComputeFastBounds() function. If that function
80 * returns false then computeFastBounds behavior is undefined otherwise it
81 * is expected to have the following behavior. Given the parent paint and
82 * the parent's bounding rect the subclass must fill in and return the
83 * storage rect, where the storage rect is with the union of the src rect
84 * and the looper's bounding rect.
85 */
86 bool canComputeFastBounds(const SkPaint& paint) const;
87 void computeFastBounds(const SkPaint& paint, const SkRect& src, SkRect* dst) const;
88
89 struct BlurShadowRec {
90 SkScalar fSigma;
91 SkVector fOffset;
92 SkColor fColor;
93 SkBlurStyle fStyle;
94 };
95 /**
96 * If this looper can be interpreted as having two layers, such that
97 * 1. The first layer (bottom most) just has a blur and translate
98 * 2. The second layer has no modifications to either paint or canvas
99 * 3. No other layers.
100 * then return true, and if not null, fill out the BlurShadowRec).
101 *
102 * If any of the above are not met, return false and ignore the BlurShadowRec parameter.
103 */
104 virtual bool asABlurShadow(BlurShadowRec*) const;
105
106 static SkFlattenable::Type GetFlattenableType() {
107 return kSkDrawLooper_Type;
108 }
109
110 SkFlattenable::Type getFlattenableType() const override {
111 return kSkDrawLooper_Type;
112 }
113
114 static sk_sp<SkDrawLooper> Deserialize(const void* data, size_t size,
115 const SkDeserialProcs* procs = nullptr) {
116 return sk_sp<SkDrawLooper>(static_cast<SkDrawLooper*>(
117 SkFlattenable::Deserialize(
118 kSkDrawLooper_Type, data, size, procs).release()));
119 }
120
121 void apply(SkCanvas* canvas, const SkPaint& paint,
122 std::function<void(SkCanvas*, const SkPaint&)>);
123
124protected:
125 SkDrawLooper() {}
126
127private:
128 typedef SkFlattenable INHERITED;
129};
130
131#endif
132