2011-04-05 17:08:27 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2011-04-05 17:08:27 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2011-04-05 17:08:27 +00:00
|
|
|
#include "GrTexture.h"
|
2011-07-29 15:13:20 +00:00
|
|
|
|
2011-04-05 17:08:27 +00:00
|
|
|
#include "GrContext.h"
|
2013-03-25 18:19:00 +00:00
|
|
|
#include "GrDrawTargetCaps.h"
|
2011-05-02 21:14:59 +00:00
|
|
|
#include "GrGpu.h"
|
2011-07-29 15:13:20 +00:00
|
|
|
#include "GrRenderTarget.h"
|
2012-06-04 20:05:28 +00:00
|
|
|
#include "GrResourceCache.h"
|
2011-05-02 12:53:34 +00:00
|
|
|
|
2013-04-09 15:04:12 +00:00
|
|
|
GrTexture::~GrTexture() {
|
|
|
|
if (NULL != fRenderTarget.get()) {
|
|
|
|
fRenderTarget.get()->owningTextureDestroyed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
/**
|
2012-06-22 12:41:43 +00:00
|
|
|
* This method allows us to interrupt the normal deletion process and place
|
|
|
|
* textures back in the texture cache when their ref count goes to zero.
|
|
|
|
*/
|
|
|
|
void GrTexture::internal_dispose() const {
|
2014-05-09 20:46:48 +00:00
|
|
|
if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit) &&
|
2012-06-22 12:41:43 +00:00
|
|
|
NULL != this->INHERITED::getContext()) {
|
|
|
|
GrTexture* nonConstThis = const_cast<GrTexture *>(this);
|
2014-07-21 21:24:01 +00:00
|
|
|
this->ref(); // restore ref count to initial setting
|
2012-06-22 12:41:43 +00:00
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
|
2012-06-22 12:41:43 +00:00
|
|
|
nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
// Note: "this" texture might be freed inside addExistingTextureToCache
|
2012-06-25 17:26:29 +00:00
|
|
|
// if it is purged.
|
2012-06-22 12:41:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->INHERITED::internal_dispose();
|
|
|
|
}
|
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
void GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) {
|
2014-05-05 19:09:13 +00:00
|
|
|
if (mipMapsDirty) {
|
|
|
|
if (kValid_MipMapsStatus == fMipMapsStatus) {
|
|
|
|
fMipMapsStatus = kAllocated_MipMapsStatus;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
|
|
|
|
fMipMapsStatus = kValid_MipMapsStatus;
|
|
|
|
if (sizeChanged) {
|
|
|
|
// This must not be called until after changing fMipMapsStatus.
|
|
|
|
this->didChangeGpuMemorySize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GrTexture::gpuMemorySize() const {
|
|
|
|
size_t textureSize = (size_t) fDesc.fWidth *
|
|
|
|
fDesc.fHeight *
|
|
|
|
GrBytesPerPixel(fDesc.fConfig);
|
2014-05-30 13:55:58 +00:00
|
|
|
|
|
|
|
if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
|
2014-07-16 22:21:13 +00:00
|
|
|
textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
|
2014-05-30 13:55:58 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
if (this->impl()->hasMipMaps()) {
|
2014-05-05 19:09:13 +00:00
|
|
|
// We don't have to worry about the mipmaps being a different size than
|
|
|
|
// we'd expect because we never change fDesc.fWidth/fHeight.
|
|
|
|
textureSize *= 2;
|
|
|
|
}
|
|
|
|
return textureSize;
|
|
|
|
}
|
|
|
|
|
2011-04-05 17:08:27 +00:00
|
|
|
bool GrTexture::readPixels(int left, int top, int width, int height,
|
2011-11-16 20:36:03 +00:00
|
|
|
GrPixelConfig config, void* buffer,
|
2012-08-20 19:22:38 +00:00
|
|
|
size_t rowBytes, uint32_t pixelOpsFlags) {
|
2011-04-05 17:08:27 +00:00
|
|
|
// go through context so that all necessary flushing occurs
|
2011-11-16 20:36:03 +00:00
|
|
|
GrContext* context = this->getContext();
|
|
|
|
if (NULL == context) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-05 17:08:27 +00:00
|
|
|
return context->readTexturePixels(this,
|
2012-08-20 19:22:38 +00:00
|
|
|
left, top, width, height,
|
|
|
|
config, buffer, rowBytes,
|
|
|
|
pixelOpsFlags);
|
2011-11-16 20:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GrTexture::writePixels(int left, int top, int width, int height,
|
|
|
|
GrPixelConfig config, const void* buffer,
|
2012-08-20 19:22:38 +00:00
|
|
|
size_t rowBytes, uint32_t pixelOpsFlags) {
|
2011-11-16 20:36:03 +00:00
|
|
|
// go through context so that all necessary flushing occurs
|
|
|
|
GrContext* context = this->getContext();
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context->writeTexturePixels(this,
|
2012-08-20 19:22:38 +00:00
|
|
|
left, top, width, height,
|
|
|
|
config, buffer, rowBytes,
|
|
|
|
pixelOpsFlags);
|
2011-04-05 17:08:27 +00:00
|
|
|
}
|
2011-07-26 12:32:36 +00:00
|
|
|
|
2014-07-29 15:01:52 +00:00
|
|
|
void GrTexture::abandonReleaseCommon() {
|
|
|
|
// In debug builds the resource cache tracks removed/exclusive textures and has an unref'ed ptr.
|
|
|
|
// After abandon() or release() the resource cache will be unreachable (getContext() == NULL).
|
|
|
|
// So we readd the texture to the cache here so that it is removed from the exclusive list and
|
|
|
|
// there is no longer an unref'ed ptr to the texture in the cache.
|
|
|
|
if (this->impl()->isSetFlag((GrTextureFlags)GrTextureImpl::kReturnToCache_FlagBit)) {
|
|
|
|
SkASSERT(!this->wasDestroyed());
|
|
|
|
this->ref(); // restores the ref the resource cache gave up when it marked this exclusive.
|
|
|
|
this->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
|
|
|
|
this->getContext()->addExistingTextureToCache(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 12:41:43 +00:00
|
|
|
void GrTexture::onRelease() {
|
2014-07-29 15:01:52 +00:00
|
|
|
this->abandonReleaseCommon();
|
2014-05-09 20:46:48 +00:00
|
|
|
SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
|
2012-09-05 18:37:39 +00:00
|
|
|
INHERITED::onRelease();
|
2012-06-22 12:41:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 15:13:20 +00:00
|
|
|
void GrTexture::onAbandon() {
|
2014-07-29 15:01:52 +00:00
|
|
|
this->abandonReleaseCommon();
|
2013-04-09 15:04:12 +00:00
|
|
|
if (NULL != fRenderTarget.get()) {
|
2011-07-29 15:13:20 +00:00
|
|
|
fRenderTarget->abandon();
|
|
|
|
}
|
2012-09-05 18:37:39 +00:00
|
|
|
INHERITED::onAbandon();
|
2011-07-29 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 12:48:45 +00:00
|
|
|
void GrTexture::validateDesc() const {
|
|
|
|
if (NULL != this->asRenderTarget()) {
|
|
|
|
// This texture has a render target
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
|
2012-06-04 12:48:45 +00:00
|
|
|
|
|
|
|
if (NULL != this->asRenderTarget()->getStencilBuffer()) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
|
2012-06-04 12:48:45 +00:00
|
|
|
} else {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
|
2012-06-04 12:48:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
|
2012-06-04 12:48:45 +00:00
|
|
|
} else {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
|
|
|
|
SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
|
|
|
|
SkASSERT(0 == fDesc.fSampleCnt);
|
2012-06-04 12:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 20:05:28 +00:00
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
// These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
|
2012-08-08 10:42:44 +00:00
|
|
|
// key
|
2012-12-20 15:13:01 +00:00
|
|
|
enum TextureFlags {
|
|
|
|
/**
|
|
|
|
* The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
|
|
|
|
* hardware doesn't support that feature.
|
2012-06-04 20:05:28 +00:00
|
|
|
*/
|
2012-12-20 15:13:01 +00:00
|
|
|
kStretchToPOT_TextureFlag = 0x1,
|
|
|
|
/**
|
2013-07-25 18:49:07 +00:00
|
|
|
* The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
|
|
|
|
* stretched texture should be bilerped.
|
2012-06-04 20:05:28 +00:00
|
|
|
*/
|
2013-07-25 18:49:07 +00:00
|
|
|
kBilerp_TextureFlag = 0x2,
|
2012-06-04 20:05:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2012-12-20 15:13:01 +00:00
|
|
|
GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
|
|
|
|
const GrTextureParams* params,
|
|
|
|
const GrTextureDesc& desc) {
|
|
|
|
GrResourceKey::ResourceFlags flags = 0;
|
|
|
|
bool tiled = NULL != params && params->isTiled();
|
2013-03-25 15:38:39 +00:00
|
|
|
if (tiled && !gpu->caps()->npotTextureTileSupport()) {
|
2014-06-06 13:35:28 +00:00
|
|
|
if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
|
2012-12-20 15:13:01 +00:00
|
|
|
flags |= kStretchToPOT_TextureFlag;
|
2013-07-25 18:49:07 +00:00
|
|
|
switch(params->filterMode()) {
|
|
|
|
case GrTextureParams::kNone_FilterMode:
|
|
|
|
break;
|
|
|
|
case GrTextureParams::kBilerp_FilterMode:
|
|
|
|
case GrTextureParams::kMipMap_FilterMode:
|
|
|
|
flags |= kBilerp_TextureFlag;
|
|
|
|
break;
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 15:13:01 +00:00
|
|
|
return flags;
|
|
|
|
}
|
2012-06-04 20:05:28 +00:00
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
GrResourceKey::ResourceType texture_resource_type() {
|
|
|
|
static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
|
|
|
|
return gType;
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
2013-02-07 00:35:25 +00:00
|
|
|
|
|
|
|
// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
|
|
|
|
GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
|
|
|
|
// 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.
|
|
|
|
bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
|
|
|
|
if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
|
|
|
|
return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
|
|
|
|
} else {
|
|
|
|
return desc.fOrigin;
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu,
|
2012-07-25 21:27:09 +00:00
|
|
|
const GrTextureParams* params,
|
2012-06-04 20:05:28 +00:00
|
|
|
const GrTextureDesc& desc,
|
2012-12-20 15:13:01 +00:00
|
|
|
const GrCacheID& cacheID) {
|
|
|
|
GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
|
|
|
|
return GrResourceKey(cacheID, texture_resource_type(), flags);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) {
|
2012-12-20 15:13:01 +00:00
|
|
|
GrCacheID::Key idKey;
|
|
|
|
// Instead of a client-provided key of the texture contents we create a key from the
|
|
|
|
// descriptor.
|
2013-02-05 19:50:46 +00:00
|
|
|
GR_STATIC_ASSERT(sizeof(idKey) >= 16);
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(desc.fHeight < (1 << 16));
|
|
|
|
SkASSERT(desc.fWidth < (1 << 16));
|
2012-12-20 15:13:01 +00:00
|
|
|
idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
|
|
|
|
idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
|
|
|
|
idKey.fData32[2] = desc.fFlags;
|
2013-02-07 00:35:25 +00:00
|
|
|
idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
|
2013-02-05 19:50:46 +00:00
|
|
|
static const int kPadSize = sizeof(idKey) - 16;
|
|
|
|
GR_STATIC_ASSERT(kPadSize >= 0);
|
|
|
|
memset(idKey.fData8 + 16, 0, kPadSize);
|
2012-12-20 15:13:01 +00:00
|
|
|
|
|
|
|
GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
|
|
|
|
return GrResourceKey(cacheID, texture_resource_type(), 0);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) {
|
2012-12-20 15:13:01 +00:00
|
|
|
return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 20:46:48 +00:00
|
|
|
bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) {
|
2013-07-25 18:49:07 +00:00
|
|
|
return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|