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" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 10 | #include "GrSurfacePriv.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 11 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 12 | #include "SkBitmap.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 13 | #include "SkGrPriv.h" |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 14 | #include "SkImageEncoder.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 16 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 17 | size_t GrSurface::WorseCaseSize(const GrSurfaceDesc& desc) { |
| 18 | size_t size; |
| 19 | |
| 20 | bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 21 | if (isRenderTarget) { |
| 22 | // We own one color value for each MSAA sample. |
| 23 | int colorValuesPerPixel = SkTMax(1, desc.fSampleCnt); |
| 24 | if (desc.fSampleCnt) { |
| 25 | // Worse case, we own the resolve buffer so that is one more sample per pixel. |
| 26 | colorValuesPerPixel += 1; |
| 27 | } |
| 28 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
| 29 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
| 30 | size_t colorBytes = GrBytesPerPixel(desc.fConfig); |
| 31 | SkASSERT(colorBytes > 0); |
| 32 | size = colorValuesPerPixel * desc.fWidth * desc.fHeight * colorBytes; |
| 33 | } else { |
| 34 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 35 | size = GrCompressedFormatDataSize(desc.fConfig, desc.fWidth, desc.fHeight); |
| 36 | } else { |
| 37 | size = (size_t) desc.fWidth * desc.fHeight * GrBytesPerPixel(desc.fConfig); |
| 38 | } |
| 39 | |
| 40 | size += size/3; // in case we have to mipmap |
| 41 | } |
| 42 | |
| 43 | return size; |
| 44 | } |
| 45 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 46 | template<typename T> static bool adjust_params(int surfaceWidth, |
| 47 | int surfaceHeight, |
| 48 | size_t bpp, |
| 49 | int* left, int* top, int* width, int* height, |
| 50 | T** data, |
| 51 | size_t* rowBytes) { |
| 52 | if (!*rowBytes) { |
| 53 | *rowBytes = *width * bpp; |
| 54 | } |
| 55 | |
| 56 | SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height); |
| 57 | SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 58 | |
| 59 | if (!subRect.intersect(bounds)) { |
| 60 | return false; |
| 61 | } |
| 62 | *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) + |
| 63 | (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp); |
| 64 | |
| 65 | *left = subRect.fLeft; |
| 66 | *top = subRect.fTop; |
| 67 | *width = subRect.width(); |
| 68 | *height = subRect.height(); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth, |
| 73 | int surfaceHeight, |
| 74 | size_t bpp, |
| 75 | int* left, int* top, int* width, int* height, |
| 76 | void** data, |
| 77 | size_t* rowBytes) { |
| 78 | return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data, |
| 79 | rowBytes); |
| 80 | } |
| 81 | |
| 82 | bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth, |
| 83 | int surfaceHeight, |
| 84 | size_t bpp, |
| 85 | int* left, int* top, int* width, int* height, |
| 86 | const void** data, |
| 87 | size_t* rowBytes) { |
| 88 | return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, |
| 89 | data, rowBytes); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 95 | bool GrSurface::writePixels(int left, int top, int width, int height, |
| 96 | GrPixelConfig config, const void* buffer, size_t rowBytes, |
| 97 | uint32_t pixelOpsFlags) { |
| 98 | // go through context so that all necessary flushing occurs |
| 99 | GrContext* context = this->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 100 | if (nullptr == context) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 101 | return false; |
| 102 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 103 | return context->writeSurfacePixels(this, left, top, width, height, config, buffer, |
| 104 | rowBytes, pixelOpsFlags); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool GrSurface::readPixels(int left, int top, int width, int height, |
| 108 | GrPixelConfig config, void* buffer, size_t rowBytes, |
| 109 | uint32_t pixelOpsFlags) { |
| 110 | // go through context so that all necessary flushing occurs |
| 111 | GrContext* context = this->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | if (nullptr == context) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 115 | return context->readSurfacePixels(this, left, top, width, height, config, buffer, |
| 116 | rowBytes, pixelOpsFlags); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 117 | } |
| 118 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 119 | SkImageInfo GrSurface::info(SkAlphaType alphaType) const { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 120 | SkColorType colorType; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 121 | SkColorProfileType profileType; |
| 122 | if (!GrPixelConfig2ColorAndProfileType(this->config(), &colorType, &profileType)) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 123 | sk_throw(); |
| 124 | } |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 125 | return SkImageInfo::Make(this->width(), this->height(), colorType, alphaType, |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 126 | profileType); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 127 | } |
| 128 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 129 | // TODO: This should probably be a non-member helper function. It might only be needed in |
| 130 | // debug or developer builds. |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 131 | bool GrSurface::savePixels(const char* filename) { |
| 132 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 133 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 136 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 137 | bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig, |
| 138 | bm.getPixels()); |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 139 | if (!result) { |
| 140 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 141 | return false; |
| 142 | } |
skia.committer@gmail.com | 1d3bfdc | 2013-10-01 07:01:46 +0000 | [diff] [blame] | 143 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 144 | // remove any previous version of this file |
| 145 | remove(filename); |
| 146 | |
| 147 | if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 148 | SkDebugf("------ failed to encode %s\n", filename); |
| 149 | remove(filename); // remove any partial file |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 155 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 156 | void GrSurface::flushWrites() { |
| 157 | if (!this->wasDestroyed()) { |
| 158 | this->getContext()->flushSurfaceWrites(this); |
| 159 | } |
| 160 | } |
| 161 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 162 | void GrSurface::prepareForExternalIO() { |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 163 | if (!this->wasDestroyed()) { |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 164 | this->getContext()->prepareSurfaceForExternalIO(this); |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 168 | bool GrSurface::hasPendingRead() const { |
| 169 | const GrTexture* thisTex = this->asTexture(); |
| 170 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 171 | return true; |
| 172 | } |
| 173 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 174 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool GrSurface::hasPendingWrite() const { |
| 181 | const GrTexture* thisTex = this->asTexture(); |
| 182 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 183 | return true; |
| 184 | } |
| 185 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 186 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | bool GrSurface::hasPendingIO() const { |
| 193 | const GrTexture* thisTex = this->asTexture(); |
| 194 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 195 | return true; |
| 196 | } |
| 197 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 198 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 203 | |
| 204 | void GrSurface::onRelease() { |
| 205 | this->invokeReleaseProc(); |
| 206 | this->INHERITED::onRelease(); |
| 207 | } |
| 208 | |
| 209 | void GrSurface::onAbandon() { |
| 210 | this->invokeReleaseProc(); |
| 211 | this->INHERITED::onAbandon(); |
| 212 | } |