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