OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 if (texture) { | 356 if (texture) { |
357 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); | 357 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
358 GrDrawState* drawState = fGpu->drawState(); | 358 GrDrawState* drawState = fGpu->drawState(); |
359 drawState->setRenderTarget(texture->asRenderTarget()); | 359 drawState->setRenderTarget(texture->asRenderTarget()); |
360 | 360 |
361 // if filtering is not desired then we want to ensure all | 361 // if filtering is not desired then we want to ensure all |
362 // texels in the resampled image are copies of texels from | 362 // texels in the resampled image are copies of texels from |
363 // the original. | 363 // the original. |
364 GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTexturePara
ms::kBilerp_FilterMode : | 364 GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTexturePara
ms::kBilerp_FilterMode : |
365 GrTexturePara
ms::kNone_FilterMode); | 365 GrTexturePara
ms::kNone_FilterMode); |
366 drawState->addColorTextureEffect(clampedTexture, SkMatrix::I(), params); | 366 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), param
s); |
367 | 367 |
368 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttrib
s), | 368 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttrib
s), |
369 2 * sizeof(SkPoint)); | 369 2 * sizeof(SkPoint)); |
370 | 370 |
371 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0); | 371 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0); |
372 | 372 |
373 if (arg.succeeded()) { | 373 if (arg.succeeded()) { |
374 SkPoint* verts = (SkPoint*) arg.vertices(); | 374 SkPoint* verts = (SkPoint*) arg.vertices(); |
375 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 *
sizeof(SkPoint)); | 375 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 *
sizeof(SkPoint)); |
376 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint)); | 376 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint)); |
(...skipping 1101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1478 match = kExact_ScratchTexMatch; | 1478 match = kExact_ScratchTexMatch; |
1479 } | 1479 } |
1480 ast.set(this, desc, match); | 1480 ast.set(this, desc, match); |
1481 GrTexture* texture = ast.texture(); | 1481 GrTexture* texture = ast.texture(); |
1482 if (texture) { | 1482 if (texture) { |
1483 // compute a matrix to perform the draw | 1483 // compute a matrix to perform the draw |
1484 SkMatrix textureMatrix; | 1484 SkMatrix textureMatrix; |
1485 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); | 1485 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); |
1486 textureMatrix.postIDiv(src->width(), src->height()); | 1486 textureMatrix.postIDiv(src->width(), src->height()); |
1487 | 1487 |
1488 SkAutoTUnref<const GrEffect> effect; | 1488 SkAutoTUnref<const GrFragmentProcessor> fp; |
1489 if (unpremul) { | 1489 if (unpremul) { |
1490 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMa
trix)); | 1490 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix
)); |
1491 if (effect) { | 1491 if (fp) { |
1492 unpremul = false; // we no longer need to do this on CPU aft
er the read back. | 1492 unpremul = false; // we no longer need to do this on CPU aft
er the read back. |
1493 } | 1493 } |
1494 } | 1494 } |
1495 // If we failed to create a PM->UPM effect and have no other convers
ions to perform then | 1495 // If we failed to create a PM->UPM effect and have no other convers
ions to perform then |
1496 // there is no longer any point to using the scratch. | 1496 // there is no longer any point to using the scratch. |
1497 if (effect || flipY || swapRAndB) { | 1497 if (fp || flipY || swapRAndB) { |
1498 if (!effect) { | 1498 if (!fp) { |
1499 effect.reset(GrConfigConversionEffect::Create( | 1499 fp.reset(GrConfigConversionEffect::Create( |
1500 src, | 1500 src, swapRAndB, GrConfigConversionEffect::kNone_PMCo
nversion, |
1501 swapRAndB, | 1501 textureMatrix)); |
1502 GrConfigConversionEffect::kN
one_PMConversion, | |
1503 textureMatrix)); | |
1504 } | 1502 } |
1505 swapRAndB = false; // we will handle the swap in the draw. | 1503 swapRAndB = false; // we will handle the swap in the draw. |
1506 | 1504 |
1507 // We protect the existing geometry here since it may not be | 1505 // We protect the existing geometry here since it may not be |
1508 // clear to the caller that a draw operation (i.e., drawSimpleRe
ct) | 1506 // clear to the caller that a draw operation (i.e., drawSimpleRe
ct) |
1509 // can be invoked in this method | 1507 // can be invoked in this method |
1510 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget:
:kReset_ASRInit); | 1508 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget:
:kReset_ASRInit); |
1511 GrDrawState* drawState = fGpu->drawState(); | 1509 GrDrawState* drawState = fGpu->drawState(); |
1512 SkASSERT(effect); | 1510 SkASSERT(fp); |
1513 drawState->addColorEffect(effect); | 1511 drawState->addColorProcessor(fp); |
1514 | 1512 |
1515 drawState->setRenderTarget(texture->asRenderTarget()); | 1513 drawState->setRenderTarget(texture->asRenderTarget()); |
1516 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar
(height)); | 1514 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar
(height)); |
1517 fGpu->drawSimpleRect(rect); | 1515 fGpu->drawSimpleRect(rect); |
1518 // we want to read back from the scratch's origin | 1516 // we want to read back from the scratch's origin |
1519 left = 0; | 1517 left = 0; |
1520 top = 0; | 1518 top = 0; |
1521 target = texture->asRenderTarget(); | 1519 target = texture->asRenderTarget(); |
1522 } | 1520 } |
1523 } | 1521 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1591 sampleM.setIDiv(src->width(), src->height()); | 1589 sampleM.setIDiv(src->width(), src->height()); |
1592 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height()); | 1590 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height()); |
1593 if (topLeft) { | 1591 if (topLeft) { |
1594 srcRect.offset(*topLeft); | 1592 srcRect.offset(*topLeft); |
1595 } | 1593 } |
1596 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height()); | 1594 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height()); |
1597 if (!srcRect.intersect(srcBounds)) { | 1595 if (!srcRect.intersect(srcBounds)) { |
1598 return; | 1596 return; |
1599 } | 1597 } |
1600 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTo
p)); | 1598 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTo
p)); |
1601 drawState->addColorTextureEffect(src, sampleM); | 1599 drawState->addColorTextureProcessor(src, sampleM); |
1602 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(s
rcRect.height())); | 1600 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(s
rcRect.height())); |
1603 fGpu->drawSimpleRect(dstR); | 1601 fGpu->drawSimpleRect(dstR); |
1604 } | 1602 } |
1605 | 1603 |
1606 bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, | 1604 bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, |
1607 int left, int top, int width, int height
, | 1605 int left, int top, int width, int height
, |
1608 GrPixelConfig srcConfig, | 1606 GrPixelConfig srcConfig, |
1609 const void* buffer, | 1607 const void* buffer, |
1610 size_t rowBytes, | 1608 size_t rowBytes, |
1611 uint32_t flags) { | 1609 uint32_t flags) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1656 GrTextureDesc desc; | 1654 GrTextureDesc desc; |
1657 desc.fWidth = width; | 1655 desc.fWidth = width; |
1658 desc.fHeight = height; | 1656 desc.fHeight = height; |
1659 desc.fConfig = writeConfig; | 1657 desc.fConfig = writeConfig; |
1660 GrAutoScratchTexture ast(this, desc); | 1658 GrAutoScratchTexture ast(this, desc); |
1661 GrTexture* texture = ast.texture(); | 1659 GrTexture* texture = ast.texture(); |
1662 if (NULL == texture) { | 1660 if (NULL == texture) { |
1663 return false; | 1661 return false; |
1664 } | 1662 } |
1665 | 1663 |
1666 SkAutoTUnref<const GrEffect> effect; | 1664 SkAutoTUnref<const GrFragmentProcessor> fp; |
1667 SkMatrix textureMatrix; | 1665 SkMatrix textureMatrix; |
1668 textureMatrix.setIDiv(texture->width(), texture->height()); | 1666 textureMatrix.setIDiv(texture->width(), texture->height()); |
1669 | 1667 |
1670 // allocate a tmp buffer and sw convert the pixels to premul | 1668 // allocate a tmp buffer and sw convert the pixels to premul |
1671 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); | 1669 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
1672 | 1670 |
1673 if (kUnpremul_PixelOpsFlag & flags) { | 1671 if (kUnpremul_PixelOpsFlag & flags) { |
1674 if (!GrPixelConfigIs8888(srcConfig)) { | 1672 if (!GrPixelConfigIs8888(srcConfig)) { |
1675 return false; | 1673 return false; |
1676 } | 1674 } |
1677 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix
)); | 1675 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix)); |
1678 // handle the unpremul step on the CPU if we couldn't create an effect t
o do it. | 1676 // handle the unpremul step on the CPU if we couldn't create an effect t
o do it. |
1679 if (NULL == effect) { | 1677 if (NULL == fp) { |
1680 SkSrcPixelInfo srcPI; | 1678 SkSrcPixelInfo srcPI; |
1681 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) { | 1679 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) { |
1682 return false; | 1680 return false; |
1683 } | 1681 } |
1684 srcPI.fAlphaType = kUnpremul_SkAlphaType; | 1682 srcPI.fAlphaType = kUnpremul_SkAlphaType; |
1685 srcPI.fPixels = buffer; | 1683 srcPI.fPixels = buffer; |
1686 srcPI.fRowBytes = rowBytes; | 1684 srcPI.fRowBytes = rowBytes; |
1687 | 1685 |
1688 tmpPixels.reset(width * height); | 1686 tmpPixels.reset(width * height); |
1689 | 1687 |
1690 SkDstPixelInfo dstPI; | 1688 SkDstPixelInfo dstPI; |
1691 dstPI.fColorType = srcPI.fColorType; | 1689 dstPI.fColorType = srcPI.fColorType; |
1692 dstPI.fAlphaType = kPremul_SkAlphaType; | 1690 dstPI.fAlphaType = kPremul_SkAlphaType; |
1693 dstPI.fPixels = tmpPixels.get(); | 1691 dstPI.fPixels = tmpPixels.get(); |
1694 dstPI.fRowBytes = 4 * width; | 1692 dstPI.fRowBytes = 4 * width; |
1695 | 1693 |
1696 if (!srcPI.convertPixelsTo(&dstPI, width, height)) { | 1694 if (!srcPI.convertPixelsTo(&dstPI, width, height)) { |
1697 return false; | 1695 return false; |
1698 } | 1696 } |
1699 | 1697 |
1700 buffer = tmpPixels.get(); | 1698 buffer = tmpPixels.get(); |
1701 rowBytes = 4 * width; | 1699 rowBytes = 4 * width; |
1702 } | 1700 } |
1703 } | 1701 } |
1704 if (NULL == effect) { | 1702 if (NULL == fp) { |
1705 effect.reset(GrConfigConversionEffect::Create(texture, | 1703 fp.reset(GrConfigConversionEffect::Create(texture, |
1706 swapRAndB, | 1704 swapRAndB, |
1707 GrConfigConversionEffect::
kNone_PMConversion, | 1705 GrConfigConversionEffect::
kNone_PMConversion, |
1708 textureMatrix)); | 1706 textureMatrix)); |
1709 } | 1707 } |
1710 | 1708 |
1711 if (!this->writeTexturePixels(texture, | 1709 if (!this->writeTexturePixels(texture, |
1712 0, 0, width, height, | 1710 0, 0, width, height, |
1713 writeConfig, buffer, rowBytes, | 1711 writeConfig, buffer, rowBytes, |
1714 flags & ~kUnpremul_PixelOpsFlag)) { | 1712 flags & ~kUnpremul_PixelOpsFlag)) { |
1715 return false; | 1713 return false; |
1716 } | 1714 } |
1717 | 1715 |
1718 // writeRenderTargetPixels can be called in the midst of drawing another | 1716 // writeRenderTargetPixels can be called in the midst of drawing another |
1719 // object (e.g., when uploading a SW path rendering to the gpu while | 1717 // object (e.g., when uploading a SW path rendering to the gpu while |
1720 // drawing a rect) so preserve the current geometry. | 1718 // drawing a rect) so preserve the current geometry. |
1721 SkMatrix matrix; | 1719 SkMatrix matrix; |
1722 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); | 1720 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
1723 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRI
nit, &matrix); | 1721 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRI
nit, &matrix); |
1724 GrDrawState* drawState = fGpu->drawState(); | 1722 GrDrawState* drawState = fGpu->drawState(); |
1725 SkASSERT(effect); | 1723 SkASSERT(fp); |
1726 drawState->addColorEffect(effect); | 1724 drawState->addColorProcessor(fp); |
1727 | 1725 |
1728 drawState->setRenderTarget(target); | 1726 drawState->setRenderTarget(target); |
1729 | 1727 |
1730 fGpu->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heig
ht))); | 1728 fGpu->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heig
ht))); |
1731 return true; | 1729 return true; |
1732 } | 1730 } |
1733 //////////////////////////////////////////////////////////////////////////////// | 1731 //////////////////////////////////////////////////////////////////////////////// |
1734 | 1732 |
1735 GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, | 1733 GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, |
1736 BufferedDraw buffered, | 1734 BufferedDraw buffered, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1871 namespace { | 1869 namespace { |
1872 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { | 1870 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
1873 GrConfigConversionEffect::PMConversion pmToUPM; | 1871 GrConfigConversionEffect::PMConversion pmToUPM; |
1874 GrConfigConversionEffect::PMConversion upmToPM; | 1872 GrConfigConversionEffect::PMConversion upmToPM; |
1875 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); | 1873 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); |
1876 *pmToUPMValue = pmToUPM; | 1874 *pmToUPMValue = pmToUPM; |
1877 *upmToPMValue = upmToPM; | 1875 *upmToPMValue = upmToPM; |
1878 } | 1876 } |
1879 } | 1877 } |
1880 | 1878 |
1881 const GrEffect* GrContext::createPMToUPMEffect(GrTexture* texture, | 1879 const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture, |
1882 bool swapRAndB, | 1880 bool swapRAndB, |
1883 const SkMatrix& matrix) { | 1881 const SkMatrix& matrix
) { |
1884 if (!fDidTestPMConversions) { | 1882 if (!fDidTestPMConversions) { |
1885 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); | 1883 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
1886 fDidTestPMConversions = true; | 1884 fDidTestPMConversions = true; |
1887 } | 1885 } |
1888 GrConfigConversionEffect::PMConversion pmToUPM = | 1886 GrConfigConversionEffect::PMConversion pmToUPM = |
1889 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); | 1887 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
1890 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { | 1888 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { |
1891 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, mat
rix); | 1889 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, mat
rix); |
1892 } else { | 1890 } else { |
1893 return NULL; | 1891 return NULL; |
1894 } | 1892 } |
1895 } | 1893 } |
1896 | 1894 |
1897 const GrEffect* GrContext::createUPMToPMEffect(GrTexture* texture, | 1895 const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture, |
1898 bool swapRAndB, | 1896 bool swapRAndB, |
1899 const SkMatrix& matrix) { | 1897 const SkMatrix& matrix
) { |
1900 if (!fDidTestPMConversions) { | 1898 if (!fDidTestPMConversions) { |
1901 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); | 1899 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
1902 fDidTestPMConversions = true; | 1900 fDidTestPMConversions = true; |
1903 } | 1901 } |
1904 GrConfigConversionEffect::PMConversion upmToPM = | 1902 GrConfigConversionEffect::PMConversion upmToPM = |
1905 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); | 1903 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
1906 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { | 1904 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { |
1907 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, mat
rix); | 1905 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, mat
rix); |
1908 } else { | 1906 } else { |
1909 return NULL; | 1907 return NULL; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1941 fResourceCache->printStats(); | 1939 fResourceCache->printStats(); |
1942 } | 1940 } |
1943 #endif | 1941 #endif |
1944 | 1942 |
1945 #if GR_GPU_STATS | 1943 #if GR_GPU_STATS |
1946 const GrContext::GPUStats* GrContext::gpuStats() const { | 1944 const GrContext::GPUStats* GrContext::gpuStats() const { |
1947 return fGpu->gpuStats(); | 1945 return fGpu->gpuStats(); |
1948 } | 1946 } |
1949 #endif | 1947 #endif |
1950 | 1948 |
OLD | NEW |