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