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