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