robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 1 | /* |
| 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 | #include "GrAARectRenderer.h" |
| 9 | #include "GrRefCnt.h" |
| 10 | #include "GrGpu.h" |
| 11 | |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 12 | SK_DEFINE_INST_COUNT(GrAARectRenderer) |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 16 | static void aa_rect_attributes(bool useCoverage, const GrVertexAttrib** attribs, int* count) { |
| 17 | static const GrVertexAttrib kCoverageAttribs[] = { |
| 18 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 19 | {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBinding}, |
| 20 | }; |
| 21 | static const GrVertexAttrib kColorAttribs[] = { |
| 22 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 23 | {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kColor_GrVertexAttribBinding}, |
| 24 | }; |
| 25 | *attribs = useCoverage ? kCoverageAttribs : kColorAttribs; |
| 26 | *count = 2; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 27 | } |
| 28 | |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 29 | static void set_inset_fan(GrPoint* pts, size_t stride, |
| 30 | const GrRect& r, SkScalar dx, SkScalar dy) { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 31 | pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
| 32 | r.fRight - dx, r.fBottom - dy, stride); |
| 33 | } |
| 34 | |
| 35 | }; |
| 36 | |
| 37 | void GrAARectRenderer::reset() { |
| 38 | GrSafeSetNull(fAAFillRectIndexBuffer); |
| 39 | GrSafeSetNull(fAAStrokeRectIndexBuffer); |
| 40 | } |
| 41 | |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 42 | static const uint16_t gFillAARectIdx[] = { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 43 | 0, 1, 5, 5, 4, 0, |
| 44 | 1, 2, 6, 6, 5, 1, |
| 45 | 2, 3, 7, 7, 6, 2, |
| 46 | 3, 0, 4, 4, 7, 3, |
| 47 | 4, 5, 6, 6, 7, 4, |
| 48 | }; |
| 49 | |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 50 | static const int kIndicesPerAAFillRect = GR_ARRAY_COUNT(gFillAARectIdx); |
| 51 | static const int kVertsPerAAFillRect = 8; |
| 52 | static const int kNumAAFillRectsInIndexBuffer = 256; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 53 | |
| 54 | GrIndexBuffer* GrAARectRenderer::aaFillRectIndexBuffer(GrGpu* gpu) { |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 55 | static const size_t kAAFillRectIndexBufferSize = kIndicesPerAAFillRect * |
| 56 | sizeof(uint16_t) * |
| 57 | kNumAAFillRectsInIndexBuffer; |
| 58 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 59 | if (NULL == fAAFillRectIndexBuffer) { |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 60 | fAAFillRectIndexBuffer = gpu->createIndexBuffer(kAAFillRectIndexBufferSize, false); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 61 | if (NULL != fAAFillRectIndexBuffer) { |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 62 | uint16_t* data = (uint16_t*) fAAFillRectIndexBuffer->lock(); |
| 63 | bool useTempData = (NULL == data); |
| 64 | if (useTempData) { |
| 65 | data = SkNEW_ARRAY(uint16_t, kNumAAFillRectsInIndexBuffer * kIndicesPerAAFillRect); |
| 66 | } |
| 67 | for (int i = 0; i < kNumAAFillRectsInIndexBuffer; ++i) { |
| 68 | // Each AA filled rect is drawn with 8 vertices and 10 triangles (8 around |
| 69 | // the inner rect (for AA) and 2 for the inner rect. |
| 70 | int baseIdx = i * kIndicesPerAAFillRect; |
| 71 | uint16_t baseVert = (uint16_t)(i * kVertsPerAAFillRect); |
| 72 | for (int j = 0; j < kIndicesPerAAFillRect; ++j) { |
| 73 | data[baseIdx+j] = baseVert + gFillAARectIdx[j]; |
| 74 | } |
| 75 | } |
| 76 | if (useTempData) { |
| 77 | if (!fAAFillRectIndexBuffer->updateData(data, kAAFillRectIndexBufferSize)) { |
| 78 | GrCrash("Can't get AA Fill Rect indices into buffer!"); |
| 79 | } |
| 80 | SkDELETE_ARRAY(data); |
| 81 | } else { |
| 82 | fAAFillRectIndexBuffer->unlock(); |
| 83 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 86 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 87 | return fAAFillRectIndexBuffer; |
| 88 | } |
| 89 | |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 90 | static const uint16_t gStrokeAARectIdx[] = { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 91 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 92 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 93 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 94 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 95 | |
| 96 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 97 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 98 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 99 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 100 | |
| 101 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 102 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 103 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 104 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 105 | }; |
| 106 | |
| 107 | int GrAARectRenderer::aaStrokeRectIndexCount() { |
| 108 | return GR_ARRAY_COUNT(gStrokeAARectIdx); |
| 109 | } |
| 110 | |
| 111 | GrIndexBuffer* GrAARectRenderer::aaStrokeRectIndexBuffer(GrGpu* gpu) { |
| 112 | if (NULL == fAAStrokeRectIndexBuffer) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | fAAStrokeRectIndexBuffer = |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 114 | gpu->createIndexBuffer(sizeof(gStrokeAARectIdx), false); |
| 115 | if (NULL != fAAStrokeRectIndexBuffer) { |
| 116 | #if GR_DEBUG |
| 117 | bool updated = |
| 118 | #endif |
| 119 | fAAStrokeRectIndexBuffer->updateData(gStrokeAARectIdx, |
| 120 | sizeof(gStrokeAARectIdx)); |
| 121 | GR_DEBUGASSERT(updated); |
| 122 | } |
| 123 | } |
| 124 | return fAAStrokeRectIndexBuffer; |
| 125 | } |
| 126 | |
| 127 | void GrAARectRenderer::fillAARect(GrGpu* gpu, |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 128 | GrDrawTarget* target, |
| 129 | const GrRect& devRect, |
| 130 | bool useVertexCoverage) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 131 | GrDrawState* drawState = target->drawState(); |
| 132 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 133 | const GrVertexAttrib* attribs; |
| 134 | int attribCount; |
| 135 | aa_rect_attributes(useVertexCoverage, &attribs, &attribCount); |
| 136 | drawState->setVertexAttribs(attribs, attribCount); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 137 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 138 | GrDrawTarget::AutoReleaseGeometry geo(target, 8, 0); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 139 | if (!geo.succeeded()) { |
| 140 | GrPrintf("Failed to get space for vertices!\n"); |
| 141 | return; |
| 142 | } |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 143 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 144 | GrIndexBuffer* indexBuffer = this->aaFillRectIndexBuffer(gpu); |
| 145 | if (NULL == indexBuffer) { |
| 146 | GrPrintf("Failed to create index buffer!\n"); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 151 | size_t vsize = drawState->getVertexSize(); |
| 152 | GrAssert(sizeof(GrPoint) + sizeof(GrColor) == vsize); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 153 | |
| 154 | GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts); |
| 155 | GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize); |
| 156 | |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 157 | set_inset_fan(fan0Pos, vsize, devRect, -SK_ScalarHalf, -SK_ScalarHalf); |
| 158 | set_inset_fan(fan1Pos, vsize, devRect, SK_ScalarHalf, SK_ScalarHalf); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 159 | |
| 160 | verts += sizeof(GrPoint); |
| 161 | for (int i = 0; i < 4; ++i) { |
| 162 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
| 163 | } |
| 164 | |
| 165 | GrColor innerColor; |
| 166 | if (useVertexCoverage) { |
| 167 | innerColor = 0xffffffff; |
| 168 | } else { |
| 169 | innerColor = target->getDrawState().getColor(); |
| 170 | } |
| 171 | |
| 172 | verts += 4 * vsize; |
| 173 | for (int i = 0; i < 4; ++i) { |
| 174 | *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor; |
| 175 | } |
| 176 | |
| 177 | target->setIndexSourceToBuffer(indexBuffer); |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 178 | target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, |
| 179 | kVertsPerAAFillRect, |
| 180 | kIndicesPerAAFillRect); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void GrAARectRenderer::strokeAARect(GrGpu* gpu, |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 184 | GrDrawTarget* target, |
| 185 | const GrRect& devRect, |
| 186 | const GrVec& devStrokeSize, |
| 187 | bool useVertexCoverage) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 188 | GrDrawState* drawState = target->drawState(); |
| 189 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 190 | const SkScalar& dx = devStrokeSize.fX; |
| 191 | const SkScalar& dy = devStrokeSize.fY; |
| 192 | const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); |
| 193 | const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 194 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 195 | SkScalar spare; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 196 | { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 197 | SkScalar w = devRect.width() - dx; |
| 198 | SkScalar h = devRect.height() - dy; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 199 | spare = GrMin(w, h); |
| 200 | } |
| 201 | |
| 202 | if (spare <= 0) { |
| 203 | GrRect r(devRect); |
| 204 | r.inset(-rx, -ry); |
| 205 | this->fillAARect(gpu, target, r, useVertexCoverage); |
| 206 | return; |
| 207 | } |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 208 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 209 | const GrVertexAttrib* attribs; |
| 210 | int attribCount; |
| 211 | aa_rect_attributes(useVertexCoverage, &attribs, &attribCount); |
| 212 | drawState->setVertexAttribs(attribs, attribCount); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 213 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 214 | GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 215 | if (!geo.succeeded()) { |
| 216 | GrPrintf("Failed to get space for vertices!\n"); |
| 217 | return; |
| 218 | } |
| 219 | GrIndexBuffer* indexBuffer = this->aaStrokeRectIndexBuffer(gpu); |
| 220 | if (NULL == indexBuffer) { |
| 221 | GrPrintf("Failed to create index buffer!\n"); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 226 | size_t vsize = drawState->getVertexSize(); |
| 227 | GrAssert(sizeof(GrPoint) + sizeof(GrColor) == vsize); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 228 | |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 229 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
| 230 | // coverage, one on the exterior of the stroke and the other on the interior. |
| 231 | // The following pointers refer to the four rects, from outermost to innermost. |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 232 | GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts); |
| 233 | GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize); |
| 234 | GrPoint* fan2Pos = reinterpret_cast<GrPoint*>(verts + 8 * vsize); |
| 235 | GrPoint* fan3Pos = reinterpret_cast<GrPoint*>(verts + 12 * vsize); |
| 236 | |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 237 | set_inset_fan(fan0Pos, vsize, devRect, |
| 238 | -rx - SK_ScalarHalf, -ry - SK_ScalarHalf); |
| 239 | set_inset_fan(fan1Pos, vsize, devRect, |
| 240 | -rx + SK_ScalarHalf, -ry + SK_ScalarHalf); |
| 241 | set_inset_fan(fan2Pos, vsize, devRect, |
| 242 | rx - SK_ScalarHalf, ry - SK_ScalarHalf); |
| 243 | set_inset_fan(fan3Pos, vsize, devRect, |
| 244 | rx + SK_ScalarHalf, ry + SK_ScalarHalf); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 245 | |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 246 | // The outermost rect has 0 coverage |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 247 | verts += sizeof(GrPoint); |
| 248 | for (int i = 0; i < 4; ++i) { |
| 249 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
| 250 | } |
| 251 | |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 252 | // The inner two rects have full coverage |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 253 | GrColor innerColor; |
| 254 | if (useVertexCoverage) { |
| 255 | innerColor = 0xffffffff; |
| 256 | } else { |
| 257 | innerColor = target->getDrawState().getColor(); |
| 258 | } |
| 259 | verts += 4 * vsize; |
| 260 | for (int i = 0; i < 8; ++i) { |
| 261 | *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor; |
| 262 | } |
| 263 | |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 264 | // The innermost rect has full coverage |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 265 | verts += 8 * vsize; |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 266 | for (int i = 0; i < 4; ++i) { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 267 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
| 268 | } |
| 269 | |
| 270 | target->setIndexSourceToBuffer(indexBuffer); |
| 271 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 272 | 0, 0, 16, aaStrokeRectIndexCount()); |
| 273 | } |