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