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
10#include "SkBitmap.h"
11#include "SkGr.h"
12#include "SkImageEncoder.h"
13#include <stdio.h>
14
15SkImageInfo GrSurface::info() const {
16    SkColorType colorType;
17    if (!GrPixelConfig2ColorType(this->config(), &colorType)) {
18        sk_throw();
19    }
20    return SkImageInfo::Make(this->width(), this->height(), colorType, kPremul_SkAlphaType);
21}
22
23bool GrSurface::savePixels(const char* filename) {
24    SkBitmap bm;
25    if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
26        return false;
27    }
28
29    bool result = readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
30                             bm.getPixels());
31    if (!result) {
32        SkDebugf("------ failed to read pixels for %s\n", filename);
33        return false;
34    }
35
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}
47
48bool 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
60bool 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
72bool 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}
83