blob: 37fd73f4f415a82f526a88d018043a1f3bddc3f4 [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +00001/*
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"
bsalomonafbf2d62014-09-30 12:18:44 -07009#include "GrSurfacePriv.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000010
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000011#include "SkBitmap.h"
reed@google.combf790232013-12-13 19:45:58 +000012#include "SkGr.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000013#include "SkImageEncoder.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000014#include <stdio.h>
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000015
reed6c225732014-06-09 19:52:07 -070016SkImageInfo GrSurface::info() const {
reede5ea5002014-09-03 11:54:58 -070017 SkColorType colorType;
18 if (!GrPixelConfig2ColorType(this->config(), &colorType)) {
reed@google.combf790232013-12-13 19:45:58 +000019 sk_throw();
20 }
reede5ea5002014-09-03 11:54:58 -070021 return SkImageInfo::Make(this->width(), this->height(), colorType, kPremul_SkAlphaType);
reed@google.combf790232013-12-13 19:45:58 +000022}
23
bsalomonafbf2d62014-09-30 12:18:44 -070024// TODO: This should probably be a non-member helper function. It might only be needed in
25// debug or developer builds.
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000026bool GrSurface::savePixels(const char* filename) {
27 SkBitmap bm;
reed84825042014-09-02 12:50:45 -070028 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
reed@google.com9ebcac52014-01-24 18:53:42 +000029 return false;
30 }
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000031
bsalomonafbf2d62014-09-30 12:18:44 -070032 bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
33 bm.getPixels());
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000034 if (!result) {
35 SkDebugf("------ failed to read pixels for %s\n", filename);
36 return false;
37 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +000038
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000039 // 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}
bsalomon8d034a12014-09-22 12:21:08 -070050
51bool 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
63bool 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
75bool 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}
bsalomonafbf2d62014-09-30 12:18:44 -070086
87bool 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}