skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 "SkGpuDevice.h" |
| 9 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 10 | #include "GrBlurUtils.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 11 | #include "GrContext.h" |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 12 | #include "GrDrawContextPriv.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 13 | #include "GrGpu.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 14 | #include "GrImageIDTextureAdjuster.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 15 | #include "GrStyle.h" |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 16 | #include "GrTracing.h" |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 17 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 18 | #include "SkCanvasPriv.h" |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 19 | #include "SkDraw.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 20 | #include "SkErrorInternals.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 21 | #include "SkGlyphCache.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 22 | #include "SkGr.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 23 | #include "SkGrPriv.h" |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 24 | #include "SkImage_Base.h" |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 25 | #include "SkImageCacherator.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 26 | #include "SkImageFilter.h" |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 27 | #include "SkImageFilterCache.h" |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 28 | #include "SkLatticeIter.h" |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 29 | #include "SkMaskFilter.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 30 | #include "SkPathEffect.h" |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 31 | #include "SkPicture.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 32 | #include "SkPictureData.h" |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 33 | #include "SkRasterClip.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 34 | #include "SkRRect.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 35 | #include "SkRecord.h" |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 36 | #include "SkSpecialImage.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 37 | #include "SkStroke.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 38 | #include "SkSurface.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 39 | #include "SkSurface_Gpu.h" |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 40 | #include "SkTLazy.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 41 | #include "SkUtils.h" |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 42 | #include "SkVertState.h" |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 43 | #include "SkXfermode.h" |
joshualitt | a61c817 | 2015-08-17 10:51:22 -0700 | [diff] [blame] | 44 | #include "batches/GrRectBatchFactory.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 45 | #include "effects/GrBicubicEffect.h" |
| 46 | #include "effects/GrDashingEffect.h" |
| 47 | #include "effects/GrSimpleTextureEffect.h" |
| 48 | #include "effects/GrTextureDomain.h" |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 49 | #include "text/GrTextUtils.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 50 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 51 | #if SK_SUPPORT_GPU |
| 52 | |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 53 | #define ASSERT_SINGLE_OWNER \ |
| 54 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());) |
| 55 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 56 | enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 }; |
| 57 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 58 | #if 0 |
| 59 | extern bool (*gShouldDrawProc)(); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 60 | #define CHECK_SHOULD_DRAW(draw) \ |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 61 | do { \ |
| 62 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 63 | this->prepareDraw(draw); \ |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 64 | } while (0) |
| 65 | #else |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 66 | #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw) |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 71 | /** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation |
| 72 | should fail. */ |
| 73 | bool SkGpuDevice::CheckAlphaTypeAndGetFlags( |
| 74 | const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) { |
| 75 | *flags = 0; |
| 76 | if (info) { |
| 77 | switch (info->alphaType()) { |
| 78 | case kPremul_SkAlphaType: |
| 79 | break; |
| 80 | case kOpaque_SkAlphaType: |
| 81 | *flags |= SkGpuDevice::kIsOpaque_Flag; |
| 82 | break; |
| 83 | default: // If it is unpremul or unknown don't try to render |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | if (kClear_InitContents == init) { |
| 88 | *flags |= kNeedClear_Flag; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
robertphillips | 15c42ca | 2016-08-04 08:45:02 -0700 | [diff] [blame] | 93 | sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrDrawContext> drawContext, |
| 94 | int width, int height, |
| 95 | InitContents init) { |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 96 | if (!drawContext || drawContext->wasAbandoned()) { |
| 97 | return nullptr; |
| 98 | } |
| 99 | unsigned flags; |
| 100 | if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) { |
| 101 | return nullptr; |
| 102 | } |
robertphillips | 15c42ca | 2016-08-04 08:45:02 -0700 | [diff] [blame] | 103 | return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 104 | } |
| 105 | |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 106 | sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted, |
| 107 | const SkImageInfo& info, int sampleCount, |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 108 | GrSurfaceOrigin origin, |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 109 | const SkSurfaceProps* props, InitContents init) { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 110 | unsigned flags; |
| 111 | if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | return nullptr; |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 113 | } |
| 114 | |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 115 | sk_sp<GrDrawContext> drawContext(MakeDrawContext(context, budgeted, info, |
| 116 | sampleCount, origin, props)); |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 117 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | return nullptr; |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 121 | return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext), |
| 122 | info.width(), info.height(), flags)); |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 123 | } |
| 124 | |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 125 | SkGpuDevice::SkGpuDevice(sk_sp<GrDrawContext> drawContext, int width, int height, unsigned flags) |
| 126 | : INHERITED(drawContext->surfaceProps()) |
| 127 | , fContext(SkRef(drawContext->accessRenderTarget()->getContext())) |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 128 | , fDrawContext(std::move(drawContext)) { |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 129 | fSize.set(width, height); |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 130 | fOpaque = SkToBool(flags & kIsOpaque_Flag); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 131 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 132 | if (flags & kNeedClear_Flag) { |
| 133 | this->clearAll(); |
| 134 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 135 | } |
| 136 | |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 137 | sk_sp<GrDrawContext> SkGpuDevice::MakeDrawContext(GrContext* context, |
| 138 | SkBudgeted budgeted, |
| 139 | const SkImageInfo& origInfo, |
| 140 | int sampleCount, |
| 141 | GrSurfaceOrigin origin, |
| 142 | const SkSurfaceProps* surfaceProps) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 143 | if (kUnknown_SkColorType == origInfo.colorType() || |
| 144 | origInfo.width() < 0 || origInfo.height() < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 146 | } |
| 147 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 148 | if (!context) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | return nullptr; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 150 | } |
| 151 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 152 | SkColorType ct = origInfo.colorType(); |
| 153 | SkAlphaType at = origInfo.alphaType(); |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 154 | SkColorSpace* cs = origInfo.colorSpace(); |
brianosman | 6b08652 | 2016-04-14 12:39:00 -0700 | [diff] [blame] | 155 | if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 156 | at = kOpaque_SkAlphaType; // force this setting |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 157 | } |
| 158 | if (kOpaque_SkAlphaType != at) { |
| 159 | at = kPremul_SkAlphaType; // force this setting |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 160 | } |
brianosman | 6b08652 | 2016-04-14 12:39:00 -0700 | [diff] [blame] | 161 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 162 | GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps()); |
brianosman | 6b08652 | 2016-04-14 12:39:00 -0700 | [diff] [blame] | 163 | if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) { |
| 164 | // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32 |
| 165 | ct = kN32_SkColorType; |
| 166 | } |
| 167 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 168 | GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps()); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 169 | |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 170 | return context->makeDrawContext(SkBackingFit::kExact, // Why exact? |
| 171 | origInfo.width(), origInfo.height(), |
| 172 | config, sk_ref_sp(cs), sampleCount, |
| 173 | origin, surfaceProps, budgeted); |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 174 | } |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 175 | |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 176 | sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw, |
| 177 | SkSpecialImage* srcImg, |
| 178 | int left, int top, |
| 179 | SkIPoint* offset, |
| 180 | const SkImageFilter* filter) { |
| 181 | SkASSERT(srcImg->isTextureBacked()); |
| 182 | SkASSERT(filter); |
| 183 | |
| 184 | SkMatrix matrix = *draw.fMatrix; |
| 185 | matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
| 186 | const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top); |
| 187 | SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache()); |
| 188 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
| 189 | |
| 190 | return filter->filterImage(srcImg, ctx, offset); |
| 191 | } |
| 192 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 193 | /////////////////////////////////////////////////////////////////////////////// |
| 194 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 195 | bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 196 | int x, int y) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 197 | ASSERT_SINGLE_OWNER |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 198 | |
| 199 | // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 200 | GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps()); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 201 | if (kUnknown_GrPixelConfig == config) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | uint32_t flags = 0; |
| 206 | if (kUnpremul_SkAlphaType == dstInfo.alphaType()) { |
| 207 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 208 | } |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 209 | return fDrawContext->accessRenderTarget()->readPixels(x, y, |
| 210 | dstInfo.width(), dstInfo.height(), |
| 211 | config, dstPixels, |
| 212 | dstRowBytes, flags); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 213 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 214 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 215 | bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, |
| 216 | int x, int y) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 217 | ASSERT_SINGLE_OWNER |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 218 | // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 219 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 220 | if (kUnknown_GrPixelConfig == config) { |
| 221 | return false; |
| 222 | } |
| 223 | uint32_t flags = 0; |
| 224 | if (kUnpremul_SkAlphaType == info.alphaType()) { |
| 225 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 226 | } |
robertphillips | 15c42ca | 2016-08-04 08:45:02 -0700 | [diff] [blame] | 227 | return fDrawContext->accessRenderTarget()->writePixels(x, y, info.width(), info.height(), |
| 228 | config, pixels, rowBytes, flags); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 229 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 230 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 231 | bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 232 | ASSERT_SINGLE_OWNER |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 236 | // call this every draw call, to ensure that the context reflects our state, |
| 237 | // and not the state from some other canvas/device |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 238 | void SkGpuDevice::prepareDraw(const SkDraw& draw) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 239 | ASSERT_SINGLE_OWNER |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 240 | |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 241 | fClip.reset(draw.fClipStack, &this->getOrigin()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 242 | } |
| 243 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 244 | GrDrawContext* SkGpuDevice::accessDrawContext() { |
| 245 | ASSERT_SINGLE_OWNER |
| 246 | return fDrawContext.get(); |
| 247 | } |
| 248 | |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 249 | void SkGpuDevice::clearAll() { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 250 | ASSERT_SINGLE_OWNER |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 251 | GrColor color = 0; |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 252 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext); |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 253 | SkIRect rect = SkIRect::MakeWH(this->width(), this->height()); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 254 | fDrawContext->clear(&rect, color, true); |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 255 | } |
| 256 | |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 257 | void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 258 | ASSERT_SINGLE_OWNER |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 259 | |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 260 | SkBudgeted budgeted = fDrawContext->drawContextPriv().isBudgeted(); |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 261 | |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 262 | sk_sp<GrDrawContext> newDC(MakeDrawContext(this->context(), |
| 263 | budgeted, |
| 264 | this->imageInfo(), |
| 265 | fDrawContext->numColorSamples(), |
| 266 | fDrawContext->origin(), |
| 267 | &this->surfaceProps())); |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 268 | if (!newDC) { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (shouldRetainContent) { |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 273 | if (fDrawContext->wasAbandoned()) { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 274 | return; |
| 275 | } |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 276 | newDC->copySurface(fDrawContext->asTexture().get(), |
| 277 | SkIRect::MakeWH(this->width(), this->height()), |
| 278 | SkIPoint::Make(0, 0)); |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 279 | } |
| 280 | |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 281 | fDrawContext = newDC; |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 282 | } |
| 283 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 284 | /////////////////////////////////////////////////////////////////////////////// |
| 285 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 286 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 287 | ASSERT_SINGLE_OWNER |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 288 | CHECK_SHOULD_DRAW(draw); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 289 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 290 | |
| 291 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 292 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 295 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 296 | fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // must be in SkCanvas::PointMode order |
| 300 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 301 | kPoints_GrPrimitiveType, |
| 302 | kLines_GrPrimitiveType, |
| 303 | kLineStrip_GrPrimitiveType |
| 304 | }; |
| 305 | |
ethannicholas | 330bb95 | 2015-07-17 06:44:02 -0700 | [diff] [blame] | 306 | // suppress antialiasing on axis-aligned integer-coordinate lines |
| 307 | static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) { |
| 308 | if (mode == SkCanvas::PointMode::kPoints_PointMode) { |
| 309 | return false; |
| 310 | } |
| 311 | if (count == 2) { |
| 312 | // We do not antialias as long as the primary axis of the line is integer-aligned, even if |
| 313 | // the other coordinates are not. This does mean the two end pixels of the line will be |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 314 | // sharp even when they shouldn't be, but turning antialiasing on (as things stand |
ethannicholas | 330bb95 | 2015-07-17 06:44:02 -0700 | [diff] [blame] | 315 | // currently) means that the line will turn into a two-pixel-wide blur. While obviously a |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 316 | // more complete fix is possible down the road, for the time being we accept the error on |
ethannicholas | 330bb95 | 2015-07-17 06:44:02 -0700 | [diff] [blame] | 317 | // the two end pixels as being the lesser of two evils. |
| 318 | if (pts[0].fX == pts[1].fX) { |
| 319 | return ((int) pts[0].fX) != pts[0].fX; |
| 320 | } |
| 321 | if (pts[0].fY == pts[1].fY) { |
| 322 | return ((int) pts[0].fY) != pts[0].fY; |
| 323 | } |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 328 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| 329 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 330 | ASSERT_SINGLE_OWNER |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 331 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 332 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 333 | |
| 334 | SkScalar width = paint.getStrokeWidth(); |
| 335 | if (width < 0) { |
| 336 | return; |
| 337 | } |
| 338 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 339 | if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 340 | GrStyle style(paint, SkPaint::kStroke_Style); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 341 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 342 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, |
brianosman | 1638c0d | 2016-07-25 05:12:53 -0700 | [diff] [blame] | 343 | &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 344 | return; |
| 345 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 346 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 347 | path.setIsVolatile(true); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 348 | path.moveTo(pts[0]); |
| 349 | path.lineTo(pts[1]); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 350 | fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 351 | return; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 352 | } |
| 353 | |
ethannicholas | 330bb95 | 2015-07-17 06:44:02 -0700 | [diff] [blame] | 354 | // we only handle non-antialiased hairlines and paints without path effects or mask filters, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 355 | // else we let the SkDraw call our drawPath() |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 356 | if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() || |
ethannicholas | 330bb95 | 2015-07-17 06:44:02 -0700 | [diff] [blame] | 357 | (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 358 | draw.drawPoints(mode, count, pts, paint, true); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 363 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 366 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 367 | fDrawContext->drawVertices(fClip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 368 | grPaint, |
| 369 | *draw.fMatrix, |
| 370 | gPointMode2PrimtiveType[mode], |
| 371 | SkToS32(count), |
| 372 | (SkPoint*)pts, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 373 | nullptr, |
| 374 | nullptr, |
| 375 | nullptr, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 376 | 0); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /////////////////////////////////////////////////////////////////////////////// |
| 380 | |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 381 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 382 | ASSERT_SINGLE_OWNER |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 383 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 384 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 385 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 386 | |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 387 | // A couple reasons we might need to call drawPath. |
| 388 | if (paint.getMaskFilter() || paint.getPathEffect()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 389 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 390 | path.setIsVolatile(true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 391 | path.addRect(rect); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 392 | GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(), |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 393 | fClip, path, paint, |
| 394 | *draw.fMatrix, nullptr, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 395 | draw.fRC->getBounds(), true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | |
| 399 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 400 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 401 | return; |
| 402 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 403 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 404 | GrStyle style(paint); |
| 405 | fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /////////////////////////////////////////////////////////////////////////////// |
| 409 | |
robertphillips | 9aff85a | 2016-08-05 07:51:29 -0700 | [diff] [blame] | 410 | void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 411 | const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 412 | ASSERT_SINGLE_OWNER |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 413 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 414 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 415 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 416 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 417 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 418 | return; |
| 419 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 420 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 421 | GrStyle style(paint); |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 422 | if (paint.getMaskFilter()) { |
| 423 | // try to hit the fast path for drawing filtered round rects |
| 424 | |
| 425 | SkRRect devRRect; |
robertphillips | 9aff85a | 2016-08-05 07:51:29 -0700 | [diff] [blame] | 426 | if (rrect.transform(*draw.fMatrix, &devRRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 427 | if (devRRect.allCornersCircular()) { |
| 428 | SkRect maskRect; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 429 | if (paint.getMaskFilter()->canFilterMaskGPU(devRRect, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 430 | draw.fRC->getBounds(), |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 431 | *draw.fMatrix, |
| 432 | &maskRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 433 | SkIRect finalIRect; |
| 434 | maskRect.roundOut(&finalIRect); |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 435 | if (draw.fRC->quickReject(finalIRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 436 | // clipped out |
| 437 | return; |
| 438 | } |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 439 | if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 440 | fDrawContext.get(), |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 441 | &grPaint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 442 | fClip, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 443 | *draw.fMatrix, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 444 | style.strokeRec(), |
robertphillips | 27cdd94 | 2016-08-10 16:25:25 -0700 | [diff] [blame] | 445 | rrect, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 446 | devRRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | } |
| 452 | } |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 453 | } |
| 454 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 455 | if (paint.getMaskFilter() || style.pathEffect()) { |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 456 | // The only mask filter the native rrect drawing code could've handle was taken |
| 457 | // care of above. |
| 458 | // A path effect will presumably transform this rrect into something else. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 459 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 460 | path.setIsVolatile(true); |
robertphillips | 9aff85a | 2016-08-05 07:51:29 -0700 | [diff] [blame] | 461 | path.addRRect(rrect); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 462 | GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(), |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 463 | fClip, path, paint, |
| 464 | *draw.fMatrix, nullptr, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 465 | draw.fRC->getBounds(), true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 468 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 469 | SkASSERT(!style.pathEffect()); |
robertphillips | 514450c | 2015-11-24 05:36:02 -0800 | [diff] [blame] | 470 | |
robertphillips | 9aff85a | 2016-08-05 07:51:29 -0700 | [diff] [blame] | 471 | fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rrect, style); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 472 | } |
| 473 | |
robertphillips | d770610 | 2016-02-25 09:28:08 -0800 | [diff] [blame] | 474 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 475 | void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 476 | const SkRRect& inner, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 477 | ASSERT_SINGLE_OWNER |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 478 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext); |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 479 | CHECK_SHOULD_DRAW(draw); |
| 480 | |
robertphillips | d770610 | 2016-02-25 09:28:08 -0800 | [diff] [blame] | 481 | if (outer.isEmpty()) { |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | if (inner.isEmpty()) { |
| 486 | return this->drawRRect(draw, outer, paint); |
| 487 | } |
| 488 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 489 | SkStrokeRec stroke(paint); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 490 | |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 491 | if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) { |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 492 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 493 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, |
brianosman | 1638c0d | 2016-07-25 05:12:53 -0700 | [diff] [blame] | 494 | &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 495 | return; |
| 496 | } |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 497 | |
| 498 | fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner); |
| 499 | return; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 503 | path.setIsVolatile(true); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 504 | path.addRRect(outer); |
| 505 | path.addRRect(inner); |
| 506 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 507 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 508 | GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(), |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 509 | fClip, path, paint, |
| 510 | *draw.fMatrix, nullptr, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 511 | draw.fRC->getBounds(), true); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 515 | ///////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 516 | |
robertphillips | 514450c | 2015-11-24 05:36:02 -0800 | [diff] [blame] | 517 | void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 518 | ASSERT_SINGLE_OWNER |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 519 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 520 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 521 | |
robertphillips | 514450c | 2015-11-24 05:36:02 -0800 | [diff] [blame] | 522 | // Presumably the path effect warps this to something other than an oval |
| 523 | if (paint.getPathEffect()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 524 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 525 | path.setIsVolatile(true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 526 | path.addOval(oval); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 527 | this->drawPath(draw, path, paint, nullptr, true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 528 | return; |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 529 | } |
| 530 | |
robertphillips | 514450c | 2015-11-24 05:36:02 -0800 | [diff] [blame] | 531 | if (paint.getMaskFilter()) { |
| 532 | // The RRect path can handle special case blurring |
| 533 | SkRRect rr = SkRRect::MakeOval(oval); |
| 534 | return this->drawRRect(draw, rr, paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 538 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 539 | return; |
| 540 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 541 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 542 | fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | #include "SkMaskFilter.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 546 | |
| 547 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 548 | void SkGpuDevice::drawStrokedLine(const SkPoint points[2], |
| 549 | const SkDraw& draw, |
| 550 | const SkPaint& origPaint) { |
| 551 | ASSERT_SINGLE_OWNER |
| 552 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext); |
| 553 | CHECK_SHOULD_DRAW(draw); |
| 554 | |
| 555 | // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix |
| 556 | // entry point |
| 557 | SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap()); |
| 558 | SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle()); |
| 559 | SkASSERT(!origPaint.getPathEffect()); |
| 560 | SkASSERT(!origPaint.getMaskFilter()); |
| 561 | |
| 562 | const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth(); |
| 563 | SkASSERT(halfWidth > 0); |
| 564 | |
| 565 | SkVector v = points[1] - points[0]; |
| 566 | |
| 567 | SkScalar length = SkPoint::Normalize(&v); |
| 568 | if (!length) { |
| 569 | v.fX = 1.0f; |
| 570 | v.fY = 0.0f; |
| 571 | } |
| 572 | |
| 573 | SkPaint newPaint(origPaint); |
| 574 | newPaint.setStyle(SkPaint::kFill_Style); |
| 575 | |
| 576 | SkScalar xtraLength = 0.0f; |
| 577 | if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) { |
| 578 | xtraLength = halfWidth; |
| 579 | } |
| 580 | |
| 581 | SkPoint mid = points[0] + points[1]; |
| 582 | mid.scale(0.5f); |
| 583 | |
| 584 | SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength, |
| 585 | mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength); |
| 586 | SkMatrix m; |
| 587 | m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY); |
| 588 | |
| 589 | SkMatrix local = m; |
| 590 | |
| 591 | m.postConcat(*draw.fMatrix); |
| 592 | |
| 593 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 594 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), newPaint, m, &grPaint)) { |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
| 598 | fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local); |
| 599 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 600 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 601 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
| 602 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 603 | bool pathIsMutable) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 604 | ASSERT_SINGLE_OWNER |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 605 | if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) { |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 606 | SkPoint points[2]; |
| 607 | if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 && |
| 608 | !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() && |
| 609 | draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) { |
| 610 | // Path-based stroking looks better for thin rects |
| 611 | SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth(); |
robertphillips | f2204c9 | 2016-06-02 10:57:59 -0700 | [diff] [blame] | 612 | if (strokeWidth >= 1.0f) { |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 613 | // Round capping support is currently disabled b.c. it would require |
| 614 | // a RRect batch that takes a localMatrix. |
| 615 | this->drawStrokedLine(points, draw, paint); |
| 616 | return; |
| 617 | } |
| 618 | } |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 619 | bool isClosed; |
| 620 | SkRect rect; |
| 621 | if (origSrcPath.isRect(&rect, &isClosed) && isClosed) { |
| 622 | this->drawRect(draw, rect, paint); |
| 623 | return; |
| 624 | } |
| 625 | if (origSrcPath.isOval(&rect)) { |
| 626 | this->drawOval(draw, rect, paint); |
| 627 | return; |
| 628 | } |
| 629 | SkRRect rrect; |
| 630 | if (origSrcPath.isRRect(&rrect)) { |
| 631 | this->drawRRect(draw, rrect, paint); |
| 632 | return; |
| 633 | } |
| 634 | } |
| 635 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 636 | CHECK_SHOULD_DRAW(draw); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 637 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 638 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 639 | GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(), |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 640 | fClip, origSrcPath, paint, |
| 641 | *draw.fMatrix, prePathMatrix, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 642 | draw.fRC->getBounds(), pathIsMutable); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static const int kBmpSmallTileSize = 1 << 10; |
| 646 | |
| 647 | static inline int get_tile_count(const SkIRect& srcRect, int tileSize) { |
| 648 | int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1; |
| 649 | int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1; |
| 650 | return tilesX * tilesY; |
| 651 | } |
| 652 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 653 | static int determine_tile_size(const SkIRect& src, int maxTileSize) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 654 | if (maxTileSize <= kBmpSmallTileSize) { |
| 655 | return maxTileSize; |
| 656 | } |
| 657 | |
| 658 | size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize); |
| 659 | size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize); |
| 660 | |
| 661 | maxTileTotalTileSize *= maxTileSize * maxTileSize; |
| 662 | smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize; |
| 663 | |
| 664 | if (maxTileTotalTileSize > 2 * smallTotalTileSize) { |
| 665 | return kBmpSmallTileSize; |
| 666 | } else { |
| 667 | return maxTileSize; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what |
| 672 | // pixels from the bitmap are necessary. |
robertphillips | e576874 | 2016-05-13 11:20:46 -0700 | [diff] [blame] | 673 | static void determine_clipped_src_rect(int width, int height, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 674 | const GrClip& clip, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 675 | const SkMatrix& viewMatrix, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 676 | const SkMatrix& srcToDstRect, |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 677 | const SkISize& imageSize, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 678 | const SkRect* srcRectPtr, |
| 679 | SkIRect* clippedSrcIRect) { |
robertphillips | e576874 | 2016-05-13 11:20:46 -0700 | [diff] [blame] | 680 | clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr); |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 681 | SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect); |
| 682 | if (!inv.invert(&inv)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 683 | clippedSrcIRect->setEmpty(); |
| 684 | return; |
| 685 | } |
| 686 | SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect); |
| 687 | inv.mapRect(&clippedSrcRect); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 688 | if (srcRectPtr) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 689 | if (!clippedSrcRect.intersect(*srcRectPtr)) { |
| 690 | clippedSrcIRect->setEmpty(); |
| 691 | return; |
| 692 | } |
| 693 | } |
| 694 | clippedSrcRect.roundOut(clippedSrcIRect); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 695 | SkIRect bmpBounds = SkIRect::MakeSize(imageSize); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 696 | if (!clippedSrcIRect->intersect(bmpBounds)) { |
| 697 | clippedSrcIRect->setEmpty(); |
| 698 | } |
| 699 | } |
| 700 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 701 | bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect, |
| 702 | const SkMatrix& viewMatrix, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 703 | const SkMatrix& srcToDstRect, |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 704 | const GrTextureParams& params, |
| 705 | const SkRect* srcRectPtr, |
| 706 | int maxTileSize, |
| 707 | int* tileSize, |
| 708 | SkIRect* clippedSubset) const { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 709 | ASSERT_SINGLE_OWNER |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 710 | // if it's larger than the max tile size, then we have no choice but tiling. |
| 711 | if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) { |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 712 | determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix, |
| 713 | srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 714 | *tileSize = determine_tile_size(*clippedSubset, maxTileSize); |
| 715 | return true; |
| 716 | } |
| 717 | |
bsalomon | 1a1d0b8 | 2015-10-16 07:49:42 -0700 | [diff] [blame] | 718 | // If the image would only produce 4 tiles of the smaller size, don't bother tiling it. |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 719 | const size_t area = imageRect.width() * imageRect.height(); |
| 720 | if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) { |
| 721 | return false; |
| 722 | } |
| 723 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 724 | // At this point we know we could do the draw by uploading the entire bitmap |
| 725 | // as a texture. However, if the texture would be large compared to the |
| 726 | // cache size and we don't require most of it for this draw then tile to |
| 727 | // reduce the amount of upload and cache spill. |
| 728 | |
| 729 | // assumption here is that sw bitmap size is a good proxy for its size as |
| 730 | // a texture |
| 731 | size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels |
| 732 | size_t cacheSize; |
| 733 | fContext->getResourceCacheLimits(nullptr, &cacheSize); |
| 734 | if (bmpSize < cacheSize / 2) { |
| 735 | return false; |
| 736 | } |
| 737 | |
bsalomon | 1a1d0b8 | 2015-10-16 07:49:42 -0700 | [diff] [blame] | 738 | // Figure out how much of the src we will need based on the src rect and clipping. Reject if |
| 739 | // tiling memory savings would be < 50%. |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 740 | determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix, |
| 741 | srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 742 | *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile. |
| 743 | size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) * |
| 744 | kBmpSmallTileSize * kBmpSmallTileSize; |
| 745 | |
| 746 | return usedTileBytes < 2 * bmpSize; |
| 747 | } |
| 748 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 749 | bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr, |
| 750 | SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 751 | const SkMatrix& viewMatrix, |
| 752 | const SkMatrix& srcToDstRect) const { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 753 | ASSERT_SINGLE_OWNER |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 754 | // if image is explictly texture backed then just use the texture |
| 755 | if (as_IB(image)->peekTexture()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 756 | return false; |
| 757 | } |
| 758 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 759 | GrTextureParams params; |
| 760 | bool doBicubic; |
| 761 | GrTextureParams::FilterMode textureFilterMode = |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 762 | GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 763 | |
| 764 | int tileFilterPad; |
| 765 | if (doBicubic) { |
| 766 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
| 767 | } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) { |
| 768 | tileFilterPad = 0; |
| 769 | } else { |
| 770 | tileFilterPad = 1; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 771 | } |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 772 | params.setFilterMode(textureFilterMode); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 773 | |
bsalomon | 8c07b7a | 2015-11-02 11:36:52 -0800 | [diff] [blame] | 774 | int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 775 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 776 | // these are output, which we safely ignore, as we just want to know the predicate |
| 777 | int outTileSize; |
| 778 | SkIRect outClippedSrcRect; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 779 | |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 780 | return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect, |
| 781 | params, srcRectPtr, maxTileSize, &outTileSize, |
| 782 | &outClippedSrcRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 783 | } |
| 784 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 785 | void SkGpuDevice::drawBitmap(const SkDraw& origDraw, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 786 | const SkBitmap& bitmap, |
| 787 | const SkMatrix& m, |
| 788 | const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 789 | ASSERT_SINGLE_OWNER |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 790 | CHECK_SHOULD_DRAW(origDraw); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 791 | SkMatrix viewMatrix; |
| 792 | viewMatrix.setConcat(*origDraw.fMatrix, m); |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 793 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 794 | int maxTileSize = fContext->caps()->maxTileSize(); |
| 795 | |
| 796 | // The tile code path doesn't currently support AA, so if the paint asked for aa and we could |
| 797 | // draw untiled, then we bypass checking for tiling purely for optimization reasons. |
robertphillips | 071457b | 2016-05-16 09:50:03 -0700 | [diff] [blame] | 798 | bool drawAA = !fDrawContext->isUnifiedMultisampled() && |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 799 | paint.isAntiAlias() && |
| 800 | bitmap.width() <= maxTileSize && |
| 801 | bitmap.height() <= maxTileSize; |
| 802 | |
| 803 | bool skipTileCheck = drawAA || paint.getMaskFilter(); |
| 804 | |
| 805 | if (!skipTileCheck) { |
| 806 | SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height()); |
| 807 | int tileSize; |
| 808 | SkIRect clippedSrcRect; |
| 809 | |
| 810 | GrTextureParams params; |
| 811 | bool doBicubic; |
| 812 | GrTextureParams::FilterMode textureFilterMode = |
| 813 | GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(), |
| 814 | &doBicubic); |
| 815 | |
| 816 | int tileFilterPad; |
| 817 | |
| 818 | if (doBicubic) { |
| 819 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
| 820 | } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) { |
| 821 | tileFilterPad = 0; |
| 822 | } else { |
| 823 | tileFilterPad = 1; |
| 824 | } |
| 825 | params.setFilterMode(textureFilterMode); |
| 826 | |
| 827 | int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad; |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 828 | if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, |
| 829 | SkMatrix::I(), params, &srcRect, maxTileSizeForFilter, |
| 830 | &tileSize, &clippedSrcRect)) { |
| 831 | this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect, |
| 832 | params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize, |
| 833 | doBicubic); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 834 | return; |
| 835 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 836 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 837 | GrBitmapTextureMaker maker(fContext, bitmap); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 838 | this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint, |
| 839 | viewMatrix, fClip, paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 840 | } |
| 841 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 842 | // This method outsets 'iRect' by 'outset' all around and then clamps its extents to |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 843 | // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner |
| 844 | // of 'iRect' for all possible outsets/clamps. |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 845 | static inline void clamped_outset_with_offset(SkIRect* iRect, |
| 846 | int outset, |
| 847 | SkPoint* offset, |
| 848 | const SkIRect& clamp) { |
| 849 | iRect->outset(outset, outset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 850 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 851 | int leftClampDelta = clamp.fLeft - iRect->fLeft; |
| 852 | if (leftClampDelta > 0) { |
| 853 | offset->fX -= outset - leftClampDelta; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 854 | iRect->fLeft = clamp.fLeft; |
| 855 | } else { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 856 | offset->fX -= outset; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 857 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 858 | |
| 859 | int topClampDelta = clamp.fTop - iRect->fTop; |
| 860 | if (topClampDelta > 0) { |
| 861 | offset->fY -= outset - topClampDelta; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 862 | iRect->fTop = clamp.fTop; |
| 863 | } else { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 864 | offset->fY -= outset; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | if (iRect->fRight > clamp.fRight) { |
| 868 | iRect->fRight = clamp.fRight; |
| 869 | } |
| 870 | if (iRect->fBottom > clamp.fBottom) { |
| 871 | iRect->fBottom = clamp.fBottom; |
| 872 | } |
| 873 | } |
| 874 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 875 | // Break 'bitmap' into several tiles to draw it since it has already |
| 876 | // been determined to be too large to fit in VRAM |
| 877 | void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 878 | const SkMatrix& viewMatrix, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 879 | const SkMatrix& dstMatrix, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 880 | const SkRect& srcRect, |
| 881 | const SkIRect& clippedSrcIRect, |
| 882 | const GrTextureParams& params, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 883 | const SkPaint& origPaint, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 884 | SkCanvas::SrcRectConstraint constraint, |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 885 | int tileSize, |
| 886 | bool bicubic) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 887 | ASSERT_SINGLE_OWNER |
ericrk | 369e937 | 2016-02-05 15:32:36 -0800 | [diff] [blame] | 888 | |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 889 | // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries. |
ericrk | 369e937 | 2016-02-05 15:32:36 -0800 | [diff] [blame] | 890 | SK_HISTOGRAM_BOOLEAN("DrawTiled", true); |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 891 | LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality()); |
ericrk | 369e937 | 2016-02-05 15:32:36 -0800 | [diff] [blame] | 892 | |
commit-bot@chromium.org | 9d5e3f1 | 2014-05-01 21:23:19 +0000 | [diff] [blame] | 893 | // The following pixel lock is technically redundant, but it is desirable |
| 894 | // to lock outside of the tile loop to prevent redecoding the whole image |
| 895 | // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that |
| 896 | // is larger than the limit of the discardable memory pool. |
| 897 | SkAutoLockPixels alp(bitmap); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 898 | |
| 899 | const SkPaint* paint = &origPaint; |
| 900 | SkPaint tempPaint; |
robertphillips | 071457b | 2016-05-16 09:50:03 -0700 | [diff] [blame] | 901 | if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 902 | // Drop antialiasing to avoid seams at tile boundaries. |
| 903 | tempPaint = origPaint; |
| 904 | tempPaint.setAntiAlias(false); |
| 905 | paint = &tempPaint; |
| 906 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 907 | SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect); |
| 908 | |
| 909 | int nx = bitmap.width() / tileSize; |
| 910 | int ny = bitmap.height() / tileSize; |
| 911 | for (int x = 0; x <= nx; x++) { |
| 912 | for (int y = 0; y <= ny; y++) { |
| 913 | SkRect tileR; |
| 914 | tileR.set(SkIntToScalar(x * tileSize), |
| 915 | SkIntToScalar(y * tileSize), |
| 916 | SkIntToScalar((x + 1) * tileSize), |
| 917 | SkIntToScalar((y + 1) * tileSize)); |
| 918 | |
| 919 | if (!SkRect::Intersects(tileR, clippedSrcRect)) { |
| 920 | continue; |
| 921 | } |
| 922 | |
| 923 | if (!tileR.intersect(srcRect)) { |
| 924 | continue; |
| 925 | } |
| 926 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 927 | SkIRect iTileR; |
| 928 | tileR.roundOut(&iTileR); |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 929 | SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft), |
| 930 | SkIntToScalar(iTileR.fTop)); |
| 931 | SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY, |
| 932 | tileR.width(), tileR.height()); |
| 933 | dstMatrix.mapRect(&rectToDraw); |
robertphillips | ec8bb94 | 2014-11-21 10:16:25 -0800 | [diff] [blame] | 934 | if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 935 | SkIRect iClampRect; |
| 936 | |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 937 | if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 938 | // In bleed mode we want to always expand the tile on all edges |
| 939 | // but stay within the bitmap bounds |
| 940 | iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 941 | } else { |
| 942 | // In texture-domain/clamp mode we only want to expand the |
| 943 | // tile on edges interior to "srcRect" (i.e., we want to |
| 944 | // not bleed across the original clamped edges) |
| 945 | srcRect.roundOut(&iClampRect); |
| 946 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 947 | int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1; |
| 948 | clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 949 | } |
| 950 | |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 951 | SkBitmap tmpB; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 952 | if (bitmap.extractSubset(&tmpB, iTileR)) { |
| 953 | // now offset it to make it "local" to our tmp bitmap |
| 954 | tileR.offset(-offset.fX, -offset.fY); |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 955 | GrTextureParams paramsTemp = params; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 956 | // de-optimized this determination |
| 957 | bool needsTextureDomain = true; |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 958 | this->drawBitmapTile(tmpB, |
| 959 | viewMatrix, |
| 960 | rectToDraw, |
| 961 | tileR, |
| 962 | paramsTemp, |
| 963 | *paint, |
| 964 | constraint, |
| 965 | bicubic, |
| 966 | needsTextureDomain); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 972 | void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap, |
| 973 | const SkMatrix& viewMatrix, |
| 974 | const SkRect& dstRect, |
| 975 | const SkRect& srcRect, |
| 976 | const GrTextureParams& params, |
| 977 | const SkPaint& paint, |
| 978 | SkCanvas::SrcRectConstraint constraint, |
| 979 | bool bicubic, |
| 980 | bool needsTextureDomain) { |
bsalomon | 9c58654 | 2015-11-02 12:33:21 -0800 | [diff] [blame] | 981 | // We should have already handled bitmaps larger than the max texture size. |
| 982 | SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() && |
| 983 | bitmap.height() <= fContext->caps()->maxTextureSize()); |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 984 | // We should be respecting the max tile size by the time we get here. |
| 985 | SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() && |
| 986 | bitmap.height() <= fContext->caps()->maxTileSize()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 987 | |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 988 | sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap, params, |
| 989 | fDrawContext->sourceGammaTreatment()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 990 | if (nullptr == texture) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
brianosman | 500bb3e | 2016-07-22 10:33:07 -0700 | [diff] [blame] | 994 | sk_sp<GrColorSpaceXform> colorSpaceXform = |
| 995 | GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace()); |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 996 | |
| 997 | SkScalar iw = 1.f / texture->width(); |
| 998 | SkScalar ih = 1.f / texture->height(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 999 | |
egdaniel | 79da63f | 2015-10-09 10:55:16 -0700 | [diff] [blame] | 1000 | SkMatrix texMatrix; |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1001 | // Compute a matrix that maps the rect we will draw to the src rect. |
| 1002 | texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kStart_ScaleToFit); |
| 1003 | texMatrix.postScale(iw, ih); |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 1004 | |
| 1005 | // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring |
| 1006 | // the rest from the SkPaint. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1007 | sk_sp<GrFragmentProcessor> fp; |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 1008 | |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1009 | if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1010 | // Use a constrained texture domain to avoid color bleeding |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1011 | SkRect domain; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1012 | if (srcRect.width() > SK_Scalar1) { |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1013 | domain.fLeft = (srcRect.fLeft + 0.5f) * iw; |
| 1014 | domain.fRight = (srcRect.fRight - 0.5f) * iw; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1015 | } else { |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1016 | domain.fLeft = domain.fRight = srcRect.centerX() * iw; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1017 | } |
| 1018 | if (srcRect.height() > SK_Scalar1) { |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1019 | domain.fTop = (srcRect.fTop + 0.5f) * ih; |
| 1020 | domain.fBottom = (srcRect.fBottom - 0.5f) * ih; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1021 | } else { |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1022 | domain.fTop = domain.fBottom = srcRect.centerY() * ih; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1023 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1024 | if (bicubic) { |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1025 | fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1026 | domain); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1027 | } else { |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1028 | fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1029 | domain, GrTextureDomain::kClamp_Mode, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 1030 | params.filterMode()); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1031 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1032 | } else if (bicubic) { |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 1033 | SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode()); |
| 1034 | SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() }; |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1035 | fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1036 | } else { |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1037 | fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1040 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1041 | if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, viewMatrix, |
| 1042 | std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(), |
| 1043 | &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1044 | return; |
| 1045 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1046 | |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1047 | fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1050 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1051 | int left, int top, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1052 | ASSERT_SINGLE_OWNER |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1053 | CHECK_SHOULD_DRAW(draw); |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1054 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1055 | |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1056 | if (fContext->abandoned()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1060 | sk_sp<GrTexture> texture; |
| 1061 | { |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1062 | SkAutoLockPixels alp(bitmap, true); |
| 1063 | if (!bitmap.readyToDraw()) { |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | // draw sprite neither filters nor tiles. |
| 1068 | texture.reset(GrRefCachedBitmapTexture(fContext, bitmap, |
| 1069 | GrTextureParams::ClampNoFilter(), |
| 1070 | SkSourceGammaTreatment::kRespect)); |
| 1071 | if (!texture) { |
| 1072 | return; |
| 1073 | } |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1074 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1075 | |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1076 | SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX, |
| 1077 | bitmap.pixelRefOrigin().fY, |
| 1078 | bitmap.width(), |
| 1079 | bitmap.height()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1080 | |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1081 | sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect, |
| 1082 | bitmap.getGenerationID(), |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 1083 | std::move(texture), |
| 1084 | sk_ref_sp(bitmap.colorSpace()), |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1085 | &this->surfaceProps())); |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1086 | |
| 1087 | this->drawSpecial(draw, srcImg.get(), left, top, paint); |
| 1088 | } |
| 1089 | |
| 1090 | |
| 1091 | void SkGpuDevice::drawSpecial(const SkDraw& draw, |
| 1092 | SkSpecialImage* special1, |
| 1093 | int left, int top, |
| 1094 | const SkPaint& paint) { |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1095 | ASSERT_SINGLE_OWNER |
| 1096 | CHECK_SHOULD_DRAW(draw); |
| 1097 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext); |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1098 | |
| 1099 | SkIPoint offset = { 0, 0 }; |
| 1100 | |
| 1101 | sk_sp<SkSpecialImage> result; |
| 1102 | if (paint.getImageFilter()) { |
| 1103 | result = this->filterTexture(draw, special1, left, top, |
| 1104 | &offset, |
| 1105 | paint.getImageFilter()); |
| 1106 | if (!result) { |
| 1107 | return; |
| 1108 | } |
| 1109 | } else { |
| 1110 | result = sk_ref_sp(special1); |
| 1111 | } |
| 1112 | |
| 1113 | SkASSERT(result->isTextureBacked()); |
| 1114 | sk_sp<GrTexture> texture = result->asTextureRef(fContext); |
| 1115 | |
| 1116 | SkPaint tmpUnfiltered(paint); |
| 1117 | tmpUnfiltered.setImageFilter(nullptr); |
| 1118 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1119 | GrPaint grPaint; |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 1120 | sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), nullptr, |
| 1121 | SkMatrix::I())); |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1122 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1123 | fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1124 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1125 | fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1126 | } |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1127 | if (!SkPaintToGrPaintReplaceShader(this->context(), fDrawContext.get(), tmpUnfiltered, |
| 1128 | std::move(fp), &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1129 | return; |
| 1130 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1131 | |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1132 | const SkIRect& subset = result->subset(); |
| 1133 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 1134 | fDrawContext->fillRectToRect(fClip, |
| 1135 | grPaint, |
| 1136 | SkMatrix::I(), |
robertphillips | 970587b | 2016-07-14 14:12:55 -0700 | [diff] [blame] | 1137 | SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, |
| 1138 | subset.width(), subset.height())), |
| 1139 | SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(), |
| 1140 | SkIntToScalar(subset.fTop) / texture->height(), |
| 1141 | SkIntToScalar(subset.width()) / texture->width(), |
| 1142 | SkIntToScalar(subset.height()) / texture->height())); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1145 | void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
| 1146 | const SkRect* src, const SkRect& origDst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 1147 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1148 | ASSERT_SINGLE_OWNER |
lsalzman | 973ed24 | 2016-01-14 13:06:41 -0800 | [diff] [blame] | 1149 | CHECK_SHOULD_DRAW(draw); |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1150 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1151 | // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must |
| 1152 | // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which |
| 1153 | // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds |
| 1154 | // then we use the src-to-dst mapping to compute a new clipped dst rect. |
| 1155 | const SkRect* dst = &origDst; |
| 1156 | const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1157 | // Compute matrix from the two rectangles |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1158 | if (!src) { |
| 1159 | src = &bmpBounds; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1160 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1161 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1162 | SkMatrix srcToDstMatrix; |
| 1163 | if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) { |
| 1164 | return; |
| 1165 | } |
| 1166 | SkRect tmpSrc, tmpDst; |
| 1167 | if (src != &bmpBounds) { |
| 1168 | if (!bmpBounds.contains(*src)) { |
| 1169 | tmpSrc = *src; |
| 1170 | if (!tmpSrc.intersect(bmpBounds)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1171 | return; // nothing to draw |
| 1172 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1173 | src = &tmpSrc; |
| 1174 | srcToDstMatrix.mapRect(&tmpDst, *src); |
| 1175 | dst = &tmpDst; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1179 | int maxTileSize = fContext->caps()->maxTileSize(); |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1180 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1181 | // The tile code path doesn't currently support AA, so if the paint asked for aa and we could |
| 1182 | // draw untiled, then we bypass checking for tiling purely for optimization reasons. |
robertphillips | 071457b | 2016-05-16 09:50:03 -0700 | [diff] [blame] | 1183 | bool drawAA = !fDrawContext->isUnifiedMultisampled() && |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1184 | paint.isAntiAlias() && |
| 1185 | bitmap.width() <= maxTileSize && |
| 1186 | bitmap.height() <= maxTileSize; |
| 1187 | |
| 1188 | bool skipTileCheck = drawAA || paint.getMaskFilter(); |
| 1189 | |
| 1190 | if (!skipTileCheck) { |
| 1191 | int tileSize; |
| 1192 | SkIRect clippedSrcRect; |
| 1193 | |
| 1194 | GrTextureParams params; |
| 1195 | bool doBicubic; |
| 1196 | GrTextureParams::FilterMode textureFilterMode = |
| 1197 | GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix, |
| 1198 | &doBicubic); |
| 1199 | |
| 1200 | int tileFilterPad; |
| 1201 | |
| 1202 | if (doBicubic) { |
| 1203 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
| 1204 | } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) { |
| 1205 | tileFilterPad = 0; |
| 1206 | } else { |
| 1207 | tileFilterPad = 1; |
| 1208 | } |
| 1209 | params.setFilterMode(textureFilterMode); |
| 1210 | |
| 1211 | int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad; |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1212 | if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix, |
| 1213 | srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize, |
| 1214 | &clippedSrcRect)) { |
| 1215 | this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect, |
| 1216 | params, paint, constraint, tileSize, doBicubic); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1217 | return; |
| 1218 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1219 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 1220 | GrBitmapTextureMaker maker(fContext, bitmap); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1221 | this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1224 | sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) { |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1225 | SkAutoLockPixels alp(bitmap, true); |
| 1226 | if (!bitmap.readyToDraw()) { |
| 1227 | return nullptr; |
| 1228 | } |
| 1229 | |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1230 | sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap, |
| 1231 | GrTextureParams::ClampNoFilter(), |
| 1232 | SkSourceGammaTreatment::kRespect); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1233 | if (!texture) { |
| 1234 | return nullptr; |
| 1235 | } |
| 1236 | |
| 1237 | return SkSpecialImage::MakeFromGpu(bitmap.bounds(), |
| 1238 | bitmap.getGenerationID(), |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1239 | texture, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 1240 | sk_ref_sp(bitmap.colorSpace()), |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1241 | &this->surfaceProps()); |
| 1242 | } |
| 1243 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1244 | sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) { |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1245 | SkPixmap pm; |
| 1246 | if (image->isTextureBacked()) { |
| 1247 | GrTexture* texture = as_IB(image)->peekTexture(); |
| 1248 | |
| 1249 | return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()), |
| 1250 | image->uniqueID(), |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 1251 | sk_ref_sp(texture), |
| 1252 | sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()), |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1253 | &this->surfaceProps()); |
| 1254 | } else if (image->peekPixels(&pm)) { |
| 1255 | SkBitmap bm; |
| 1256 | |
| 1257 | bm.installPixels(pm); |
| 1258 | return this->makeSpecial(bm); |
| 1259 | } else { |
| 1260 | return nullptr; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() { |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1265 | sk_sp<GrTexture> texture(this->accessDrawContext()->asTexture()); |
| 1266 | if (!texture) { |
robertphillips | 04d6218 | 2016-07-15 12:21:33 -0700 | [diff] [blame] | 1267 | // When the device doesn't have a texture, we create a temporary texture. |
| 1268 | // TODO: we should actually only copy the portion of the source needed to apply the image |
| 1269 | // filter |
| 1270 | texture.reset(fContext->textureProvider()->createTexture(this->accessDrawContext()->desc(), |
| 1271 | SkBudgeted::kYes)); |
| 1272 | if (!texture) { |
| 1273 | return nullptr; |
| 1274 | } |
| 1275 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1276 | if (!fContext->copySurface(texture.get(), this->accessDrawContext()->accessRenderTarget())){ |
robertphillips | 04d6218 | 2016-07-15 12:21:33 -0700 | [diff] [blame] | 1277 | return nullptr; |
| 1278 | } |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | const SkImageInfo ii = this->imageInfo(); |
| 1282 | const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height()); |
| 1283 | |
| 1284 | return SkSpecialImage::MakeFromGpu(srcRect, |
| 1285 | kNeedNewImageUniqueID_SpecialImage, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 1286 | std::move(texture), |
| 1287 | sk_ref_sp(ii.colorSpace()), |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1288 | &this->surfaceProps()); |
| 1289 | } |
| 1290 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1291 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1292 | int left, int top, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 1293 | SkASSERT(!paint.getImageFilter()); |
| 1294 | |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1295 | ASSERT_SINGLE_OWNER |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1296 | // clear of the source device must occur before CHECK_SHOULD_DRAW |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1297 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext); |
kkinnunen | 2e4414e | 2015-02-19 07:20:40 -0800 | [diff] [blame] | 1298 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1299 | // drawDevice is defined to be in device coords. |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1300 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1301 | |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1302 | SkGpuDevice* dev = static_cast<SkGpuDevice*>(device); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1303 | sk_sp<SkSpecialImage> srcImg(dev->snapSpecial()); |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1304 | if (!srcImg) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | |
robertphillips | 1b5f968 | 2016-07-15 08:01:12 -0700 | [diff] [blame] | 1308 | this->drawSpecial(draw, srcImg.get(), left, top, paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1311 | void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y, |
| 1312 | const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1313 | ASSERT_SINGLE_OWNER |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1314 | SkMatrix viewMatrix = *draw.fMatrix; |
| 1315 | viewMatrix.preTranslate(x, y); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 1316 | uint32_t pinnedUniqueID; |
| 1317 | if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 1318 | CHECK_SHOULD_DRAW(draw); |
brianosman | 5814b8a | 2016-08-18 06:43:03 -0700 | [diff] [blame^] | 1319 | GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID, |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 1320 | as_IB(image)->onImageInfo().colorSpace()); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1321 | this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint, |
| 1322 | viewMatrix, fClip, paint); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 1323 | return; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1324 | } else { |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1325 | SkBitmap bm; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1326 | if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint, |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1327 | paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1328 | // only support tiling as bitmap at the moment, so force raster-version |
| 1329 | if (!as_IB(image)->getROPixels(&bm)) { |
| 1330 | return; |
| 1331 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1332 | this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint); |
| 1333 | } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) { |
| 1334 | CHECK_SHOULD_DRAW(draw); |
| 1335 | GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1336 | this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint, |
| 1337 | viewMatrix, fClip, paint); |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1338 | } else if (as_IB(image)->getROPixels(&bm)) { |
| 1339 | this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1340 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1345 | const SkRect& dst, const SkPaint& paint, |
| 1346 | SkCanvas::SrcRectConstraint constraint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1347 | ASSERT_SINGLE_OWNER |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 1348 | uint32_t pinnedUniqueID; |
| 1349 | if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 1350 | CHECK_SHOULD_DRAW(draw); |
brianosman | 5814b8a | 2016-08-18 06:43:03 -0700 | [diff] [blame^] | 1351 | GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID, |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 1352 | as_IB(image)->onImageInfo().colorSpace()); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1353 | this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 1354 | return; |
| 1355 | } |
| 1356 | SkBitmap bm; |
bsalomon | e553b64 | 2016-08-17 09:02:09 -0700 | [diff] [blame] | 1357 | SkMatrix srcToDstRect; |
| 1358 | srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())), |
| 1359 | dst, SkMatrix::kFill_ScaleToFit); |
| 1360 | if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix, |
| 1361 | srcToDstRect)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 1362 | // only support tiling as bitmap at the moment, so force raster-version |
| 1363 | if (!as_IB(image)->getROPixels(&bm)) { |
| 1364 | return; |
| 1365 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1366 | this->drawBitmapRect(draw, bm, src, dst, paint, constraint); |
| 1367 | } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) { |
| 1368 | CHECK_SHOULD_DRAW(draw); |
| 1369 | GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1370 | this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint); |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1371 | } else if (as_IB(image)->getROPixels(&bm)) { |
| 1372 | this->drawBitmapRect(draw, bm, src, dst, paint, constraint); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1373 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1376 | void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer, |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1377 | const SkIRect& center, const SkRect& dst, const SkPaint& paint) { |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1378 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1379 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1380 | CHECK_SHOULD_DRAW(draw); |
| 1381 | |
joshualitt | edb3644 | 2015-11-19 14:29:30 -0800 | [diff] [blame] | 1382 | bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() || |
robertphillips | 071457b | 2016-05-16 09:50:03 -0700 | [diff] [blame] | 1383 | fDrawContext->isUnifiedMultisampled(); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1384 | bool doBicubic; |
| 1385 | GrTextureParams::FilterMode textureFilterMode = |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1386 | GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(), |
| 1387 | &doBicubic); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1388 | if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) { |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 1389 | SkLatticeIter iter(producer->width(), producer->height(), center, dst); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1390 | |
| 1391 | SkRect srcR, dstR; |
| 1392 | while (iter.next(&srcR, &dstR)) { |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1393 | this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint, |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1394 | *draw.fMatrix, fClip, paint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1395 | } |
| 1396 | return; |
| 1397 | } |
| 1398 | |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1399 | static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1400 | sk_sp<GrFragmentProcessor> fp( |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1401 | producer->createFragmentProcessor(SkMatrix::I(), |
| 1402 | SkRect::MakeIWH(producer->width(), producer->height()), |
| 1403 | GrTextureProducer::kNo_FilterConstraint, true, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1404 | &kMode, fDrawContext->getColorSpace(), |
| 1405 | fDrawContext->sourceGammaTreatment())); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1406 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1407 | if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, *draw.fMatrix, |
| 1408 | std::move(fp), producer->isAlphaOnly(), &grPaint)) { |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1409 | return; |
| 1410 | } |
| 1411 | |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1412 | fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(), |
| 1413 | producer->height(), center, dst); |
| 1414 | } |
| 1415 | |
| 1416 | void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image, |
| 1417 | const SkIRect& center, const SkRect& dst, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1418 | ASSERT_SINGLE_OWNER |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 1419 | uint32_t pinnedUniqueID; |
| 1420 | if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) { |
| 1421 | CHECK_SHOULD_DRAW(draw); |
brianosman | 5814b8a | 2016-08-18 06:43:03 -0700 | [diff] [blame^] | 1422 | GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID, |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 1423 | as_IB(image)->onImageInfo().colorSpace()); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1424 | this->drawProducerNine(draw, &adjuster, center, dst, paint); |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1425 | } else { |
| 1426 | SkBitmap bm; |
| 1427 | if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) { |
| 1428 | GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint); |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 1429 | this->drawProducerNine(draw, &maker, center, dst, paint); |
bsalomon | 2bbd0c6 | 2015-12-09 12:50:56 -0800 | [diff] [blame] | 1430 | } else if (as_IB(image)->getROPixels(&bm)) { |
| 1431 | this->drawBitmapNine(draw, bm, center, dst, paint); |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center, |
| 1437 | const SkRect& dst, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1438 | ASSERT_SINGLE_OWNER |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 1439 | GrBitmapTextureMaker maker(fContext, bitmap); |
| 1440 | this->drawProducerNine(draw, &maker, center, dst, paint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1441 | } |
| 1442 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1443 | /////////////////////////////////////////////////////////////////////////////// |
| 1444 | |
| 1445 | // must be in SkCanvas::VertexMode order |
| 1446 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1447 | kTriangles_GrPrimitiveType, |
| 1448 | kTriangleStrip_GrPrimitiveType, |
| 1449 | kTriangleFan_GrPrimitiveType, |
| 1450 | }; |
| 1451 | |
| 1452 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1453 | int vertexCount, const SkPoint vertices[], |
| 1454 | const SkPoint texs[], const SkColor colors[], |
| 1455 | SkXfermode* xmode, |
| 1456 | const uint16_t indices[], int indexCount, |
| 1457 | const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1458 | ASSERT_SINGLE_OWNER |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1459 | CHECK_SHOULD_DRAW(draw); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1460 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext); |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1461 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1462 | // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color. |
| 1463 | if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1464 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1465 | texs = nullptr; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1466 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1467 | SkPaint copy(paint); |
| 1468 | copy.setStyle(SkPaint::kStroke_Style); |
| 1469 | copy.setStrokeWidth(0); |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1470 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1471 | GrPaint grPaint; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1472 | // we ignore the shader if texs is null. |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1473 | if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), copy, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1474 | return; |
| 1475 | } |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1476 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1477 | int triangleCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1478 | int n = (nullptr == indices) ? vertexCount : indexCount; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1479 | switch (vmode) { |
| 1480 | case SkCanvas::kTriangles_VertexMode: |
bsalomon | a098dd4 | 2014-08-06 11:01:44 -0700 | [diff] [blame] | 1481 | triangleCount = n / 3; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1482 | break; |
| 1483 | case SkCanvas::kTriangleStrip_VertexMode: |
| 1484 | case SkCanvas::kTriangleFan_VertexMode: |
bsalomon | a098dd4 | 2014-08-06 11:01:44 -0700 | [diff] [blame] | 1485 | triangleCount = n - 2; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1486 | break; |
| 1487 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1488 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1489 | VertState state(vertexCount, indices, indexCount); |
| 1490 | VertState::Proc vertProc = state.chooseProc(vmode); |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1491 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1492 | //number of indices for lines per triangle with kLines |
| 1493 | indexCount = triangleCount * 6; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1494 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1495 | SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]); |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1496 | int i = 0; |
| 1497 | while (vertProc(&state)) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1498 | lineIndices[i] = state.f0; |
| 1499 | lineIndices[i + 1] = state.f1; |
| 1500 | lineIndices[i + 2] = state.f1; |
| 1501 | lineIndices[i + 3] = state.f2; |
| 1502 | lineIndices[i + 4] = state.f2; |
| 1503 | lineIndices[i + 5] = state.f0; |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1504 | i += 6; |
| 1505 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1506 | fDrawContext->drawVertices(fClip, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1507 | grPaint, |
| 1508 | *draw.fMatrix, |
| 1509 | kLines_GrPrimitiveType, |
| 1510 | vertexCount, |
| 1511 | vertices, |
| 1512 | texs, |
| 1513 | colors, |
| 1514 | lineIndices.get(), |
| 1515 | indexCount); |
| 1516 | return; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1519 | GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode]; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1520 | |
| 1521 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1522 | if (colors) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1523 | // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after |
| 1524 | // interpolation. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1525 | convertedColors.reset(vertexCount); |
| 1526 | for (int i = 0; i < vertexCount; ++i) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1527 | convertedColors[i] = SkColorToPremulGrColor(colors[i]); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1528 | } |
| 1529 | colors = convertedColors.get(); |
| 1530 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1531 | GrPaint grPaint; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1532 | if (texs && paint.getShader()) { |
| 1533 | if (colors) { |
| 1534 | // When there are texs and colors the shader and colors are combined using xmode. A null |
| 1535 | // xmode is defined to mean modulate. |
| 1536 | SkXfermode::Mode colorMode; |
| 1537 | if (xmode) { |
| 1538 | if (!xmode->asMode(&colorMode)) { |
| 1539 | return; |
| 1540 | } |
| 1541 | } else { |
| 1542 | colorMode = SkXfermode::kModulate_Mode; |
| 1543 | } |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1544 | if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), paint, |
| 1545 | *draw.fMatrix, colorMode, false, &grPaint)) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1546 | return; |
| 1547 | } |
| 1548 | } else { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1549 | // We have a shader, but no colors to blend it against. |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1550 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, |
| 1551 | &grPaint)) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1552 | return; |
| 1553 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1554 | } |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1555 | } else { |
| 1556 | if (colors) { |
| 1557 | // We have colors, but either have no shader or no texture coords (which implies that |
| 1558 | // we should ignore the shader). |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1559 | if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fDrawContext.get(), paint, |
| 1560 | &grPaint)) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1561 | return; |
| 1562 | } |
| 1563 | } else { |
| 1564 | // No colors and no shaders. Just draw with the paint color. |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1565 | if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), paint, &grPaint)) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 1566 | return; |
| 1567 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1568 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1571 | fDrawContext->drawVertices(fClip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1572 | grPaint, |
| 1573 | *draw.fMatrix, |
| 1574 | primType, |
| 1575 | vertexCount, |
| 1576 | vertices, |
| 1577 | texs, |
| 1578 | colors, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1579 | indices, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1580 | indexCount); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /////////////////////////////////////////////////////////////////////////////// |
| 1584 | |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 1585 | void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[], |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 1586 | const SkRect texRect[], const SkColor colors[], int count, |
| 1587 | SkXfermode::Mode mode, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1588 | ASSERT_SINGLE_OWNER |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 1589 | if (paint.isAntiAlias()) { |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 1590 | this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint); |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 1591 | return; |
| 1592 | } |
| 1593 | |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 1594 | CHECK_SHOULD_DRAW(draw); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1595 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext); |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1596 | |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 1597 | SkPaint p(paint); |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 1598 | p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)); |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 1599 | |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 1600 | GrPaint grPaint; |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 1601 | if (colors) { |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1602 | if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), p, *draw.fMatrix, |
| 1603 | mode, true, &grPaint)) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1604 | return; |
| 1605 | } |
| 1606 | } else { |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1607 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), p, *draw.fMatrix, &grPaint)) { |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 1608 | return; |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 1609 | } |
| 1610 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1611 | |
| 1612 | SkDEBUGCODE(this->validate();) |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1613 | fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors); |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /////////////////////////////////////////////////////////////////////////////// |
| 1617 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1618 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1619 | size_t byteLength, SkScalar x, SkScalar y, |
| 1620 | const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1621 | ASSERT_SINGLE_OWNER |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1622 | CHECK_SHOULD_DRAW(draw); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1623 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1624 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1625 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1626 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1627 | return; |
| 1628 | } |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1629 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1630 | SkDEBUGCODE(this->validate();) |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1631 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1632 | fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1633 | (const char *)text, byteLength, x, y, draw.fRC->getBounds()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 1636 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength, |
| 1637 | const SkScalar pos[], int scalarsPerPos, |
| 1638 | const SkPoint& offset, const SkPaint& paint) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1639 | ASSERT_SINGLE_OWNER |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1640 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1641 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1642 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1643 | GrPaint grPaint; |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 1644 | if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1645 | return; |
| 1646 | } |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1647 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1648 | SkDEBUGCODE(this->validate();) |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1649 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1650 | fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 1651 | (const char *)text, byteLength, pos, scalarsPerPos, offset, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1652 | draw.fRC->getBounds()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 1655 | void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 1656 | const SkPaint& paint, SkDrawFilter* drawFilter) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1657 | ASSERT_SINGLE_OWNER |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 1658 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext); |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 1659 | CHECK_SHOULD_DRAW(draw); |
| 1660 | |
| 1661 | SkDEBUGCODE(this->validate();) |
| 1662 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1663 | fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix, |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1664 | blob, x, y, drawFilter, draw.fRC->getBounds()); |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1667 | /////////////////////////////////////////////////////////////////////////////// |
| 1668 | |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1669 | bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const { |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 1670 | return GrTextUtils::ShouldDisableLCD(paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | void SkGpuDevice::flush() { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1674 | ASSERT_SINGLE_OWNER |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 1675 | |
robertphillips | 8c523e0 | 2016-07-26 07:41:00 -0700 | [diff] [blame] | 1676 | fDrawContext->prepareForExternalIO(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /////////////////////////////////////////////////////////////////////////////// |
| 1680 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 1681 | SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1682 | ASSERT_SINGLE_OWNER |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1683 | |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 1684 | SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1685 | |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 1686 | // layers are never drawn in repeat modes, so we can request an approx |
hcm | 4396fa5 | 2014-10-27 21:43:30 -0700 | [diff] [blame] | 1687 | // match and ignore any padding. |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 1688 | SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox |
| 1689 | : SkBackingFit::kExact; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1690 | |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 1691 | sk_sp<GrDrawContext> dc(fContext->makeDrawContext(fit, |
| 1692 | cinfo.fInfo.width(), cinfo.fInfo.height(), |
| 1693 | fDrawContext->config(), |
| 1694 | sk_ref_sp(fDrawContext->getColorSpace()), |
| 1695 | fDrawContext->desc().fSampleCnt, |
| 1696 | kDefault_GrSurfaceOrigin, |
| 1697 | &props)); |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 1698 | if (!dc) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1699 | SkErrorInternals::SetError( kInternalError_SkError, |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1700 | "---- failed to create gpu device texture [%d %d]\n", |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1701 | cinfo.fInfo.width(), cinfo.fInfo.height()); |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 1702 | return nullptr; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1703 | } |
robertphillips | ca6eafc | 2016-05-17 09:57:46 -0700 | [diff] [blame] | 1704 | |
| 1705 | // Skia's convention is to only clear a device if it is non-opaque. |
| 1706 | InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents; |
| 1707 | |
| 1708 | return SkGpuDevice::Make(std::move(dc), |
| 1709 | cinfo.fInfo.width(), cinfo.fInfo.height(), |
| 1710 | init).release(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1713 | sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1714 | ASSERT_SINGLE_OWNER |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1715 | // TODO: Change the signature of newSurface to take a budgeted parameter. |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 1716 | static const SkBudgeted kBudgeted = SkBudgeted::kNo; |
robertphillips | e576874 | 2016-05-13 11:20:46 -0700 | [diff] [blame] | 1717 | return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt, |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 1718 | fDrawContext->origin(), &props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 1721 | SkImageFilterCache* SkGpuDevice::getImageFilterCache() { |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 1722 | ASSERT_SINGLE_OWNER |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1723 | // We always return a transient cache, so it is freed after each |
| 1724 | // filter traversal. |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 1725 | return SkImageFilterCache::Create(kDefaultImageFilterCacheSize); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1726 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 1727 | |
| 1728 | #endif |