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:
334e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    /**
3479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Holds state during a draw. Users call next() until it returns false.
3579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *
3679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Subclasses of SkDrawLooper should create a subclass of this object to
3779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  hold state specific to their subclass.
384e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com     */
39e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.org    class SK_API Context : ::SkNoncopyable {
4079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    public:
4179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        Context() {}
4279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual ~Context() {}
4379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
4479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        /**
4579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  Called in a loop on objects returned by SkDrawLooper::createContext().
4679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  Each time true is returned, the object is drawn (possibly with a modified
4779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  canvas and/or paint). When false is finally returned, drawing for the object
4879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  stops.
4979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *
5079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  On each call, the paint will be in its original state, but the
5179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  canvas will be as it was following the previous call to next() or
5279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  createContext().
5379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *
5479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  The implementation must ensure that, when next() finally returns
5579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  false, the canvas has been restored to the state it was
5679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  initially, before createContext() was first called.
5779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         */
5879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual bool next(SkCanvas* canvas, SkPaint* paint) = 0;
5979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    };
604e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com
614e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    /**
6279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Called right before something is being drawn. Returns a Context
6379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  whose next() method should be called until it returns false.
6479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  The caller has to ensure that the storage pointer provides enough
6579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  memory for the Context. The required size can be queried by calling
6679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  contextSize(). It is also the caller's responsibility to destroy the
6779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  object after use.
684e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com     */
6979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual Context* createContext(SkCanvas*, void* storage) const = 0;
7079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
7179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    /**
7279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org      *  Returns the number of bytes needed to store subclasses of Context (belonging to the
7379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org      *  corresponding SkDrawLooper subclass).
7479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org      */
7579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual size_t contextSize() const = 0;
7679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
77fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
789efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com    /**
799efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * The fast bounds functions are used to enable the paint to be culled early
809efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * in the drawing pipeline. If a subclass can support this feature it must
819efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * return true for the canComputeFastBounds() function.  If that function
829efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * returns false then computeFastBounds behavior is undefined otherwise it
839efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * is expected to have the following behavior. Given the parent paint and
849efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * the parent's bounding rect the subclass must fill in and return the
859efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * storage rect, where the storage rect is with the union of the src rect
869efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * and the looper's bounding rect.
879efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     */
8805d611574ac7dd07c055c9a7426199b804e6061fmtklein    bool canComputeFastBounds(const SkPaint& paint) const;
8905d611574ac7dd07c055c9a7426199b804e6061fmtklein    void computeFastBounds(const SkPaint& paint, const SkRect& src, SkRect* dst) const;
909efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com
91daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    struct BlurShadowRec {
92daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkScalar        fSigma;
93daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkVector        fOffset;
94daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkColor         fColor;
95daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkBlurStyle     fStyle;
96daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkBlurQuality   fQuality;
97daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    };
98daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    /**
99daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  If this looper can be interpreted as having two layers, such that
100daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      1. The first layer (bottom most) just has a blur and translate
101daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      2. The second layer has no modifications to either paint or canvas
102daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      3. No other layers.
103daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  then return true, and if not null, fill out the BlurShadowRec).
104daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *
105daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  If any of the above are not met, return false and ignore the BlurShadowRec parameter.
106daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     */
107daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    virtual bool asABlurShadow(BlurShadowRec*) const;
108daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
1090f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_PUREVIRT()
110c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    SK_DEFINE_FLATTENABLE_TYPE(SkDrawLooper)
1114991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawLooper() {}
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFlattenable INHERITED;
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
120