GrSWMaskHelper.h revision 296b1ccf9b8e9c8b945645efcbaa9c71c7135f58
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 "GrRenderTargetContext.h"
13#include "SkAutoPixmapStorage.h"
14#include "SkBitmap.h"
15#include "SkDraw.h"
16#include "SkMatrix.h"
17#include "SkRasterClip.h"
18#include "SkRegion.h"
19#include "SkTypes.h"
20
21class GrClip;
22class GrPaint;
23class GrShape;
24class GrStyle;
25class GrTexture;
26struct GrUserStencilSettings;
27
28/**
29 * The GrSWMaskHelper helps generate clip masks using the software rendering
30 * path. It is intended to be used as:
31 *
32 *   GrSWMaskHelper helper(context);
33 *   helper.init(...);
34 *
35 *      draw one or more paths/rects specifying the required boolean ops
36 *
37 *   toTexture();   // to get it from the internal bitmap to the GPU
38 *
39 * The result of this process will be the final mask (on the GPU) in the
40 * upper left hand corner of the texture.
41 */
42class GrSWMaskHelper : SkNoncopyable {
43public:
44    GrSWMaskHelper() { }
45
46    // set up the internal state in preparation for draws. Since many masks
47    // may be accumulated in the helper during creation, "resultBounds"
48    // allows the caller to specify the region of interest - to limit the
49    // amount of work.
50    bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
51
52    // Draw a single rect into the accumulation bitmap using the specified op
53    void drawRect(const SkRect& rect, SkRegion::Op op, GrAA, uint8_t alpha);
54
55    // Draw a single path into the accumuation bitmap using the specified op
56    void drawShape(const GrShape&, SkRegion::Op op, GrAA, uint8_t alpha);
57
58    sk_sp<GrTextureProxy> toTextureProxy(GrContext*, SkBackingFit fit);
59
60    // Convert mask generation results to a signed distance field
61    void toSDF(unsigned char* sdf);
62
63    // Reset the internal bitmap
64    void clear(uint8_t alpha) {
65        fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF));
66    }
67
68    // Canonical usage utility that draws a single path and uploads it
69    // to the GPU. The result is returned.
70    static sk_sp<GrTextureProxy> DrawShapeMaskToTexture(GrContext*,
71                                                        const GrShape&,
72                                                        const SkIRect& resultBounds,
73                                                        GrAA,
74                                                        SkBackingFit,
75                                                        const SkMatrix* matrix);
76
77    // This utility draws a path mask generated by DrawShapeMaskToTexture using a provided paint.
78    // The rectangle is drawn in device space. The 'viewMatrix' will be used to ensure the correct
79    // local coords are provided to any fragment processors in the paint.
80    static void DrawToTargetWithShapeMask(sk_sp<GrTextureProxy>,
81                                          GrRenderTargetContext*,
82                                          GrPaint&& paint,
83                                          const GrUserStencilSettings& userStencilSettings,
84                                          const GrClip&,
85                                          const SkMatrix& viewMatrix,
86                                          const SkIPoint& textureOriginInDeviceSpace,
87                                          const SkIRect& deviceSpaceRectToDraw);
88
89private:
90    SkMatrix            fMatrix;
91    SkAutoPixmapStorage fPixels;
92    SkDraw              fDraw;
93    SkRasterClip        fRasterClip;
94
95    typedef SkNoncopyable INHERITED;
96};
97
98#endif // GrSWMaskHelper_DEFINED
99