blob: 63d7d6417811394da493b5214e171175507b9183 [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
robertphillipsc5035e72016-03-17 06:58:39 -070011#include "SkAutoPixmapStorage.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000012#include "GrColor.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipelineBuilder.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000014#include "SkBitmap.h"
15#include "SkDraw.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000016#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "SkRasterClip.h"
18#include "SkRegion.h"
krajcevski25a67bc2014-07-29 11:44:26 -070019#include "SkTextureCompressor.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000020#include "SkTypes.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000021
joshualitt44701df2015-02-23 14:44:57 -080022class GrClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000023class GrContext;
24class GrTexture;
25class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000026class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000027class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000028
29/**
30 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000031 * path. It is intended to be used as:
32 *
33 * GrSWMaskHelper helper(context);
34 * helper.init(...);
35 *
36 * draw one or more paths/rects specifying the required boolean ops
37 *
38 * toTexture(); // to get it from the internal bitmap to the GPU
39 *
40 * The result of this process will be the final mask (on the GPU) in the
41 * upper left hand corner of the texture.
robertphillips@google.com58b20212012-06-27 20:44:52 +000042 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000043class GrSWMaskHelper : SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000044public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000045 GrSWMaskHelper(GrContext* context)
krajcevskib8ccc2f2014-08-07 08:15:14 -070046 : fContext(context)
47 , fCompressionMode(kNone_CompressionMode) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000048 }
49
robertphillips@google.com366f1c62012-06-29 21:38:47 +000050 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000051 // may be accumulated in the helper during creation, "resultBounds"
52 // allows the caller to specify the region of interest - to limit the
krajcevski71614ac2014-08-13 10:36:18 -070053 // amount of work. allowCompression should be set to false if you plan on using
54 // your own texture to draw into, and not a scratch texture via getTexture().
55 bool init(const SkIRect& resultBounds, const SkMatrix* matrix, bool allowCompression = true);
robertphillips@google.com58b20212012-06-27 20:44:52 +000056
robertphillips@google.com366f1c62012-06-29 21:38:47 +000057 // Draw a single rect into the accumulation bitmap using the specified op
bsalomon6663acf2016-05-10 09:14:17 -070058 void draw(const SkRect& rect, SkRegion::Op op, 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
bsalomon6663acf2016-05-10 09:14:17 -070061 void draw(const SkPath& path, const GrStyle& style, 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 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000065 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000066
jvanverthfa38a302014-10-06 05:59:05 -070067 // Convert mask generation results to a signed distance field
68 void toSDF(unsigned char* sdf);
halcanary9d524f22016-03-29 09:03:52 -070069
robertphillips@google.com366f1c62012-06-29 21:38:47 +000070 // Reset the internal bitmap
71 void clear(uint8_t alpha) {
reed41e010c2015-06-09 12:16:53 -070072 fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF));
robertphillips@google.com58b20212012-06-27 20:44:52 +000073 }
74
robertphillips@google.com366f1c62012-06-29 21:38:47 +000075 // Canonical usage utility that draws a single path and uploads it
jvanverthfa38a302014-10-06 05:59:05 -070076 // to the GPU. The result is returned.
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000077 static GrTexture* DrawPathMaskToTexture(GrContext* context,
78 const SkPath& path,
bsalomon6663acf2016-05-10 09:14:17 -070079 const GrStyle& style,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000080 const SkIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000081 bool antiAlias,
joshualitt8059eb92014-12-29 15:10:07 -080082 const SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000083
84 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000085 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000086 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000088 // geometry and the current drawState. The current drawState is altered to
89 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000090 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000091 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000092 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000093 // output of DrawPathMaskToTexture.
94 static void DrawToTargetWithPathMask(GrTexture* texture,
95 GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080096 GrPipelineBuilder* pipelineBuilder,
cdalton862cff32016-05-12 15:09:48 -070097 const GrClip&,
joshualitt2e3b3e32014-12-09 13:31:14 -080098 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -080099 const SkMatrix& viewMatrix,
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:
bsalomone3059732014-10-14 11:47:22 -0700103 // Helper function to get a scratch texture suitable for capturing the
104 // result (i.e., right size & format)
105 GrTexture* createTexture();
106
robertphillips@google.com58b20212012-06-27 20:44:52 +0000107 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000108 SkMatrix fMatrix;
reed41e010c2015-06-09 12:16:53 -0700109 SkAutoPixmapStorage fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000110 SkDraw fDraw;
111 SkRasterClip fRasterClip;
112
krajcevskib8ccc2f2014-08-07 08:15:14 -0700113 // This enum says whether or not we should compress the mask:
114 // kNone_CompressionMode: compression is not supported on this device.
115 // kCompress_CompressionMode: compress the bitmap before it gets sent to the gpu
116 // kBlitter_CompressionMode: write to the bitmap using a special compressed blitter.
117 enum CompressionMode {
118 kNone_CompressionMode,
119 kCompress_CompressionMode,
120 kBlitter_CompressionMode,
121 } fCompressionMode;
122
123 // This is the buffer into which we store our compressed data. This buffer is
124 // only allocated (non-null) if fCompressionMode is kBlitter_CompressionMode
125 SkAutoMalloc fCompressedBuffer;
krajcevskib3abe902014-07-30 13:08:11 -0700126
127 // This is the desired format within which to compress the
krajcevskib8ccc2f2014-08-07 08:15:14 -0700128 // texture. This value is only valid if fCompressionMode is not kNone_CompressionMode.
krajcevski25a67bc2014-07-29 11:44:26 -0700129 SkTextureCompressor::Format fCompressedFormat;
krajcevski25a67bc2014-07-29 11:44:26 -0700130
krajcevskifb4f5cb2014-06-12 09:20:38 -0700131 // Actually sends the texture data to the GPU. This is called from
132 // toTexture with the data filled in depending on the texture config.
bsalomonf2703d82014-10-28 14:33:06 -0700133 void sendTextureData(GrTexture *texture, const GrSurfaceDesc& desc,
bsalomonef3fcd82014-12-12 08:51:38 -0800134 const void *data, size_t rowbytes);
krajcevskifb4f5cb2014-06-12 09:20:38 -0700135
krajcevskib577c552014-07-16 13:26:43 -0700136 // Compresses the bitmap stored in fBM and sends the compressed data
137 // to the GPU to be stored in 'texture' using sendTextureData.
bsalomonf2703d82014-10-28 14:33:06 -0700138 void compressTextureData(GrTexture *texture, const GrSurfaceDesc& desc);
krajcevskib577c552014-07-16 13:26:43 -0700139
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000140 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000141};
142
robertphillips@google.comfe659432012-06-28 01:01:53 +0000143#endif // GrSWMaskHelper_DEFINED