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 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 44 | SkImageInfo GrSurface::info() const { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 45 | SkColorType colorType; |
| 46 | if (!GrPixelConfig2ColorType(this->config(), &colorType)) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 47 | sk_throw(); |
| 48 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 49 | return SkImageInfo::Make(this->width(), this->height(), colorType, kPremul_SkAlphaType); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 52 | // TODO: This should probably be a non-member helper function. It might only be needed in |
| 53 | // debug or developer builds. |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 54 | bool GrSurface::savePixels(const char* filename) { |
| 55 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 56 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 59 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 60 | bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig, |
| 61 | bm.getPixels()); |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 62 | if (!result) { |
| 63 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 64 | return false; |
| 65 | } |
skia.committer@gmail.com | 1d3bfdc | 2013-10-01 07:01:46 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 67 | // remove any previous version of this file |
| 68 | remove(filename); |
| 69 | |
| 70 | if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 71 | SkDebugf("------ failed to encode %s\n", filename); |
| 72 | remove(filename); // remove any partial file |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 78 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 79 | void GrSurface::flushWrites() { |
| 80 | if (!this->wasDestroyed()) { |
| 81 | this->getContext()->flushSurfaceWrites(this); |
| 82 | } |
| 83 | } |
| 84 | |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame^] | 85 | void GrSurface::prepareForExternalRead() { |
| 86 | if (!this->wasDestroyed()) { |
| 87 | this->getContext()->prepareSurfaceForExternalRead(this); |
| 88 | } |
| 89 | } |
| 90 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 91 | bool GrSurface::hasPendingRead() const { |
| 92 | const GrTexture* thisTex = this->asTexture(); |
| 93 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 94 | return true; |
| 95 | } |
| 96 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 97 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | bool GrSurface::hasPendingWrite() const { |
| 104 | const GrTexture* thisTex = this->asTexture(); |
| 105 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 106 | return true; |
| 107 | } |
| 108 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 109 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool GrSurface::hasPendingIO() const { |
| 116 | const GrTexture* thisTex = this->asTexture(); |
| 117 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 118 | return true; |
| 119 | } |
| 120 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 121 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 126 | |
| 127 | bool GrSurface::isSameAs(const GrSurface* other) const { |
| 128 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 129 | if (thisRT) { |
| 130 | return thisRT == other->asRenderTarget(); |
| 131 | } else { |
| 132 | const GrTexture* thisTex = this->asTexture(); |
| 133 | SkASSERT(thisTex); // We must be one or the other |
| 134 | return thisTex == other->asTexture(); |
| 135 | } |
| 136 | } |