Index: src/image/SkImage_Gpu.cpp |
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp |
index a728c3dae594f41385891352110363be3ded29c2..12f592c52586499f9e6084834062954ab32ec7b5 100644 |
--- a/src/image/SkImage_Gpu.cpp |
+++ b/src/image/SkImage_Gpu.cpp |
@@ -16,6 +16,7 @@ |
#include "SkGrPixelRef.h" |
#include "SkGrPriv.h" |
#include "SkImage_Gpu.h" |
+#include "SkMipMap.h" |
#include "SkPixelRef.h" |
SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex, |
@@ -348,8 +349,30 @@ private: |
}; |
size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
- const DeferredTextureImageUsageParams[], |
+ const DeferredTextureImageUsageParams params[], |
int paramCnt, void* buffer) const { |
+ bool shouldUseMipMaps = false; |
+ for (int currentParamIndex = paramCnt - 1; currentParamIndex >= 0; currentParamIndex--) { |
+ const SkMatrix& currentMatrix = params[currentParamIndex].fMatrix; |
+ SkFilterQuality currentFilterQuality = params[currentParamIndex].fQuality; |
+ |
+ // Use mipmaps if either |
+ // 1.) it is a perspective matrix, or |
+ // 2.) the quality is med/high and the scale is < 1 |
+ if (currentMatrix.hasPerspective()) { |
+ shouldUseMipMaps = true; |
+ } |
+ if (currentFilterQuality == kMedium_SkFilterQuality || |
+ currentFilterQuality == kHigh_SkFilterQuality) { |
+ SkScalar minAxisScale = currentMatrix.getMinScale(); |
+ if (minAxisScale != -1.f) { |
ericrk
2016/06/06 22:57:40
nit - just && this and the following if to reduce
cblume
2016/06/08 17:50:33
Done.
|
+ if (minAxisScale < 1.f) { |
+ shouldUseMipMaps = true; |
+ } |
+ } |
+ } |
+ } |
+ |
const bool fillMode = SkToBool(buffer); |
if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
return 0; |
@@ -391,7 +414,19 @@ size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox |
SkASSERT(!pixmap.ctable()); |
} |
} |
+ SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
int mipMapLevelCount = 1; |
+ if (shouldUseMipMaps) { |
+ mipMapLevelCount = SkMipMap::ComputeLevelCount(width(), height()) + 1; |
ericrk
2016/06/06 22:57:40
nit: thanks for fixing this up - please add a comm
cblume
2016/06/08 17:50:33
Done.
|
+ |
+ // SkMipMap will generate |
ericrk
2016/06/06 22:57:40
nit: comment isn't finished?
cblume
2016/06/08 17:50:33
Done.
|
+ for (int currentMipMapLevelIndex = mipMapLevelCount - 1; currentMipMapLevelIndex >= 0; |
+ currentMipMapLevelIndex--) { |
+ SkISize mipSize = SkMipMap::ComputeLevelSize(width(), height(), currentMipMapLevelIndex); |
+ SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.fHeight, at); |
+ pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr)); |
ericrk
2016/06/06 22:57:40
This will add in the size of level 0... isn't leve
cblume
2016/06/08 17:50:33
We need both, I believe.
393/408 gets the size of
|
+ } |
+ } |
size_t size = 0; |
size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
size += dtiSize; |
@@ -425,6 +460,32 @@ size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox |
dti->fData.fMipMapLevelCount = mipMapLevelCount; |
dti->fData.fMipMapLevelData[0].fPixelData = pixels; |
dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes; |
+ |
+ // Fill in the mipmap levels if they exist |
+ intptr_t mipLevelPtr = bufferAsInt + pixelOffset; |
+ if (shouldUseMipMaps) { |
+ SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr)); |
+ // SkMipMap holds only the mipmap levels it generates. |
+ // A programmer can use the data they provided to SkMipMap::Build as level 0. |
+ // So the SkMipMap provides levels 1-x but it stores them in its own |
+ // range 0-(x-1). |
+ // This iterates 1-x. |
+ for (int currentMipMapLevelIndex = 1; currentMipMapLevelIndex < mipMapLevelCount; |
+ currentMipMapLevelIndex++) { |
+ SkISize prevMipSize = SkMipMap::ComputeLevelSize(width(), height(), currentMipMapLevelIndex - 1); |
+ SkImageInfo prevMipInfo = SkImageInfo::MakeN32(prevMipSize.fWidth, prevMipSize.fHeight, at); |
+ mipLevelPtr += SkAlign8(SkAutoPixmapStorage::AllocSize(prevMipInfo, nullptr)); |
+ |
+ SkMipMap::Level currentMipMapLevel; |
+ mipmaps->getLevel(currentMipMapLevelIndex - 1, ¤tMipMapLevel); |
+ memcpy(reinterpret_cast<void*>(mipLevelPtr), currentMipMapLevel.fPixmap.addr(), |
+ currentMipMapLevel.fPixmap.getSafeSize()); |
+ dti->fData.fMipMapLevelData[currentMipMapLevelIndex].fPixelData = |
+ reinterpret_cast<void*>(mipLevelPtr); |
+ dti->fData.fMipMapLevelData[currentMipMapLevelIndex].fRowBytes = |
+ currentMipMapLevel.fPixmap.rowBytes(); |
+ } |
+ } |
return size; |
} |
@@ -443,11 +504,23 @@ sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con |
SkASSERT(dti->fData.fColorTableData); |
colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt)); |
} |
- SkASSERT(dti->fData.fMipMapLevelCount == 1); |
- SkPixmap pixmap; |
- pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData, |
- dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get()); |
- return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
+ int mipLevelCount = dti->fData.fMipMapLevelCount; |
+ SkASSERT(mipLevelCount >= 1); |
+ if (mipLevelCount == 1) { |
+ SkPixmap pixmap; |
+ pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData, |
+ dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get()); |
+ return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
+ } else { |
+ SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]); |
+ for (int i = 0; i < mipLevelCount; i++) { |
+ texels[i].fPixels = dti->fData.fMipMapLevelData[i].fPixelData; |
+ texels[i].fRowBytes = dti->fData.fMipMapLevelData[i].fRowBytes; |
+ } |
+ |
+ return SkImage::MakeTextureFromMipMap(context, dti->fData.fInfo, texels.get(), |
+ mipLevelCount, SkBudgeted::kYes); |
+ } |
} |
/////////////////////////////////////////////////////////////////////////////////////////////////// |