blob: daf3111d553f929291ef312e92ab4ec57ad8fd2b [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"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000012#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000013#include "GrNoncopyable.h"
14#include "SkBitmap.h"
15#include "SkDraw.h"
16#include "SkRasterClip.h"
17#include "SkRegion.h"
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000018#include "GrDrawState.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019
20class GrAutoScratchTexture;
21class GrContext;
22class GrTexture;
23class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000024class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000025class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000026
27/**
28 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000029 * 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.
robertphillips@google.com58b20212012-06-27 20:44:52 +000040 */
41class GrSWMaskHelper : public GrNoncopyable {
42public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 GrSWMaskHelper(GrContext* context)
robertphillips@google.com58b20212012-06-27 20:44:52 +000044 : fContext(context) {
45 }
46
robertphillips@google.com366f1c62012-06-29 21:38:47 +000047 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000048 // may be accumulated in the helper during creation, "resultBounds"
49 // allows the caller to specify the region of interest - to limit the
robertphillips@google.com366f1c62012-06-29 21:38:47 +000050 // amount of work.
bsalomon@google.comb9086a02012-11-01 18:02:54 +000051 bool init(const GrIRect& resultBounds, const SkMatrix* matrix);
robertphillips@google.com58b20212012-06-27 20:44:52 +000052
robertphillips@google.com366f1c62012-06-29 21:38:47 +000053 // Draw a single rect into the accumulation bitmap using the specified op
rmistry@google.comd6176b02012-08-23 18:14:13 +000054 void draw(const GrRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000055 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000056
robertphillips@google.com366f1c62012-06-29 21:38:47 +000057 // Draw a single path into the accumuation bitmap using the specified op
sugoi@google.com5f74cf82012-12-17 21:16:45 +000058 void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
sugoi@google.com12b4e272012-12-06 20:13:11 +000059 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000060
robertphillips@google.com366f1c62012-06-29 21:38:47 +000061 // Helper function to get a scratch texture suitable for capturing the
62 // result (i.e., right size & format)
63 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000064
robertphillips@google.com366f1c62012-06-29 21:38:47 +000065 // Move the mask generation results from the internal bitmap to the gpu.
66 // The space outside of the mask is cleared using "alpha"
67 void toTexture(GrTexture* texture, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000068
robertphillips@google.com366f1c62012-06-29 21:38:47 +000069 // Reset the internal bitmap
70 void clear(uint8_t alpha) {
71 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000072 }
73
robertphillips@google.com366f1c62012-06-29 21:38:47 +000074 // Canonical usage utility that draws a single path and uploads it
75 // to the GPU. The result is returned in "result".
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000076 static GrTexture* DrawPathMaskToTexture(GrContext* context,
77 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000078 const SkStrokeRec& stroke,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000079 const GrIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000080 bool antiAlias,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000081 SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000082
83 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000084 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000085 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000087 // geometry and the current drawState. The current drawState is altered to
88 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000089 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000090 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000092 // output of DrawPathMaskToTexture.
93 static void DrawToTargetWithPathMask(GrTexture* texture,
94 GrDrawTarget* target,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000095 const GrIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000096
robertphillips@google.com58b20212012-06-27 20:44:52 +000097protected:
98private:
99 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000100 SkMatrix fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000101 SkBitmap fBM;
102 SkDraw fDraw;
103 SkRasterClip fRasterClip;
104
105 typedef GrNoncopyable INHERITED;
106};
107
robertphillips@google.comfe659432012-06-28 01:01:53 +0000108#endif // GrSWMaskHelper_DEFINED