blob: a7be0f47d96cdb890c159623843e6c861a9face5 [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
robertphillips6e83ac72015-08-13 05:19:14 -070016size_t GrSurface::WorseCaseSize(const GrSurfaceDesc& desc) {
17 size_t size;
18
19 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
20 if (isRenderTarget) {
21 // We own one color value for each MSAA sample.
22 int colorValuesPerPixel = SkTMax(1, desc.fSampleCnt);
23 if (desc.fSampleCnt) {
24 // Worse case, we own the resolve buffer so that is one more sample per pixel.
25 colorValuesPerPixel += 1;
26 }
27 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
28 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
29 size_t colorBytes = GrBytesPerPixel(desc.fConfig);
30 SkASSERT(colorBytes > 0);
31 size = colorValuesPerPixel * desc.fWidth * desc.fHeight * colorBytes;
32 } else {
33 if (GrPixelConfigIsCompressed(desc.fConfig)) {
34 size = GrCompressedFormatDataSize(desc.fConfig, desc.fWidth, desc.fHeight);
35 } else {
36 size = (size_t) desc.fWidth * desc.fHeight * GrBytesPerPixel(desc.fConfig);
37 }
38
39 size += size/3; // in case we have to mipmap
40 }
41
42 return size;
43}
44
bsalomone8d21e82015-07-16 08:23:13 -070045template<typename T> static bool adjust_params(int surfaceWidth,
46 int surfaceHeight,
47 size_t bpp,
48 int* left, int* top, int* width, int* height,
49 T** data,
50 size_t* rowBytes) {
51 if (!*rowBytes) {
52 *rowBytes = *width * bpp;
53 }
54
55 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
56 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
57
58 if (!subRect.intersect(bounds)) {
59 return false;
60 }
61 *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) +
62 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
63
64 *left = subRect.fLeft;
65 *top = subRect.fTop;
66 *width = subRect.width();
67 *height = subRect.height();
68 return true;
69}
70
71bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth,
72 int surfaceHeight,
73 size_t bpp,
74 int* left, int* top, int* width, int* height,
75 void** data,
76 size_t* rowBytes) {
77 return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data,
78 rowBytes);
79}
80
81bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth,
82 int surfaceHeight,
83 size_t bpp,
84 int* left, int* top, int* width, int* height,
85 const void** data,
86 size_t* rowBytes) {
87 return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height,
88 data, rowBytes);
89}
90
91
92//////////////////////////////////////////////////////////////////////////////
93
bsalomon81beccc2014-10-13 12:32:55 -070094bool GrSurface::writePixels(int left, int top, int width, int height,
95 GrPixelConfig config, const void* buffer, size_t rowBytes,
96 uint32_t pixelOpsFlags) {
97 // go through context so that all necessary flushing occurs
98 GrContext* context = this->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -070099 if (nullptr == context) {
bsalomon81beccc2014-10-13 12:32:55 -0700100 return false;
101 }
102 return context->writeSurfacePixels(this, left, top, width, height, config, buffer, rowBytes,
103 pixelOpsFlags);
104}
105
106bool GrSurface::readPixels(int left, int top, int width, int height,
107 GrPixelConfig config, void* buffer, size_t rowBytes,
108 uint32_t pixelOpsFlags) {
109 // go through context so that all necessary flushing occurs
110 GrContext* context = this->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -0700111 if (nullptr == context) {
bsalomon81beccc2014-10-13 12:32:55 -0700112 return false;
113 }
bsalomone8d21e82015-07-16 08:23:13 -0700114 return context->readSurfacePixels(this, left, top, width, height, config, buffer,
115 rowBytes, pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700116}
117
bsalomon74f681d2015-06-23 14:38:48 -0700118SkImageInfo GrSurface::info(SkAlphaType alphaType) const {
reede5ea5002014-09-03 11:54:58 -0700119 SkColorType colorType;
jvanverthfa1e8a72014-12-22 08:31:49 -0800120 SkColorProfileType profileType;
121 if (!GrPixelConfig2ColorAndProfileType(this->config(), &colorType, &profileType)) {
reed@google.combf790232013-12-13 19:45:58 +0000122 sk_throw();
123 }
bsalomon74f681d2015-06-23 14:38:48 -0700124 return SkImageInfo::Make(this->width(), this->height(), colorType, alphaType,
jvanverthfa1e8a72014-12-22 08:31:49 -0800125 profileType);
reed@google.combf790232013-12-13 19:45:58 +0000126}
127
bsalomonafbf2d62014-09-30 12:18:44 -0700128// TODO: This should probably be a non-member helper function. It might only be needed in
129// debug or developer builds.
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000130bool GrSurface::savePixels(const char* filename) {
131 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700132 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000133 return false;
134 }
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000135
bsalomonafbf2d62014-09-30 12:18:44 -0700136 bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
137 bm.getPixels());
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000138 if (!result) {
139 SkDebugf("------ failed to read pixels for %s\n", filename);
140 return false;
141 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +0000142
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000143 // remove any previous version of this file
144 remove(filename);
145
146 if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
147 SkDebugf("------ failed to encode %s\n", filename);
148 remove(filename); // remove any partial file
149 return false;
150 }
151
152 return true;
153}
bsalomon8d034a12014-09-22 12:21:08 -0700154
bsalomonf80bfed2014-10-07 05:56:02 -0700155void GrSurface::flushWrites() {
156 if (!this->wasDestroyed()) {
157 this->getContext()->flushSurfaceWrites(this);
158 }
159}
160
bsalomonc49e8682015-06-30 11:37:35 -0700161void GrSurface::prepareForExternalIO() {
bsalomon87a94eb2014-11-03 14:28:32 -0800162 if (!this->wasDestroyed()) {
bsalomonc49e8682015-06-30 11:37:35 -0700163 this->getContext()->prepareSurfaceForExternalIO(this);
bsalomon87a94eb2014-11-03 14:28:32 -0800164 }
165}
166
bsalomon8d034a12014-09-22 12:21:08 -0700167bool GrSurface::hasPendingRead() const {
168 const GrTexture* thisTex = this->asTexture();
169 if (thisTex && thisTex->internalHasPendingRead()) {
170 return true;
171 }
172 const GrRenderTarget* thisRT = this->asRenderTarget();
173 if (thisRT && thisRT->internalHasPendingRead()) {
174 return true;
175 }
176 return false;
177}
178
179bool GrSurface::hasPendingWrite() const {
180 const GrTexture* thisTex = this->asTexture();
181 if (thisTex && thisTex->internalHasPendingWrite()) {
182 return true;
183 }
184 const GrRenderTarget* thisRT = this->asRenderTarget();
185 if (thisRT && thisRT->internalHasPendingWrite()) {
186 return true;
187 }
188 return false;
189}
190
191bool GrSurface::hasPendingIO() const {
192 const GrTexture* thisTex = this->asTexture();
193 if (thisTex && thisTex->internalHasPendingIO()) {
194 return true;
195 }
196 const GrRenderTarget* thisRT = this->asRenderTarget();
197 if (thisRT && thisRT->internalHasPendingIO()) {
198 return true;
199 }
200 return false;
201}
reedde499882015-06-18 13:41:40 -0700202
203void GrSurface::onRelease() {
204 this->invokeReleaseProc();
205 this->INHERITED::onRelease();
206}
207
208void GrSurface::onAbandon() {
209 this->invokeReleaseProc();
210 this->INHERITED::onAbandon();
211}