blob: b90205b1a1e93afbb882c51d5c698d93255bb309 [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"
egdaniel8dd688b2015-01-22 10:16:09 -080012#include "GrPipelineBuilder.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
joshualitt44701df2015-02-23 14:44:57 -080021class GrClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000022class 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 // 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);
69
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,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000079 const SkStrokeRec& stroke,
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,
joshualitt2e3b3e32014-12-09 13:31:14 -080097 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -080098 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000099 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000100
robertphillips@google.com58b20212012-06-27 20:44:52 +0000101private:
bsalomone3059732014-10-14 11:47:22 -0700102 // Helper function to get a scratch texture suitable for capturing the
103 // result (i.e., right size & format)
104 GrTexture* createTexture();
105
robertphillips@google.com58b20212012-06-27 20:44:52 +0000106 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000107 SkMatrix fMatrix;
reed41e010c2015-06-09 12:16:53 -0700108 SkAutoPixmapStorage fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000109 SkDraw fDraw;
110 SkRasterClip fRasterClip;
111
krajcevskib8ccc2f2014-08-07 08:15:14 -0700112 // This enum says whether or not we should compress the mask:
113 // kNone_CompressionMode: compression is not supported on this device.
114 // kCompress_CompressionMode: compress the bitmap before it gets sent to the gpu
115 // kBlitter_CompressionMode: write to the bitmap using a special compressed blitter.
116 enum CompressionMode {
117 kNone_CompressionMode,
118 kCompress_CompressionMode,
119 kBlitter_CompressionMode,
120 } fCompressionMode;
121
122 // This is the buffer into which we store our compressed data. This buffer is
123 // only allocated (non-null) if fCompressionMode is kBlitter_CompressionMode
124 SkAutoMalloc fCompressedBuffer;
krajcevskib3abe902014-07-30 13:08:11 -0700125
126 // This is the desired format within which to compress the
krajcevskib8ccc2f2014-08-07 08:15:14 -0700127 // texture. This value is only valid if fCompressionMode is not kNone_CompressionMode.
krajcevski25a67bc2014-07-29 11:44:26 -0700128 SkTextureCompressor::Format fCompressedFormat;
krajcevski25a67bc2014-07-29 11:44:26 -0700129
krajcevskifb4f5cb2014-06-12 09:20:38 -0700130 // Actually sends the texture data to the GPU. This is called from
131 // toTexture with the data filled in depending on the texture config.
bsalomonf2703d82014-10-28 14:33:06 -0700132 void sendTextureData(GrTexture *texture, const GrSurfaceDesc& desc,
bsalomonef3fcd82014-12-12 08:51:38 -0800133 const void *data, size_t rowbytes);
krajcevskifb4f5cb2014-06-12 09:20:38 -0700134
krajcevskib577c552014-07-16 13:26:43 -0700135 // Compresses the bitmap stored in fBM and sends the compressed data
136 // to the GPU to be stored in 'texture' using sendTextureData.
bsalomonf2703d82014-10-28 14:33:06 -0700137 void compressTextureData(GrTexture *texture, const GrSurfaceDesc& desc);
krajcevskib577c552014-07-16 13:26:43 -0700138
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000139 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000140};
141
robertphillips@google.comfe659432012-06-28 01:01:53 +0000142#endif // GrSWMaskHelper_DEFINED