Refactor createTexture and onCreateTexture

BUG=skia:

Review URL: https://codereview.chromium.org/1102663002
This commit is contained in:
egdaniel 2015-04-22 13:27:39 -07:00 committed by Commit bot
parent b30938ba7f
commit b0e1be207f
5 changed files with 73 additions and 65 deletions

View File

@ -37,8 +37,25 @@ void GrGpu::contextAbandoned() {}
////////////////////////////////////////////////////////////////////////////////
GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
namespace {
GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
// By default, GrRenderTargets are GL's normal orientation so that they
// can be drawn to by the outside world without the client having
// to render upside down.
if (kDefault_GrSurfaceOrigin == origin) {
return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
} else {
return origin;
}
}
}
GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
const void* srcData, size_t rowBytes) {
GrSurfaceDesc desc = origDesc;
if (!this->caps()->isConfigTexturable(desc.fConfig)) {
return NULL;
}
@ -49,9 +66,32 @@ GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
}
GrTexture *tex = NULL;
if (isRT) {
int maxRTSize = this->caps()->maxRenderTargetSize();
if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
return NULL;
}
} else {
int maxSize = this->caps()->maxTextureSize();
if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
return NULL;
}
}
GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
GrGpuResource::kUncached_LifeCycle;
desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
// Attempt to catch un- or wrongly initialized sample counts;
SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
if (GrPixelConfigIsCompressed(desc.fConfig)) {
// We shouldn't be rendering into this
SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
SkASSERT(!isRT);
SkASSERT(0 == desc.fSampleCnt);
if (!this->caps()->npotTextureTileSupport() &&
(!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
@ -59,10 +99,10 @@ GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
}
this->handleDirtyContext();
tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
} else {
this->handleDirtyContext();
tex = this->onCreateTexture(desc, budgeted, srcData, rowBytes);
tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
}
if (!this->caps()->reuseScratchTextures() && !isRT) {
tex->resourcePriv().removeScratchKey();

View File

@ -437,9 +437,13 @@ private:
virtual void onResetContext(uint32_t resetBits) = 0;
// overridden by backend-specific derived class to create objects.
virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted,
// Texture size and sample size will have already been validated in base class before
// onCreateTexture/CompressedTexture are called.
virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
GrGpuResource::LifeCycle lifeCycle,
const void* srcData, size_t rowBytes) = 0;
virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
GrGpuResource::LifeCycle lifeCycle,
const void* srcData) = 0;
virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;

View File

@ -175,12 +175,12 @@ public:
private:
void onResetContext(uint32_t resetBits) override {}
GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
size_t rowBytes) override {
GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
const void* srcData, size_t rowBytes) override {
return NULL;
}
GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
const void* srcData) override {
return NULL;
}

View File

@ -416,7 +416,8 @@ GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
GrGLTexture* texture = NULL;
if (renderTarget) {
GrGLRenderTarget::IDDesc rtIDDesc;
if (!this->createRenderTargetObjects(surfDesc, false, idDesc.fTextureID, &rtIDDesc)) {
if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
idDesc.fTextureID, &rtIDDesc)) {
return NULL;
}
texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
@ -811,13 +812,14 @@ static bool renderbuffer_storage_msaa(GrGLContext& ctx,
return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
}
bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, bool budgeted, GrGLuint texID,
bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
GrGpuResource::LifeCycle lifeCycle,
GrGLuint texID,
GrGLRenderTarget::IDDesc* idDesc) {
idDesc->fMSColorRenderbufferID = 0;
idDesc->fRTFBOID = 0;
idDesc->fTexFBOID = 0;
idDesc->fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
GrGpuResource::kUncached_LifeCycle;
idDesc->fLifeCycle = lifeCycle;
GrGLenum status;
@ -930,13 +932,9 @@ static size_t as_size_t(int x) {
}
#endif
GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted,
GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
GrGpuResource::LifeCycle lifeCycle,
const void* srcData, size_t rowBytes) {
GrSurfaceDesc desc = origDesc;
// Attempt to catch un- or wrongly initialized sample counts;
SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
// We fail if the MSAA was requested and is not available.
if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
//SkDebugf("MSAA RT requested but not supported on this platform.");
@ -945,31 +943,9 @@ GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted
bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
// If the sample count exceeds the max then we clamp it.
desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
//SkDebugf("MSAA RT requested but not supported on this platform.");
return return_null_texture();
}
if (renderTarget) {
int maxRTSize = this->caps()->maxRenderTargetSize();
if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
return return_null_texture();
}
} else {
int maxSize = this->caps()->maxTextureSize();
if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
return return_null_texture();
}
}
GrGLTexture::IDDesc idDesc;
GL_CALL(GenTextures(1, &idDesc.fTextureID));
idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
GrGpuResource::kUncached_LifeCycle;
idDesc.fLifeCycle = lifeCycle;
if (!idDesc.fTextureID) {
return return_null_texture();
@ -1020,7 +996,7 @@ GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted
GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
GrGLRenderTarget::IDDesc rtIDDesc;
if (!this->createRenderTargetObjects(desc, budgeted, idDesc.fTextureID, &rtIDDesc)) {
if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fTextureID, &rtIDDesc)) {
GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
return return_null_texture();
}
@ -1036,30 +1012,17 @@ GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted
return tex;
}
GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, bool budgeted,
GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
GrGpuResource::LifeCycle lifeCycle,
const void* srcData) {
if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
return return_null_texture();
}
// Make sure that we're not flipping Y.
GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
return return_null_texture();
}
GrSurfaceDesc desc = origDesc;
desc.fOrigin = texOrigin;
int maxSize = this->caps()->maxTextureSize();
if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
return return_null_texture();
}
GrGLTexture::IDDesc idDesc;
GL_CALL(GenTextures(1, &idDesc.fTextureID));
idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
GrGpuResource::kUncached_LifeCycle;
idDesc.fLifeCycle = lifeCycle;
if (!idDesc.fTextureID) {
return return_null_texture();

View File

@ -114,9 +114,10 @@ private:
// GrGpu overrides
void onResetContext(uint32_t resetBits) override;
GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
size_t rowBytes) override;
GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
const void* srcData, size_t rowBytes) override;
GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
GrGpuResource::LifeCycle lifeCycle,
const void* srcData) override;
GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override;
GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override;
@ -277,8 +278,8 @@ private:
int left = 0, int top = 0,
int width = -1, int height = -1);
bool createRenderTargetObjects(const GrSurfaceDesc&, bool budgeted, GrGLuint texID,
GrGLRenderTarget::IDDesc*);
bool createRenderTargetObjects(const GrSurfaceDesc&, GrGpuResource::LifeCycle lifeCycle,
GrGLuint texID, GrGLRenderTarget::IDDesc*);
enum TempFBOTarget {
kSrc_TempFBOTarget,