blob: 0aa5f681ab2d1a07657e9814b77ff20b039d9766 [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
bsalomon81beccc2014-10-13 12:32:55 -070016bool GrSurface::writePixels(int left, int top, int width, int height,
17 GrPixelConfig config, const void* buffer, size_t rowBytes,
18 uint32_t pixelOpsFlags) {
19 // go through context so that all necessary flushing occurs
20 GrContext* context = this->getContext();
21 if (NULL == context) {
22 return false;
23 }
24 return context->writeSurfacePixels(this, left, top, width, height, config, buffer, rowBytes,
25 pixelOpsFlags);
26}
27
28bool GrSurface::readPixels(int left, int top, int width, int height,
29 GrPixelConfig config, void* buffer, size_t rowBytes,
30 uint32_t pixelOpsFlags) {
31 // go through context so that all necessary flushing occurs
32 GrContext* context = this->getContext();
33 if (NULL == context) {
34 return false;
35 }
36 GrRenderTarget* target = this->asRenderTarget();
37 if (target) {
38 return context->readRenderTargetPixels(target, left, top, width, height, config, buffer,
39 rowBytes, pixelOpsFlags);
40 }
41 return false;
42}
43
reed6c225732014-06-09 19:52:07 -070044SkImageInfo GrSurface::info() const {
reede5ea5002014-09-03 11:54:58 -070045 SkColorType colorType;
46 if (!GrPixelConfig2ColorType(this->config(), &colorType)) {
reed@google.combf790232013-12-13 19:45:58 +000047 sk_throw();
48 }
reede5ea5002014-09-03 11:54:58 -070049 return SkImageInfo::Make(this->width(), this->height(), colorType, kPremul_SkAlphaType);
reed@google.combf790232013-12-13 19:45:58 +000050}
51
bsalomonafbf2d62014-09-30 12:18:44 -070052// TODO: This should probably be a non-member helper function. It might only be needed in
53// debug or developer builds.
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000054bool GrSurface::savePixels(const char* filename) {
55 SkBitmap bm;
reed84825042014-09-02 12:50:45 -070056 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
reed@google.com9ebcac52014-01-24 18:53:42 +000057 return false;
58 }
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000059
bsalomonafbf2d62014-09-30 12:18:44 -070060 bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
61 bm.getPixels());
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000062 if (!result) {
63 SkDebugf("------ failed to read pixels for %s\n", filename);
64 return false;
65 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +000066
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000067 // remove any previous version of this file
68 remove(filename);
69
70 if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
71 SkDebugf("------ failed to encode %s\n", filename);
72 remove(filename); // remove any partial file
73 return false;
74 }
75
76 return true;
77}
bsalomon8d034a12014-09-22 12:21:08 -070078
bsalomonf80bfed2014-10-07 05:56:02 -070079void GrSurface::flushWrites() {
80 if (!this->wasDestroyed()) {
81 this->getContext()->flushSurfaceWrites(this);
82 }
83}
84
bsalomon8d034a12014-09-22 12:21:08 -070085bool GrSurface::hasPendingRead() const {
86 const GrTexture* thisTex = this->asTexture();
87 if (thisTex && thisTex->internalHasPendingRead()) {
88 return true;
89 }
90 const GrRenderTarget* thisRT = this->asRenderTarget();
91 if (thisRT && thisRT->internalHasPendingRead()) {
92 return true;
93 }
94 return false;
95}
96
97bool GrSurface::hasPendingWrite() const {
98 const GrTexture* thisTex = this->asTexture();
99 if (thisTex && thisTex->internalHasPendingWrite()) {
100 return true;
101 }
102 const GrRenderTarget* thisRT = this->asRenderTarget();
103 if (thisRT && thisRT->internalHasPendingWrite()) {
104 return true;
105 }
106 return false;
107}
108
109bool GrSurface::hasPendingIO() const {
110 const GrTexture* thisTex = this->asTexture();
111 if (thisTex && thisTex->internalHasPendingIO()) {
112 return true;
113 }
114 const GrRenderTarget* thisRT = this->asRenderTarget();
115 if (thisRT && thisRT->internalHasPendingIO()) {
116 return true;
117 }
118 return false;
119}
bsalomonafbf2d62014-09-30 12:18:44 -0700120
121bool GrSurface::isSameAs(const GrSurface* other) const {
122 const GrRenderTarget* thisRT = this->asRenderTarget();
123 if (thisRT) {
124 return thisRT == other->asRenderTarget();
125 } else {
126 const GrTexture* thisTex = this->asTexture();
127 SkASSERT(thisTex); // We must be one or the other
128 return thisTex == other->asTexture();
129 }
130}