blob: 886945c71036052812e3e0e003ad931b1dfd672d [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"
bsalomonf276ac52015-10-09 13:36:42 -07009#include "GrContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070010#include "GrSurfacePriv.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000011
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000012#include "SkBitmap.h"
bsalomonf276ac52015-10-09 13:36:42 -070013#include "SkGrPriv.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000014#include "SkImageEncoder.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000015#include <stdio.h>
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000016
robertphillips6e83ac72015-08-13 05:19:14 -070017size_t GrSurface::WorseCaseSize(const GrSurfaceDesc& desc) {
18 size_t size;
19
20 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
21 if (isRenderTarget) {
22 // We own one color value for each MSAA sample.
23 int colorValuesPerPixel = SkTMax(1, desc.fSampleCnt);
24 if (desc.fSampleCnt) {
25 // Worse case, we own the resolve buffer so that is one more sample per pixel.
26 colorValuesPerPixel += 1;
27 }
28 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
29 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
30 size_t colorBytes = GrBytesPerPixel(desc.fConfig);
31 SkASSERT(colorBytes > 0);
32 size = colorValuesPerPixel * desc.fWidth * desc.fHeight * colorBytes;
33 } else {
34 if (GrPixelConfigIsCompressed(desc.fConfig)) {
35 size = GrCompressedFormatDataSize(desc.fConfig, desc.fWidth, desc.fHeight);
36 } else {
37 size = (size_t) desc.fWidth * desc.fHeight * GrBytesPerPixel(desc.fConfig);
38 }
39
40 size += size/3; // in case we have to mipmap
41 }
42
43 return size;
44}
45
bsalomone8d21e82015-07-16 08:23:13 -070046template<typename T> static bool adjust_params(int surfaceWidth,
47 int surfaceHeight,
48 size_t bpp,
49 int* left, int* top, int* width, int* height,
50 T** data,
51 size_t* rowBytes) {
52 if (!*rowBytes) {
53 *rowBytes = *width * bpp;
54 }
55
56 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
57 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
58
59 if (!subRect.intersect(bounds)) {
60 return false;
61 }
62 *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) +
63 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
64
65 *left = subRect.fLeft;
66 *top = subRect.fTop;
67 *width = subRect.width();
68 *height = subRect.height();
69 return true;
70}
71
72bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth,
73 int surfaceHeight,
74 size_t bpp,
75 int* left, int* top, int* width, int* height,
76 void** data,
77 size_t* rowBytes) {
78 return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data,
79 rowBytes);
80}
81
82bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth,
83 int surfaceHeight,
84 size_t bpp,
85 int* left, int* top, int* width, int* height,
86 const void** data,
87 size_t* rowBytes) {
88 return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height,
89 data, rowBytes);
90}
91
92
93//////////////////////////////////////////////////////////////////////////////
94
bsalomon81beccc2014-10-13 12:32:55 -070095bool GrSurface::writePixels(int left, int top, int width, int height,
96 GrPixelConfig config, const void* buffer, size_t rowBytes,
97 uint32_t pixelOpsFlags) {
98 // go through context so that all necessary flushing occurs
99 GrContext* context = this->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -0700100 if (nullptr == context) {
bsalomon81beccc2014-10-13 12:32:55 -0700101 return false;
102 }
cblume55f2d2d2016-02-26 13:20:48 -0800103 return context->writeSurfacePixels(this, left, top, width, height, config, buffer,
104 rowBytes, pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700105}
106
107bool GrSurface::readPixels(int left, int top, int width, int height,
108 GrPixelConfig config, void* buffer, size_t rowBytes,
109 uint32_t pixelOpsFlags) {
110 // go through context so that all necessary flushing occurs
111 GrContext* context = this->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -0700112 if (nullptr == context) {
bsalomon81beccc2014-10-13 12:32:55 -0700113 return false;
114 }
bsalomone8d21e82015-07-16 08:23:13 -0700115 return context->readSurfacePixels(this, left, top, width, height, config, buffer,
116 rowBytes, pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700117}
118
bsalomon74f681d2015-06-23 14:38:48 -0700119SkImageInfo GrSurface::info(SkAlphaType alphaType) const {
reede5ea5002014-09-03 11:54:58 -0700120 SkColorType colorType;
jvanverthfa1e8a72014-12-22 08:31:49 -0800121 SkColorProfileType profileType;
122 if (!GrPixelConfig2ColorAndProfileType(this->config(), &colorType, &profileType)) {
reed@google.combf790232013-12-13 19:45:58 +0000123 sk_throw();
124 }
bsalomon74f681d2015-06-23 14:38:48 -0700125 return SkImageInfo::Make(this->width(), this->height(), colorType, alphaType,
jvanverthfa1e8a72014-12-22 08:31:49 -0800126 profileType);
reed@google.combf790232013-12-13 19:45:58 +0000127}
128
bsalomonafbf2d62014-09-30 12:18:44 -0700129// TODO: This should probably be a non-member helper function. It might only be needed in
130// debug or developer builds.
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000131bool GrSurface::savePixels(const char* filename) {
132 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700133 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000134 return false;
135 }
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000136
bsalomonafbf2d62014-09-30 12:18:44 -0700137 bool result = this->readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
138 bm.getPixels());
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000139 if (!result) {
140 SkDebugf("------ failed to read pixels for %s\n", filename);
141 return false;
142 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +0000143
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +0000144 // remove any previous version of this file
145 remove(filename);
146
147 if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
148 SkDebugf("------ failed to encode %s\n", filename);
149 remove(filename); // remove any partial file
150 return false;
151 }
152
153 return true;
154}
bsalomon8d034a12014-09-22 12:21:08 -0700155
bsalomonf80bfed2014-10-07 05:56:02 -0700156void GrSurface::flushWrites() {
157 if (!this->wasDestroyed()) {
158 this->getContext()->flushSurfaceWrites(this);
159 }
160}
161
bsalomonc49e8682015-06-30 11:37:35 -0700162void GrSurface::prepareForExternalIO() {
bsalomon87a94eb2014-11-03 14:28:32 -0800163 if (!this->wasDestroyed()) {
bsalomonc49e8682015-06-30 11:37:35 -0700164 this->getContext()->prepareSurfaceForExternalIO(this);
bsalomon87a94eb2014-11-03 14:28:32 -0800165 }
166}
167
bsalomon8d034a12014-09-22 12:21:08 -0700168bool GrSurface::hasPendingRead() const {
169 const GrTexture* thisTex = this->asTexture();
170 if (thisTex && thisTex->internalHasPendingRead()) {
171 return true;
172 }
173 const GrRenderTarget* thisRT = this->asRenderTarget();
174 if (thisRT && thisRT->internalHasPendingRead()) {
175 return true;
176 }
177 return false;
178}
179
180bool GrSurface::hasPendingWrite() const {
181 const GrTexture* thisTex = this->asTexture();
182 if (thisTex && thisTex->internalHasPendingWrite()) {
183 return true;
184 }
185 const GrRenderTarget* thisRT = this->asRenderTarget();
186 if (thisRT && thisRT->internalHasPendingWrite()) {
187 return true;
188 }
189 return false;
190}
191
192bool GrSurface::hasPendingIO() const {
193 const GrTexture* thisTex = this->asTexture();
194 if (thisTex && thisTex->internalHasPendingIO()) {
195 return true;
196 }
197 const GrRenderTarget* thisRT = this->asRenderTarget();
198 if (thisRT && thisRT->internalHasPendingIO()) {
199 return true;
200 }
201 return false;
202}
reedde499882015-06-18 13:41:40 -0700203
204void GrSurface::onRelease() {
205 this->invokeReleaseProc();
206 this->INHERITED::onRelease();
207}
208
209void GrSurface::onAbandon() {
210 this->invokeReleaseProc();
211 this->INHERITED::onAbandon();
212}