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