robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +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 "GrSoftwarePathRenderer.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 9 | #include "GrAuditTrail.h" |
| 10 | #include "GrClip.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 13 | #include "GrOpFlushState.h" |
| 14 | #include "GrOpList.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 15 | #include "GrResourceProvider.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 16 | #include "GrSWMaskHelper.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 17 | #include "SkMakeUnique.h" |
| 18 | #include "SkSemaphore.h" |
| 19 | #include "SkTaskGroup.h" |
| 20 | #include "SkTraceEvent.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 21 | #include "ops/GrDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 22 | #include "ops/GrRectOpFactory.h" |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 23 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 24 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 25 | bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 26 | // Pass on any style that applies. The caller will apply the style if a suitable renderer is |
| 27 | // not found and try again with the new GrShape. |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 28 | return !args.fShape->style().applies() && SkToBool(fResourceProvider) && |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 29 | (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone); |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 30 | } |
| 31 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 32 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 33 | static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix, |
| 34 | SkIRect* devBounds) { |
| 35 | SkRect shapeBounds = shape.styledBounds(); |
| 36 | if (shapeBounds.isEmpty()) { |
| 37 | return false; |
| 38 | } |
| 39 | SkRect shapeDevBounds; |
| 40 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
Brian Salomon | c1c607e | 2016-12-20 11:41:43 -0500 | [diff] [blame] | 41 | // Even though these are "unclipped" bounds we still clip to the int32_t range. |
| 42 | // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
| 43 | // would round down to this value when cast to a float, but who really cares. |
| 44 | // INT32_MIN is exactly representable. |
| 45 | static constexpr int32_t kMaxInt = 2147483520; |
| 46 | if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
| 47 | return false; |
| 48 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 49 | shapeDevBounds.roundOut(devBounds); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 54 | // is no intersection. |
| 55 | static bool get_shape_and_clip_bounds(int width, int height, |
| 56 | const GrClip& clip, |
| 57 | const GrShape& shape, |
| 58 | const SkMatrix& matrix, |
| 59 | SkIRect* unclippedDevShapeBounds, |
| 60 | SkIRect* clippedDevShapeBounds, |
| 61 | SkIRect* devClipBounds) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 62 | // compute bounds as intersection of rt size, clip, and path |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 63 | clip.getConservativeBounds(width, height, devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 64 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 65 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
| 66 | *unclippedDevShapeBounds = SkIRect::EmptyIRect(); |
| 67 | *clippedDevShapeBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 68 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 69 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 70 | if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) { |
| 71 | *clippedDevShapeBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 78 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 79 | void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 80 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 81 | const GrUserStencilSettings& userStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 82 | const GrClip& clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 83 | const SkMatrix& viewMatrix, |
| 84 | const SkRect& rect, |
| 85 | const SkMatrix& localMatrix) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 86 | renderTargetContext->addDrawOp(clip, |
| 87 | GrRectOpFactory::MakeNonAAFillWithLocalMatrix( |
| 88 | std::move(paint), viewMatrix, localMatrix, rect, |
| 89 | GrAAType::kNone, &userStencilSettings)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 92 | void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 93 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 94 | const GrUserStencilSettings& userStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 95 | const GrClip& clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 96 | const SkMatrix& viewMatrix, |
| 97 | const SkIRect& devClipBounds, |
| 98 | const SkIRect& devPathBounds) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 99 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 100 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 101 | return; |
| 102 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 103 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 104 | SkRect rect; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 105 | if (devClipBounds.fTop < devPathBounds.fTop) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | rect.iset(devClipBounds.fLeft, devClipBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 107 | devClipBounds.fRight, devPathBounds.fTop); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 108 | DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip, |
| 109 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 110 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 111 | if (devClipBounds.fLeft < devPathBounds.fLeft) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | rect.iset(devClipBounds.fLeft, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 113 | devPathBounds.fLeft, devPathBounds.fBottom); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 114 | DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip, |
| 115 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 116 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 117 | if (devClipBounds.fRight > devPathBounds.fRight) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 118 | rect.iset(devPathBounds.fRight, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 119 | devClipBounds.fRight, devPathBounds.fBottom); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 120 | DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip, |
| 121 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 122 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 123 | if (devClipBounds.fBottom > devPathBounds.fBottom) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | rect.iset(devClipBounds.fLeft, devPathBounds.fBottom, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 125 | devClipBounds.fRight, devClipBounds.fBottom); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 126 | DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 127 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 131 | void GrSoftwarePathRenderer::DrawToTargetWithShapeMask( |
| 132 | sk_sp<GrTextureProxy> proxy, |
| 133 | GrRenderTargetContext* renderTargetContext, |
| 134 | GrPaint&& paint, |
| 135 | const GrUserStencilSettings& userStencilSettings, |
| 136 | const GrClip& clip, |
| 137 | const SkMatrix& viewMatrix, |
| 138 | const SkIPoint& textureOriginInDeviceSpace, |
| 139 | const SkIRect& deviceSpaceRectToDraw) { |
| 140 | SkMatrix invert; |
| 141 | if (!viewMatrix.invert(&invert)) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw); |
| 146 | |
| 147 | // We use device coords to compute the texture coordinates. We take the device coords and apply |
| 148 | // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling |
| 149 | // matrix to normalized coords. |
| 150 | SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX), |
| 151 | SkIntToScalar(-textureOriginInDeviceSpace.fY)); |
| 152 | maskMatrix.preConcat(viewMatrix); |
| 153 | paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make( |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame^] | 154 | std::move(proxy), nullptr, maskMatrix, GrSamplerState::Filter::kNearest)); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 155 | DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip, SkMatrix::I(), |
| 156 | dstRect, invert); |
| 157 | } |
| 158 | |
| 159 | static sk_sp<GrTextureProxy> make_deferred_mask_texture_proxy(GrContext* context, SkBackingFit fit, |
| 160 | int width, int height) { |
| 161 | GrSurfaceDesc desc; |
| 162 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 163 | desc.fWidth = width; |
| 164 | desc.fHeight = height; |
| 165 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 166 | |
| 167 | sk_sp<GrSurfaceContext> sContext = |
| 168 | context->contextPriv().makeDeferredSurfaceContext(desc, fit, SkBudgeted::kYes); |
| 169 | if (!sContext || !sContext->asTextureProxy()) { |
| 170 | return nullptr; |
| 171 | } |
| 172 | return sContext->asTextureProxyRef(); |
| 173 | } |
| 174 | |
| 175 | namespace { |
| 176 | |
| 177 | class GrMaskUploaderPrepareCallback : public GrPrepareCallback { |
| 178 | public: |
| 179 | GrMaskUploaderPrepareCallback(sk_sp<GrTextureProxy> proxy, const SkIRect& maskBounds, |
| 180 | const SkMatrix& viewMatrix, const GrShape& shape, GrAA aa) |
| 181 | : fProxy(std::move(proxy)) |
| 182 | , fMaskBounds(maskBounds) |
| 183 | , fViewMatrix(viewMatrix) |
| 184 | , fShape(shape) |
| 185 | , fAA(aa) |
| 186 | , fWaited(false) {} |
| 187 | |
| 188 | ~GrMaskUploaderPrepareCallback() override { |
| 189 | if (!fWaited) { |
| 190 | // This can happen if our owning op list fails to instantiate (so it never prepares) |
| 191 | fPixelsReady.wait(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void operator()(GrOpFlushState* flushState) override { |
| 196 | TRACE_EVENT0("skia", "Mask Uploader Pre Flush Callback"); |
| 197 | auto uploadMask = [this](GrDrawOp::WritePixelsFn& writePixelsFn) { |
| 198 | TRACE_EVENT0("skia", "Mask Upload"); |
| 199 | this->fPixelsReady.wait(); |
| 200 | this->fWaited = true; |
| 201 | // If the worker thread was unable to allocate pixels, this check will fail, and we'll |
| 202 | // end up drawing with an uninitialized mask texture, but at least we won't crash. |
| 203 | if (this->fPixels.addr()) { |
| 204 | writePixelsFn(this->fProxy.get(), 0, 0, |
| 205 | this->fPixels.width(), this->fPixels.height(), |
| 206 | kAlpha_8_GrPixelConfig, |
| 207 | this->fPixels.addr(), this->fPixels.rowBytes()); |
Brian Osman | d41dc17 | 2017-09-01 11:40:08 -0400 | [diff] [blame] | 208 | // Free this memory immediately, so it can be recycled. This avoids memory pressure |
| 209 | // when there is a large amount of threaded work still running during flush. |
| 210 | this->fPixels.reset(); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | flushState->addASAPUpload(std::move(uploadMask)); |
| 214 | } |
| 215 | |
| 216 | SkAutoPixmapStorage* getPixels() { return &fPixels; } |
| 217 | SkSemaphore* getSemaphore() { return &fPixelsReady; } |
| 218 | const SkIRect& getMaskBounds() const { return fMaskBounds; } |
| 219 | const SkMatrix* getViewMatrix() const { return &fViewMatrix; } |
| 220 | const GrShape& getShape() const { return fShape; } |
| 221 | GrAA getAA() const { return fAA; } |
| 222 | |
| 223 | private: |
| 224 | // NOTE: This ref cnt isn't thread safe! |
| 225 | sk_sp<GrTextureProxy> fProxy; |
| 226 | SkAutoPixmapStorage fPixels; |
| 227 | SkSemaphore fPixelsReady; |
| 228 | |
| 229 | SkIRect fMaskBounds; |
| 230 | SkMatrix fViewMatrix; |
| 231 | GrShape fShape; |
| 232 | GrAA fAA; |
| 233 | bool fWaited; |
| 234 | }; |
| 235 | |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 236 | } |
| 237 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 238 | //////////////////////////////////////////////////////////////////////////////// |
| 239 | // return true on success; false on failure |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 240 | bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 241 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 242 | "GrSoftwarePathRenderer::onDrawPath"); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 243 | if (!fResourceProvider) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 247 | // We really need to know if the shape will be inverse filled or not |
| 248 | bool inverseFilled = false; |
| 249 | SkTLazy<GrShape> tmpShape; |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 250 | SkASSERT(!args.fShape->style().applies()); |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 251 | // If the path is hairline, ignore inverse fill. |
| 252 | inverseFilled = args.fShape->inverseFilled() && |
| 253 | !IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 254 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 255 | SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds; |
| 256 | // To prevent overloading the cache with entries during animations we limit the cache of masks |
| 257 | // to cases where the matrix preserves axis alignment. |
| 258 | bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() && |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 259 | args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 260 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 261 | if (!get_shape_and_clip_bounds(args.fRenderTargetContext->width(), |
| 262 | args.fRenderTargetContext->height(), |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 263 | *args.fClip, *args.fShape, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 264 | *args.fViewMatrix, &unclippedDevShapeBounds, |
| 265 | &clippedDevShapeBounds, |
| 266 | &devClipBounds)) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 267 | if (inverseFilled) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 268 | DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint), |
| 269 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, |
| 270 | devClipBounds, unclippedDevShapeBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 271 | } |
| 272 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 273 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 274 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 275 | const SkIRect* boundsForMask = &clippedDevShapeBounds; |
| 276 | if (useCache) { |
| 277 | // Use the cache only if >50% of the path is visible. |
| 278 | int unclippedWidth = unclippedDevShapeBounds.width(); |
| 279 | int unclippedHeight = unclippedDevShapeBounds.height(); |
| 280 | int unclippedArea = unclippedWidth * unclippedHeight; |
| 281 | int clippedArea = clippedDevShapeBounds.width() * clippedDevShapeBounds.height(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 282 | int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize(); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 283 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 284 | unclippedHeight > maxTextureSize) { |
| 285 | useCache = false; |
| 286 | } else { |
| 287 | boundsForMask = &unclippedDevShapeBounds; |
| 288 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 291 | GrUniqueKey maskKey; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 292 | if (useCache) { |
| 293 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 294 | SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX); |
| 295 | SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY); |
| 296 | SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX); |
| 297 | SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 298 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 299 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 300 | // Fractional translate does not affect caching on Android. This is done for better cache |
| 301 | // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix |
| 302 | // at all when caching paths. |
| 303 | GrUniqueKey::Builder builder(&maskKey, kDomain, 4 + args.fShape->unstyledKeySize()); |
| 304 | #else |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 305 | SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX); |
| 306 | SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY); |
| 307 | // Allow 8 bits each in x and y of subpixel positioning. |
| 308 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 309 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 310 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize()); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 311 | #endif |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 312 | builder[0] = SkFloat2Bits(sx); |
| 313 | builder[1] = SkFloat2Bits(sy); |
| 314 | builder[2] = SkFloat2Bits(kx); |
| 315 | builder[3] = SkFloat2Bits(ky); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 316 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 317 | args.fShape->writeUnstyledKey(&builder[4]); |
| 318 | #else |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 319 | builder[4] = fracX | (fracY >> 8); |
| 320 | args.fShape->writeUnstyledKey(&builder[5]); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 321 | #endif |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 324 | sk_sp<GrTextureProxy> proxy; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 325 | if (useCache) { |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 326 | proxy = fResourceProvider->findProxyByUniqueKey(maskKey, kTopLeft_GrSurfaceOrigin); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 327 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 328 | if (!proxy) { |
Robert Phillips | 417b7f4 | 2016-12-14 09:12:13 -0500 | [diff] [blame] | 329 | SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 330 | GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 331 | |
| 332 | SkTaskGroup* taskGroup = args.fContext->contextPriv().getTaskGroup(); |
| 333 | if (taskGroup) { |
| 334 | proxy = make_deferred_mask_texture_proxy(args.fContext, fit, |
| 335 | boundsForMask->width(), |
| 336 | boundsForMask->height()); |
| 337 | if (!proxy) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | auto uploader = skstd::make_unique<GrMaskUploaderPrepareCallback>( |
| 342 | proxy, *boundsForMask, *args.fViewMatrix, *args.fShape, aa); |
| 343 | GrMaskUploaderPrepareCallback* uploaderRaw = uploader.get(); |
| 344 | |
| 345 | auto drawAndUploadMask = [uploaderRaw] { |
| 346 | TRACE_EVENT0("skia", "Threaded SW Mask Render"); |
| 347 | GrSWMaskHelper helper(uploaderRaw->getPixels()); |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 348 | if (helper.init(uploaderRaw->getMaskBounds())) { |
| 349 | helper.drawShape(uploaderRaw->getShape(), *uploaderRaw->getViewMatrix(), |
| 350 | SkRegion::kReplace_Op, uploaderRaw->getAA(), 0xFF); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 351 | } else { |
| 352 | SkDEBUGFAIL("Unable to allocate SW mask."); |
| 353 | } |
| 354 | uploaderRaw->getSemaphore()->signal(); |
| 355 | }; |
| 356 | taskGroup->add(std::move(drawAndUploadMask)); |
| 357 | args.fRenderTargetContext->getOpList()->addPrepareCallback(std::move(uploader)); |
| 358 | } else { |
| 359 | GrSWMaskHelper helper; |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 360 | if (!helper.init(*boundsForMask)) { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 361 | return false; |
| 362 | } |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 363 | helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 364 | proxy = helper.toTextureProxy(args.fContext, fit); |
| 365 | } |
| 366 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 367 | if (!proxy) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | if (useCache) { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 371 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 372 | fResourceProvider->assignUniqueKeyToProxy(maskKey, proxy.get()); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 373 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 374 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 375 | if (inverseFilled) { |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 376 | DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 377 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds, |
| 378 | unclippedDevShapeBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 379 | } |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 380 | DrawToTargetWithShapeMask( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 381 | std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 382 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, |
| 383 | SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 384 | |
| 385 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 386 | } |