1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDrawLooper_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkDrawLooper_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com#include "SkBlurTypes.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkFlattenable.h"
15daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com#include "SkPoint.h"
16daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com#include "SkColor.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkCanvas;
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkPaint;
20c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.comstruct SkRect;
214991b8f23482afc1494fd17647421ce68de53331robertphillips@google.comclass SkString;
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkDrawLooper
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Subclasses of SkDrawLooper can be attached to a SkPaint. Where they are,
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    and something is drawn to a canvas with that paint, the looper subclass will
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    be called, allowing it to modify the canvas and/or paint for that draw call.
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    More than that, via the next() method, the looper can modify the draw to be
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    invoked multiple times (hence the name loop-er), allow it to perform effects
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    like shadows or frame/fills, that require more than one pass.
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
317ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkDrawLooper : public SkFlattenable {
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
330456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com    SK_DECLARE_INST_COUNT(SkDrawLooper)
340456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com
354e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    /**
3679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Holds state during a draw. Users call next() until it returns false.
3779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *
3879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Subclasses of SkDrawLooper should create a subclass of this object to
3979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  hold state specific to their subclass.
404e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com     */
41e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.org    class SK_API Context : ::SkNoncopyable {
4279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    public:
4379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        Context() {}
4479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual ~Context() {}
4579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
4679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        /**
4779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  Called in a loop on objects returned by SkDrawLooper::createContext().
4879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  Each time true is returned, the object is drawn (possibly with a modified
4979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  canvas and/or paint). When false is finally returned, drawing for the object
5079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  stops.
5179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *
5279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  On each call, the paint will be in its original state, but the
5379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  canvas will be as it was following the previous call to next() or
5479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  createContext().
5579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *
5679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  The implementation must ensure that, when next() finally returns
5779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  false, the canvas has been restored to the state it was
5879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  initially, before createContext() was first called.
5979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         */
6079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual bool next(SkCanvas* canvas, SkPaint* paint) = 0;
6179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    };
624e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com
634e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    /**
6479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Called right before something is being drawn. Returns a Context
6579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  whose next() method should be called until it returns false.
6679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  The caller has to ensure that the storage pointer provides enough
6779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  memory for the Context. The required size can be queried by calling
6879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  contextSize(). It is also the caller's responsibility to destroy the
6979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  object after use.
704e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com     */
7179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual Context* createContext(SkCanvas*, void* storage) const = 0;
7279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
7379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    /**
7479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org      *  Returns the number of bytes needed to store subclasses of Context (belonging to the
7579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org      *  corresponding SkDrawLooper subclass).
7679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org      */
7779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual size_t contextSize() const = 0;
7879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
79fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
809efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com    /**
819efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * The fast bounds functions are used to enable the paint to be culled early
829efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * in the drawing pipeline. If a subclass can support this feature it must
839efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * return true for the canComputeFastBounds() function.  If that function
849efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * returns false then computeFastBounds behavior is undefined otherwise it
859efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * is expected to have the following behavior. Given the parent paint and
869efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * the parent's bounding rect the subclass must fill in and return the
879efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * storage rect, where the storage rect is with the union of the src rect
889efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * and the looper's bounding rect.
899efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     */
9079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual bool canComputeFastBounds(const SkPaint& paint) const;
919efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com    virtual void computeFastBounds(const SkPaint& paint,
9279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org                                   const SkRect& src, SkRect* dst) const;
939efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com
94daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    struct BlurShadowRec {
95daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkScalar        fSigma;
96daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkVector        fOffset;
97daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkColor         fColor;
98daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkBlurStyle     fStyle;
99daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkBlurQuality   fQuality;
100daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    };
101daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    /**
102daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  If this looper can be interpreted as having two layers, such that
103daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      1. The first layer (bottom most) just has a blur and translate
104daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      2. The second layer has no modifications to either paint or canvas
105daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      3. No other layers.
106daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  then return true, and if not null, fill out the BlurShadowRec).
107daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *
108daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  If any of the above are not met, return false and ignore the BlurShadowRec parameter.
109daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     */
110daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    virtual bool asABlurShadow(BlurShadowRec*) const;
111daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
1120f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_PUREVIRT()
113c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    SK_DEFINE_FLATTENABLE_TYPE(SkDrawLooper)
1144991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawLooper() {}
1179fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
1188b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    SkDrawLooper(SkReadBuffer& buffer) : INHERITED(buffer) {}
1199fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#endif
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFlattenable INHERITED;
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
126