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