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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1595 sampleM.setIDiv(src->width(), src->height()); | 1593 sampleM.setIDiv(src->width(), src->height()); |
1596 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height()); | 1594 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height()); |
1597 if (topLeft) { | 1595 if (topLeft) { |
1598 srcRect.offset(*topLeft); | 1596 srcRect.offset(*topLeft); |
1599 } | 1597 } |
1600 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height()); | 1598 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height()); |
1601 if (!srcRect.intersect(srcBounds)) { | 1599 if (!srcRect.intersect(srcBounds)) { |
1602 return; | 1600 return; |
1603 } | 1601 } |
1604 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTo
p)); | 1602 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTo
p)); |
1605 drawState->addColorTextureEffect(src, sampleM); | 1603 drawState->addColorTextureProcessor(src, sampleM); |
1606 | 1604 |
1607 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(s
rcRect.height())); | 1605 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(s
rcRect.height())); |
1608 target->drawSimpleRect(dstR); | 1606 target->drawSimpleRect(dstR); |
1609 } | 1607 } |
1610 | 1608 |
1611 bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, | 1609 bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, |
1612 int left, int top, int width, int height
, | 1610 int left, int top, int width, int height
, |
1613 GrPixelConfig srcConfig, | 1611 GrPixelConfig srcConfig, |
1614 const void* buffer, | 1612 const void* buffer, |
1615 size_t rowBytes, | 1613 size_t rowBytes, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1661 GrTextureDesc desc; | 1659 GrTextureDesc desc; |
1662 desc.fWidth = width; | 1660 desc.fWidth = width; |
1663 desc.fHeight = height; | 1661 desc.fHeight = height; |
1664 desc.fConfig = writeConfig; | 1662 desc.fConfig = writeConfig; |
1665 GrAutoScratchTexture ast(this, desc); | 1663 GrAutoScratchTexture ast(this, desc); |
1666 GrTexture* texture = ast.texture(); | 1664 GrTexture* texture = ast.texture(); |
1667 if (NULL == texture) { | 1665 if (NULL == texture) { |
1668 return false; | 1666 return false; |
1669 } | 1667 } |
1670 | 1668 |
1671 SkAutoTUnref<const GrEffect> effect; | 1669 SkAutoTUnref<const GrFragmentProcessor> fp; |
1672 SkMatrix textureMatrix; | 1670 SkMatrix textureMatrix; |
1673 textureMatrix.setIDiv(texture->width(), texture->height()); | 1671 textureMatrix.setIDiv(texture->width(), texture->height()); |
1674 | 1672 |
1675 // allocate a tmp buffer and sw convert the pixels to premul | 1673 // allocate a tmp buffer and sw convert the pixels to premul |
1676 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); | 1674 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
1677 | 1675 |
1678 if (kUnpremul_PixelOpsFlag & flags) { | 1676 if (kUnpremul_PixelOpsFlag & flags) { |
1679 if (!GrPixelConfigIs8888(srcConfig)) { | 1677 if (!GrPixelConfigIs8888(srcConfig)) { |
1680 return false; | 1678 return false; |
1681 } | 1679 } |
1682 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix
)); | 1680 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix)); |
1683 // handle the unpremul step on the CPU if we couldn't create an effect t
o do it. | 1681 // handle the unpremul step on the CPU if we couldn't create an effect t
o do it. |
1684 if (NULL == effect) { | 1682 if (NULL == fp) { |
1685 SkSrcPixelInfo srcPI; | 1683 SkSrcPixelInfo srcPI; |
1686 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) { | 1684 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) { |
1687 return false; | 1685 return false; |
1688 } | 1686 } |
1689 srcPI.fAlphaType = kUnpremul_SkAlphaType; | 1687 srcPI.fAlphaType = kUnpremul_SkAlphaType; |
1690 srcPI.fPixels = buffer; | 1688 srcPI.fPixels = buffer; |
1691 srcPI.fRowBytes = rowBytes; | 1689 srcPI.fRowBytes = rowBytes; |
1692 | 1690 |
1693 tmpPixels.reset(width * height); | 1691 tmpPixels.reset(width * height); |
1694 | 1692 |
1695 SkDstPixelInfo dstPI; | 1693 SkDstPixelInfo dstPI; |
1696 dstPI.fColorType = srcPI.fColorType; | 1694 dstPI.fColorType = srcPI.fColorType; |
1697 dstPI.fAlphaType = kPremul_SkAlphaType; | 1695 dstPI.fAlphaType = kPremul_SkAlphaType; |
1698 dstPI.fPixels = tmpPixels.get(); | 1696 dstPI.fPixels = tmpPixels.get(); |
1699 dstPI.fRowBytes = 4 * width; | 1697 dstPI.fRowBytes = 4 * width; |
1700 | 1698 |
1701 if (!srcPI.convertPixelsTo(&dstPI, width, height)) { | 1699 if (!srcPI.convertPixelsTo(&dstPI, width, height)) { |
1702 return false; | 1700 return false; |
1703 } | 1701 } |
1704 | 1702 |
1705 buffer = tmpPixels.get(); | 1703 buffer = tmpPixels.get(); |
1706 rowBytes = 4 * width; | 1704 rowBytes = 4 * width; |
1707 } | 1705 } |
1708 } | 1706 } |
1709 if (NULL == effect) { | 1707 if (NULL == fp) { |
1710 effect.reset(GrConfigConversionEffect::Create(texture, | 1708 fp.reset(GrConfigConversionEffect::Create(texture, |
1711 swapRAndB, | 1709 swapRAndB, |
1712 GrConfigConversionEffect::
kNone_PMConversion, | 1710 GrConfigConversionEffect::
kNone_PMConversion, |
1713 textureMatrix)); | 1711 textureMatrix)); |
1714 } | 1712 } |
1715 | 1713 |
1716 if (!this->writeTexturePixels(texture, | 1714 if (!this->writeTexturePixels(texture, |
1717 0, 0, width, height, | 1715 0, 0, width, height, |
1718 writeConfig, buffer, rowBytes, | 1716 writeConfig, buffer, rowBytes, |
1719 flags & ~kUnpremul_PixelOpsFlag)) { | 1717 flags & ~kUnpremul_PixelOpsFlag)) { |
1720 return false; | 1718 return false; |
1721 } | 1719 } |
1722 | 1720 |
1723 // TODO: Usually this could go to fDrawBuffer but currently | 1721 // TODO: Usually this could go to fDrawBuffer but currently |
1724 // writeRenderTargetPixels can be called in the midst of drawing another | 1722 // writeRenderTargetPixels can be called in the midst of drawing another |
1725 // object (e.g., when uploading a SW path rendering to the gpu while | 1723 // object (e.g., when uploading a SW path rendering to the gpu while |
1726 // drawing a rect). So we always draw directly to GrGpu and preserve the cur
rent geometry. | 1724 // drawing a rect). So we always draw directly to GrGpu and preserve the cur
rent geometry. |
1727 // But that means we also have to flush the draw buffer if there is a pendin
g IO operation to | 1725 // But that means we also have to flush the draw buffer if there is a pendin
g IO operation to |
1728 // the render target. | 1726 // the render target. |
1729 if (!(kDontFlush_PixelOpsFlag & flags) && target->hasPendingIO()) { | 1727 if (!(kDontFlush_PixelOpsFlag & flags) && target->hasPendingIO()) { |
1730 this->flush(); | 1728 this->flush(); |
1731 } | 1729 } |
1732 SkMatrix matrix; | 1730 SkMatrix matrix; |
1733 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); | 1731 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
1734 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRI
nit, &matrix); | 1732 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRI
nit, &matrix); |
1735 GrDrawState* drawState = fGpu->drawState(); | 1733 GrDrawState* drawState = fGpu->drawState(); |
1736 SkASSERT(effect); | 1734 SkASSERT(fp); |
1737 drawState->addColorEffect(effect); | 1735 drawState->addColorProcessor(fp); |
1738 | 1736 |
1739 drawState->setRenderTarget(target); | 1737 drawState->setRenderTarget(target); |
1740 | 1738 |
1741 fGpu->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heig
ht))); | 1739 fGpu->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heig
ht))); |
1742 return true; | 1740 return true; |
1743 } | 1741 } |
1744 //////////////////////////////////////////////////////////////////////////////// | 1742 //////////////////////////////////////////////////////////////////////////////// |
1745 | 1743 |
1746 GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, | 1744 GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, |
1747 BufferedDraw buffered, | 1745 BufferedDraw buffered, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1882 namespace { | 1880 namespace { |
1883 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { | 1881 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
1884 GrConfigConversionEffect::PMConversion pmToUPM; | 1882 GrConfigConversionEffect::PMConversion pmToUPM; |
1885 GrConfigConversionEffect::PMConversion upmToPM; | 1883 GrConfigConversionEffect::PMConversion upmToPM; |
1886 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); | 1884 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); |
1887 *pmToUPMValue = pmToUPM; | 1885 *pmToUPMValue = pmToUPM; |
1888 *upmToPMValue = upmToPM; | 1886 *upmToPMValue = upmToPM; |
1889 } | 1887 } |
1890 } | 1888 } |
1891 | 1889 |
1892 const GrEffect* GrContext::createPMToUPMEffect(GrTexture* texture, | 1890 const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture, |
1893 bool swapRAndB, | 1891 bool swapRAndB, |
1894 const SkMatrix& matrix) { | 1892 const SkMatrix& matrix
) { |
1895 if (!fDidTestPMConversions) { | 1893 if (!fDidTestPMConversions) { |
1896 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); | 1894 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
1897 fDidTestPMConversions = true; | 1895 fDidTestPMConversions = true; |
1898 } | 1896 } |
1899 GrConfigConversionEffect::PMConversion pmToUPM = | 1897 GrConfigConversionEffect::PMConversion pmToUPM = |
1900 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); | 1898 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
1901 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { | 1899 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { |
1902 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, mat
rix); | 1900 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, mat
rix); |
1903 } else { | 1901 } else { |
1904 return NULL; | 1902 return NULL; |
1905 } | 1903 } |
1906 } | 1904 } |
1907 | 1905 |
1908 const GrEffect* GrContext::createUPMToPMEffect(GrTexture* texture, | 1906 const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture, |
1909 bool swapRAndB, | 1907 bool swapRAndB, |
1910 const SkMatrix& matrix) { | 1908 const SkMatrix& matrix
) { |
1911 if (!fDidTestPMConversions) { | 1909 if (!fDidTestPMConversions) { |
1912 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); | 1910 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
1913 fDidTestPMConversions = true; | 1911 fDidTestPMConversions = true; |
1914 } | 1912 } |
1915 GrConfigConversionEffect::PMConversion upmToPM = | 1913 GrConfigConversionEffect::PMConversion upmToPM = |
1916 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); | 1914 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
1917 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { | 1915 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { |
1918 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, mat
rix); | 1916 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, mat
rix); |
1919 } else { | 1917 } else { |
1920 return NULL; | 1918 return NULL; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1952 fResourceCache->printStats(); | 1950 fResourceCache->printStats(); |
1953 } | 1951 } |
1954 #endif | 1952 #endif |
1955 | 1953 |
1956 #if GR_GPU_STATS | 1954 #if GR_GPU_STATS |
1957 const GrContext::GPUStats* GrContext::gpuStats() const { | 1955 const GrContext::GPUStats* GrContext::gpuStats() const { |
1958 return fGpu->gpuStats(); | 1956 return fGpu->gpuStats(); |
1959 } | 1957 } |
1960 #endif | 1958 #endif |
1961 | 1959 |
OLD | NEW |