blob: dff02b0a80bd630a5cb6dd6f9879b4aff23c769e [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 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
Greg Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040011#include "src/core/SkAutoPixmapStorage.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040015#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrGpu.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrOpList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040020#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040021#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040022#include "src/gpu/GrSurfacePriv.h"
23#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040025#include "src/gpu/effects/GrBicubicEffect.h"
Brian Osman45580d32016-11-23 09:37:01 -050026
Robert Phillips2de8cfa2017-06-28 10:33:41 -040027#define ASSERT_SINGLE_OWNER \
28 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050029#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050030
31// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
32// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
33// stack. When this occurs with a closed GrOpList, a new one will be allocated
34// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050035GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomond6287472019-06-24 15:50:07 -040036 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -040037 SkAlphaType alphaType,
Brian Salomonbd3d8d32019-07-02 09:16:28 -040038 sk_sp<SkColorSpace> colorSpace)
39 : fContext(context), fColorSpaceInfo(colorType, alphaType, std::move(colorSpace)) {}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040040
Brian Salomon4d2d6f42019-07-26 14:15:11 -040041const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
42
Robert Phillips0d075de2019-03-04 11:08:13 -050043GrAuditTrail* GrSurfaceContext::auditTrail() {
44 return fContext->priv().auditTrail();
45}
46
47GrDrawingManager* GrSurfaceContext::drawingManager() {
48 return fContext->priv().drawingManager();
49}
50
51const GrDrawingManager* GrSurfaceContext::drawingManager() const {
52 return fContext->priv().drawingManager();
53}
54
55#ifdef SK_DEBUG
56GrSingleOwner* GrSurfaceContext::singleOwner() {
57 return fContext->priv().singleOwner();
58}
59#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040060
Brian Salomon1d435302019-07-01 13:05:28 -040061bool GrSurfaceContext::readPixels(const GrPixelInfo& origDstInfo, void* dst, size_t rowBytes,
62 SkIPoint pt, GrContext* direct) {
63 ASSERT_SINGLE_OWNER
64 RETURN_FALSE_IF_ABANDONED
65 SkDEBUGCODE(this->validate();)
66 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -040067
Brian Salomon1d435302019-07-01 13:05:28 -040068 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040069 return false;
70 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040071
Brian Salomon1d435302019-07-01 13:05:28 -040072 if (!dst) {
73 return false;
74 }
75
Brian Salomon1047a492019-07-02 12:25:21 -040076 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -040077 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -040078 rowBytes = tightRowBytes;
79 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -040080 return false;
81 }
82
83 if (!origDstInfo.isValid()) {
84 return false;
85 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040086
87 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
88
89 // MDB TODO: delay this instantiation until later in the method
90 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
91 return false;
92 }
93
94 GrSurface* srcSurface = srcProxy->peekSurface();
95
Brian Salomon1d435302019-07-01 13:05:28 -040096 auto dstInfo = origDstInfo;
97 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040098 return false;
99 }
Brian Salomon1047a492019-07-02 12:25:21 -0400100 // Our tight row bytes may have been changed by clipping.
101 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400102
Brian Salomon1d435302019-07-01 13:05:28 -0400103 bool premul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
104 dstInfo.alphaType() == kPremul_SkAlphaType;
105 bool unpremul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
106 dstInfo.alphaType() == kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400107
Brian Salomon1d435302019-07-01 13:05:28 -0400108 bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(),
109 dstInfo.colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400110
111 const GrCaps* caps = direct->priv().caps();
112 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
113 // care so much about getImageData performance. However, in order to ensure putImageData/
114 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
115 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
116 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400117 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
118 GrRenderable::kYes);
Brian Salomon1d435302019-07-01 13:05:28 -0400119 bool canvas2DFastPath = unpremul && !needColorConversion &&
120 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
121 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
122 SkToBool(srcProxy->asTextureProxy()) &&
123 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
124 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400125 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400126 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400127
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400128 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400129 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400130 return false;
131 }
132
Brian Salomondc0710f2019-07-01 14:59:32 -0400133 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400134 GrColorType colorType = canvas2DFastPath ? GrColorType::kRGBA_8888
135 : this->colorSpaceInfo().colorType();
136 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr
137 : this->colorSpaceInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400138
Brian Salomonbf6b9792019-08-21 09:38:10 -0400139 auto tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400140 SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), colorType, std::move(cs),
141 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400142 if (!tempCtx) {
143 return false;
144 }
145
146 std::unique_ptr<GrFragmentProcessor> fp;
147 if (canvas2DFastPath) {
148 fp = direct->priv().createPMToUPMEffect(
149 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
150 SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400151 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400152 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400153 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400154 }
Brian Salomon1d435302019-07-01 13:05:28 -0400155 // The render target context is incorrectly tagged as kPremul even though we're writing
156 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
157 // double unpremul.
158 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400159 } else {
160 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
161 }
162 if (!fp) {
163 return false;
164 }
165 GrPaint paint;
166 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
167 paint.addColorFragmentProcessor(std::move(fp));
168
169 tempCtx->asRenderTargetContext()->fillRectToRect(
170 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400171 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
172 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400173
Brian Salomon1d435302019-07-01 13:05:28 -0400174 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400175 }
176
Greg Daniel6eb8c242019-06-05 10:22:24 -0400177 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400178
Brian Salomon1d435302019-07-01 13:05:28 -0400179 auto supportedRead = caps->supportedReadPixelsColorType(
Greg Daniel00fb7242019-07-18 14:28:01 -0400180 this->colorSpaceInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400181
Brian Salomon1047a492019-07-02 12:25:21 -0400182 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
183
184 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400185 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186
Brian Salomonf30b1c12019-06-20 12:25:02 -0400187 std::unique_ptr<char[]> tmpPixels;
188 GrPixelInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400189 void* readDst = dst;
190 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400191 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400192 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
193 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
194 size_t tmpRB = tmpInfo.minRowBytes();
195 size_t size = tmpRB * tmpInfo.height();
196 // Chrome MSAN bots require the data to be initialized (hence the ()).
197 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400198
Brian Salomonf30b1c12019-06-20 12:25:02 -0400199 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400200 readRB = tmpRB;
201 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400202 }
203
204 direct->priv().flushSurface(srcProxy);
205
Brian Salomon1d435302019-07-01 13:05:28 -0400206 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400207 dstInfo.height(), this->colorSpaceInfo().colorType(),
208 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400209 return false;
210 }
211
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400213 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400214 }
215 return true;
216}
Robert Phillips0d075de2019-03-04 11:08:13 -0500217
Brian Salomon1d435302019-07-01 13:05:28 -0400218bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
219 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400220 ASSERT_SINGLE_OWNER
221 RETURN_FALSE_IF_ABANDONED
222 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400223 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400224
Brian Salomon1d435302019-07-01 13:05:28 -0400225 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500226 return false;
227 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500228
Brian Salomon1d435302019-07-01 13:05:28 -0400229 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500230 return false;
231 }
232
Brian Salomon1d435302019-07-01 13:05:28 -0400233 if (!src) {
234 return false;
235 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400236
Brian Salomon1047a492019-07-02 12:25:21 -0400237 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400238 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400239 rowBytes = tightRowBytes;
240 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400241 return false;
242 }
243
244 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400245 return false;
246 }
247
248 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
249 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
250 return false;
251 }
252
253 GrSurface* dstSurface = dstProxy->peekSurface();
254
Brian Salomon1d435302019-07-01 13:05:28 -0400255 auto srcInfo = origSrcInfo;
256 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400257 return false;
258 }
Brian Salomon1047a492019-07-02 12:25:21 -0400259 // Our tight row bytes may have been changed by clipping.
260 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400261
Brian Salomon1d435302019-07-01 13:05:28 -0400262 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
263 srcInfo.alphaType() == kUnpremul_SkAlphaType;
264 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
265 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400266
Brian Salomon1d435302019-07-01 13:05:28 -0400267 bool needColorConversion = SkColorSpaceXformSteps::Required(
268 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400269
270 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400271
272 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
273 GrRenderable::kNo);
274
Greg Daniel6eb8c242019-06-05 10:22:24 -0400275 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
276 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400277 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
278 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
279 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
280 SkToBool(this->asRenderTargetContext()) &&
281 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
282 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400283 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400284 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400285
286 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
287 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400288 desc.fWidth = srcInfo.width();
289 desc.fHeight = srcInfo.height();
Brian Salomond6287472019-06-24 15:50:07 -0400290 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400291
292 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400293 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400294 if (canvas2DFastPath) {
295 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400296 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400297 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400298 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400299 } else {
300 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400301 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400302 format = dstProxy->backendFormat().makeTexture2D();
303 if (!format.isValid()) {
304 return false;
305 }
Brian Salomone7499c72019-06-24 12:12:36 -0400306 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400307 }
308
Greg Daniel2e52ad12019-06-13 10:04:16 -0400309 // It is more efficient for us to write pixels into a top left origin so we prefer that.
310 // However, if the final proxy isn't a render target then we must use a copy to move the
311 // data into it which requires the origins to match. If the final proxy is a render target
312 // we can use a draw instead which doesn't have this origin restriction. Thus for render
313 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400314 GrSurfaceOrigin tempOrigin =
315 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400317 format, desc, GrRenderable::kNo, 1, tempOrigin, SkBackingFit::kApprox,
Brian Salomone8a766b2019-07-19 14:24:36 -0400318 SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel2e52ad12019-06-13 10:04:16 -0400319
Greg Daniel6eb8c242019-06-05 10:22:24 -0400320 if (!tempProxy) {
321 return false;
322 }
323 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400324 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400325 if (!tempCtx) {
326 return false;
327 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400328
329 // In the fast path we always write the srcData to the temp context as though it were RGBA.
330 // When the data is really BGRA the write will cause the R and B channels to be swapped in
331 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
332 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400333 if (canvas2DFastPath) {
334 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
335 }
336 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337 return false;
338 }
339
340 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400341 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400342 if (canvas2DFastPath) {
343 fp = direct->priv().createUPMToPMEffect(
344 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400345 // Important: check the original src color type here!
346 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400347 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
348 }
349 } else {
350 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
351 }
352 if (!fp) {
353 return false;
354 }
355 GrPaint paint;
356 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
357 paint.addColorFragmentProcessor(std::move(fp));
358 this->asRenderTargetContext()->fillRectToRect(
359 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400360 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
361 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400362 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400363 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
364 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400365 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366 return false;
367 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400368 }
369 return true;
370 }
371
Brian Salomon1d435302019-07-01 13:05:28 -0400372 GrColorType allowedColorType =
Brian Salomon01915c02019-08-02 09:57:21 -0400373 caps->supportedWritePixelsColorType(this->colorSpaceInfo().colorType(),
374 dstProxy->backendFormat(),
375 srcInfo.colorType()).fColorType;
Brian Salomon1d435302019-07-01 13:05:28 -0400376 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400377 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
378 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400379 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400380
Brian Salomonf30b1c12019-06-20 12:25:02 -0400381 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400382 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400383 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400384 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
385 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
386 srcInfo.height());
387 auto tmpRB = tmpInfo.minRowBytes();
388 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400389
Brian Salomon1d435302019-07-01 13:05:28 -0400390 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400391
Brian Salomon1d435302019-07-01 13:05:28 -0400392 srcColorType = tmpInfo.colorType();
393 rowBytes = tmpRB;
394 src = tmpPixels.get();
395 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400396 }
397
398 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
399 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
400 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
401 // destination proxy)
402 // TODO: should this policy decision just be moved into the drawing manager?
403 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
404
Brian Salomonf77c1462019-08-01 15:19:29 -0400405 return direct->priv().getGpu()->writePixels(
406 dstSurface, pt.fX, pt.fY, srcInfo.width(), srcInfo.height(),
407 this->colorSpaceInfo().colorType(), srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500408}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400409
410bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
411 ASSERT_SINGLE_OWNER
412 RETURN_FALSE_IF_ABANDONED
413 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400414 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400415
Brian Salomon947efe22019-07-16 15:36:11 -0400416 const GrCaps* caps = fContext->priv().caps();
417
Greg Daniel46cfbc62019-06-07 11:43:30 -0400418 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
419 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Brian Salomon947efe22019-07-16 15:36:11 -0400420 SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
421 caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
422 this->asSurfaceProxy()->backendFormat()));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400423
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600424 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400425 return false;
426 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400427
Greg Daniele227fe42019-08-21 13:52:24 -0400428 return this->drawingManager()->newCopyRenderTask(sk_ref_sp(src), srcRect,
429 this->asSurfaceProxyRef(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400430}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400431
Brian Salomonbf6b9792019-08-21 09:38:10 -0400432std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
433 const SkImageInfo& info,
434 const SkIRect& srcRect,
435 SkSurface::RescaleGamma rescaleGamma,
436 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400437 auto direct = fContext->priv().asDirectContext();
438 if (!direct) {
439 return nullptr;
440 }
441 auto rtProxy = this->asRenderTargetProxy();
442 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
443 return nullptr;
444 }
445
446 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
447 if (info.alphaType() == kUnpremul_SkAlphaType) {
448 return nullptr;
449 }
450
451 int srcW = srcRect.width();
452 int srcH = srcRect.height();
453 int srcX = srcRect.fLeft;
454 int srcY = srcRect.fTop;
455 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(this->asTextureProxy());
456 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
457 if (!texProxy) {
458 texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), GrMipMapped::kNo, srcRect,
459 SkBackingFit::kApprox, SkBudgeted::kNo);
460 if (!texProxy) {
461 return nullptr;
462 }
463 srcX = 0;
464 srcY = 0;
465 constraint = SkCanvas::kFast_SrcRectConstraint;
466 }
467
468 float sx = (float)info.width() / srcW;
469 float sy = (float)info.height() / srcH;
470
471 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
472 int stepsX;
473 int stepsY;
474 if (rescaleQuality > kNone_SkFilterQuality) {
475 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
476 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
477 } else {
478 stepsX = sx != 1.f;
479 stepsY = sy != 1.f;
480 }
481 SkASSERT(stepsX || stepsY);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400482 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
483 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
484 std::unique_ptr<GrRenderTargetContext> tempA;
485 std::unique_ptr<GrRenderTargetContext> tempB;
486
Brian Salomone9ad9982019-07-22 16:17:41 -0400487 // Assume we should ignore the rescale linear request if the surface has no color space since
488 // it's unclear how we'd linearize from an unknown color space.
489 if (rescaleGamma == SkSurface::kLinear && this->colorSpaceInfo().colorSpace() &&
490 !this->colorSpaceInfo().colorSpace()->gammaIsLinear()) {
491 auto cs = this->colorSpaceInfo().colorSpace()->makeLinearGamma();
492 auto xform = GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
493 this->colorSpaceInfo().alphaType(), cs.get(),
494 kPremul_SkAlphaType);
495 // We'll fall back to kRGBA_8888 if half float not supported.
496 auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
497 SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo,
498 kTopLeft_GrSurfaceOrigin);
499 if (!linearRTC) {
500 return nullptr;
501 }
502 linearRTC->drawTexture(GrNoClip(), texProxy, GrSamplerState::Filter::kNearest,
503 SkBlendMode::kSrc, SK_PMColor4fWHITE, SkRect::Make(srcRect),
504 SkRect::MakeWH(srcW, srcH), GrAA::kNo, GrQuadAAFlags::kNone,
505 constraint, SkMatrix::I(), std::move(xform));
506 texProxy = linearRTC->asTextureProxyRef();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400507 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400508 srcX = 0;
509 srcY = 0;
510 constraint = SkCanvas::kFast_SrcRectConstraint;
511 }
512 while (stepsX || stepsY) {
513 int nextW = info.width();
514 int nextH = info.height();
515 if (stepsX < 0) {
516 nextW = info.width() << (-stepsX - 1);
517 stepsX++;
518 } else if (stepsX != 0) {
519 if (stepsX > 1) {
520 nextW = srcW * 2;
521 }
522 --stepsX;
523 }
524 if (stepsY < 0) {
525 nextH = info.height() << (-stepsY - 1);
526 stepsY++;
527 } else if (stepsY != 0) {
528 if (stepsY > 1) {
529 nextH = srcH * 2;
530 }
531 --stepsY;
532 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400533 auto input = tempA ? tempA.get() : this;
534 GrColorType colorType = input->colorSpaceInfo().colorType();
535 auto cs = input->colorSpaceInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400536 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400537 auto prevAlphaType = input->colorSpaceInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400538 if (!stepsX && !stepsY) {
539 // Might as well fold conversion to final info in the last step.
540 cs = info.refColorSpace();
541 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400542 xform = GrColorSpaceXform::Make(input->colorSpaceInfo().colorSpace(),
543 input->colorSpaceInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400544 info.alphaType());
545 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400546 tempB = fContext->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomone9ad9982019-07-22 16:17:41 -0400547 SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo,
548 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400549 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400550 return nullptr;
551 }
552 auto dstRect = SkRect::MakeWH(nextW, nextH);
553 if (rescaleQuality == kHigh_SkFilterQuality) {
554 SkMatrix matrix;
555 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
556 std::unique_ptr<GrFragmentProcessor> fp;
557 auto dir = GrBicubicEffect::Direction::kXY;
558 if (nextW == srcW) {
559 dir = GrBicubicEffect::Direction::kY;
560 } else if (nextH == srcH) {
561 dir = GrBicubicEffect::Direction::kX;
562 }
563 if (srcW != texProxy->width() || srcH != texProxy->height()) {
564 auto domain = GrTextureDomain::MakeTexelDomain(
565 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
566 fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType);
567 } else {
568 fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType);
569 }
570 if (xform) {
571 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
572 }
573 GrPaint paint;
574 paint.addColorFragmentProcessor(std::move(fp));
575 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400576 tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
Brian Salomone9ad9982019-07-22 16:17:41 -0400577 dstRect);
578 } else {
579 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
580 : GrSamplerState::Filter::kBilerp;
581 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400582 tempB->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE,
Brian Salomone9ad9982019-07-22 16:17:41 -0400583 srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint,
584 SkMatrix::I(), std::move(xform));
585 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400586 texProxy = tempB->asTextureProxyRef();
587 tempA = std::move(tempB);
Brian Salomone9ad9982019-07-22 16:17:41 -0400588 srcX = srcY = 0;
589 srcW = nextW;
590 srcH = nextH;
591 constraint = SkCanvas::kFast_SrcRectConstraint;
592 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400593 SkASSERT(tempA);
594 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400595}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400596
597GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
598 const SkIRect& rect) {
599 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
600 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
601 auto direct = fContext->priv().asDirectContext();
602 if (!direct) {
603 return {};
604 }
605 auto rtProxy = this->asRenderTargetProxy();
606 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
607 return {};
608 }
609
610 auto proxy = this->asSurfaceProxy();
611 auto supportedRead = this->caps()->supportedReadPixelsColorType(
612 this->colorSpaceInfo().colorType(), proxy->backendFormat(), dstCT);
613 // Fail if read color type does not have all of dstCT's color channels and those missing color
614 // channels are in the src.
615 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
616 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
617 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
618 if ((~legalReadComponents & dstComponents) & srcComponents) {
619 return {};
620 }
621
622 if (!this->caps()->transferBufferSupport() ||
623 !supportedRead.fOffsetAlignmentForTransferBuffer) {
624 return {};
625 }
626
627 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
628 size_t size = rowBytes * rect.height();
629 auto buffer = direct->priv().resourceProvider()->createBuffer(
630 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
631 if (!buffer) {
632 return {};
633 }
634 auto srcRect = rect;
635 bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin;
636 if (flip) {
637 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
638 this->height() - rect.fTop);
639 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400640 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
641 this->colorSpaceInfo().colorType(),
642 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400643 PixelTransferResult result;
644 result.fTransferBuffer = std::move(buffer);
645 auto at = this->colorSpaceInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400646 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400647 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
648 void* dst, const void* src) {
649 GrPixelInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
650 GrPixelInfo dstInfo(dstCT, at, nullptr, w, h);
651 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
652 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400653 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400654 };
655 }
656 return result;
657}