158b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com/*
258b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com * Copyright 2012 Google Inc.
358b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com *
458b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
558b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com * found in the LICENSE file.
658b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com */
758b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
858b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#ifndef GrSWMaskHelper_DEFINED
958b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#define GrSWMaskHelper_DEFINED
1058b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
1158b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#include "GrColor.h"
12a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "GrDrawState.h"
1358b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#include "SkBitmap.h"
1458b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#include "SkDraw.h"
15a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "SkMatrix.h"
1658b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#include "SkRasterClip.h"
1758b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com#include "SkRegion.h"
18a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "SkTypes.h"
1958b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
2058b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.comclass GrAutoScratchTexture;
2158b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.comclass GrContext;
2258b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.comclass GrTexture;
2358b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.comclass SkPath;
245f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.comclass SkStrokeRec;
255dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.comclass GrDrawTarget;
2658b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
2758b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com/**
2858b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com * The GrSWMaskHelper helps generate clip masks using the software rendering
29366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com * path. It is intended to be used as:
30366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *
31366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *   GrSWMaskHelper helper(context);
32366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *   helper.init(...);
33366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *
34366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *      draw one or more paths/rects specifying the required boolean ops
35366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *
36366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *   toTexture();   // to get it from the internal bitmap to the GPU
37366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com *
38366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com * The result of this process will be the final mask (on the GPU) in the
39366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com * upper left hand corner of the texture.
4058b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com */
41e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass GrSWMaskHelper : SkNoncopyable {
4258b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.compublic:
43d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    GrSWMaskHelper(GrContext* context)
4458b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    : fContext(context) {
4558b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    }
4658b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
47366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // set up the internal state in preparation for draws. Since many masks
48d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // may be accumulated in the helper during creation, "resultBounds"
49d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // allows the caller to specify the region of interest - to limit the
50366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // amount of work.
5184e922bfb3e7a03f6e35beb2f1e4f2657e7cd3e9reed@google.com    bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
5258b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
53366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // Draw a single rect into the accumulation bitmap using the specified op
54fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    void draw(const SkRect& rect, SkRegion::Op op,
55366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com              bool antiAlias, uint8_t alpha);
5658b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
57366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // Draw a single path into the accumuation bitmap using the specified op
585f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.com    void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
5912b4e27ae1a29460e91a59f38122483e1faec697sugoi@google.com              bool antiAlias, uint8_t alpha);
6058b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
61366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // Helper function to get a scratch texture suitable for capturing the
62366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // result (i.e., right size & format)
63366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    bool getTexture(GrAutoScratchTexture* texture);
6458b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
65366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // Move the mask generation results from the internal bitmap to the gpu.
66d92cf2ebbfbb9d737ea4a551e64ffccb08376875robertphillips@google.com    void toTexture(GrTexture* texture);
6758b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
68366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // Reset the internal bitmap
69366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    void clear(uint8_t alpha) {
70366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com        fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
7158b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    }
7258b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
73366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // Canonical usage utility that draws a single path and uploads it
74366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com    // to the GPU. The result is returned in "result".
755dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    static GrTexture* DrawPathMaskToTexture(GrContext* context,
765dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com                                            const SkPath& path,
775f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.com                                            const SkStrokeRec& stroke,
78fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                                            const SkIRect& resultBounds,
795dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com                                            bool antiAlias,
80b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                                            SkMatrix* matrix);
815dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com
825dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    // This utility routine is used to add a path's mask to some other draw.
83d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // The ClipMaskManager uses it to accumulate clip masks while the
845dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
85d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // It draws with "texture" as a path mask into "target" using "rect" as
865dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    // geometry and the current drawState. The current drawState is altered to
875dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    // accommodate the mask.
88d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // Note that this method assumes that the GrPaint::kTotalStages slot in
895dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    // the draw state can be used to hold the mask texture stage.
90d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    // This method is really only intended to be used with the
915dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    // output of DrawPathMaskToTexture.
925dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com    static void DrawToTargetWithPathMask(GrTexture* texture,
935dfb67219a308edecafbe09eebb35c5e149db6e6robertphillips@google.com                                         GrDrawTarget* target,
94fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                                         const SkIRect& rect);
95366f1c6a09f63c76e78145cb08028f66062f31fdrobertphillips@google.com
9658b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.comprotected:
9758b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.comprivate:
9858b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    GrContext*      fContext;
99b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com    SkMatrix        fMatrix;
10058b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    SkBitmap        fBM;
10158b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    SkDraw          fDraw;
10258b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com    SkRasterClip    fRasterClip;
10358b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
104fb4f5cb39e31b8544b526074683a65bb716b7acfkrajcevski    // Actually sends the texture data to the GPU. This is called from
105fb4f5cb39e31b8544b526074683a65bb716b7acfkrajcevski    // toTexture with the data filled in depending on the texture config.
106fb4f5cb39e31b8544b526074683a65bb716b7acfkrajcevski    void sendTextureData(GrTexture *texture, const GrTextureDesc& desc,
107fb4f5cb39e31b8544b526074683a65bb716b7acfkrajcevski                         const void *data, int rowbytes);
108fb4f5cb39e31b8544b526074683a65bb716b7acfkrajcevski
109a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    typedef SkNoncopyable INHERITED;
11058b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com};
11158b20215f6d88b7e1cacae0b76226fced5109293robertphillips@google.com
112fe65943d309dc687f618d16e394dff3baa526426robertphillips@google.com#endif // GrSWMaskHelper_DEFINED
113