blob: 9bd9e8b5ef51b24290d80969e19dba961382d8da [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
bsalomone8d21e82015-07-16 08:23:13 -070016template<typename T> static bool adjust_params(int surfaceWidth,
17 int surfaceHeight,
18 size_t bpp,
19 int* left, int* top, int* width, int* height,
20 T** data,
21 size_t* rowBytes) {
22 if (!*rowBytes) {
23 *rowBytes = *width * bpp;
24 }
25
26 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
27 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
28
29 if (!subRect.intersect(bounds)) {
30 return false;
31 }
32 *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) +
33 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
34
35 *left = subRect.fLeft;
36 *top = subRect.fTop;
37 *width = subRect.width();
38 *height = subRect.height();
39 return true;
40}
41
42bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth,
43 int surfaceHeight,
44 size_t bpp,
45 int* left, int* top, int* width, int* height,
46 void** data,
47 size_t* rowBytes) {
48 return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data,
49 rowBytes);
50}
51
52bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth,
53 int surfaceHeight,
54 size_t bpp,
55 int* left, int* top, int* width, int* height,
56 const void** data,
57 size_t* rowBytes) {
58 return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height,
59 data, rowBytes);
60}
61
62
63//////////////////////////////////////////////////////////////////////////////
64
bsalomon81beccc2014-10-13 12:32:55 -070065bool GrSurface::writePixels(int left, int top, int width, int height,
66 GrPixelConfig config, const void* buffer, size_t rowBytes,
67 uint32_t pixelOpsFlags) {
68 // go through context so that all necessary flushing occurs
69 GrContext* context = this->getContext();
70 if (NULL == context) {
71 return false;
72 }
73 return context->writeSurfacePixels(this, left, top, width, height, config, buffer, rowBytes,
74 pixelOpsFlags);
75}
76
77bool GrSurface::readPixels(int left, int top, int width, int height,
78 GrPixelConfig config, void* buffer, size_t rowBytes,
79 uint32_t pixelOpsFlags) {
80 // go through context so that all necessary flushing occurs
81 GrContext* context = this->getContext();
82 if (NULL == context) {
83 return false;
84 }
bsalomone8d21e82015-07-16 08:23:13 -070085 return context->readSurfacePixels(this, left, top, width, height, config, buffer,
86 rowBytes, pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -070087}
88
bsalomon74f681d2015-06-23 14:38:48 -070089SkImageInfo GrSurface::info(SkAlphaType alphaType) const {
reede5ea5002014-09-03 11:54:58 -070090 SkColorType colorType;
jvanverthfa1e8a72014-12-22 08:31:49 -080091 SkColorProfileType profileType;
92 if (!GrPixelConfig2ColorAndProfileType(this->config(), &colorType, &profileType)) {
reed@google.combf790232013-12-13 19:45:58 +000093 sk_throw();
94 }
bsalomon74f681d2015-06-23 14:38:48 -070095 return SkImageInfo::Make(this->width(), this->height(), colorType, alphaType,
jvanverthfa1e8a72014-12-22 08:31:49 -080096 profileType);
reed@google.combf790232013-12-13 19:45:58 +000097}
98
bsalomonafbf2d62014-09-30 12:18:44 -070099// TODO: This should probably be a non-member helper function. It might only be needed in
100// debug or developer builds.
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000101bool GrSurface::savePixels(const char* filename) {
102 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700103 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000104 return false;
105 }
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000106
bsalomonafbf2d62014-09-30 12:18:44 -0700107 bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
108 bm.getPixels());
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000109 if (!result) {
110 SkDebugf("------ failed to read pixels for %s\n", filename);
111 return false;
112 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +0000113
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000114 // remove any previous version of this file
115 remove(filename);
116
117 if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
118 SkDebugf("------ failed to encode %s\n", filename);
119 remove(filename); // remove any partial file
120 return false;
121 }
122
123 return true;
124}
bsalomon8d034a12014-09-22 12:21:08 -0700125
bsalomonf80bfed2014-10-07 05:56:02 -0700126void GrSurface::flushWrites() {
127 if (!this->wasDestroyed()) {
128 this->getContext()->flushSurfaceWrites(this);
129 }
130}
131
bsalomonc49e8682015-06-30 11:37:35 -0700132void GrSurface::prepareForExternalIO() {
bsalomon87a94eb2014-11-03 14:28:32 -0800133 if (!this->wasDestroyed()) {
bsalomonc49e8682015-06-30 11:37:35 -0700134 this->getContext()->prepareSurfaceForExternalIO(this);
bsalomon87a94eb2014-11-03 14:28:32 -0800135 }
136}
137
bsalomon8d034a12014-09-22 12:21:08 -0700138bool GrSurface::hasPendingRead() const {
139 const GrTexture* thisTex = this->asTexture();
140 if (thisTex && thisTex->internalHasPendingRead()) {
141 return true;
142 }
143 const GrRenderTarget* thisRT = this->asRenderTarget();
144 if (thisRT && thisRT->internalHasPendingRead()) {
145 return true;
146 }
147 return false;
148}
149
150bool GrSurface::hasPendingWrite() const {
151 const GrTexture* thisTex = this->asTexture();
152 if (thisTex && thisTex->internalHasPendingWrite()) {
153 return true;
154 }
155 const GrRenderTarget* thisRT = this->asRenderTarget();
156 if (thisRT && thisRT->internalHasPendingWrite()) {
157 return true;
158 }
159 return false;
160}
161
162bool GrSurface::hasPendingIO() const {
163 const GrTexture* thisTex = this->asTexture();
164 if (thisTex && thisTex->internalHasPendingIO()) {
165 return true;
166 }
167 const GrRenderTarget* thisRT = this->asRenderTarget();
168 if (thisRT && thisRT->internalHasPendingIO()) {
169 return true;
170 }
171 return false;
172}
reedde499882015-06-18 13:41:40 -0700173
174void GrSurface::onRelease() {
175 this->invokeReleaseProc();
176 this->INHERITED::onRelease();
177}
178
179void GrSurface::onAbandon() {
180 this->invokeReleaseProc();
181 this->INHERITED::onAbandon();
182}