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" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 11 | #include "GrSurfacePriv.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 13 | #include "SkBitmap.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 14 | #include "SkGrPriv.h" |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 16 | #include "SkMathPriv.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 18 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 19 | GrSurface::~GrSurface() { |
| 20 | if (fLastOpList) { |
| 21 | fLastOpList->clearTarget(); |
| 22 | } |
| 23 | SkSafeUnref(fLastOpList); |
| 24 | |
| 25 | // check that invokeReleaseProc has been called (if needed) |
| 26 | SkASSERT(NULL == fReleaseProc); |
| 27 | } |
| 28 | |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 29 | size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 30 | size_t size; |
| 31 | |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 32 | int width = useNextPow2 ? GrNextPow2(desc.fWidth) : desc.fWidth; |
| 33 | int height = useNextPow2 ? GrNextPow2(desc.fHeight) : desc.fHeight; |
| 34 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 35 | bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 36 | if (isRenderTarget) { |
| 37 | // We own one color value for each MSAA sample. |
| 38 | int colorValuesPerPixel = SkTMax(1, desc.fSampleCnt); |
| 39 | if (desc.fSampleCnt) { |
| 40 | // Worse case, we own the resolve buffer so that is one more sample per pixel. |
| 41 | colorValuesPerPixel += 1; |
| 42 | } |
| 43 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
| 44 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 45 | size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig); |
robertphillips | 4c56b9f | 2016-08-17 08:02:51 -0700 | [diff] [blame] | 46 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 47 | // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces). |
| 48 | // Unfortunately Chromium seems to want to do this. |
| 49 | //SkASSERT(colorBytes > 0); |
| 50 | |
| 51 | size = colorValuesPerPixel * colorBytes; |
| 52 | size += colorBytes/3; // in case we have to mipmap |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 53 | } else { |
| 54 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 55 | size = GrCompressedFormatDataSize(desc.fConfig, width, height); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 56 | } else { |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 57 | size = (size_t) width * height * GrBytesPerPixel(desc.fConfig); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | size += size/3; // in case we have to mipmap |
| 61 | } |
| 62 | |
| 63 | return size; |
| 64 | } |
| 65 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 66 | size_t GrSurface::ComputeSize(const GrSurfaceDesc& desc, |
| 67 | int colorSamplesPerPixel, |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 68 | bool hasMIPMaps, |
| 69 | bool useNextPow2) { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 70 | size_t colorSize; |
| 71 | |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 72 | int width = useNextPow2 ? GrNextPow2(desc.fWidth) : desc.fWidth; |
| 73 | int height = useNextPow2 ? GrNextPow2(desc.fHeight) : desc.fHeight; |
| 74 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 75 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
| 76 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 77 | colorSize = GrCompressedFormatDataSize(desc.fConfig, width, height); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 78 | } else { |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 79 | colorSize = (size_t) width * height * GrBytesPerPixel(desc.fConfig); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 80 | } |
| 81 | SkASSERT(colorSize > 0); |
| 82 | |
| 83 | size_t finalSize = colorSamplesPerPixel * colorSize; |
| 84 | |
| 85 | if (hasMIPMaps) { |
| 86 | // We don't have to worry about the mipmaps being a different size than |
| 87 | // we'd expect because we never change fDesc.fWidth/fHeight. |
| 88 | finalSize += colorSize/3; |
| 89 | } |
| 90 | |
Robert Phillips | b446088 | 2016-11-17 14:43:51 -0500 | [diff] [blame] | 91 | SkASSERT(finalSize <= WorstCaseSize(desc, useNextPow2)); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 92 | return finalSize; |
| 93 | } |
| 94 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 95 | template<typename T> static bool adjust_params(int surfaceWidth, |
| 96 | int surfaceHeight, |
| 97 | size_t bpp, |
| 98 | int* left, int* top, int* width, int* height, |
| 99 | T** data, |
| 100 | size_t* rowBytes) { |
| 101 | if (!*rowBytes) { |
| 102 | *rowBytes = *width * bpp; |
| 103 | } |
| 104 | |
| 105 | SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height); |
| 106 | SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 107 | |
| 108 | if (!subRect.intersect(bounds)) { |
| 109 | return false; |
| 110 | } |
| 111 | *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) + |
| 112 | (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp); |
| 113 | |
| 114 | *left = subRect.fLeft; |
| 115 | *top = subRect.fTop; |
| 116 | *width = subRect.width(); |
| 117 | *height = subRect.height(); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth, |
| 122 | int surfaceHeight, |
| 123 | size_t bpp, |
| 124 | int* left, int* top, int* width, int* height, |
| 125 | void** data, |
| 126 | size_t* rowBytes) { |
| 127 | return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data, |
| 128 | rowBytes); |
| 129 | } |
| 130 | |
| 131 | bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth, |
| 132 | int surfaceHeight, |
| 133 | size_t bpp, |
| 134 | int* left, int* top, int* width, int* height, |
| 135 | const void** data, |
| 136 | size_t* rowBytes) { |
| 137 | return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, |
| 138 | data, rowBytes); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | ////////////////////////////////////////////////////////////////////////////// |
| 143 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 144 | bool GrSurface::writePixels(int left, int top, int width, int height, |
| 145 | GrPixelConfig config, const void* buffer, size_t rowBytes, |
| 146 | uint32_t pixelOpsFlags) { |
| 147 | // go through context so that all necessary flushing occurs |
| 148 | GrContext* context = this->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | if (nullptr == context) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 150 | return false; |
| 151 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 152 | return context->writeSurfacePixels(this, left, top, width, height, config, buffer, |
| 153 | rowBytes, pixelOpsFlags); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool GrSurface::readPixels(int left, int top, int width, int height, |
| 157 | GrPixelConfig config, void* buffer, size_t rowBytes, |
| 158 | uint32_t pixelOpsFlags) { |
| 159 | // go through context so that all necessary flushing occurs |
| 160 | GrContext* context = this->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | if (nullptr == context) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 164 | return context->readSurfacePixels(this, left, top, width, height, config, buffer, |
| 165 | rowBytes, pixelOpsFlags); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 166 | } |
| 167 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 168 | // TODO: This should probably be a non-member helper function. It might only be needed in |
| 169 | // debug or developer builds. |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 170 | bool GrSurface::savePixels(const char* filename) { |
| 171 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 172 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 175 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 176 | bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig, |
| 177 | bm.getPixels()); |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 178 | if (!result) { |
| 179 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 180 | return false; |
| 181 | } |
skia.committer@gmail.com | 1d3bfdc | 2013-10-01 07:01:46 +0000 | [diff] [blame] | 182 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 183 | // remove any previous version of this file |
| 184 | remove(filename); |
| 185 | |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 186 | if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) { |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 187 | SkDebugf("------ failed to encode %s\n", filename); |
| 188 | remove(filename); // remove any partial file |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | return true; |
| 193 | } |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 194 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 195 | void GrSurface::flushWrites() { |
| 196 | if (!this->wasDestroyed()) { |
| 197 | this->getContext()->flushSurfaceWrites(this); |
| 198 | } |
| 199 | } |
| 200 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 201 | bool GrSurface::hasPendingRead() const { |
| 202 | const GrTexture* thisTex = this->asTexture(); |
| 203 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 204 | return true; |
| 205 | } |
| 206 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 207 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 208 | return true; |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | bool GrSurface::hasPendingWrite() const { |
| 214 | const GrTexture* thisTex = this->asTexture(); |
| 215 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 216 | return true; |
| 217 | } |
| 218 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 219 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | bool GrSurface::hasPendingIO() const { |
| 226 | const GrTexture* thisTex = this->asTexture(); |
| 227 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 228 | return true; |
| 229 | } |
| 230 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 231 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 232 | return true; |
| 233 | } |
| 234 | return false; |
| 235 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 236 | |
| 237 | void GrSurface::onRelease() { |
| 238 | this->invokeReleaseProc(); |
| 239 | this->INHERITED::onRelease(); |
| 240 | } |
| 241 | |
| 242 | void GrSurface::onAbandon() { |
| 243 | this->invokeReleaseProc(); |
| 244 | this->INHERITED::onAbandon(); |
| 245 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 246 | |
| 247 | void GrSurface::setLastOpList(GrOpList* opList) { |
| 248 | if (fLastOpList) { |
| 249 | // The non-MDB world never closes so we can't check this condition |
| 250 | #ifdef ENABLE_MDB |
| 251 | SkASSERT(fLastOpList->isClosed()); |
| 252 | #endif |
| 253 | fLastOpList->clearTarget(); |
| 254 | } |
| 255 | |
| 256 | SkRefCnt_SafeAssign(fLastOpList, opList); |
| 257 | } |