blob: d067f07d0df167512e1259106d52dac4a4bc53b7 [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
bsalomoncf99b002014-10-02 10:42:24 -070051void GrSurface::flushWrites() {
52 if (!this->wasDestroyed()) {
53 this->getContext()->flushSurfaceWrites(this);
54 }
55}
56
bsalomon8d034a12014-09-22 12:21:08 -070057bool 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
69bool 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
81bool 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}
bsalomonafbf2d62014-09-30 12:18:44 -070092
93bool 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}