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