Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrSWMaskHelper.h
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 "GrDrawState.h"
13 #include "SkBitmap.h"
14 #include "SkDraw.h"
15 #include "SkMatrix.h"
16 #include "SkRasterClip.h"
17 #include "SkRegion.h"
18 #include "SkTextureCompressor.h"
19 #include "SkTypes.h"
20
21 class GrContext;
22 class GrTexture;
23 class SkPath;
24 class SkStrokeRec;
25 class GrDrawTarget;
26
27 /**
28  * The GrSWMaskHelper helps generate clip masks using the software rendering
29  * path. It is intended to be used as:
30  *
31  *   GrSWMaskHelper helper(context);
32  *   helper.init(...);
33  *
34  *      draw one or more paths/rects specifying the required boolean ops
35  *
36  *   toTexture();   // to get it from the internal bitmap to the GPU
37  *
38  * The result of this process will be the final mask (on the GPU) in the
39  * upper left hand corner of the texture.
40  */
41 class GrSWMaskHelper : SkNoncopyable {
42 public:
43     GrSWMaskHelper(GrContext* context)
44     : fContext(context)
45     , fCompressionMode(kNone_CompressionMode) {
46     }
47
48     // set up the internal state in preparation for draws. Since many masks
49     // may be accumulated in the helper during creation, "resultBounds"
50     // allows the caller to specify the region of interest - to limit the
51     // amount of work. allowCompression should be set to false if you plan on using
52     // your own texture to draw into, and not a scratch texture via getTexture().
53     bool init(const SkIRect& resultBounds, const SkMatrix* matrix, bool allowCompression = true);
54
55     // Draw a single rect into the accumulation bitmap using the specified op
56     void draw(const SkRect& rect, SkRegion::Op op,
57               bool antiAlias, uint8_t alpha);
58
59     // Draw a single path into the accumuation bitmap using the specified op
60     void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
61               bool antiAlias, uint8_t alpha);
62
63     // Move the mask generation results from the internal bitmap to the gpu.
64     void toTexture(GrTexture* texture);
65
66     // Convert mask generation results to a signed distance field
67     void toSDF(unsigned char* sdf);
68     
69     // Reset the internal bitmap
70     void clear(uint8_t alpha) {
71         fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
72     }
73
74     // Canonical usage utility that draws a single path and uploads it
75     // to the GPU. The result is returned.
76     static GrTexture* DrawPathMaskToTexture(GrContext* context,
77                                             const SkPath& path,
78                                             const SkStrokeRec& stroke,
79                                             const SkIRect& resultBounds,
80                                             bool antiAlias,
81                                             SkMatrix* matrix);
82
83     // This utility routine is used to add a path's mask to some other draw.
84     // The ClipMaskManager uses it to accumulate clip masks while the
85     // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
86     // It draws with "texture" as a path mask into "target" using "rect" as
87     // geometry and the current drawState. The current drawState is altered to
88     // accommodate the mask.
89     // Note that this method assumes that the GrPaint::kTotalStages slot in
90     // the draw state can be used to hold the mask texture stage.
91     // This method is really only intended to be used with the
92     // output of DrawPathMaskToTexture.
93     static void DrawToTargetWithPathMask(GrTexture* texture,
94                                          GrDrawTarget* target,
95                                          const SkIRect& rect);
96
97 private:
98     // Helper function to get a scratch texture suitable for capturing the
99     // result (i.e., right size & format)
100     GrTexture* createTexture();
101
102     GrContext*      fContext;
103     SkMatrix        fMatrix;
104     SkBitmap        fBM;
105     SkDraw          fDraw;
106     SkRasterClip    fRasterClip;
107
108     // This enum says whether or not we should compress the mask:
109     // kNone_CompressionMode: compression is not supported on this device.
110     // kCompress_CompressionMode: compress the bitmap before it gets sent to the gpu
111     // kBlitter_CompressionMode: write to the bitmap using a special compressed blitter.
112     enum CompressionMode {
113         kNone_CompressionMode,
114         kCompress_CompressionMode,
115         kBlitter_CompressionMode,
116     } fCompressionMode;
117
118     // This is the buffer into which we store our compressed data. This buffer is
119     // only allocated (non-null) if fCompressionMode is kBlitter_CompressionMode
120     SkAutoMalloc fCompressedBuffer;
121
122     // This is the desired format within which to compress the
123     // texture. This value is only valid if fCompressionMode is not kNone_CompressionMode.
124     SkTextureCompressor::Format fCompressedFormat;
125
126     // Actually sends the texture data to the GPU. This is called from
127     // toTexture with the data filled in depending on the texture config.
128     void sendTextureData(GrTexture *texture, const GrSurfaceDesc& desc,
129                          const void *data, int rowbytes);
130
131     // Compresses the bitmap stored in fBM and sends the compressed data
132     // to the GPU to be stored in 'texture' using sendTextureData.
133     void compressTextureData(GrTexture *texture, const GrSurfaceDesc& desc);
134
135     typedef SkNoncopyable INHERITED;
136 };
137
138 #endif // GrSWMaskHelper_DEFINED