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