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 | |
| 51 | bool GrSurface::hasPendingRead() const { |
| 52 | const GrTexture* thisTex = this->asTexture(); |
| 53 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 54 | return true; |
| 55 | } |
| 56 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 57 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 58 | return true; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | bool GrSurface::hasPendingWrite() const { |
| 64 | const GrTexture* thisTex = this->asTexture(); |
| 65 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 66 | return true; |
| 67 | } |
| 68 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 69 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | bool GrSurface::hasPendingIO() const { |
| 76 | const GrTexture* thisTex = this->asTexture(); |
| 77 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 78 | return true; |
| 79 | } |
| 80 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 81 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame^] | 86 | |
| 87 | bool GrSurface::isSameAs(const GrSurface* other) const { |
| 88 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 89 | if (thisRT) { |
| 90 | return thisRT == other->asRenderTarget(); |
| 91 | } else { |
| 92 | const GrTexture* thisTex = this->asTexture(); |
| 93 | SkASSERT(thisTex); // We must be one or the other |
| 94 | return thisTex == other->asTexture(); |
| 95 | } |
| 96 | } |