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
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2012-06-04 20:05:28 +00:00
|
|
|
#include "GrResourceCache.h"
|
2014-09-30 19:18:44 +00:00
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "GrTexturePriv.h"
|
2011-05-02 12:53:34 +00:00
|
|
|
|
2014-09-30 19:18:44 +00:00
|
|
|
void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
|
2014-05-05 19:09:13 +00:00
|
|
|
if (mipMapsDirty) {
|
|
|
|
if (kValid_MipMapsStatus == fMipMapsStatus) {
|
2014-09-30 19:18:44 +00:00
|
|
|
fMipMapsStatus = kAllocated_MipMapsStatus;
|
2014-05-05 19:09:13 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2014-08-11 21:19:09 +00:00
|
|
|
size_t textureSize;
|
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-08-11 21:19:09 +00:00
|
|
|
} else {
|
|
|
|
textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
|
2014-05-30 13:55:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 19:18:44 +00:00
|
|
|
if (this->texturePriv().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;
|
|
|
|
}
|
|
|
|
|
2012-06-04 12:48:45 +00:00
|
|
|
void GrTexture::validateDesc() const {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (this->asRenderTarget()) {
|
2012-06-04 12:48:45 +00:00
|
|
|
// This texture has a render target
|
2014-10-28 21:33:06 +00:00
|
|
|
SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
|
2012-06-04 12:48:45 +00:00
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
if (this->asRenderTarget()->getStencilBuffer()) {
|
2014-10-28 21:33:06 +00:00
|
|
|
SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
|
2012-06-04 12:48:45 +00:00
|
|
|
} else {
|
2014-10-28 21:33:06 +00:00
|
|
|
SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
|
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 {
|
2014-10-28 21:33:06 +00:00
|
|
|
SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
|
|
|
|
SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
|
2013-08-17 00:02:59 +00:00
|
|
|
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,
|
2014-10-28 21:33:06 +00:00
|
|
|
const GrSurfaceDesc& desc) {
|
2012-12-20 15:13:01 +00:00
|
|
|
GrResourceKey::ResourceFlags flags = 0;
|
2014-09-05 20:34:00 +00:00
|
|
|
bool tiled = 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
|
|
|
|
2013-02-07 00:35:25 +00:00
|
|
|
// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
|
2014-10-28 21:33:06 +00:00
|
|
|
GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
|
2013-02-07 00:35:25 +00:00
|
|
|
// 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.
|
2014-10-28 21:33:06 +00:00
|
|
|
bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
|
2013-02-07 00:35:25 +00:00
|
|
|
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
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2014-10-28 21:33:06 +00:00
|
|
|
GrTexture::GrTexture(GrGpu* gpu, bool isWrapped, const GrSurfaceDesc& desc)
|
2014-08-28 16:54:34 +00:00
|
|
|
: INHERITED(gpu, isWrapped, desc)
|
|
|
|
, fMipMapsStatus(kNotAllocated_MipMapsStatus) {
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
if (!isWrapped) {
|
|
|
|
this->setScratchKey(GrTexturePriv::ComputeScratchKey(desc));
|
|
|
|
}
|
2014-09-30 19:18:44 +00:00
|
|
|
// only make sense if alloc size is pow2
|
|
|
|
fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
|
|
|
|
fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
|
2014-08-28 16:54:34 +00:00
|
|
|
}
|
2014-05-09 20:46:48 +00:00
|
|
|
|
2014-09-30 19:18:44 +00:00
|
|
|
GrResourceKey GrTexturePriv::ComputeKey(const GrGpu* gpu,
|
2012-07-25 21:27:09 +00:00
|
|
|
const GrTextureParams* params,
|
2014-10-28 21:33:06 +00:00
|
|
|
const GrSurfaceDesc& desc,
|
2012-12-20 15:13:01 +00:00
|
|
|
const GrCacheID& cacheID) {
|
|
|
|
GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
|
2014-10-08 15:40:09 +00:00
|
|
|
return GrResourceKey(cacheID, ResourceType(), flags);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 21:33:06 +00:00
|
|
|
GrResourceKey GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& 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);
|
2014-10-08 15:40:09 +00:00
|
|
|
return GrResourceKey(cacheID, ResourceType(), 0);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 19:18:44 +00:00
|
|
|
bool GrTexturePriv::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-09-30 19:18:44 +00:00
|
|
|
bool GrTexturePriv::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
|
|
|
}
|