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