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