blob: 3ab7a1fe5d4e94cabbbba7b1efb6984b063b4bdb [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
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600428 return this->getOpList()->copySurface(fContext, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400429}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400430
Brian Salomonbf6b9792019-08-21 09:38:10 -0400431std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
432 const SkImageInfo& info,
433 const SkIRect& srcRect,
434 SkSurface::RescaleGamma rescaleGamma,
435 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400436 auto direct = fContext->priv().asDirectContext();
437 if (!direct) {
438 return nullptr;
439 }
440 auto rtProxy = this->asRenderTargetProxy();
441 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
442 return nullptr;
443 }
444
445 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
446 if (info.alphaType() == kUnpremul_SkAlphaType) {
447 return nullptr;
448 }
449
450 int srcW = srcRect.width();
451 int srcH = srcRect.height();
452 int srcX = srcRect.fLeft;
453 int srcY = srcRect.fTop;
454 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(this->asTextureProxy());
455 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
456 if (!texProxy) {
457 texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), GrMipMapped::kNo, srcRect,
458 SkBackingFit::kApprox, SkBudgeted::kNo);
459 if (!texProxy) {
460 return nullptr;
461 }
462 srcX = 0;
463 srcY = 0;
464 constraint = SkCanvas::kFast_SrcRectConstraint;
465 }
466
467 float sx = (float)info.width() / srcW;
468 float sy = (float)info.height() / srcH;
469
470 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
471 int stepsX;
472 int stepsY;
473 if (rescaleQuality > kNone_SkFilterQuality) {
474 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
475 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
476 } else {
477 stepsX = sx != 1.f;
478 stepsY = sy != 1.f;
479 }
480 SkASSERT(stepsX || stepsY);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400481 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
482 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
483 std::unique_ptr<GrRenderTargetContext> tempA;
484 std::unique_ptr<GrRenderTargetContext> tempB;
485
Brian Salomone9ad9982019-07-22 16:17:41 -0400486 // Assume we should ignore the rescale linear request if the surface has no color space since
487 // it's unclear how we'd linearize from an unknown color space.
488 if (rescaleGamma == SkSurface::kLinear && this->colorSpaceInfo().colorSpace() &&
489 !this->colorSpaceInfo().colorSpace()->gammaIsLinear()) {
490 auto cs = this->colorSpaceInfo().colorSpace()->makeLinearGamma();
491 auto xform = GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
492 this->colorSpaceInfo().alphaType(), cs.get(),
493 kPremul_SkAlphaType);
494 // We'll fall back to kRGBA_8888 if half float not supported.
495 auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
496 SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo,
497 kTopLeft_GrSurfaceOrigin);
498 if (!linearRTC) {
499 return nullptr;
500 }
501 linearRTC->drawTexture(GrNoClip(), texProxy, GrSamplerState::Filter::kNearest,
502 SkBlendMode::kSrc, SK_PMColor4fWHITE, SkRect::Make(srcRect),
503 SkRect::MakeWH(srcW, srcH), GrAA::kNo, GrQuadAAFlags::kNone,
504 constraint, SkMatrix::I(), std::move(xform));
505 texProxy = linearRTC->asTextureProxyRef();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400506 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400507 srcX = 0;
508 srcY = 0;
509 constraint = SkCanvas::kFast_SrcRectConstraint;
510 }
511 while (stepsX || stepsY) {
512 int nextW = info.width();
513 int nextH = info.height();
514 if (stepsX < 0) {
515 nextW = info.width() << (-stepsX - 1);
516 stepsX++;
517 } else if (stepsX != 0) {
518 if (stepsX > 1) {
519 nextW = srcW * 2;
520 }
521 --stepsX;
522 }
523 if (stepsY < 0) {
524 nextH = info.height() << (-stepsY - 1);
525 stepsY++;
526 } else if (stepsY != 0) {
527 if (stepsY > 1) {
528 nextH = srcH * 2;
529 }
530 --stepsY;
531 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400532 auto input = tempA ? tempA.get() : this;
533 GrColorType colorType = input->colorSpaceInfo().colorType();
534 auto cs = input->colorSpaceInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400535 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400536 auto prevAlphaType = input->colorSpaceInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400537 if (!stepsX && !stepsY) {
538 // Might as well fold conversion to final info in the last step.
539 cs = info.refColorSpace();
540 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400541 xform = GrColorSpaceXform::Make(input->colorSpaceInfo().colorSpace(),
542 input->colorSpaceInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400543 info.alphaType());
544 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400545 tempB = fContext->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomone9ad9982019-07-22 16:17:41 -0400546 SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo,
547 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400548 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400549 return nullptr;
550 }
551 auto dstRect = SkRect::MakeWH(nextW, nextH);
552 if (rescaleQuality == kHigh_SkFilterQuality) {
553 SkMatrix matrix;
554 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
555 std::unique_ptr<GrFragmentProcessor> fp;
556 auto dir = GrBicubicEffect::Direction::kXY;
557 if (nextW == srcW) {
558 dir = GrBicubicEffect::Direction::kY;
559 } else if (nextH == srcH) {
560 dir = GrBicubicEffect::Direction::kX;
561 }
562 if (srcW != texProxy->width() || srcH != texProxy->height()) {
563 auto domain = GrTextureDomain::MakeTexelDomain(
564 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
565 fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType);
566 } else {
567 fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType);
568 }
569 if (xform) {
570 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
571 }
572 GrPaint paint;
573 paint.addColorFragmentProcessor(std::move(fp));
574 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400575 tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
Brian Salomone9ad9982019-07-22 16:17:41 -0400576 dstRect);
577 } else {
578 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
579 : GrSamplerState::Filter::kBilerp;
580 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400581 tempB->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE,
Brian Salomone9ad9982019-07-22 16:17:41 -0400582 srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint,
583 SkMatrix::I(), std::move(xform));
584 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400585 texProxy = tempB->asTextureProxyRef();
586 tempA = std::move(tempB);
Brian Salomone9ad9982019-07-22 16:17:41 -0400587 srcX = srcY = 0;
588 srcW = nextW;
589 srcH = nextH;
590 constraint = SkCanvas::kFast_SrcRectConstraint;
591 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400592 SkASSERT(tempA);
593 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400594}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400595
596GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
597 const SkIRect& rect) {
598 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
599 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
600 auto direct = fContext->priv().asDirectContext();
601 if (!direct) {
602 return {};
603 }
604 auto rtProxy = this->asRenderTargetProxy();
605 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
606 return {};
607 }
608
609 auto proxy = this->asSurfaceProxy();
610 auto supportedRead = this->caps()->supportedReadPixelsColorType(
611 this->colorSpaceInfo().colorType(), proxy->backendFormat(), dstCT);
612 // Fail if read color type does not have all of dstCT's color channels and those missing color
613 // channels are in the src.
614 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
615 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
616 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
617 if ((~legalReadComponents & dstComponents) & srcComponents) {
618 return {};
619 }
620
621 if (!this->caps()->transferBufferSupport() ||
622 !supportedRead.fOffsetAlignmentForTransferBuffer) {
623 return {};
624 }
625
626 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
627 size_t size = rowBytes * rect.height();
628 auto buffer = direct->priv().resourceProvider()->createBuffer(
629 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
630 if (!buffer) {
631 return {};
632 }
633 auto srcRect = rect;
634 bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin;
635 if (flip) {
636 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
637 this->height() - rect.fTop);
638 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400639 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
640 this->colorSpaceInfo().colorType(),
641 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400642 PixelTransferResult result;
643 result.fTransferBuffer = std::move(buffer);
644 auto at = this->colorSpaceInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400645 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400646 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
647 void* dst, const void* src) {
648 GrPixelInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
649 GrPixelInfo dstInfo(dstCT, at, nullptr, w, h);
650 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
651 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400652 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400653 };
654 }
655 return result;
656}