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 | |
| 8 | #include "GrSurface.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 9 | #include "GrContext.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 10 | #include "GrOpList.h" |
Robert Phillips | c4f0a82 | 2017-06-13 08:11:36 -0400 | [diff] [blame] | 11 | #include "GrRenderTarget.h" |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 12 | #include "GrResourceProvider.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 13 | #include "GrSurfacePriv.h" |
Mike Reed | 84dd857 | 2017-03-08 22:21:00 -0500 | [diff] [blame] | 14 | #include "GrTexture.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 15 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 16 | #include "SkGr.h" |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 17 | #include "SkMathPriv.h" |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 18 | |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 19 | size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 20 | size_t size; |
| 21 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 22 | int width = useNextPow2 |
| 23 | ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(desc.fWidth)) |
| 24 | : desc.fWidth; |
| 25 | int height = useNextPow2 |
| 26 | ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(desc.fHeight)) |
| 27 | : desc.fHeight; |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 28 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 29 | bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 30 | if (isRenderTarget) { |
| 31 | // We own one color value for each MSAA sample. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 32 | SkASSERT(desc.fSampleCnt >= 1); |
| 33 | int colorValuesPerPixel = desc.fSampleCnt; |
| 34 | if (desc.fSampleCnt > 1) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 35 | // Worse case, we own the resolve buffer so that is one more sample per pixel. |
| 36 | colorValuesPerPixel += 1; |
| 37 | } |
| 38 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 39 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 40 | size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig); |
robertphillips | 4c56b9f | 2016-08-17 08:02:51 -0700 | [diff] [blame] | 41 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 42 | // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces). |
| 43 | // Unfortunately Chromium seems to want to do this. |
| 44 | //SkASSERT(colorBytes > 0); |
| 45 | |
| 46 | size = colorValuesPerPixel * colorBytes; |
| 47 | size += colorBytes/3; // in case we have to mipmap |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 48 | } else { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 49 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 50 | size = GrCompressedFormatDataSize(desc.fConfig, width, height); |
| 51 | } else { |
| 52 | size = (size_t)width * height * GrBytesPerPixel(desc.fConfig); |
| 53 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 54 | |
| 55 | size += size/3; // in case we have to mipmap |
| 56 | } |
| 57 | |
| 58 | return size; |
| 59 | } |
| 60 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 61 | size_t GrSurface::ComputeSize(GrPixelConfig config, |
| 62 | int width, |
| 63 | int height, |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 64 | int colorSamplesPerPixel, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 65 | GrMipMapped mipMapped, |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 66 | bool useNextPow2) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 67 | size_t colorSize; |
| 68 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 69 | width = useNextPow2 |
| 70 | ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(width)) |
| 71 | : width; |
| 72 | height = useNextPow2 |
| 73 | ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(height)) |
| 74 | : height; |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 75 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 76 | SkASSERT(kUnknown_GrPixelConfig != config); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 77 | if (GrPixelConfigIsCompressed(config)) { |
| 78 | colorSize = GrCompressedFormatDataSize(config, width, height); |
| 79 | } else { |
| 80 | colorSize = (size_t)width * height * GrBytesPerPixel(config); |
| 81 | } |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 82 | SkASSERT(colorSize > 0); |
| 83 | |
| 84 | size_t finalSize = colorSamplesPerPixel * colorSize; |
| 85 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 86 | if (GrMipMapped::kYes == mipMapped) { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 87 | // We don't have to worry about the mipmaps being a different size than |
| 88 | // we'd expect because we never change fDesc.fWidth/fHeight. |
| 89 | finalSize += colorSize/3; |
| 90 | } |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 91 | return finalSize; |
| 92 | } |
| 93 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 94 | template<typename T> static bool adjust_params(int surfaceWidth, |
| 95 | int surfaceHeight, |
| 96 | size_t bpp, |
| 97 | int* left, int* top, int* width, int* height, |
| 98 | T** data, |
| 99 | size_t* rowBytes) { |
| 100 | if (!*rowBytes) { |
| 101 | *rowBytes = *width * bpp; |
| 102 | } |
| 103 | |
| 104 | SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height); |
| 105 | SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 106 | |
| 107 | if (!subRect.intersect(bounds)) { |
| 108 | return false; |
| 109 | } |
| 110 | *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) + |
| 111 | (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp); |
| 112 | |
| 113 | *left = subRect.fLeft; |
| 114 | *top = subRect.fTop; |
| 115 | *width = subRect.width(); |
| 116 | *height = subRect.height(); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth, |
| 121 | int surfaceHeight, |
| 122 | size_t bpp, |
| 123 | int* left, int* top, int* width, int* height, |
| 124 | void** data, |
| 125 | size_t* rowBytes) { |
| 126 | return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data, |
| 127 | rowBytes); |
| 128 | } |
| 129 | |
| 130 | bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth, |
| 131 | int surfaceHeight, |
| 132 | size_t bpp, |
| 133 | int* left, int* top, int* width, int* height, |
| 134 | const void** data, |
| 135 | size_t* rowBytes) { |
| 136 | return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, |
| 137 | data, rowBytes); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 143 | bool GrSurface::hasPendingRead() const { |
| 144 | const GrTexture* thisTex = this->asTexture(); |
| 145 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 146 | return true; |
| 147 | } |
| 148 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 149 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | bool GrSurface::hasPendingWrite() const { |
| 156 | const GrTexture* thisTex = this->asTexture(); |
| 157 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 158 | return true; |
| 159 | } |
| 160 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 161 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 162 | return true; |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | bool GrSurface::hasPendingIO() const { |
| 168 | const GrTexture* thisTex = this->asTexture(); |
| 169 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 170 | return true; |
| 171 | } |
| 172 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 173 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 178 | |
| 179 | void GrSurface::onRelease() { |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 180 | this->invokeReleaseProc(); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 181 | this->INHERITED::onRelease(); |
| 182 | } |
| 183 | |
| 184 | void GrSurface::onAbandon() { |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 185 | this->invokeReleaseProc(); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 186 | this->INHERITED::onAbandon(); |
| 187 | } |