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