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