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