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