Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrShadowRRectOp.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkRRectPriv.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrDrawOpTest.h" |
| 14 | #include "src/gpu/GrMemoryPool.h" |
| 15 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrProgramInfo.h" |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/effects/GrShadowGeoProc.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 21 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 24 | // Circle Data |
| 25 | // |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 26 | // We have two possible cases for geometry for a circle: |
| 27 | |
| 28 | // In the case of a normal fill, we draw geometry for the circle as an octagon. |
| 29 | static const uint16_t gFillCircleIndices[] = { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 30 | // enter the octagon |
| 31 | // clang-format off |
| 32 | 0, 1, 8, 1, 2, 8, |
| 33 | 2, 3, 8, 3, 4, 8, |
| 34 | 4, 5, 8, 5, 6, 8, |
| 35 | 6, 7, 8, 7, 0, 8, |
| 36 | // clang-format on |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // For stroked circles, we use two nested octagons. |
| 40 | static const uint16_t gStrokeCircleIndices[] = { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 41 | // enter the octagon |
| 42 | // clang-format off |
| 43 | 0, 1, 9, 0, 9, 8, |
| 44 | 1, 2, 10, 1, 10, 9, |
| 45 | 2, 3, 11, 2, 11, 10, |
| 46 | 3, 4, 12, 3, 12, 11, |
| 47 | 4, 5, 13, 4, 13, 12, |
| 48 | 5, 6, 14, 5, 14, 13, |
| 49 | 6, 7, 15, 6, 15, 14, |
| 50 | 7, 0, 8, 7, 8, 15, |
| 51 | // clang-format on |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static const int kIndicesPerFillCircle = SK_ARRAY_COUNT(gFillCircleIndices); |
| 55 | static const int kIndicesPerStrokeCircle = SK_ARRAY_COUNT(gStrokeCircleIndices); |
| 56 | static const int kVertsPerStrokeCircle = 16; |
| 57 | static const int kVertsPerFillCircle = 9; |
| 58 | |
| 59 | static int circle_type_to_vert_count(bool stroked) { |
| 60 | return stroked ? kVertsPerStrokeCircle : kVertsPerFillCircle; |
| 61 | } |
| 62 | |
| 63 | static int circle_type_to_index_count(bool stroked) { |
| 64 | return stroked ? kIndicesPerStrokeCircle : kIndicesPerFillCircle; |
| 65 | } |
| 66 | |
| 67 | static const uint16_t* circle_type_to_indices(bool stroked) { |
| 68 | return stroked ? gStrokeCircleIndices : gFillCircleIndices; |
| 69 | } |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 72 | // RoundRect Data |
| 73 | // |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 74 | // The geometry for a shadow roundrect is similar to a 9-patch: |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 75 | // ____________ |
| 76 | // |_|________|_| |
| 77 | // | | | | |
| 78 | // | | | | |
| 79 | // | | | | |
| 80 | // |_|________|_| |
| 81 | // |_|________|_| |
| 82 | // |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 83 | // However, each corner is rendered as a fan rather than a simple quad, as below. (The diagram |
| 84 | // shows the upper part of the upper left corner. The bottom triangle would similarly be split |
| 85 | // into two triangles.) |
| 86 | // ________ |
| 87 | // |\ \ | |
| 88 | // | \ \ | |
| 89 | // | \\ | |
| 90 | // | \| |
| 91 | // -------- |
| 92 | // |
| 93 | // The center of the fan handles the curve of the corner. For roundrects where the stroke width |
| 94 | // is greater than the corner radius, the outer triangles blend from the curve to the straight |
| 95 | // sides. Otherwise these triangles will be degenerate. |
| 96 | // |
| 97 | // In the case where the stroke width is greater than the corner radius and the |
| 98 | // blur radius (overstroke), we add additional geometry to mark out the rectangle in the center. |
| 99 | // This rectangle extends the coverage values of the center edges of the 9-patch. |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 100 | // ____________ |
| 101 | // |_|________|_| |
| 102 | // | |\ ____ /| | |
| 103 | // | | | | | | |
| 104 | // | | |____| | | |
| 105 | // |_|/______\|_| |
| 106 | // |_|________|_| |
| 107 | // |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 108 | // For filled rrects we reuse the stroke geometry but add an additional quad to the center. |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 109 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 110 | static const uint16_t gRRectIndices[] = { |
| 111 | // clang-format off |
| 112 | // overstroke quads |
| 113 | // we place this at the beginning so that we can skip these indices when rendering as filled |
| 114 | 0, 6, 25, 0, 25, 24, |
| 115 | 6, 18, 27, 6, 27, 25, |
| 116 | 18, 12, 26, 18, 26, 27, |
| 117 | 12, 0, 24, 12, 24, 26, |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 118 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 119 | // corners |
| 120 | 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, |
| 121 | 6, 11, 10, 6, 10, 9, 6, 9, 8, 6, 8, 7, |
| 122 | 12, 17, 16, 12, 16, 15, 12, 15, 14, 12, 14, 13, |
| 123 | 18, 19, 20, 18, 20, 21, 18, 21, 22, 18, 22, 23, |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 124 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 125 | // edges |
| 126 | 0, 5, 11, 0, 11, 6, |
| 127 | 6, 7, 19, 6, 19, 18, |
| 128 | 18, 23, 17, 18, 17, 12, |
| 129 | 12, 13, 1, 12, 1, 0, |
| 130 | |
| 131 | // fill quad |
| 132 | // we place this at the end so that we can skip these indices when rendering as stroked |
| 133 | 0, 6, 18, 0, 18, 12, |
| 134 | // clang-format on |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 135 | }; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 136 | |
| 137 | // overstroke count |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 138 | static const int kIndicesPerOverstrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 139 | // simple stroke count skips overstroke indices |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 140 | static const int kIndicesPerStrokeRRect = kIndicesPerOverstrokeRRect - 6*4; |
| 141 | // fill count adds final quad to stroke count |
| 142 | static const int kIndicesPerFillRRect = kIndicesPerStrokeRRect + 6; |
| 143 | static const int kVertsPerStrokeRRect = 24; |
| 144 | static const int kVertsPerOverstrokeRRect = 28; |
| 145 | static const int kVertsPerFillRRect = 24; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 146 | |
| 147 | enum RRectType { |
| 148 | kFill_RRectType, |
| 149 | kStroke_RRectType, |
| 150 | kOverstroke_RRectType, |
| 151 | }; |
| 152 | |
| 153 | static int rrect_type_to_vert_count(RRectType type) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 154 | switch (type) { |
| 155 | case kFill_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 156 | return kVertsPerFillRRect; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 157 | case kStroke_RRectType: |
| 158 | return kVertsPerStrokeRRect; |
| 159 | case kOverstroke_RRectType: |
| 160 | return kVertsPerOverstrokeRRect; |
| 161 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 162 | SK_ABORT("Invalid type"); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int rrect_type_to_index_count(RRectType type) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 166 | switch (type) { |
| 167 | case kFill_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 168 | return kIndicesPerFillRRect; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 169 | case kStroke_RRectType: |
| 170 | return kIndicesPerStrokeRRect; |
| 171 | case kOverstroke_RRectType: |
| 172 | return kIndicesPerOverstrokeRRect; |
| 173 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 174 | SK_ABORT("Invalid type"); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static const uint16_t* rrect_type_to_indices(RRectType type) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 178 | switch (type) { |
| 179 | case kFill_RRectType: |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 180 | case kStroke_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 181 | return gRRectIndices + 6*4; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 182 | case kOverstroke_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 183 | return gRRectIndices; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 184 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 185 | SK_ABORT("Invalid type"); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 188 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 189 | namespace { |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 190 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 191 | class ShadowCircularRRectOp final : public GrMeshDrawOp { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 192 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 193 | DEFINE_OP_CLASS_ID |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 194 | |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 195 | // An insetWidth > 1/2 rect width or height indicates a simple fill. |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 196 | ShadowCircularRRectOp(GrColor color, const SkRect& devRect, |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 197 | float devRadius, bool isCircle, float blurRadius, float insetWidth, |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 198 | GrSurfaceProxyView falloffView) |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 199 | : INHERITED(ClassID()) |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 200 | , fFalloffView(std::move(falloffView)) { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 201 | SkRect bounds = devRect; |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 202 | SkASSERT(insetWidth > 0); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 203 | SkScalar innerRadius = 0.0f; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 204 | SkScalar outerRadius = devRadius; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 205 | SkScalar umbraInset; |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 206 | |
| 207 | RRectType type = kFill_RRectType; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 208 | if (isCircle) { |
| 209 | umbraInset = 0; |
| 210 | } else { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 211 | umbraInset = std::max(outerRadius, blurRadius); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 214 | // If stroke is greater than width or height, this is still a fill, |
| 215 | // otherwise we compute stroke params. |
| 216 | if (isCircle) { |
| 217 | innerRadius = devRadius - insetWidth; |
| 218 | type = innerRadius > 0 ? kStroke_RRectType : kFill_RRectType; |
| 219 | } else { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 220 | if (insetWidth <= 0.5f*std::min(devRect.width(), devRect.height())) { |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 221 | // We don't worry about a real inner radius, we just need to know if we |
| 222 | // need to create overstroke vertices. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 223 | innerRadius = std::max(insetWidth - umbraInset, 0.0f); |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 224 | type = innerRadius > 0 ? kOverstroke_RRectType : kStroke_RRectType; |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 225 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 228 | this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 229 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 230 | fGeoData.emplace_back(Geometry{color, outerRadius, umbraInset, innerRadius, |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 231 | blurRadius, bounds, type, isCircle}); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 232 | if (isCircle) { |
| 233 | fVertCount = circle_type_to_vert_count(kStroke_RRectType == type); |
| 234 | fIndexCount = circle_type_to_index_count(kStroke_RRectType == type); |
| 235 | } else { |
| 236 | fVertCount = rrect_type_to_vert_count(type); |
| 237 | fIndexCount = rrect_type_to_index_count(type); |
| 238 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 241 | const char* name() const override { return "ShadowCircularRRectOp"; } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 242 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 243 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 244 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 245 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 246 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 247 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 250 | private: |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 251 | struct Geometry { |
| 252 | GrColor fColor; |
| 253 | SkScalar fOuterRadius; |
| 254 | SkScalar fUmbraInset; |
| 255 | SkScalar fInnerRadius; |
| 256 | SkScalar fBlurRadius; |
| 257 | SkRect fDevBounds; |
| 258 | RRectType fType; |
| 259 | bool fIsCircle; |
| 260 | }; |
| 261 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 262 | struct CircleVertex { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 263 | SkPoint fPos; |
| 264 | GrColor fColor; |
| 265 | SkPoint fOffset; |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 266 | SkScalar fDistanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 267 | }; |
| 268 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 269 | void fillInCircleVerts(const Geometry& args, bool isStroked, CircleVertex** verts) const { |
| 270 | |
| 271 | GrColor color = args.fColor; |
| 272 | SkScalar outerRadius = args.fOuterRadius; |
| 273 | SkScalar innerRadius = args.fInnerRadius; |
| 274 | SkScalar blurRadius = args.fBlurRadius; |
| 275 | SkScalar distanceCorrection = outerRadius / blurRadius; |
| 276 | |
| 277 | const SkRect& bounds = args.fDevBounds; |
| 278 | |
| 279 | // The inner radius in the vertex data must be specified in normalized space. |
| 280 | innerRadius = innerRadius / outerRadius; |
| 281 | |
| 282 | SkPoint center = SkPoint::Make(bounds.centerX(), bounds.centerY()); |
| 283 | SkScalar halfWidth = 0.5f * bounds.width(); |
| 284 | SkScalar octOffset = 0.41421356237f; // sqrt(2) - 1 |
| 285 | |
| 286 | (*verts)->fPos = center + SkPoint::Make(-octOffset * halfWidth, -halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 287 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 288 | (*verts)->fOffset = SkPoint::Make(-octOffset, -1); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 289 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 290 | (*verts)++; |
| 291 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 292 | (*verts)->fPos = center + SkPoint::Make(octOffset * halfWidth, -halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 293 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 294 | (*verts)->fOffset = SkPoint::Make(octOffset, -1); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 295 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 296 | (*verts)++; |
| 297 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 298 | (*verts)->fPos = center + SkPoint::Make(halfWidth, -octOffset * halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 299 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 300 | (*verts)->fOffset = SkPoint::Make(1, -octOffset); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 301 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 302 | (*verts)++; |
| 303 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 304 | (*verts)->fPos = center + SkPoint::Make(halfWidth, octOffset * halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 305 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 306 | (*verts)->fOffset = SkPoint::Make(1, octOffset); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 307 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 308 | (*verts)++; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 309 | |
| 310 | (*verts)->fPos = center + SkPoint::Make(octOffset * halfWidth, halfWidth); |
| 311 | (*verts)->fColor = color; |
| 312 | (*verts)->fOffset = SkPoint::Make(octOffset, 1); |
| 313 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 314 | (*verts)++; |
| 315 | |
| 316 | (*verts)->fPos = center + SkPoint::Make(-octOffset * halfWidth, halfWidth); |
| 317 | (*verts)->fColor = color; |
| 318 | (*verts)->fOffset = SkPoint::Make(-octOffset, 1); |
| 319 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 320 | (*verts)++; |
| 321 | |
| 322 | (*verts)->fPos = center + SkPoint::Make(-halfWidth, octOffset * halfWidth); |
| 323 | (*verts)->fColor = color; |
| 324 | (*verts)->fOffset = SkPoint::Make(-1, octOffset); |
| 325 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 326 | (*verts)++; |
| 327 | |
| 328 | (*verts)->fPos = center + SkPoint::Make(-halfWidth, -octOffset * halfWidth); |
| 329 | (*verts)->fColor = color; |
| 330 | (*verts)->fOffset = SkPoint::Make(-1, -octOffset); |
| 331 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 332 | (*verts)++; |
| 333 | |
| 334 | if (isStroked) { |
| 335 | // compute the inner ring |
| 336 | |
| 337 | // cosine and sine of pi/8 |
| 338 | SkScalar c = 0.923579533f; |
| 339 | SkScalar s = 0.382683432f; |
| 340 | SkScalar r = args.fInnerRadius; |
| 341 | |
| 342 | (*verts)->fPos = center + SkPoint::Make(-s * r, -c * r); |
| 343 | (*verts)->fColor = color; |
| 344 | (*verts)->fOffset = SkPoint::Make(-s * innerRadius, -c * innerRadius); |
| 345 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 346 | (*verts)++; |
| 347 | |
| 348 | (*verts)->fPos = center + SkPoint::Make(s * r, -c * r); |
| 349 | (*verts)->fColor = color; |
| 350 | (*verts)->fOffset = SkPoint::Make(s * innerRadius, -c * innerRadius); |
| 351 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 352 | (*verts)++; |
| 353 | |
| 354 | (*verts)->fPos = center + SkPoint::Make(c * r, -s * r); |
| 355 | (*verts)->fColor = color; |
| 356 | (*verts)->fOffset = SkPoint::Make(c * innerRadius, -s * innerRadius); |
| 357 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 358 | (*verts)++; |
| 359 | |
| 360 | (*verts)->fPos = center + SkPoint::Make(c * r, s * r); |
| 361 | (*verts)->fColor = color; |
| 362 | (*verts)->fOffset = SkPoint::Make(c * innerRadius, s * innerRadius); |
| 363 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 364 | (*verts)++; |
| 365 | |
| 366 | (*verts)->fPos = center + SkPoint::Make(s * r, c * r); |
| 367 | (*verts)->fColor = color; |
| 368 | (*verts)->fOffset = SkPoint::Make(s * innerRadius, c * innerRadius); |
| 369 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 370 | (*verts)++; |
| 371 | |
| 372 | (*verts)->fPos = center + SkPoint::Make(-s * r, c * r); |
| 373 | (*verts)->fColor = color; |
| 374 | (*verts)->fOffset = SkPoint::Make(-s * innerRadius, c * innerRadius); |
| 375 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 376 | (*verts)++; |
| 377 | |
| 378 | (*verts)->fPos = center + SkPoint::Make(-c * r, s * r); |
| 379 | (*verts)->fColor = color; |
| 380 | (*verts)->fOffset = SkPoint::Make(-c * innerRadius, s * innerRadius); |
| 381 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 382 | (*verts)++; |
| 383 | |
| 384 | (*verts)->fPos = center + SkPoint::Make(-c * r, -s * r); |
| 385 | (*verts)->fColor = color; |
| 386 | (*verts)->fOffset = SkPoint::Make(-c * innerRadius, -s * innerRadius); |
| 387 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 388 | (*verts)++; |
| 389 | } else { |
| 390 | // filled |
| 391 | (*verts)->fPos = center; |
| 392 | (*verts)->fColor = color; |
| 393 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 394 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 395 | (*verts)++; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void fillInRRectVerts(const Geometry& args, CircleVertex** verts) const { |
| 400 | GrColor color = args.fColor; |
| 401 | SkScalar outerRadius = args.fOuterRadius; |
| 402 | |
| 403 | const SkRect& bounds = args.fDevBounds; |
| 404 | |
| 405 | SkScalar umbraInset = args.fUmbraInset; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 406 | SkScalar minDim = 0.5f*std::min(bounds.width(), bounds.height()); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 407 | if (umbraInset > minDim) { |
| 408 | umbraInset = minDim; |
| 409 | } |
| 410 | |
| 411 | SkScalar xInner[4] = { bounds.fLeft + umbraInset, bounds.fRight - umbraInset, |
| 412 | bounds.fLeft + umbraInset, bounds.fRight - umbraInset }; |
| 413 | SkScalar xMid[4] = { bounds.fLeft + outerRadius, bounds.fRight - outerRadius, |
| 414 | bounds.fLeft + outerRadius, bounds.fRight - outerRadius }; |
| 415 | SkScalar xOuter[4] = { bounds.fLeft, bounds.fRight, |
| 416 | bounds.fLeft, bounds.fRight }; |
| 417 | SkScalar yInner[4] = { bounds.fTop + umbraInset, bounds.fTop + umbraInset, |
| 418 | bounds.fBottom - umbraInset, bounds.fBottom - umbraInset }; |
| 419 | SkScalar yMid[4] = { bounds.fTop + outerRadius, bounds.fTop + outerRadius, |
| 420 | bounds.fBottom - outerRadius, bounds.fBottom - outerRadius }; |
| 421 | SkScalar yOuter[4] = { bounds.fTop, bounds.fTop, |
| 422 | bounds.fBottom, bounds.fBottom }; |
| 423 | |
| 424 | SkScalar blurRadius = args.fBlurRadius; |
| 425 | |
| 426 | // In the case where we have to inset more for the umbra, our two triangles in the |
| 427 | // corner get skewed to a diamond rather than a square. To correct for that, |
| 428 | // we also skew the vectors we send to the shader that help define the circle. |
| 429 | // By doing so, we end up with a quarter circle in the corner rather than the |
| 430 | // elliptical curve. |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 431 | |
| 432 | // This is a bit magical, but it gives us the correct results at extrema: |
| 433 | // a) umbraInset == outerRadius produces an orthogonal vector |
| 434 | // b) outerRadius == 0 produces a diagonal vector |
| 435 | // And visually the corner looks correct. |
| 436 | SkVector outerVec = SkVector::Make(outerRadius - umbraInset, -outerRadius - umbraInset); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 437 | outerVec.normalize(); |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 438 | // We want the circle edge to fall fractionally along the diagonal at |
| 439 | // (sqrt(2)*(umbraInset - outerRadius) + outerRadius)/sqrt(2)*umbraInset |
| 440 | // |
| 441 | // Setting the components of the diagonal offset to the following value will give us that. |
| 442 | SkScalar diagVal = umbraInset / (SK_ScalarSqrt2*(outerRadius - umbraInset) - outerRadius); |
| 443 | SkVector diagVec = SkVector::Make(diagVal, diagVal); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 444 | SkScalar distanceCorrection = umbraInset / blurRadius; |
| 445 | |
| 446 | // build corner by corner |
| 447 | for (int i = 0; i < 4; ++i) { |
| 448 | // inner point |
| 449 | (*verts)->fPos = SkPoint::Make(xInner[i], yInner[i]); |
| 450 | (*verts)->fColor = color; |
| 451 | (*verts)->fOffset = SkVector::Make(0, 0); |
| 452 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 453 | (*verts)++; |
| 454 | |
| 455 | // outer points |
| 456 | (*verts)->fPos = SkPoint::Make(xOuter[i], yInner[i]); |
| 457 | (*verts)->fColor = color; |
| 458 | (*verts)->fOffset = SkVector::Make(0, -1); |
| 459 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 460 | (*verts)++; |
| 461 | |
| 462 | (*verts)->fPos = SkPoint::Make(xOuter[i], yMid[i]); |
| 463 | (*verts)->fColor = color; |
| 464 | (*verts)->fOffset = outerVec; |
| 465 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 466 | (*verts)++; |
| 467 | |
| 468 | (*verts)->fPos = SkPoint::Make(xOuter[i], yOuter[i]); |
| 469 | (*verts)->fColor = color; |
| 470 | (*verts)->fOffset = diagVec; |
| 471 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 472 | (*verts)++; |
| 473 | |
| 474 | (*verts)->fPos = SkPoint::Make(xMid[i], yOuter[i]); |
| 475 | (*verts)->fColor = color; |
| 476 | (*verts)->fOffset = outerVec; |
| 477 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 478 | (*verts)++; |
| 479 | |
| 480 | (*verts)->fPos = SkPoint::Make(xInner[i], yOuter[i]); |
| 481 | (*verts)->fColor = color; |
| 482 | (*verts)->fOffset = SkVector::Make(0, -1); |
| 483 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 484 | (*verts)++; |
| 485 | } |
| 486 | |
| 487 | // Add the additional vertices for overstroked rrects. |
| 488 | // Effectively this is an additional stroked rrect, with its |
| 489 | // parameters equal to those in the center of the 9-patch. This will |
| 490 | // give constant values across this inner ring. |
| 491 | if (kOverstroke_RRectType == args.fType) { |
| 492 | SkASSERT(args.fInnerRadius > 0.0f); |
| 493 | |
| 494 | SkScalar inset = umbraInset + args.fInnerRadius; |
| 495 | |
| 496 | // TL |
| 497 | (*verts)->fPos = SkPoint::Make(bounds.fLeft + inset, bounds.fTop + inset); |
| 498 | (*verts)->fColor = color; |
| 499 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 500 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 501 | (*verts)++; |
| 502 | |
| 503 | // TR |
| 504 | (*verts)->fPos = SkPoint::Make(bounds.fRight - inset, bounds.fTop + inset); |
| 505 | (*verts)->fColor = color; |
| 506 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 507 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 508 | (*verts)++; |
| 509 | |
| 510 | // BL |
| 511 | (*verts)->fPos = SkPoint::Make(bounds.fLeft + inset, bounds.fBottom - inset); |
| 512 | (*verts)->fColor = color; |
| 513 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 514 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 515 | (*verts)++; |
| 516 | |
| 517 | // BR |
| 518 | (*verts)->fPos = SkPoint::Make(bounds.fRight - inset, bounds.fBottom - inset); |
| 519 | (*verts)->fColor = color; |
| 520 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 521 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 522 | (*verts)++; |
| 523 | } |
| 524 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 525 | } |
| 526 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 527 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 528 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 529 | void onCreateProgramInfo(const GrCaps* caps, |
| 530 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 531 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 532 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 533 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 534 | GrXferBarrierFlags renderPassXferBarriers) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 535 | GrGeometryProcessor* gp = GrRRectShadowGeoProc::Make(arena, fFalloffView); |
| 536 | SkASSERT(sizeof(CircleVertex) == gp->vertexStride()); |
| 537 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 538 | fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 539 | std::move(appliedClip), |
| 540 | dstProxyView, gp, |
| 541 | GrProcessorSet::MakeEmptySet(), |
| 542 | GrPrimitiveType::kTriangles, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 543 | renderPassXferBarriers, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 544 | GrPipeline::InputFlags::kNone, |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 545 | &GrUserStencilSettings::kUnused); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 548 | void onPrepareDraws(Target* target) override { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 549 | int instanceCount = fGeoData.count(); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 550 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 551 | sk_sp<const GrBuffer> vertexBuffer; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 552 | int firstVertex; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 553 | CircleVertex* verts = (CircleVertex*)target->makeVertexSpace( |
| 554 | sizeof(CircleVertex), fVertCount, &vertexBuffer, &firstVertex); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 555 | if (!verts) { |
| 556 | SkDebugf("Could not allocate vertices\n"); |
| 557 | return; |
| 558 | } |
| 559 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 560 | sk_sp<const GrBuffer> indexBuffer; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 561 | int firstIndex = 0; |
| 562 | uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
| 563 | if (!indices) { |
| 564 | SkDebugf("Could not allocate indices\n"); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | int currStartVertex = 0; |
| 569 | for (int i = 0; i < instanceCount; i++) { |
| 570 | const Geometry& args = fGeoData[i]; |
| 571 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 572 | if (args.fIsCircle) { |
| 573 | bool isStroked = SkToBool(kStroke_RRectType == args.fType); |
| 574 | this->fillInCircleVerts(args, isStroked, &verts); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 575 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 576 | const uint16_t* primIndices = circle_type_to_indices(isStroked); |
| 577 | const int primIndexCount = circle_type_to_index_count(isStroked); |
| 578 | for (int i = 0; i < primIndexCount; ++i) { |
| 579 | *indices++ = primIndices[i] + currStartVertex; |
| 580 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 581 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 582 | currStartVertex += circle_type_to_vert_count(isStroked); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 583 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 584 | } else { |
| 585 | this->fillInRRectVerts(args, &verts); |
| 586 | |
| 587 | const uint16_t* primIndices = rrect_type_to_indices(args.fType); |
| 588 | const int primIndexCount = rrect_type_to_index_count(args.fType); |
| 589 | for (int i = 0; i < primIndexCount; ++i) { |
| 590 | *indices++ = primIndices[i] + currStartVertex; |
| 591 | } |
| 592 | |
| 593 | currStartVertex += rrect_type_to_vert_count(args.fType); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 594 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 595 | } |
| 596 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 597 | fMesh = target->allocMesh(); |
| 598 | fMesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertCount - 1, |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 599 | GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 603 | if (!fProgramInfo) { |
| 604 | this->createProgramInfo(flushState); |
| 605 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 606 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 607 | if (!fProgramInfo || !fMesh) { |
| 608 | return; |
| 609 | } |
| 610 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 611 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 612 | flushState->bindTextures(fProgramInfo->primProc(), *fFalloffView.proxy(), |
| 613 | fProgramInfo->pipeline()); |
| 614 | flushState->drawMesh(*fMesh); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 615 | } |
| 616 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 617 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 618 | const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 619 | ShadowCircularRRectOp* that = t->cast<ShadowCircularRRectOp>(); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 620 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 621 | fVertCount += that->fVertCount; |
| 622 | fIndexCount += that->fIndexCount; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 623 | return CombineResult::kMerged; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 624 | } |
| 625 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 626 | #if GR_TEST_UTILS |
| 627 | SkString onDumpInfo() const override { |
| 628 | SkString string; |
| 629 | for (int i = 0; i < fGeoData.count(); ++i) { |
| 630 | string.appendf( |
| 631 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]," |
| 632 | "OuterRad: %.2f, Umbra: %.2f, InnerRad: %.2f, BlurRad: %.2f\n", |
| 633 | fGeoData[i].fColor, fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop, |
| 634 | fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom, |
| 635 | fGeoData[i].fOuterRadius, fGeoData[i].fUmbraInset, |
| 636 | fGeoData[i].fInnerRadius, fGeoData[i].fBlurRadius); |
| 637 | } |
| 638 | return string; |
| 639 | } |
| 640 | #endif |
| 641 | |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 642 | void visitProxies(const VisitProxyFunc& func) const override { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 643 | func(fFalloffView.proxy(), GrMipmapped(false)); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 644 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 645 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 646 | } |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 647 | } |
| 648 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 649 | SkSTArray<1, Geometry, true> fGeoData; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 650 | int fVertCount; |
| 651 | int fIndexCount; |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 652 | GrSurfaceProxyView fFalloffView; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 653 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 654 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 655 | GrProgramInfo* fProgramInfo = nullptr; |
| 656 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 657 | using INHERITED = GrMeshDrawOp; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 658 | }; |
| 659 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 660 | } // anonymous namespace |
| 661 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 662 | /////////////////////////////////////////////////////////////////////////////// |
| 663 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 664 | namespace GrShadowRRectOp { |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 665 | |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 666 | static GrSurfaceProxyView create_falloff_texture(GrRecordingContext* rContext) { |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 667 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 668 | GrUniqueKey key; |
| 669 | GrUniqueKey::Builder builder(&key, kDomain, 0, "Shadow Gaussian Falloff"); |
| 670 | builder.finish(); |
| 671 | |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 672 | auto threadSafeViewCache = rContext->priv().threadSafeViewCache(); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 673 | |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 674 | GrSurfaceProxyView view = threadSafeViewCache->find(key); |
| 675 | if (view) { |
| 676 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
| 677 | return view; |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 680 | static const int kWidth = 128; |
| 681 | static const size_t kRowBytes = kWidth * GrColorTypeBytesPerPixel(GrColorType::kAlpha_8); |
| 682 | SkImageInfo ii = SkImageInfo::MakeA8(kWidth, 1); |
| 683 | |
| 684 | SkBitmap bitmap; |
| 685 | bitmap.allocPixels(ii, kRowBytes); |
| 686 | |
| 687 | unsigned char* values = (unsigned char*)bitmap.getPixels(); |
| 688 | for (int i = 0; i < 128; ++i) { |
| 689 | SkScalar d = SK_Scalar1 - i / SkIntToScalar(127); |
| 690 | values[i] = SkScalarRoundToInt((SkScalarExp(-4 * d * d) - 0.018f) * 255); |
| 691 | } |
| 692 | bitmap.setImmutable(); |
| 693 | |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 694 | GrBitmapTextureMaker maker(rContext, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
| 695 | view = maker.view(GrMipmapped::kNo); |
| 696 | if (!view) { |
| 697 | return {}; |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 698 | } |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 699 | |
| 700 | view = threadSafeViewCache->add(key, view); |
| 701 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 702 | return view; |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 706 | std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 707 | GrColor color, |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 708 | const SkMatrix& viewMatrix, |
| 709 | const SkRRect& rrect, |
| 710 | SkScalar blurWidth, |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 711 | SkScalar insetWidth) { |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 712 | // Shadow rrect ops only handle simple circular rrects. |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 713 | SkASSERT(viewMatrix.isSimilarity() && SkRRectPriv::EqualRadii(rrect)); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 714 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 715 | GrSurfaceProxyView falloffView = create_falloff_texture(context); |
| 716 | if (!falloffView) { |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 717 | return nullptr; |
| 718 | } |
| 719 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 720 | // Do any matrix crunching before we reset the draw state for device coords. |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 721 | const SkRect& rrectBounds = rrect.getBounds(); |
| 722 | SkRect bounds; |
| 723 | viewMatrix.mapRect(&bounds, rrectBounds); |
| 724 | |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 725 | // Map radius and inset. As the matrix is a similarity matrix, this should be isotropic. |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 726 | SkScalar radius = SkRRectPriv::GetSimpleRadii(rrect).fX; |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 727 | SkScalar matrixFactor = viewMatrix[SkMatrix::kMScaleX] + viewMatrix[SkMatrix::kMSkewX]; |
| 728 | SkScalar scaledRadius = SkScalarAbs(radius*matrixFactor); |
| 729 | SkScalar scaledInsetWidth = SkScalarAbs(insetWidth*matrixFactor); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 730 | |
Robert Phillips | e576378 | 2019-04-17 14:38:24 -0400 | [diff] [blame] | 731 | if (scaledInsetWidth <= 0) { |
| 732 | return nullptr; |
| 733 | } |
| 734 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 735 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 736 | |
| 737 | return pool->allocate<ShadowCircularRRectOp>(color, bounds, |
| 738 | scaledRadius, |
| 739 | rrect.isOval(), |
| 740 | blurWidth, |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 741 | scaledInsetWidth, |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 742 | std::move(falloffView)); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 743 | } |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 744 | } // namespace GrShadowRRectOp |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 745 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 746 | /////////////////////////////////////////////////////////////////////////////// |
| 747 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 748 | #if GR_TEST_UTILS |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 749 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 750 | GR_DRAW_OP_TEST_DEFINE(ShadowRRectOp) { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 751 | // We may choose matrix and inset values that cause the factory to fail. We loop until we find |
| 752 | // an acceptable combination. |
Brian Osman | 4462c04 | 2018-06-08 16:35:44 -0400 | [diff] [blame] | 753 | do { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 754 | // create a similarity matrix |
| 755 | SkScalar rotate = random->nextSScalar1() * 360.f; |
| 756 | SkScalar translateX = random->nextSScalar1() * 1000.f; |
| 757 | SkScalar translateY = random->nextSScalar1() * 1000.f; |
| 758 | SkScalar scale = random->nextSScalar1() * 100.f; |
| 759 | SkMatrix viewMatrix; |
| 760 | viewMatrix.setRotate(rotate); |
| 761 | viewMatrix.postTranslate(translateX, translateY); |
| 762 | viewMatrix.postScale(scale, scale); |
| 763 | SkScalar insetWidth = random->nextSScalar1() * 72.f; |
| 764 | SkScalar blurWidth = random->nextSScalar1() * 72.f; |
| 765 | bool isCircle = random->nextBool(); |
| 766 | // This op doesn't use a full GrPaint, just a color. |
| 767 | GrColor color = paint.getColor4f().toBytes_RGBA(); |
| 768 | if (isCircle) { |
| 769 | SkRect circle = GrTest::TestSquare(random); |
| 770 | SkRRect rrect = SkRRect::MakeOval(circle); |
| 771 | if (auto op = GrShadowRRectOp::Make( |
| 772 | context, color, viewMatrix, rrect, blurWidth, insetWidth)) { |
| 773 | return op; |
| 774 | } |
| 775 | } else { |
| 776 | SkRRect rrect; |
| 777 | do { |
| 778 | // This may return a rrect with elliptical corners, which will cause an assert. |
| 779 | rrect = GrTest::TestRRectSimple(random); |
| 780 | } while (!SkRRectPriv::IsSimpleCircular(rrect)); |
| 781 | if (auto op = GrShadowRRectOp::Make( |
| 782 | context, color, viewMatrix, rrect, blurWidth, insetWidth)) { |
| 783 | return op; |
| 784 | } |
| 785 | } |
| 786 | } while (true); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | #endif |