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"
|
2015-05-22 15:01:09 +00:00
|
|
|
#include "GrCaps.h"
|
2011-05-02 21:14:59 +00:00
|
|
|
#include "GrGpu.h"
|
2014-12-30 20:50:52 +00:00
|
|
|
#include "GrResourceKey.h"
|
2015-02-23 17:06:38 +00:00
|
|
|
#include "GrRenderTarget.h"
|
|
|
|
#include "GrRenderTargetPriv.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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 19:13:39 +00:00
|
|
|
size_t GrTexture::onGpuMemorySize() 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.
|
2015-08-13 12:19:14 +00:00
|
|
|
textureSize += textureSize/3;
|
2014-05-05 19:09:13 +00:00
|
|
|
}
|
2015-08-13 12:19:14 +00:00
|
|
|
|
|
|
|
SkASSERT(!SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
|
|
|
|
SkASSERT(textureSize <= WorseCaseSize(fDesc));
|
|
|
|
|
2014-05-05 19:09:13 +00:00
|
|
|
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));
|
2015-06-12 15:59:45 +00:00
|
|
|
SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
|
2012-06-04 12:48:45 +00:00
|
|
|
} else {
|
2014-10-28 21:33:06 +00:00
|
|
|
SkASSERT(0 == (fDesc.fFlags & kRenderTarget_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-06-04 20:05:28 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-12-22 19:44:19 +00:00
|
|
|
// FIXME: This should be refactored with the code in gl/GrGLGpu.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
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2015-01-14 18:42:08 +00:00
|
|
|
GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
|
|
|
|
: INHERITED(gpu, lifeCycle, desc)
|
2014-08-28 16:54:34 +00:00
|
|
|
, fMipMapsStatus(kNotAllocated_MipMapsStatus) {
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-06-18 16:12:16 +00:00
|
|
|
if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig)) {
|
2014-12-30 20:50:52 +00:00
|
|
|
GrScratchKey key;
|
|
|
|
GrTexturePriv::ComputeScratchKey(desc, &key);
|
|
|
|
this->setScratchKey(key);
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
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-12-30 20:50:52 +00:00
|
|
|
void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
|
|
|
|
static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
|
|
|
|
|
|
|
|
GrScratchKey::Builder builder(key, kType, 2);
|
|
|
|
|
|
|
|
GrSurfaceOrigin origin = resolve_origin(desc);
|
|
|
|
uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
|
|
|
|
|
|
|
|
SkASSERT(desc.fWidth <= SK_MaxU16);
|
|
|
|
SkASSERT(desc.fHeight <= SK_MaxU16);
|
|
|
|
SkASSERT(static_cast<int>(desc.fConfig) < (1 << 6));
|
|
|
|
SkASSERT(desc.fSampleCnt < (1 << 8));
|
|
|
|
SkASSERT(flags < (1 << 10));
|
|
|
|
SkASSERT(static_cast<int>(origin) < (1 << 8));
|
|
|
|
|
|
|
|
builder[0] = desc.fWidth | (desc.fHeight << 16);
|
|
|
|
builder[1] = desc.fConfig | (desc.fSampleCnt << 6) | (flags << 14) | (origin << 24);
|
2012-06-04 20:05:28 +00:00
|
|
|
}
|