robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/gpu/GrContext.h" |
| 9 | #include "include/gpu/GrRenderTarget.h" |
| 10 | #include "include/gpu/GrSurface.h" |
| 11 | #include "include/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrOpList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrResourceProvider.h" |
| 14 | #include "src/gpu/GrSurfacePriv.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkMathPriv.h" |
| 17 | #include "src/gpu/SkGr.h" |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 18 | |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 19 | size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, GrRenderable renderable, |
| 20 | int renderTargetSampleCnt, bool binSize) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 21 | size_t size; |
| 22 | |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 23 | int width = binSize ? GrResourceProvider::MakeApprox(desc.fWidth) : desc.fWidth; |
| 24 | int height = binSize ? GrResourceProvider::MakeApprox(desc.fHeight) : desc.fHeight; |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 25 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 26 | if (renderable == GrRenderable::kYes) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 27 | // We own one color value for each MSAA sample. |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 28 | SkASSERT(renderTargetSampleCnt >= 1); |
| 29 | int colorValuesPerPixel = renderTargetSampleCnt; |
| 30 | if (renderTargetSampleCnt > 1) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 31 | // Worse case, we own the resolve buffer so that is one more sample per pixel. |
| 32 | colorValuesPerPixel += 1; |
| 33 | } |
| 34 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 35 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 36 | size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig); |
robertphillips | 4c56b9f | 2016-08-17 08:02:51 -0700 | [diff] [blame] | 37 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 38 | // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces). |
| 39 | // Unfortunately Chromium seems to want to do this. |
| 40 | //SkASSERT(colorBytes > 0); |
| 41 | |
| 42 | size = colorValuesPerPixel * colorBytes; |
| 43 | size += colorBytes/3; // in case we have to mipmap |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 44 | } else { |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 45 | SkASSERT(renderTargetSampleCnt == 1); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 46 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 47 | size = GrCompressedFormatDataSize(desc.fConfig, width, height); |
| 48 | } else { |
| 49 | size = (size_t)width * height * GrBytesPerPixel(desc.fConfig); |
| 50 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 51 | |
| 52 | size += size/3; // in case we have to mipmap |
| 53 | } |
| 54 | |
| 55 | return size; |
| 56 | } |
| 57 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 58 | size_t GrSurface::ComputeSize(GrPixelConfig config, |
| 59 | int width, |
| 60 | int height, |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 61 | int colorSamplesPerPixel, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 62 | GrMipMapped mipMapped, |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 63 | bool binSize) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 64 | size_t colorSize; |
| 65 | |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 66 | width = binSize ? GrResourceProvider::MakeApprox(width) : width; |
| 67 | height = binSize ? GrResourceProvider::MakeApprox(height) : height; |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 68 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 69 | SkASSERT(kUnknown_GrPixelConfig != config); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 70 | if (GrPixelConfigIsCompressed(config)) { |
| 71 | colorSize = GrCompressedFormatDataSize(config, width, height); |
| 72 | } else { |
| 73 | colorSize = (size_t)width * height * GrBytesPerPixel(config); |
| 74 | } |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 75 | SkASSERT(colorSize > 0); |
| 76 | |
| 77 | size_t finalSize = colorSamplesPerPixel * colorSize; |
| 78 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 79 | if (GrMipMapped::kYes == mipMapped) { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 80 | // We don't have to worry about the mipmaps being a different size than |
| 81 | // we'd expect because we never change fDesc.fWidth/fHeight. |
| 82 | finalSize += colorSize/3; |
| 83 | } |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 84 | return finalSize; |
| 85 | } |
| 86 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 87 | ////////////////////////////////////////////////////////////////////////////// |
| 88 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 89 | bool GrSurface::hasPendingRead() const { |
| 90 | const GrTexture* thisTex = this->asTexture(); |
| 91 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 92 | return true; |
| 93 | } |
| 94 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 95 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | bool GrSurface::hasPendingWrite() const { |
| 102 | const GrTexture* thisTex = this->asTexture(); |
| 103 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 104 | return true; |
| 105 | } |
| 106 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 107 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool GrSurface::hasPendingIO() const { |
| 114 | const GrTexture* thisTex = this->asTexture(); |
| 115 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 116 | return true; |
| 117 | } |
| 118 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 119 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 124 | |
| 125 | void GrSurface::onRelease() { |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 126 | this->invokeReleaseProc(); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 127 | this->INHERITED::onRelease(); |
| 128 | } |
| 129 | |
| 130 | void GrSurface::onAbandon() { |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 131 | this->invokeReleaseProc(); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 132 | this->INHERITED::onAbandon(); |
| 133 | } |