blob: 54ae089dcd74aa9ad1f722623df6fbdfd2e34be4 [file] [log] [blame]
robertphillips@google.com58b20212012-06-27 20:44:52 +00001/*
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
robertphillips@google.com366f1c62012-06-29 21:38:47 +000026 * 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.
robertphillips@google.com58b20212012-06-27 20:44:52 +000037 */
38class GrSWMaskHelper : public GrNoncopyable {
39public:
40 GrSWMaskHelper(GrContext* context)
41 : fContext(context) {
42 }
43
robertphillips@google.com366f1c62012-06-29 21:38:47 +000044 // 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);
robertphillips@google.com58b20212012-06-27 20:44:52 +000049
robertphillips@google.com366f1c62012-06-29 21:38:47 +000050 // 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);
robertphillips@google.com58b20212012-06-27 20:44:52 +000053
robertphillips@google.com366f1c62012-06-29 21:38:47 +000054 // 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);
robertphillips@google.com58b20212012-06-27 20:44:52 +000057
robertphillips@google.com366f1c62012-06-29 21:38:47 +000058 // Helper function to get a scratch texture suitable for capturing the
59 // result (i.e., right size & format)
60 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000061
robertphillips@google.com366f1c62012-06-29 21:38:47 +000062 // 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);
robertphillips@google.com58b20212012-06-27 20:44:52 +000065
robertphillips@google.com366f1c62012-06-29 21:38:47 +000066 // Reset the internal bitmap
67 void clear(uint8_t alpha) {
68 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000069 }
70
robertphillips@google.com366f1c62012-06-29 21:38:47 +000071 // 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
robertphillips@google.com58b20212012-06-27 20:44:52 +000081protected:
82private:
83 GrContext* fContext;
84 GrMatrix fMatrix;
85 SkBitmap fBM;
86 SkDraw fDraw;
87 SkRasterClip fRasterClip;
88
89 typedef GrNoncopyable INHERITED;
90};
91
robertphillips@google.comfe659432012-06-28 01:01:53 +000092#endif // GrSWMaskHelper_DEFINED