GrSWMaskHelper.h revision 366f1c6a09f63c76e78145cb08028f66062f31fd
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrSWMaskHelper_DEFINED
9#define GrSWMaskHelper_DEFINED
10
11#include "GrColor.h"
12#include "GrMatrix.h"
13#include "GrNoncopyable.h"
14#include "SkBitmap.h"
15#include "SkDraw.h"
16#include "SkRasterClip.h"
17#include "SkRegion.h"
18
19class GrAutoScratchTexture;
20class GrContext;
21class GrTexture;
22class SkPath;
23
24/**
25 * The GrSWMaskHelper helps generate clip masks using the software rendering
26 * path. It is intended to be used as:
27 *
28 *   GrSWMaskHelper helper(context);
29 *   helper.init(...);
30 *
31 *      draw one or more paths/rects specifying the required boolean ops
32 *
33 *   toTexture();   // to get it from the internal bitmap to the GPU
34 *
35 * The result of this process will be the final mask (on the GPU) in the
36 * upper left hand corner of the texture.
37 */
38class GrSWMaskHelper : public GrNoncopyable {
39public:
40    GrSWMaskHelper(GrContext* context)
41    : fContext(context) {
42    }
43
44    // set up the internal state in preparation for draws. Since many masks
45    // may be accumulated in the helper during creation, "resultBounds"
46    // allows the caller to specify the region of interest - to limit the
47    // amount of work.
48    bool init(const GrIRect& resultBounds, const GrMatrix* matrix);
49
50    // Draw a single rect into the accumulation bitmap using the specified op
51    void draw(const GrRect& rect, SkRegion::Op op,
52              bool antiAlias, uint8_t alpha);
53
54    // Draw a single path into the accumuation bitmap using the specified op
55    void draw(const SkPath& path, SkRegion::Op op,
56              GrPathFill fill, bool antiAlias, uint8_t alpha);
57
58    // Helper function to get a scratch texture suitable for capturing the
59    // result (i.e., right size & format)
60    bool getTexture(GrAutoScratchTexture* texture);
61
62    // Move the mask generation results from the internal bitmap to the gpu.
63    // The space outside of the mask is cleared using "alpha"
64    void toTexture(GrTexture* texture, uint8_t alpha);
65
66    // Reset the internal bitmap
67    void clear(uint8_t alpha) {
68        fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
69    }
70
71    // Canonical usage utility that draws a single path and uploads it
72    // to the GPU. The result is returned in "result".
73    static bool DrawToTexture(GrContext* context,
74                              const SkPath& path,
75                              const GrIRect& resultBounds,
76                              GrPathFill fill,
77                              GrAutoScratchTexture* result,
78                              bool antiAlias,
79                              GrMatrix* matrix);
80
81protected:
82private:
83    GrContext*      fContext;
84    GrMatrix        fMatrix;
85    SkBitmap        fBM;
86    SkDraw          fDraw;
87    SkRasterClip    fRasterClip;
88
89    typedef GrNoncopyable INHERITED;
90};
91
92#endif // GrSWMaskHelper_DEFINED
93