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