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 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 38 | size_t GrTexture::onGpuMemorySize() const { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 39 | return GrSurface::ComputeSize(fDesc, 1, this->texturePriv().hasMipMaps()); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 40 | } |
| 41 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 42 | void GrTexture::validateDesc() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 43 | if (this->asRenderTarget()) { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 44 | // This texture has a render target |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 45 | SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 46 | SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples()); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 47 | } else { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 48 | SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 49 | SkASSERT(0 == fDesc.fSampleCnt); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 52 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 53 | ////////////////////////////////////////////////////////////////////////////// |
| 54 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 55 | namespace { |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 56 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 57 | // FIXME: This should be refactored with the code in gl/GrGLGpu.cpp. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 58 | GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) { |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 59 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 60 | // can be drawn to by the outside world without the client having |
| 61 | // to render upside down. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 62 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag); |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 63 | if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
| 64 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 65 | } else { |
| 66 | return desc.fOrigin; |
| 67 | } |
| 68 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 69 | } |
| 70 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 71 | ////////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 72 | GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 73 | GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 74 | : INHERITED(gpu, desc) |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 75 | , fSamplerType(samplerType) |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 76 | , fHighestFilterMode(highestFilterMode) |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 77 | // Mip color mode is explicitly set after creation via GrTexturePriv |
| 78 | , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 79 | if (wasMipMapDataProvided) { |
| 80 | fMipMapsStatus = kValid_MipMapsStatus; |
| 81 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight); |
| 82 | } else { |
| 83 | fMipMapsStatus = kNotAllocated_MipMapsStatus; |
| 84 | fMaxMipMapLevel = 0; |
| 85 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 86 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 87 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 88 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
| 89 | if (!GrPixelConfigIsCompressed(fDesc.fConfig)) { |
| 90 | GrTexturePriv::ComputeScratchKey(fDesc, key); |
| 91 | } |
| 92 | } |
| 93 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 94 | void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { |
| 95 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 96 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 97 | GrSurfaceOrigin origin = resolve_origin(desc); |
| 98 | uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag; |
| 99 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 100 | // make sure desc.fConfig fits in 5 bits |
| 101 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
| 102 | SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 103 | SkASSERT(desc.fSampleCnt < (1 << 8)); |
| 104 | SkASSERT(flags < (1 << 10)); |
| 105 | SkASSERT(static_cast<int>(origin) < (1 << 8)); |
| 106 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 107 | GrScratchKey::Builder builder(key, kType, 3); |
| 108 | builder[0] = desc.fWidth; |
| 109 | builder[1] = desc.fHeight; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 110 | builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14) |
| 111 | | (origin << 24); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 112 | } |