blob: e318e113b52fba6cd793f22895dae642a7e47642 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkDrawLooper_DEFINED
11#define SkDrawLooper_DEFINED
12
reed@google.comdaaafa62014-04-29 15:20:16 +000013#include "SkBlurTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkFlattenable.h"
reed@google.comdaaafa62014-04-29 15:20:16 +000015#include "SkPoint.h"
16#include "SkColor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
Herb Derby73fe7b02017-02-08 15:12:19 -050018class SkArenaAlloc;
19class SkCanvas;
Matt Sarettcdc651d2017-03-30 12:41:48 -040020class SkColorSpaceXformer;
Herb Derby73fe7b02017-02-08 15:12:19 -050021class SkPaint;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000022struct SkRect;
Herb Derby73fe7b02017-02-08 15:12:19 -050023class SkString;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25/** \class SkDrawLooper
26 Subclasses of SkDrawLooper can be attached to a SkPaint. Where they are,
27 and something is drawn to a canvas with that paint, the looper subclass will
28 be called, allowing it to modify the canvas and/or paint for that draw call.
29 More than that, via the next() method, the looper can modify the draw to be
30 invoked multiple times (hence the name loop-er), allow it to perform effects
31 like shadows or frame/fills, that require more than one pass.
32*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000033class SK_API SkDrawLooper : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034public:
reed@google.com4e2b3d32011-04-07 14:18:59 +000035 /**
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000036 * Holds state during a draw. Users call next() until it returns false.
37 *
38 * Subclasses of SkDrawLooper should create a subclass of this object to
39 * hold state specific to their subclass.
reed@google.com4e2b3d32011-04-07 14:18:59 +000040 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000041 class SK_API Context : ::SkNoncopyable {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000042 public:
43 Context() {}
44 virtual ~Context() {}
45
46 /**
47 * Called in a loop on objects returned by SkDrawLooper::createContext().
48 * Each time true is returned, the object is drawn (possibly with a modified
49 * canvas and/or paint). When false is finally returned, drawing for the object
50 * stops.
51 *
52 * On each call, the paint will be in its original state, but the
53 * canvas will be as it was following the previous call to next() or
54 * createContext().
55 *
56 * The implementation must ensure that, when next() finally returns
57 * false, the canvas has been restored to the state it was
58 * initially, before createContext() was first called.
59 */
60 virtual bool next(SkCanvas* canvas, SkPaint* paint) = 0;
61 };
reed@google.com4e2b3d32011-04-07 14:18:59 +000062
63 /**
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000064 * Called right before something is being drawn. Returns a Context
65 * whose next() method should be called until it returns false.
reed@google.com4e2b3d32011-04-07 14:18:59 +000066 */
Herb Derby73fe7b02017-02-08 15:12:19 -050067 virtual Context* makeContext(SkCanvas*, SkArenaAlloc*) const = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
reed@google.com9efd9a02012-01-30 15:41:43 +000069 /**
70 * The fast bounds functions are used to enable the paint to be culled early
71 * in the drawing pipeline. If a subclass can support this feature it must
72 * return true for the canComputeFastBounds() function. If that function
73 * returns false then computeFastBounds behavior is undefined otherwise it
74 * is expected to have the following behavior. Given the parent paint and
75 * the parent's bounding rect the subclass must fill in and return the
76 * storage rect, where the storage rect is with the union of the src rect
77 * and the looper's bounding rect.
78 */
mtklein05d61152014-12-01 13:03:22 -080079 bool canComputeFastBounds(const SkPaint& paint) const;
80 void computeFastBounds(const SkPaint& paint, const SkRect& src, SkRect* dst) const;
reed@google.com9efd9a02012-01-30 15:41:43 +000081
reed@google.comdaaafa62014-04-29 15:20:16 +000082 struct BlurShadowRec {
83 SkScalar fSigma;
84 SkVector fOffset;
85 SkColor fColor;
86 SkBlurStyle fStyle;
Mike Reed1be1f8d2018-03-14 13:01:17 -040087#ifdef SK_SUPPORT_LEGACY_BLURMASKFILTER
reed@google.comdaaafa62014-04-29 15:20:16 +000088 SkBlurQuality fQuality;
Mike Reed1be1f8d2018-03-14 13:01:17 -040089#endif
reed@google.comdaaafa62014-04-29 15:20:16 +000090 };
91 /**
92 * If this looper can be interpreted as having two layers, such that
93 * 1. The first layer (bottom most) just has a blur and translate
94 * 2. The second layer has no modifications to either paint or canvas
95 * 3. No other layers.
96 * then return true, and if not null, fill out the BlurShadowRec).
97 *
98 * If any of the above are not met, return false and ignore the BlurShadowRec parameter.
99 */
100 virtual bool asABlurShadow(BlurShadowRec*) const;
101
Cary Clark99885412018-04-05 13:09:58 -0400102 virtual void toString(SkString* str) const = 0;
mtklein3b375452016-04-04 14:57:19 -0700103 SK_DEFINE_FLATTENABLE_TYPE(SkDrawLooper)
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105protected:
Matt Sarettcdc651d2017-03-30 12:41:48 -0400106 sk_sp<SkDrawLooper> makeColorSpace(SkColorSpaceXformer* xformer) const {
107 return this->onMakeColorSpace(xformer);
108 }
109 virtual sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const = 0;
110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SkDrawLooper() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112
113private:
Matt Sarettcdc651d2017-03-30 12:41:48 -0400114 friend class SkColorSpaceXformer;
115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 typedef SkFlattenable INHERITED;
117};
118
119#endif