| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkGpuDevice.h" | 8 #include "SkGpuDevice.h" |
| 9 | 9 |
| 10 #include "effects/GrBicubicEffect.h" | 10 #include "effects/GrBicubicEffect.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 } \ | 70 } \ |
| 71 } while (false) \ | 71 } while (false) \ |
| 72 | 72 |
| 73 /////////////////////////////////////////////////////////////////////////////// | 73 /////////////////////////////////////////////////////////////////////////////// |
| 74 | 74 |
| 75 #define CHECK_FOR_ANNOTATION(paint) \ | 75 #define CHECK_FOR_ANNOTATION(paint) \ |
| 76 do { if (paint.getAnnotation()) { return; } } while (0) | 76 do { if (paint.getAnnotation()) { return; } } while (0) |
| 77 | 77 |
| 78 /////////////////////////////////////////////////////////////////////////////// | 78 /////////////////////////////////////////////////////////////////////////////// |
| 79 | 79 |
| 80 // Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed
this |
| 81 // just accesses the backing GrTexture. Otherwise, it creates a cached texture |
| 82 // representation and releases it in the destructor. |
| 83 class AutoBitmapTexture : public SkNoncopyable { |
| 84 public: |
| 85 AutoBitmapTexture() {} |
| 80 | 86 |
| 81 class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable { | 87 AutoBitmapTexture(GrContext* context, |
| 82 public: | 88 const SkBitmap& bitmap, |
| 83 SkAutoCachedTexture() | 89 const GrTextureParams* params, |
| 84 : fDevice(NULL) | 90 GrTexture** texture) { |
| 85 , fTexture(NULL) { | 91 SkASSERT(texture); |
| 92 *texture = this->set(context, bitmap, params); |
| 86 } | 93 } |
| 87 | 94 |
| 88 SkAutoCachedTexture(SkGpuDevice* device, | 95 GrTexture* set(GrContext* context, |
| 89 const SkBitmap& bitmap, | 96 const SkBitmap& bitmap, |
| 90 const GrTextureParams* params, | 97 const GrTextureParams* params) { |
| 91 GrTexture** texture) | 98 // Either get the texture directly from the bitmap, or else use the cach
e and |
| 92 : fDevice(NULL) | 99 // remember to unref it. |
| 93 , fTexture(NULL) { | 100 if (GrTexture* bmpTexture = bitmap.getTexture()) { |
| 94 SkASSERT(texture); | 101 fTexture.reset(NULL); |
| 95 *texture = this->set(device, bitmap, params); | 102 return bmpTexture; |
| 96 } | 103 } else { |
| 97 | 104 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params)); |
| 98 ~SkAutoCachedTexture() { | 105 return fTexture.get(); |
| 99 if (fTexture) { | |
| 100 GrUnlockAndUnrefCachedBitmapTexture(fTexture); | |
| 101 } | 106 } |
| 102 } | 107 } |
| 103 | 108 |
| 104 GrTexture* set(SkGpuDevice* device, | |
| 105 const SkBitmap& bitmap, | |
| 106 const GrTextureParams* params) { | |
| 107 if (fTexture) { | |
| 108 GrUnlockAndUnrefCachedBitmapTexture(fTexture); | |
| 109 fTexture = NULL; | |
| 110 } | |
| 111 fDevice = device; | |
| 112 GrTexture* result = (GrTexture*)bitmap.getTexture(); | |
| 113 if (NULL == result) { | |
| 114 // Cannot return the native texture so look it up in our cache | |
| 115 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap
, params); | |
| 116 result = fTexture; | |
| 117 } | |
| 118 return result; | |
| 119 } | |
| 120 | |
| 121 private: | 109 private: |
| 122 SkGpuDevice* fDevice; | 110 SkAutoTUnref<GrTexture> fTexture; |
| 123 GrTexture* fTexture; | |
| 124 }; | 111 }; |
| 125 | 112 |
| 126 /////////////////////////////////////////////////////////////////////////////// | 113 /////////////////////////////////////////////////////////////////////////////// |
| 127 | 114 |
| 128 struct GrSkDrawProcs : public SkDrawProcs { | 115 struct GrSkDrawProcs : public SkDrawProcs { |
| 129 public: | 116 public: |
| 130 GrContext* fContext; | 117 GrContext* fContext; |
| 131 GrTextContext* fTextContext; | 118 GrTextContext* fTextContext; |
| 132 GrFontScaler* fFontScaler; // cached in the skia glyphcache | 119 GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 133 }; | 120 }; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 146 | 133 |
| 147 fDrawProcs = NULL; | 134 fDrawProcs = NULL; |
| 148 | 135 |
| 149 fContext = SkRef(surface->getContext()); | 136 fContext = SkRef(surface->getContext()); |
| 150 | 137 |
| 151 fNeedClear = flags & kNeedClear_Flag; | 138 fNeedClear = flags & kNeedClear_Flag; |
| 152 | 139 |
| 153 fRenderTarget = SkRef(surface->asRenderTarget()); | 140 fRenderTarget = SkRef(surface->asRenderTarget()); |
| 154 | 141 |
| 155 SkImageInfo info = surface->surfacePriv().info(); | 142 SkImageInfo info = surface->surfacePriv().info(); |
| 156 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, | 143 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface)); |
| 157 (info, surface, SkToBool(flags & kCached_Flag)))
; | |
| 158 fLegacyBitmap.setInfo(info); | 144 fLegacyBitmap.setInfo(info); |
| 159 fLegacyBitmap.setPixelRef(pr)->unref(); | 145 fLegacyBitmap.setPixelRef(pr)->unref(); |
| 160 | 146 |
| 161 this->setPixelGeometry(props.pixelGeometry()); | 147 this->setPixelGeometry(props.pixelGeometry()); |
| 162 | 148 |
| 163 bool useDFFonts = !!(flags & kDFFonts_Flag); | 149 bool useDFFonts = !!(flags & kDFFonts_Flag); |
| 164 fMainTextContext = fContext->createTextContext(fRenderTarget, this->getLeaky
Properties(), useDFFonts); | 150 fMainTextContext = fContext->createTextContext(fRenderTarget, this->getLeaky
Properties(), useDFFonts); |
| 165 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, this->getL
eakyProperties())); | 151 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, this->getL
eakyProperties())); |
| 166 } | 152 } |
| 167 | 153 |
| (...skipping 1124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1292 const SkRect& srcRect, | 1278 const SkRect& srcRect, |
| 1293 const GrTextureParams& params, | 1279 const GrTextureParams& params, |
| 1294 const SkPaint& paint, | 1280 const SkPaint& paint, |
| 1295 SkCanvas::DrawBitmapRectFlags flags, | 1281 SkCanvas::DrawBitmapRectFlags flags, |
| 1296 bool bicubic, | 1282 bool bicubic, |
| 1297 bool needsTextureDomain) { | 1283 bool needsTextureDomain) { |
| 1298 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && | 1284 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1299 bitmap.height() <= fContext->getMaxTextureSize()); | 1285 bitmap.height() <= fContext->getMaxTextureSize()); |
| 1300 | 1286 |
| 1301 GrTexture* texture; | 1287 GrTexture* texture; |
| 1302 SkAutoCachedTexture act(this, bitmap, ¶ms, &texture); | 1288 AutoBitmapTexture abt(fContext, bitmap, ¶ms, &texture); |
| 1303 if (NULL == texture) { | 1289 if (NULL == texture) { |
| 1304 return; | 1290 return; |
| 1305 } | 1291 } |
| 1306 | 1292 |
| 1307 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() }; | 1293 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() }; |
| 1308 SkRect paintRect; | 1294 SkRect paintRect; |
| 1309 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width())); | 1295 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width())); |
| 1310 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height())); | 1296 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height())); |
| 1311 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv), | 1297 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv), |
| 1312 SkScalarMul(srcRect.fTop, hInv), | 1298 SkScalarMul(srcRect.fTop, hInv), |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1387 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); | 1373 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
| 1388 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { | 1374 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1389 return; | 1375 return; |
| 1390 } | 1376 } |
| 1391 | 1377 |
| 1392 int w = bitmap.width(); | 1378 int w = bitmap.width(); |
| 1393 int h = bitmap.height(); | 1379 int h = bitmap.height(); |
| 1394 | 1380 |
| 1395 GrTexture* texture; | 1381 GrTexture* texture; |
| 1396 // draw sprite uses the default texture params | 1382 // draw sprite uses the default texture params |
| 1397 SkAutoCachedTexture act(this, bitmap, NULL, &texture); | 1383 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture); |
| 1398 | 1384 |
| 1399 SkImageFilter* filter = paint.getImageFilter(); | 1385 SkImageFilter* filter = paint.getImageFilter(); |
| 1400 // This bitmap will own the filtered result as a texture. | 1386 // This bitmap will own the filtered result as a texture. |
| 1401 SkBitmap filteredBitmap; | 1387 SkBitmap filteredBitmap; |
| 1402 | 1388 |
| 1403 if (filter) { | 1389 if (filter) { |
| 1404 SkIPoint offset = SkIPoint::Make(0, 0); | 1390 SkIPoint offset = SkIPoint::Make(0, 0); |
| 1405 SkMatrix matrix(*draw.fMatrix); | 1391 SkMatrix matrix(*draw.fMatrix); |
| 1406 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); | 1392 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
| 1407 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); | 1393 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1564 } | 1550 } |
| 1565 | 1551 |
| 1566 SkAutoLockPixels alp(src, !src.getTexture()); | 1552 SkAutoLockPixels alp(src, !src.getTexture()); |
| 1567 if (!src.getTexture() && !src.readyToDraw()) { | 1553 if (!src.getTexture() && !src.readyToDraw()) { |
| 1568 return false; | 1554 return false; |
| 1569 } | 1555 } |
| 1570 | 1556 |
| 1571 GrTexture* texture; | 1557 GrTexture* texture; |
| 1572 // We assume here that the filter will not attempt to tile the src. Otherwis
e, this cache lookup | 1558 // We assume here that the filter will not attempt to tile the src. Otherwis
e, this cache lookup |
| 1573 // must be pushed upstack. | 1559 // must be pushed upstack. |
| 1574 SkAutoCachedTexture act(this, src, NULL, &texture); | 1560 AutoBitmapTexture abt(fContext, src, NULL, &texture); |
| 1575 | 1561 |
| 1576 return filter_texture(this, fContext, texture, filter, src.width(), src.heig
ht(), ctx, | 1562 return filter_texture(this, fContext, texture, filter, src.width(), src.heig
ht(), ctx, |
| 1577 result, offset); | 1563 result, offset); |
| 1578 } | 1564 } |
| 1579 | 1565 |
| 1580 /////////////////////////////////////////////////////////////////////////////// | 1566 /////////////////////////////////////////////////////////////////////////////// |
| 1581 | 1567 |
| 1582 // must be in SkCanvas::VertexMode order | 1568 // must be in SkCanvas::VertexMode order |
| 1583 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { | 1569 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1584 kTriangles_GrPrimitiveType, | 1570 kTriangles_GrPrimitiveType, |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1795 desc.fHeight = info.height(); | 1781 desc.fHeight = info.height(); |
| 1796 desc.fSampleCnt = fRenderTarget->numSamples(); | 1782 desc.fSampleCnt = fRenderTarget->numSamples(); |
| 1797 | 1783 |
| 1798 SkAutoTUnref<GrTexture> texture; | 1784 SkAutoTUnref<GrTexture> texture; |
| 1799 // Skia's convention is to only clear a device if it is non-opaque. | 1785 // Skia's convention is to only clear a device if it is non-opaque. |
| 1800 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag; | 1786 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag; |
| 1801 | 1787 |
| 1802 #if CACHE_COMPATIBLE_DEVICE_TEXTURES | 1788 #if CACHE_COMPATIBLE_DEVICE_TEXTURES |
| 1803 // layers are never draw in repeat modes, so we can request an approx | 1789 // layers are never draw in repeat modes, so we can request an approx |
| 1804 // match and ignore any padding. | 1790 // match and ignore any padding. |
| 1805 flags |= kCached_Flag; | |
| 1806 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ? | 1791 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ? |
| 1807 GrContext::kApprox_ScratchTexMat
ch : | 1792 GrContext::kApprox_ScratchTexMat
ch : |
| 1808 GrContext::kExact_ScratchTexMatc
h; | 1793 GrContext::kExact_ScratchTexMatc
h; |
| 1809 texture.reset(fContext->lockAndRefScratchTexture(desc, match)); | 1794 texture.reset(fContext->lockAndRefScratchTexture(desc, match)); |
| 1810 #else | 1795 #else |
| 1811 texture.reset(fContext->createUncachedTexture(desc, NULL, 0)); | 1796 texture.reset(fContext->createUncachedTexture(desc, NULL, 0)); |
| 1812 #endif | 1797 #endif |
| 1813 if (texture.get()) { | 1798 if (texture.get()) { |
| 1814 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLega
cyFontHost_InitType), flags); | 1799 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLega
cyFontHost_InitType), flags); |
| 1815 } else { | 1800 } else { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1873 GrLayerHoister::UnlockLayers(fContext, atlased, nonAtlased, recycled); | 1858 GrLayerHoister::UnlockLayers(fContext, atlased, nonAtlased, recycled); |
| 1874 | 1859 |
| 1875 return true; | 1860 return true; |
| 1876 } | 1861 } |
| 1877 | 1862 |
| 1878 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { | 1863 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { |
| 1879 // We always return a transient cache, so it is freed after each | 1864 // We always return a transient cache, so it is freed after each |
| 1880 // filter traversal. | 1865 // filter traversal. |
| 1881 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); | 1866 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); |
| 1882 } | 1867 } |
| OLD | NEW |