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" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 13 | #include "GrTexture.h" |
| 14 | #include "GrTexturePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 15 | #include "GrTypes.h" |
| 16 | #include "SkMath.h" |
| 17 | #include "SkMipMap.h" |
| 18 | #include "SkTypes.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 19 | |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 20 | void GrTexture::dirtyMipMaps(bool mipMapsDirty) { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 21 | if (mipMapsDirty) { |
| 22 | if (kValid_MipMapsStatus == fMipMapsStatus) { |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 23 | fMipMapsStatus = kAllocated_MipMapsStatus; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 24 | } |
| 25 | } else { |
| 26 | const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; |
| 27 | fMipMapsStatus = kValid_MipMapsStatus; |
| 28 | if (sizeChanged) { |
| 29 | // This must not be called until after changing fMipMapsStatus. |
| 30 | this->didChangeGpuMemorySize(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 31 | // TODO(http://skbug.com/4548) - The desc and scratch key should be |
| 32 | // updated to reflect the newly-allocated mipmaps. |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 37 | size_t GrTexture::onGpuMemorySize() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 38 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1, |
| 39 | this->texturePriv().hasMipMaps(), false); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 42 | ///////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame^] | 43 | |
| 44 | namespace { |
| 45 | |
| 46 | // FIXME: This should be refactored with the code in gl/GrGLGpu.cpp. |
| 47 | GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) { |
| 48 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 49 | // can be drawn to by the outside world without the client having |
| 50 | // to render upside down. |
| 51 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 52 | if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
| 53 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 54 | } else { |
| 55 | return desc.fOrigin; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | ////////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 61 | GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 62 | GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 63 | : INHERITED(gpu, desc) |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 64 | , fSamplerType(samplerType) |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 65 | , fHighestFilterMode(highestFilterMode) |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 66 | // Mip color mode is explicitly set after creation via GrTexturePriv |
| 67 | , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 68 | if (wasMipMapDataProvided) { |
| 69 | fMipMapsStatus = kValid_MipMapsStatus; |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 70 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 71 | } else { |
| 72 | fMipMapsStatus = kNotAllocated_MipMapsStatus; |
| 73 | fMaxMipMapLevel = 0; |
| 74 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 75 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 76 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 77 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 78 | const GrRenderTarget* rt = this->asRenderTarget(); |
| 79 | int sampleCount = 0; |
| 80 | if (rt) { |
| 81 | sampleCount = rt->numStencilSamples(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 82 | } |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 83 | GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame^] | 84 | this->origin(), SkToBool(rt), sampleCount, |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 85 | this->texturePriv().hasMipMaps(), key); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 88 | void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height, |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame^] | 89 | GrSurfaceOrigin origin, bool isRenderTarget, int sampleCnt, |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 90 | bool isMipMapped, GrScratchKey* key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 91 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 92 | uint32_t flags = isRenderTarget; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 93 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 94 | SkASSERT(0 == sampleCnt || isRenderTarget); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 95 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 96 | // make sure desc.fConfig fits in 5 bits |
| 97 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 98 | SkASSERT(static_cast<int>(config) < (1 << 5)); |
| 99 | SkASSERT(sampleCnt < (1 << 8)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 100 | SkASSERT(flags < (1 << 10)); |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame^] | 101 | SkASSERT(static_cast<int>(origin) < (1 << 8)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 102 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 103 | GrScratchKey::Builder builder(key, kType, 3); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 104 | builder[0] = width; |
| 105 | builder[1] = height; |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame^] | 106 | builder[2] = config | (isMipMapped << 5) | (sampleCnt << 6) | (flags << 14) | (origin << 24); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame^] | 110 | GrSurfaceOrigin origin = resolve_origin(desc); |
| 111 | return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, origin, |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 112 | SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt, |
| 113 | desc.fIsMipMapped, key); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 114 | } |