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