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 | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 16 | bool GrSurface::writePixels(int left, int top, int width, int height, |
| 17 | GrPixelConfig config, const void* buffer, size_t rowBytes, |
| 18 | uint32_t pixelOpsFlags) { |
| 19 | // go through context so that all necessary flushing occurs |
| 20 | GrContext* context = this->getContext(); |
| 21 | if (NULL == context) { |
| 22 | return false; |
| 23 | } |
| 24 | return context->writeSurfacePixels(this, left, top, width, height, config, buffer, rowBytes, |
| 25 | pixelOpsFlags); |
| 26 | } |
| 27 | |
| 28 | bool GrSurface::readPixels(int left, int top, int width, int height, |
| 29 | GrPixelConfig config, void* buffer, size_t rowBytes, |
| 30 | uint32_t pixelOpsFlags) { |
| 31 | // go through context so that all necessary flushing occurs |
| 32 | GrContext* context = this->getContext(); |
| 33 | if (NULL == context) { |
| 34 | return false; |
| 35 | } |
| 36 | GrRenderTarget* target = this->asRenderTarget(); |
| 37 | if (target) { |
| 38 | return context->readRenderTargetPixels(target, left, top, width, height, config, buffer, |
| 39 | rowBytes, pixelOpsFlags); |
| 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame^] | 44 | SkImageInfo GrSurface::info(SkAlphaType alphaType) const { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 45 | SkColorType colorType; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 46 | SkColorProfileType profileType; |
| 47 | if (!GrPixelConfig2ColorAndProfileType(this->config(), &colorType, &profileType)) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 48 | sk_throw(); |
| 49 | } |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame^] | 50 | return SkImageInfo::Make(this->width(), this->height(), colorType, alphaType, |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 51 | profileType); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 52 | } |
| 53 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 54 | // TODO: This should probably be a non-member helper function. It might only be needed in |
| 55 | // debug or developer builds. |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 56 | bool GrSurface::savePixels(const char* filename) { |
| 57 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 58 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 59 | return false; |
| 60 | } |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 61 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 62 | bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig, |
| 63 | bm.getPixels()); |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 64 | if (!result) { |
| 65 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 66 | return false; |
| 67 | } |
skia.committer@gmail.com | 1d3bfdc | 2013-10-01 07:01:46 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 69 | // remove any previous version of this file |
| 70 | remove(filename); |
| 71 | |
| 72 | if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 73 | SkDebugf("------ failed to encode %s\n", filename); |
| 74 | remove(filename); // remove any partial file |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 80 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 81 | void GrSurface::flushWrites() { |
| 82 | if (!this->wasDestroyed()) { |
| 83 | this->getContext()->flushSurfaceWrites(this); |
| 84 | } |
| 85 | } |
| 86 | |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 87 | void GrSurface::prepareForExternalRead() { |
| 88 | if (!this->wasDestroyed()) { |
| 89 | this->getContext()->prepareSurfaceForExternalRead(this); |
| 90 | } |
| 91 | } |
| 92 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 93 | bool GrSurface::hasPendingRead() const { |
| 94 | const GrTexture* thisTex = this->asTexture(); |
| 95 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 96 | return true; |
| 97 | } |
| 98 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 99 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool GrSurface::hasPendingWrite() const { |
| 106 | const GrTexture* thisTex = this->asTexture(); |
| 107 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 108 | return true; |
| 109 | } |
| 110 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 111 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 112 | return true; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool GrSurface::hasPendingIO() const { |
| 118 | const GrTexture* thisTex = this->asTexture(); |
| 119 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 120 | return true; |
| 121 | } |
| 122 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 123 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 124 | return true; |
| 125 | } |
| 126 | return false; |
| 127 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 128 | |
| 129 | void GrSurface::onRelease() { |
| 130 | this->invokeReleaseProc(); |
| 131 | this->INHERITED::onRelease(); |
| 132 | } |
| 133 | |
| 134 | void GrSurface::onAbandon() { |
| 135 | this->invokeReleaseProc(); |
| 136 | this->INHERITED::onAbandon(); |
| 137 | } |