SkDraw.h revision 7839ce1af63bf12fe7b3caa866970bbbb3afb13d
1756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
2756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson/*
3756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson * Copyright 2006 The Android Open Source Project
4756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson *
5756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson * Use of this source code is governed by a BSD-style license that can be
6756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson * found in the LICENSE file.
7756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson */
8756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
9756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
10756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson#ifndef SkDraw_DEFINED
11756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson#define SkDraw_DEFINED
12756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
13756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson#include "SkCanvas.h"
142bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump#include "SkMask.h"
152bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump#include "SkPaint.h"
162bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
17b25938303de0976b9f189363d43033e5788e3d36John McCallclass SkBitmap;
18f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallclass SkBounder;
192bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stumpclass SkClipStack;
205a180397870944548aaadeaebf58e415885b9489John McCallclass SkDevice;
21756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlssonclass SkMatrix;
22f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallclass SkPath;
2336f893c1efe367f929d92c8b125f964c22ba189eJohn McCallclass SkRegion;
24204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCallclass SkRasterClip;
25f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstruct SkDrawProcs;
26756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlssonstruct SkRect;
27756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
28756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlssonclass SkDraw {
29d3379292f90e1381d3236c68891bb725b02464b6Anders Carlssonpublic:
30d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson    SkDraw();
318755ec336108839b9621c3b18f0e175f8a3b671cMike Stump    SkDraw(const SkDraw& src);
322acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner
33da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    void    drawPaint(const SkPaint&) const;
348755ec336108839b9621c3b18f0e175f8a3b671cMike Stump    void    drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[],
35d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson                       const SkPaint&, bool forceUseDevice = false) const;
36d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson    void    drawRect(const SkRect&, const SkPaint&) const;
37d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson    /**
3899533834ba8f3658559f334e68a518ebb6388ceaMike Stump     *  To save on mallocs, we allow a flag that tells us that srcPath is
3999533834ba8f3658559f334e68a518ebb6388ceaMike Stump     *  mutable, so that we don't have to make copies of it as we transform it.
408755ec336108839b9621c3b18f0e175f8a3b671cMike Stump     *
412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner     *  If prePathMatrix is not null, it should logically be applied before any
42da549e8995c447542d5631b8b67fcc3a9582797aJay Foad     *  stroking or other effects. If there are no effects on the paint that
438755ec336108839b9621c3b18f0e175f8a3b671cMike Stump     *  affect the geometry/rasterization, then the pre matrix can just be
4499533834ba8f3658559f334e68a518ebb6388ceaMike Stump     *  pre-concated with the current matrix.
4599533834ba8f3658559f334e68a518ebb6388ceaMike Stump     */
4699533834ba8f3658559f334e68a518ebb6388ceaMike Stump    void    drawPath(const SkPath& srcPath, const SkPaint&,
47d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson                     const SkMatrix* prePathMatrix, bool pathIsMutable) const;
488755ec336108839b9621c3b18f0e175f8a3b671cMike Stump    void    drawBitmap(const SkBitmap&, const SkMatrix&, const SkPaint&) const;
4999533834ba8f3658559f334e68a518ebb6388ceaMike Stump    void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
50d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson    void    drawText(const char text[], size_t byteLength, SkScalar x,
5161c1601e2a8534c09cb7032f791f918158abaa27John McCall                     SkScalar y, const SkPaint& paint) const;
522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    void    drawPosText(const char text[], size_t byteLength,
5361c1601e2a8534c09cb7032f791f918158abaa27John McCall                        const SkScalar pos[], SkScalar constY,
548755ec336108839b9621c3b18f0e175f8a3b671cMike Stump                        int scalarsPerPosition, const SkPaint& paint) const;
55d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson    void    drawTextOnPath(const char text[], size_t byteLength,
56d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson                        const SkPath&, const SkMatrix*, const SkPaint&) const;
57d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson#ifdef SK_BUILD_FOR_ANDROID
58b4eea691866a3fa75722da9eba735c44f140398aMike Stump    void    drawPosTextOnPath(const char text[], size_t byteLength,
5999533834ba8f3658559f334e68a518ebb6388ceaMike Stump                              const SkPoint pos[], const SkPaint& paint,
60b4eea691866a3fa75722da9eba735c44f140398aMike Stump                              const SkPath& path, const SkMatrix* matrix) const;
612acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner#endif
6261c1601e2a8534c09cb7032f791f918158abaa27John McCall    void    drawVertices(SkCanvas::VertexMode mode, int count,
638755ec336108839b9621c3b18f0e175f8a3b671cMike Stump                         const SkPoint vertices[], const SkPoint textures[],
64b4eea691866a3fa75722da9eba735c44f140398aMike Stump                         const SkColor colors[], SkXfermode* xmode,
65b4eea691866a3fa75722da9eba735c44f140398aMike Stump                         const uint16_t indices[], int ptCount,
66b4eea691866a3fa75722da9eba735c44f140398aMike Stump                         const SkPaint& paint) const;
67f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
68f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    void drawPath(const SkPath& src, const SkPaint& paint) const {
69f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        this->drawPath(src, paint, NULL, false);
702acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    }
71da549e8995c447542d5631b8b67fcc3a9582797aJay Foad
72f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    /** Helper function that creates a mask from a path and an optional maskfilter.
73f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        Note however, that the resulting mask will not have been actually filtered,
74f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        that must be done afterwards (by calling filterMask). The maskfilter is provided
75f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        solely to assist in computing the mask's bounds (if the mode requests that).
762bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump    */
77f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
782bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump                           const SkMaskFilter*, const SkMatrix* filterMatrix,
792acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                           SkMask* mask, SkMask::CreateMode mode,
80da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                           SkPaint::Style style);
818755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
822bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump    enum RectType {
832bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump        kHair_RectType,
842bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump        kFill_RectType,
852bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump        kStroke_RectType,
8699533834ba8f3658559f334e68a518ebb6388ceaMike Stump        kPath_RectType
872bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump    };
882acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner
8961c1601e2a8534c09cb7032f791f918158abaa27John McCall    /**
908755ec336108839b9621c3b18f0e175f8a3b671cMike Stump     *  Based on the paint's style, strokeWidth, and the matrix, classify how
912bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump     *  to draw the rect. If no special-case is available, returns
922bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump     *  kPath_RectType.
932bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump     *
94cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump     *  Iff RectType == kStroke_RectType, then strokeSize is set to the device
95cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump     *  width and height of the stroke.
96cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump     */
972acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
98da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                                    SkPoint* strokeSize);
998755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
100cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stumpprivate:
101cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump    void    drawText_asPaths(const char text[], size_t byteLength,
102cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump                             SkScalar x, SkScalar y, const SkPaint&) const;
10393c332a8ba2c193c435b293966d343dab15f555bJohn McCall    void    drawDevMask(const SkMask& mask, const SkPaint&) const;
1042acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    void    drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
105da549e8995c447542d5631b8b67fcc3a9582797aJay Foad
10693c332a8ba2c193c435b293966d343dab15f555bJohn McCall    /**
10793c332a8ba2c193c435b293966d343dab15f555bJohn McCall     *  Return the current clip bounds, in local coordinates, with slop to account
10893c332a8ba2c193c435b293966d343dab15f555bJohn McCall     *  for antialiasing or hairlines (i.e. device-bounds outset by 1, and then
10993c332a8ba2c193c435b293966d343dab15f555bJohn McCall     *  run through the inverse of the matrix).
11093c332a8ba2c193c435b293966d343dab15f555bJohn McCall     *
11193c332a8ba2c193c435b293966d343dab15f555bJohn McCall     *  If the matrix cannot be inverted, or the current clip is empty, return
11286a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregor     *  false and ignore bounds parameter.
1132acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner     */
114da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    bool SK_WARN_UNUSED_RESULT
1158755ec336108839b9621c3b18f0e175f8a3b671cMike Stump    computeConservativeLocalClipBounds(SkRect* bounds) const;
11686a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregor
117a5f2de2e49e25ece82dd9fd63d8f390a7cda6417John McCallpublic:
11886a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregor    const SkBitmap* fBitmap;        // required
1190f590be3808365e851352543faa6acbece50b686Mike Stump    const SkMatrix* fMatrix;        // required
1200f590be3808365e851352543faa6acbece50b686Mike Stump    const SkRegion* fClip;          // DEPRECATED
12199533834ba8f3658559f334e68a518ebb6388ceaMike Stump    const SkRasterClip* fRC;        // required
12299533834ba8f3658559f334e68a518ebb6388ceaMike Stump
12399533834ba8f3658559f334e68a518ebb6388ceaMike Stump    const SkClipStack* fClipStack;  // optional
1242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    SkDevice*       fDevice;        // optional
12561c1601e2a8534c09cb7032f791f918158abaa27John McCall    SkBounder*      fBounder;       // optional
1268755ec336108839b9621c3b18f0e175f8a3b671cMike Stump    SkDrawProcs*    fProcs;         // optional
1275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner
128256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall#ifdef SK_DEBUG
129256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall    void validate() const;
130256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall#else
131256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall    void validate() const {}
132256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall#endif
133256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall};
134256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall
135256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall#endif
136256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall