blob: 4c06786280a4b11087ef669fbd56072dc27d384d [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"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "GrDrawState.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000013#include "SkBitmap.h"
14#include "SkDraw.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000015#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000016#include "SkRasterClip.h"
17#include "SkRegion.h"
krajcevski25a67bc2014-07-29 11:44:26 -070018#include "SkTextureCompressor.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020
21class GrAutoScratchTexture;
22class GrContext;
23class GrTexture;
24class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000025class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000026class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000027
28/**
29 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000030 * 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.
robertphillips@google.com58b20212012-06-27 20:44:52 +000041 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000042class GrSWMaskHelper : SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000043public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 GrSWMaskHelper(GrContext* context)
krajcevskib3abe902014-07-30 13:08:11 -070045 : fContext(context), fCompressMask(false) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000046 }
47
robertphillips@google.com366f1c62012-06-29 21:38:47 +000048 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 // may be accumulated in the helper during creation, "resultBounds"
50 // allows the caller to specify the region of interest - to limit the
robertphillips@google.com366f1c62012-06-29 21:38:47 +000051 // amount of work.
reed@google.com84e922b2013-11-04 20:57:36 +000052 bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
robertphillips@google.com58b20212012-06-27 20:44:52 +000053
robertphillips@google.com366f1c62012-06-29 21:38:47 +000054 // Draw a single rect into the accumulation bitmap using the specified op
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000055 void draw(const SkRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000056 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000057
robertphillips@google.com366f1c62012-06-29 21:38:47 +000058 // Draw a single path into the accumuation bitmap using the specified op
sugoi@google.com5f74cf82012-12-17 21:16:45 +000059 void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
sugoi@google.com12b4e272012-12-06 20:13:11 +000060 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000061
robertphillips@google.com366f1c62012-06-29 21:38:47 +000062 // Helper function to get a scratch texture suitable for capturing the
63 // result (i.e., right size & format)
64 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000065
robertphillips@google.com366f1c62012-06-29 21:38:47 +000066 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000067 void toTexture(GrTexture* texture);
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,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000079 const SkIRect& 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,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000095 const SkIRect& 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
krajcevskib3abe902014-07-30 13:08:11 -0700105 // This flag says whether or not we should compress the mask. If
106 // it is true, then fCompressedFormat is always valid.
107 bool fCompressMask;
108
109 // This is the desired format within which to compress the
110 // texture. This value is only valid if fCompressMask is true.
krajcevski25a67bc2014-07-29 11:44:26 -0700111 SkTextureCompressor::Format fCompressedFormat;
krajcevski25a67bc2014-07-29 11:44:26 -0700112
krajcevskifb4f5cb2014-06-12 09:20:38 -0700113 // Actually sends the texture data to the GPU. This is called from
114 // toTexture with the data filled in depending on the texture config.
115 void sendTextureData(GrTexture *texture, const GrTextureDesc& desc,
116 const void *data, int rowbytes);
117
krajcevskib577c552014-07-16 13:26:43 -0700118 // Compresses the bitmap stored in fBM and sends the compressed data
119 // to the GPU to be stored in 'texture' using sendTextureData.
120 void compressTextureData(GrTexture *texture, const GrTextureDesc& desc);
121
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000122 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000123};
124
robertphillips@google.comfe659432012-06-28 01:01:53 +0000125#endif // GrSWMaskHelper_DEFINED