blob: c7807691d41f9b6924a44bacf1adb45b55cc3d28 [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"
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;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000024class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000025
26/**
27 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000028 * path. It is intended to be used as:
29 *
30 * GrSWMaskHelper helper(context);
31 * helper.init(...);
32 *
33 * draw one or more paths/rects specifying the required boolean ops
34 *
35 * toTexture(); // to get it from the internal bitmap to the GPU
36 *
37 * The result of this process will be the final mask (on the GPU) in the
38 * upper left hand corner of the texture.
robertphillips@google.com58b20212012-06-27 20:44:52 +000039 */
40class GrSWMaskHelper : public GrNoncopyable {
41public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000042 GrSWMaskHelper(GrContext* context)
robertphillips@google.com58b20212012-06-27 20:44:52 +000043 : fContext(context) {
44 }
45
robertphillips@google.com366f1c62012-06-29 21:38:47 +000046 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000047 // may be accumulated in the helper during creation, "resultBounds"
48 // allows the caller to specify the region of interest - to limit the
robertphillips@google.com366f1c62012-06-29 21:38:47 +000049 // amount of work.
50 bool init(const GrIRect& resultBounds, const GrMatrix* matrix);
robertphillips@google.com58b20212012-06-27 20:44:52 +000051
robertphillips@google.com366f1c62012-06-29 21:38:47 +000052 // Draw a single rect into the accumulation bitmap using the specified op
rmistry@google.comd6176b02012-08-23 18:14:13 +000053 void draw(const GrRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000054 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000055
robertphillips@google.com366f1c62012-06-29 21:38:47 +000056 // Draw a single path into the accumuation bitmap using the specified op
rmistry@google.comd6176b02012-08-23 18:14:13 +000057 void draw(const SkPath& path, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000058 GrPathFill fill, bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000059
robertphillips@google.com366f1c62012-06-29 21:38:47 +000060 // Helper function to get a scratch texture suitable for capturing the
61 // result (i.e., right size & format)
62 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000063
robertphillips@google.com366f1c62012-06-29 21:38:47 +000064 // Move the mask generation results from the internal bitmap to the gpu.
65 // The space outside of the mask is cleared using "alpha"
66 void toTexture(GrTexture* texture, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000067
robertphillips@google.com366f1c62012-06-29 21:38:47 +000068 // Reset the internal bitmap
69 void clear(uint8_t alpha) {
70 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000071 }
72
robertphillips@google.com366f1c62012-06-29 21:38:47 +000073 // Canonical usage utility that draws a single path and uploads it
74 // to the GPU. The result is returned in "result".
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000075 static GrTexture* DrawPathMaskToTexture(GrContext* context,
76 const SkPath& path,
77 const GrIRect& resultBounds,
78 GrPathFill fill,
79 bool antiAlias,
80 GrMatrix* matrix);
81
82 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000083 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000084 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000085 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000086 // geometry and the current drawState. The current drawState is altered to
87 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000088 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000089 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000090 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000091 // output of DrawPathMaskToTexture.
92 static void DrawToTargetWithPathMask(GrTexture* texture,
93 GrDrawTarget* target,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000094 const GrIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000095
robertphillips@google.com58b20212012-06-27 20:44:52 +000096protected:
97private:
98 GrContext* fContext;
99 GrMatrix fMatrix;
100 SkBitmap fBM;
101 SkDraw fDraw;
102 SkRasterClip fRasterClip;
103
104 typedef GrNoncopyable INHERITED;
105};
106
robertphillips@google.comfe659432012-06-28 01:01:53 +0000107#endif // GrSWMaskHelper_DEFINED