Factor GrTexture into public GrTexture and private GrTextureImpl.

R=jvanverth@google.com, robertphillips@google.com

Author: bsalomon@google.com

Review URL: https://codereview.chromium.org/275903002

git-svn-id: http://skia.googlecode.com/svn/trunk@14680 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2014-05-09 17:37:55 +00:00
parent f9dc07c80a
commit bd465d141b
7 changed files with 122 additions and 117 deletions

View File

@ -62,7 +62,7 @@ public:
} }
static GrResourceKey ComputeKey(const GrTextureDesc& desc) { static GrResourceKey ComputeKey(const GrTextureDesc& desc) {
return GrTexture::ComputeScratchKey(desc); return GrTextureImpl::ComputeScratchKey(desc);
} }
int fID; int fID;

View File

@ -1098,7 +1098,7 @@ public:
// lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is // lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is
// set and re-ref the texture, thereby restoring the cache's ref. // set and re-ref the texture, thereby restoring the cache's ref.
SkASSERT(texture->getRefCnt() > 1); SkASSERT(texture->getRefCnt() > 1);
texture->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit); texture->impl()->setFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
texture->unref(); texture->unref();
SkASSERT(NULL != texture->getCacheEntry()); SkASSERT(NULL != texture->getCacheEntry());

View File

@ -15,12 +15,98 @@
class GrResourceKey; class GrResourceKey;
class GrTextureParams; class GrTextureParams;
class GrTextureImpl;
class GrTexture : public GrSurface { class GrTexture : public GrSurface {
public: public:
SK_DECLARE_INST_COUNT(GrTexture) /**
// from GrResource * Approximate number of bytes used by the texture
*/
virtual size_t gpuMemorySize() const SK_OVERRIDE;
// GrSurface overrides
virtual bool readPixels(int left, int top, int width, int height,
GrPixelConfig config,
void* buffer,
size_t rowBytes = 0,
uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
virtual void writePixels(int left, int top, int width, int height,
GrPixelConfig config,
const void* buffer,
size_t rowBytes = 0,
uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.get(); }
virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fRenderTarget.get(); }
/**
* Convert from texels to normalized texture coords for POT textures only. Please don't add
* new callsites for these functions. They are slated for removal.
*/
SkFixed normalizeFixedX(SkFixed x) const {
SkASSERT(GrIsPow2(fDesc.fWidth));
return x >> fShiftFixedX;
}
SkFixed normalizeFixedY(SkFixed y) const {
SkASSERT(GrIsPow2(fDesc.fHeight));
return y >> fShiftFixedY;
}
/**
* Return the native ID or handle to the texture, depending on the
* platform. e.g. on OpenGL, return the texture ID.
*/
virtual GrBackendObject getTextureHandle() const = 0;
#ifdef SK_DEBUG
void validate() const {
this->INHERITED::validate();
this->validateDesc();
}
#endif
GrTextureImpl* impl() { return reinterpret_cast<GrTextureImpl*>(this); }
const GrTextureImpl* impl() const { return reinterpret_cast<const GrTextureImpl*>(this); }
protected:
// A texture refs its rt representation but not vice-versa. It is up to
// the subclass constructor to initialize this pointer.
SkAutoTUnref<GrRenderTarget> fRenderTarget;
GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
: INHERITED(gpu, isWrapped, desc)
, fRenderTarget(NULL) {
// only make sense if alloc size is pow2
fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
}
virtual ~GrTexture();
// GrResource overrides
virtual void onRelease() SK_OVERRIDE;
virtual void onAbandon() SK_OVERRIDE;
void validateDesc() const;
private:
virtual void internal_dispose() const SK_OVERRIDE;
// these two shift a fixed-point value into normalized coordinates
// for this texture if the texture is power of two sized.
int fShiftFixedX;
int fShiftFixedY;
typedef GrSurface INHERITED;
};
class GrTextureImpl : public GrTexture {
public:
SK_DECLARE_INST_COUNT(GrTextureImpl)
/** /**
* Informational texture flags * Informational texture flags
*/ */
@ -50,63 +136,9 @@ public:
return kValid_MipMapsStatus != fMipMapsStatus; return kValid_MipMapsStatus != fMipMapsStatus;
} }
/** bool hasMipMaps() const {
* Approximate number of bytes used by the texture return kNotAllocated_MipMapsStatus != fMipMapsStatus;
*/
virtual size_t gpuMemorySize() const SK_OVERRIDE;
// GrSurface overrides
virtual bool readPixels(int left, int top, int width, int height,
GrPixelConfig config,
void* buffer,
size_t rowBytes = 0,
uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
virtual void writePixels(int left, int top, int width, int height,
GrPixelConfig config,
const void* buffer,
size_t rowBytes = 0,
uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
/**
* @return this texture
*/
virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
/**
* Retrieves the render target underlying this texture that can be passed to
* GrGpu::setRenderTarget().
*
* @return handle to render target or NULL if the texture is not a
* render target
*/
virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
return fRenderTarget.get();
} }
virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
return fRenderTarget.get();
}
// GrTexture
/**
* Convert from texels to normalized texture coords for POT textures
* only.
*/
SkFixed normalizeFixedX(SkFixed x) const {
SkASSERT(GrIsPow2(fDesc.fWidth));
return x >> fShiftFixedX;
}
SkFixed normalizeFixedY(SkFixed y) const {
SkASSERT(GrIsPow2(fDesc.fHeight));
return y >> fShiftFixedY;
}
/**
* Return the native ID or handle to the texture, depending on the
* platform. e.g. on OpenGL, return the texture ID.
*/
virtual GrBackendObject getTextureHandle() const = 0;
/** /**
* Call this when the state of the native API texture object is * Call this when the state of the native API texture object is
@ -114,14 +146,6 @@ public:
*/ */
virtual void invalidateCachedState() = 0; virtual void invalidateCachedState() = 0;
#ifdef SK_DEBUG
void validate() const {
this->INHERITED::validate();
this->validateDesc();
}
#endif
static GrResourceKey ComputeKey(const GrGpu* gpu, static GrResourceKey ComputeKey(const GrGpu* gpu,
const GrTextureParams* params, const GrTextureParams* params,
const GrTextureDesc& desc, const GrTextureDesc& desc,
@ -131,26 +155,10 @@ public:
static bool NeedsBilerp(const GrResourceKey& key); static bool NeedsBilerp(const GrResourceKey& key);
protected: protected:
// A texture refs its rt representation but not vice-versa. It is up to GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
// the subclass constructor to initialize this pointer.
SkAutoTUnref<GrRenderTarget> fRenderTarget;
GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
: INHERITED(gpu, isWrapped, desc) : INHERITED(gpu, isWrapped, desc)
, fRenderTarget(NULL)
, fMipMapsStatus(kNotAllocated_MipMapsStatus) { , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
// only make sense if alloc size is pow2
fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
} }
virtual ~GrTexture();
// GrResource overrides
virtual void onRelease() SK_OVERRIDE;
virtual void onAbandon() SK_OVERRIDE;
void validateDesc() const;
private: private:
enum MipMapsStatus { enum MipMapsStatus {
@ -159,16 +167,9 @@ private:
kValid_MipMapsStatus kValid_MipMapsStatus
}; };
// these two shift a fixed-point value into normalized coordinates
// for this texture if the texture is power of two sized.
int fShiftFixedX;
int fShiftFixedY;
MipMapsStatus fMipMapsStatus; MipMapsStatus fMipMapsStatus;
virtual void internal_dispose() const SK_OVERRIDE; typedef GrTexture INHERITED;
typedef GrSurface INHERITED;
}; };
/** /**
@ -204,6 +205,7 @@ public:
fTexture.reset(SkSafeRef(texture)); fTexture.reset(SkSafeRef(texture));
return texture; return texture;
} }
private: private:
SkAutoTUnref<GrTexture> fTexture; SkAutoTUnref<GrTexture> fTexture;
SkIPoint fOffset; SkIPoint fOffset;

View File

@ -237,7 +237,7 @@ void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes)
GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc, GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
const GrCacheID& cacheID, const GrCacheID& cacheID,
const GrTextureParams* params) { const GrTextureParams* params) {
GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID); GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID);
GrCacheable* resource = fResourceCache->find(resourceKey); GrCacheable* resource = fResourceCache->find(resourceKey);
SkSafeRef(resource); SkSafeRef(resource);
return static_cast<GrTexture*>(resource); return static_cast<GrTexture*>(resource);
@ -246,7 +246,7 @@ GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
bool GrContext::isTextureInCache(const GrTextureDesc& desc, bool GrContext::isTextureInCache(const GrTextureDesc& desc,
const GrCacheID& cacheID, const GrCacheID& cacheID,
const GrTextureParams* params) const { const GrTextureParams* params) const {
GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID); GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID);
return fResourceCache->hasKey(resourceKey); return fResourceCache->hasKey(resourceKey);
} }
@ -383,13 +383,13 @@ GrTexture* GrContext::createTexture(const GrTextureParams* params,
void* srcData, void* srcData,
size_t rowBytes, size_t rowBytes,
GrResourceKey* cacheKey) { GrResourceKey* cacheKey) {
GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID); GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID);
GrTexture* texture; GrTexture* texture;
if (GrTexture::NeedsResizing(resourceKey)) { if (GrTextureImpl::NeedsResizing(resourceKey)) {
texture = this->createResizedTexture(desc, cacheID, texture = this->createResizedTexture(desc, cacheID,
srcData, rowBytes, srcData, rowBytes,
GrTexture::NeedsBilerp(resourceKey)); GrTextureImpl::NeedsBilerp(resourceKey));
} else { } else {
texture= fGpu->createTexture(desc, srcData, rowBytes); texture= fGpu->createTexture(desc, srcData, rowBytes);
} }
@ -413,7 +413,7 @@ static GrTexture* create_scratch_texture(GrGpu* gpu,
const GrTextureDesc& desc) { const GrTextureDesc& desc) {
GrTexture* texture = gpu->createTexture(desc, NULL, 0); GrTexture* texture = gpu->createTexture(desc, NULL, 0);
if (NULL != texture) { if (NULL != texture) {
GrResourceKey key = GrTexture::ComputeScratchKey(texture->desc()); GrResourceKey key = GrTextureImpl::ComputeScratchKey(texture->desc());
// Adding a resource could put us overbudget. Try to free up the // Adding a resource could put us overbudget. Try to free up the
// necessary space before adding it. // necessary space before adding it.
resourceCache->purgeAsNeeded(1, texture->gpuMemorySize()); resourceCache->purgeAsNeeded(1, texture->gpuMemorySize());
@ -453,7 +453,7 @@ GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, Scra
int origHeight = desc.fHeight; int origHeight = desc.fHeight;
do { do {
GrResourceKey key = GrTexture::ComputeScratchKey(desc); GrResourceKey key = GrTextureImpl::ComputeScratchKey(desc);
// Ensure we have exclusive access to the texture so future 'find' calls don't return it // Ensure we have exclusive access to the texture so future 'find' calls don't return it
resource = fResourceCache->find(key, GrResourceCache::kHide_OwnershipFlag); resource = fResourceCache->find(key, GrResourceCache::kHide_OwnershipFlag);
if (NULL != resource) { if (NULL != resource) {
@ -543,7 +543,7 @@ void GrContext::unlockScratchTexture(GrTexture* texture) {
// Instead, give up the cache's ref and leave the decision up to // Instead, give up the cache's ref and leave the decision up to
// addExistingTextureToCache once its ref count reaches 0. For // addExistingTextureToCache once its ref count reaches 0. For
// this to work we need to leave it in the exclusive list. // this to work we need to leave it in the exclusive list.
texture->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit); texture->impl()->setFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
// Give up the cache's ref to the texture // Give up the cache's ref to the texture
texture->unref(); texture->unref();
} }

View File

@ -26,13 +26,12 @@ GrTexture::~GrTexture() {
* textures back in the texture cache when their ref count goes to zero. * textures back in the texture cache when their ref count goes to zero.
*/ */
void GrTexture::internal_dispose() const { void GrTexture::internal_dispose() const {
if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit) &&
if (this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit) &&
NULL != this->INHERITED::getContext()) { NULL != this->INHERITED::getContext()) {
GrTexture* nonConstThis = const_cast<GrTexture *>(this); GrTexture* nonConstThis = const_cast<GrTexture *>(this);
this->fRefCnt = 1; // restore ref count to initial setting this->fRefCnt = 1; // restore ref count to initial setting
nonConstThis->resetFlag((GrTextureFlags) kReturnToCache_FlagBit); nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis); nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
// Note: "this" texture might be freed inside addExistingTextureToCache // Note: "this" texture might be freed inside addExistingTextureToCache
@ -44,7 +43,7 @@ void GrTexture::internal_dispose() const {
this->INHERITED::internal_dispose(); this->INHERITED::internal_dispose();
} }
void GrTexture::dirtyMipMaps(bool mipMapsDirty) { void GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) {
if (mipMapsDirty) { if (mipMapsDirty) {
if (kValid_MipMapsStatus == fMipMapsStatus) { if (kValid_MipMapsStatus == fMipMapsStatus) {
fMipMapsStatus = kAllocated_MipMapsStatus; fMipMapsStatus = kAllocated_MipMapsStatus;
@ -63,7 +62,7 @@ size_t GrTexture::gpuMemorySize() const {
size_t textureSize = (size_t) fDesc.fWidth * size_t textureSize = (size_t) fDesc.fWidth *
fDesc.fHeight * fDesc.fHeight *
GrBytesPerPixel(fDesc.fConfig); GrBytesPerPixel(fDesc.fConfig);
if (kNotAllocated_MipMapsStatus != fMipMapsStatus) { if (this->impl()->hasMipMaps()) {
// We don't have to worry about the mipmaps being a different size than // We don't have to worry about the mipmaps being a different size than
// we'd expect because we never change fDesc.fWidth/fHeight. // we'd expect because we never change fDesc.fWidth/fHeight.
textureSize *= 2; textureSize *= 2;
@ -100,7 +99,7 @@ void GrTexture::writePixels(int left, int top, int width, int height,
} }
void GrTexture::onRelease() { void GrTexture::onRelease() {
SkASSERT(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit)); SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
INHERITED::onRelease(); INHERITED::onRelease();
} }
@ -130,6 +129,8 @@ void GrTexture::validateDesc() const {
} }
} }
//////////////////////////////////////////////////////////////////////////////
// These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture // These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
// key // key
enum TextureFlags { enum TextureFlags {
@ -186,7 +187,9 @@ GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
} }
} }
GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu, //////////////////////////////////////////////////////////////////////////////
GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu,
const GrTextureParams* params, const GrTextureParams* params,
const GrTextureDesc& desc, const GrTextureDesc& desc,
const GrCacheID& cacheID) { const GrCacheID& cacheID) {
@ -194,7 +197,7 @@ GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
return GrResourceKey(cacheID, texture_resource_type(), flags); return GrResourceKey(cacheID, texture_resource_type(), flags);
} }
GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) { GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) {
GrCacheID::Key idKey; GrCacheID::Key idKey;
// Instead of a client-provided key of the texture contents we create a key from the // Instead of a client-provided key of the texture contents we create a key from the
// descriptor. // descriptor.
@ -213,10 +216,10 @@ GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) {
return GrResourceKey(cacheID, texture_resource_type(), 0); return GrResourceKey(cacheID, texture_resource_type(), 0);
} }
bool GrTexture::NeedsResizing(const GrResourceKey& key) { bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) {
return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
} }
bool GrTexture::NeedsBilerp(const GrResourceKey& key) { bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) {
return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
} }

View File

@ -45,7 +45,7 @@ private:
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class GrGLTexture : public GrTexture { class GrGLTexture : public GrTextureImpl {
public: public:
struct TexParams { struct TexParams {
@ -105,7 +105,7 @@ private:
const Desc& textureDesc, const Desc& textureDesc,
const GrGLRenderTarget::Desc* rtDesc); const GrGLRenderTarget::Desc* rtDesc);
typedef GrTexture INHERITED; typedef GrTextureImpl INHERITED;
}; };
#endif #endif

View File

@ -479,7 +479,7 @@ bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
if (this->uploadTexData(desc, false, if (this->uploadTexData(desc, false,
left, top, width, height, left, top, width, height,
config, buffer, rowBytes)) { config, buffer, rowBytes)) {
texture->dirtyMipMaps(true); texture->impl()->dirtyMipMaps(true);
return true; return true;
} else { } else {
return false; return false;
@ -1583,8 +1583,8 @@ void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
} }
GrTexture *texture = rt->asTexture(); GrTexture *texture = rt->asTexture();
if (texture) { if (NULL != texture) {
texture->dirtyMipMaps(true); texture->impl()->dirtyMipMaps(true);
} }
} }