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