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" |
| 9 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 11 | #include "SkGr.h" |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 14 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 15 | SkImageInfo GrSurface::info() const { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 16 | SkColorType colorType; |
| 17 | if (!GrPixelConfig2ColorType(this->config(), &colorType)) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 18 | sk_throw(); |
| 19 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 20 | return SkImageInfo::Make(this->width(), this->height(), colorType, kPremul_SkAlphaType); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 21 | } |
| 22 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 23 | bool GrSurface::savePixels(const char* filename) { |
| 24 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 25 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 26 | return false; |
| 27 | } |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 28 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 29 | bool result = readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig, |
| 30 | bm.getPixels()); |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 31 | if (!result) { |
| 32 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 33 | return false; |
| 34 | } |
skia.committer@gmail.com | 1d3bfdc | 2013-10-01 07:01:46 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | 6c5d9a1 | 2013-09-30 18:05:43 +0000 | [diff] [blame] | 36 | // remove any previous version of this file |
| 37 | remove(filename); |
| 38 | |
| 39 | if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 40 | SkDebugf("------ failed to encode %s\n", filename); |
| 41 | remove(filename); // remove any partial file |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | return true; |
| 46 | } |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 47 | |
| 48 | bool GrSurface::hasPendingRead() const { |
| 49 | const GrTexture* thisTex = this->asTexture(); |
| 50 | if (thisTex && thisTex->internalHasPendingRead()) { |
| 51 | return true; |
| 52 | } |
| 53 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 54 | if (thisRT && thisRT->internalHasPendingRead()) { |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | bool GrSurface::hasPendingWrite() const { |
| 61 | const GrTexture* thisTex = this->asTexture(); |
| 62 | if (thisTex && thisTex->internalHasPendingWrite()) { |
| 63 | return true; |
| 64 | } |
| 65 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 66 | if (thisRT && thisRT->internalHasPendingWrite()) { |
| 67 | return true; |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | bool GrSurface::hasPendingIO() const { |
| 73 | const GrTexture* thisTex = this->asTexture(); |
| 74 | if (thisTex && thisTex->internalHasPendingIO()) { |
| 75 | return true; |
| 76 | } |
| 77 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 78 | if (thisRT && thisRT->internalHasPendingIO()) { |
| 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |