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