bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 8 | #include "GrContext.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 9 | #include "GrCaps.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 10 | #include "GrGpu.h" |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 11 | #include "GrResourceKey.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
| 13 | #include "GrRenderTargetPriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 14 | #include "GrTexture.h" |
| 15 | #include "GrTexturePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 16 | #include "GrTypes.h" |
| 17 | #include "SkMath.h" |
| 18 | #include "SkMipMap.h" |
| 19 | #include "SkTypes.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 20 | |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 21 | void GrTexture::dirtyMipMaps(bool mipMapsDirty) { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 22 | if (mipMapsDirty) { |
| 23 | if (kValid_MipMapsStatus == fMipMapsStatus) { |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 24 | fMipMapsStatus = kAllocated_MipMapsStatus; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 25 | } |
| 26 | } else { |
| 27 | const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; |
| 28 | fMipMapsStatus = kValid_MipMapsStatus; |
| 29 | if (sizeChanged) { |
| 30 | // This must not be called until after changing fMipMapsStatus. |
| 31 | this->didChangeGpuMemorySize(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 32 | // TODO(http://skbug.com/4548) - The desc and scratch key should be |
| 33 | // updated to reflect the newly-allocated mipmaps. |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 38 | size_t GrTexture::onGpuMemorySize() const { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 39 | size_t textureSize; |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 40 | |
| 41 | if (GrPixelConfigIsCompressed(fDesc.fConfig)) { |
krajcevski | 7ef2162 | 2014-07-16 15:21:13 -0700 | [diff] [blame] | 42 | textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight); |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 43 | } else { |
| 44 | textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig); |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 47 | if (this->texturePriv().hasMipMaps()) { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 48 | // We don't have to worry about the mipmaps being a different size than |
| 49 | // we'd expect because we never change fDesc.fWidth/fHeight. |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 50 | textureSize += textureSize/3; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 51 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 52 | |
| 53 | SkASSERT(!SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
robertphillips | f1c6cd7 | 2016-08-18 07:11:13 -0700 | [diff] [blame] | 54 | SkASSERT(textureSize <= WorstCaseSize(fDesc)); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 55 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 56 | return textureSize; |
| 57 | } |
| 58 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 59 | void GrTexture::validateDesc() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 60 | if (this->asRenderTarget()) { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 61 | // This texture has a render target |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 62 | SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 63 | SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples()); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 64 | } else { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 65 | SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 66 | SkASSERT(0 == fDesc.fSampleCnt); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 70 | ////////////////////////////////////////////////////////////////////////////// |
| 71 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 72 | namespace { |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 73 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 74 | // FIXME: This should be refactored with the code in gl/GrGLGpu.cpp. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 75 | GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) { |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 76 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 77 | // can be drawn to by the outside world without the client having |
| 78 | // to render upside down. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 79 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag); |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 80 | if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
| 81 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 82 | } else { |
| 83 | return desc.fOrigin; |
| 84 | } |
| 85 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 86 | } |
| 87 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 88 | ////////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 89 | GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType, |
| 90 | bool wasMipMapDataProvided) |
| 91 | : INHERITED(gpu, desc) |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 92 | , fSamplerType(samplerType) |
| 93 | // Gamma treatment is explicitly set after creation via GrTexturePriv |
| 94 | , fGammaTreatment(SkSourceGammaTreatment::kIgnore) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 95 | if (wasMipMapDataProvided) { |
| 96 | fMipMapsStatus = kValid_MipMapsStatus; |
| 97 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight); |
| 98 | } else { |
| 99 | fMipMapsStatus = kNotAllocated_MipMapsStatus; |
| 100 | fMaxMipMapLevel = 0; |
| 101 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 102 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 103 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 104 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
| 105 | if (!GrPixelConfigIsCompressed(fDesc.fConfig)) { |
| 106 | GrTexturePriv::ComputeScratchKey(fDesc, key); |
| 107 | } |
| 108 | } |
| 109 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 110 | void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { |
| 111 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 112 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 113 | GrSurfaceOrigin origin = resolve_origin(desc); |
| 114 | uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag; |
| 115 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 116 | // make sure desc.fConfig fits in 5 bits |
| 117 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
| 118 | SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 119 | SkASSERT(desc.fSampleCnt < (1 << 8)); |
| 120 | SkASSERT(flags < (1 << 10)); |
| 121 | SkASSERT(static_cast<int>(origin) < (1 << 8)); |
| 122 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 123 | GrScratchKey::Builder builder(key, kType, 3); |
| 124 | builder[0] = desc.fWidth; |
| 125 | builder[1] = desc.fHeight; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 126 | builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14) |
| 127 | | (origin << 24); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 128 | } |