blob: b23ee2c7c0b9e6bb70b8e8af05839fd353c0e392 [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)
krajcevskib8ccc2f2014-08-07 08:15:14 -070045 : fContext(context)
46 , fCompressionMode(kNone_CompressionMode) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000047 }
48
robertphillips@google.com366f1c62012-06-29 21:38:47 +000049 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000050 // may be accumulated in the helper during creation, "resultBounds"
51 // allows the caller to specify the region of interest - to limit the
krajcevski71614ac2014-08-13 10:36:18 -070052 // amount of work. allowCompression should be set to false if you plan on using
53 // your own texture to draw into, and not a scratch texture via getTexture().
54 bool init(const SkIRect& resultBounds, const SkMatrix* matrix, bool allowCompression = true);
robertphillips@google.com58b20212012-06-27 20:44:52 +000055
robertphillips@google.com366f1c62012-06-29 21:38:47 +000056 // Draw a single rect into the accumulation bitmap using the specified op
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000057 void draw(const SkRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000058 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000059
robertphillips@google.com366f1c62012-06-29 21:38:47 +000060 // Draw a single path into the accumuation bitmap using the specified op
sugoi@google.com5f74cf82012-12-17 21:16:45 +000061 void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
sugoi@google.com12b4e272012-12-06 20:13:11 +000062 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000063
robertphillips@google.com366f1c62012-06-29 21:38:47 +000064 // Helper function to get a scratch texture suitable for capturing the
65 // result (i.e., right size & format)
66 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000067
robertphillips@google.com366f1c62012-06-29 21:38:47 +000068 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000069 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000070
jvanverthfa38a302014-10-06 05:59:05 -070071 // Convert mask generation results to a signed distance field
72 void toSDF(unsigned char* sdf);
73
robertphillips@google.com366f1c62012-06-29 21:38:47 +000074 // Reset the internal bitmap
75 void clear(uint8_t alpha) {
76 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000077 }
78
robertphillips@google.com366f1c62012-06-29 21:38:47 +000079 // Canonical usage utility that draws a single path and uploads it
jvanverthfa38a302014-10-06 05:59:05 -070080 // to the GPU. The result is returned.
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000081 static GrTexture* DrawPathMaskToTexture(GrContext* context,
82 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000083 const SkStrokeRec& stroke,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000084 const SkIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000085 bool antiAlias,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000086 SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000087
88 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000089 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000090 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000092 // geometry and the current drawState. The current drawState is altered to
93 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000094 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000095 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000096 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000097 // output of DrawPathMaskToTexture.
98 static void DrawToTargetWithPathMask(GrTexture* texture,
99 GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000100 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000101
robertphillips@google.com58b20212012-06-27 20:44:52 +0000102private:
103 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000104 SkMatrix fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000105 SkBitmap fBM;
106 SkDraw fDraw;
107 SkRasterClip fRasterClip;
108
krajcevskib8ccc2f2014-08-07 08:15:14 -0700109 // This enum says whether or not we should compress the mask:
110 // kNone_CompressionMode: compression is not supported on this device.
111 // kCompress_CompressionMode: compress the bitmap before it gets sent to the gpu
112 // kBlitter_CompressionMode: write to the bitmap using a special compressed blitter.
113 enum CompressionMode {
114 kNone_CompressionMode,
115 kCompress_CompressionMode,
116 kBlitter_CompressionMode,
117 } fCompressionMode;
118
119 // This is the buffer into which we store our compressed data. This buffer is
120 // only allocated (non-null) if fCompressionMode is kBlitter_CompressionMode
121 SkAutoMalloc fCompressedBuffer;
krajcevskib3abe902014-07-30 13:08:11 -0700122
123 // This is the desired format within which to compress the
krajcevskib8ccc2f2014-08-07 08:15:14 -0700124 // texture. This value is only valid if fCompressionMode is not kNone_CompressionMode.
krajcevski25a67bc2014-07-29 11:44:26 -0700125 SkTextureCompressor::Format fCompressedFormat;
krajcevski25a67bc2014-07-29 11:44:26 -0700126
krajcevskifb4f5cb2014-06-12 09:20:38 -0700127 // Actually sends the texture data to the GPU. This is called from
128 // toTexture with the data filled in depending on the texture config.
129 void sendTextureData(GrTexture *texture, const GrTextureDesc& desc,
130 const void *data, int rowbytes);
131
krajcevskib577c552014-07-16 13:26:43 -0700132 // Compresses the bitmap stored in fBM and sends the compressed data
133 // to the GPU to be stored in 'texture' using sendTextureData.
134 void compressTextureData(GrTexture *texture, const GrTextureDesc& desc);
135
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000136 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000137};
138
robertphillips@google.comfe659432012-06-28 01:01:53 +0000139#endif // GrSWMaskHelper_DEFINED