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