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