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