blob: 1a62316eb0fa8481562ff7642574e4e46549abd3 [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 Osman45580d32016-11-23 09:37:01 -050025
Robert Phillips2de8cfa2017-06-28 10:33:41 -040026#define ASSERT_SINGLE_OWNER \
27 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050028#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050029
30// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
31// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
32// stack. When this occurs with a closed GrOpList, a new one will be allocated
33// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050034GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomond6287472019-06-24 15:50:07 -040035 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -040036 SkAlphaType alphaType,
37 sk_sp<SkColorSpace> colorSpace,
38 GrPixelConfig config)
Brian Salomond6287472019-06-24 15:50:07 -040039 : fContext(context), fColorSpaceInfo(colorType, alphaType, std::move(colorSpace), config) {}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040040
Robert Phillips0d075de2019-03-04 11:08:13 -050041GrAuditTrail* GrSurfaceContext::auditTrail() {
42 return fContext->priv().auditTrail();
43}
44
45GrDrawingManager* GrSurfaceContext::drawingManager() {
46 return fContext->priv().drawingManager();
47}
48
49const GrDrawingManager* GrSurfaceContext::drawingManager() const {
50 return fContext->priv().drawingManager();
51}
52
53#ifdef SK_DEBUG
54GrSingleOwner* GrSurfaceContext::singleOwner() {
55 return fContext->priv().singleOwner();
56}
57#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040058static bool valid_premul_color_type(GrColorType ct) {
59 switch (ct) {
60 case GrColorType::kUnknown: return false;
61 case GrColorType::kAlpha_8: return false;
Greg Daniel48fec762019-06-18 17:06:43 -040062 case GrColorType::kBGR_565: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040063 case GrColorType::kABGR_4444: return true;
64 case GrColorType::kRGBA_8888: return true;
65 case GrColorType::kRGB_888x: return false;
66 case GrColorType::kRG_88: return false;
67 case GrColorType::kBGRA_8888: return true;
68 case GrColorType::kRGBA_1010102: return true;
69 case GrColorType::kGray_8: return false;
70 case GrColorType::kAlpha_F16: return false;
71 case GrColorType::kRGBA_F16: return true;
72 case GrColorType::kRGBA_F16_Clamped: return true;
73 case GrColorType::kRG_F32: return false;
74 case GrColorType::kRGBA_F32: return true;
Robert Phillipsfe18de52019-06-06 17:21:50 -040075 case GrColorType::kR_16: return false;
76 case GrColorType::kRG_1616: return false;
Robert Phillips66a46032019-06-18 08:00:42 -040077 // Experimental (for Y416 and mutant P016/P010)
78 case GrColorType::kRGBA_16161616: return false;
Brian Salomone14cfbe2019-06-24 15:00:58 -040079 case GrColorType::kRG_F16: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040080 }
81 SK_ABORT("Invalid GrColorType");
82 return false;
83}
84
85// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
86// (skbug.com/6718)
87static bool valid_premul_config(GrPixelConfig config) {
88 switch (config) {
89 case kUnknown_GrPixelConfig: return false;
90 case kAlpha_8_GrPixelConfig: return false;
91 case kGray_8_GrPixelConfig: return false;
92 case kRGB_565_GrPixelConfig: return false;
93 case kRGBA_4444_GrPixelConfig: return true;
94 case kRGBA_8888_GrPixelConfig: return true;
95 case kRGB_888_GrPixelConfig: return false;
96 case kRGB_888X_GrPixelConfig: return false;
97 case kRG_88_GrPixelConfig: return false;
98 case kBGRA_8888_GrPixelConfig: return true;
99 case kSRGBA_8888_GrPixelConfig: return true;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400100 case kRGBA_1010102_GrPixelConfig: return true;
101 case kRGBA_float_GrPixelConfig: return true;
102 case kRG_float_GrPixelConfig: return false;
103 case kAlpha_half_GrPixelConfig: return false;
104 case kRGBA_half_GrPixelConfig: return true;
105 case kRGBA_half_Clamped_GrPixelConfig: return true;
106 case kRGB_ETC1_GrPixelConfig: return false;
107 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
108 case kAlpha_8_as_Red_GrPixelConfig: return false;
109 case kAlpha_half_as_Red_GrPixelConfig: return false;
110 case kGray_8_as_Lum_GrPixelConfig: return false;
111 case kGray_8_as_Red_GrPixelConfig: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400112 case kR_16_GrPixelConfig: return false;
113 case kRG_1616_GrPixelConfig: return false;
Robert Phillips66a46032019-06-18 08:00:42 -0400114 // Experimental (for Y416 and mutant P016/P010)
115 case kRGBA_16161616_GrPixelConfig: return false;
116 case kRG_half_GrPixelConfig: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400117 }
118 SK_ABORT("Invalid GrPixelConfig");
119 return false;
120}
121
122static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
123 bool premulConversion) {
124 // We only allow premul <-> unpremul conversions for some formats
125 if (premulConversion &&
126 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
127 return false;
128 }
129 return true;
130}
131
132bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width,
133 int height, GrColorType dstColorType,
134 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
135 uint32_t pixelOpsFlags) {
136 SkASSERT(buffer);
137
138 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
139
140 // MDB TODO: delay this instantiation until later in the method
141 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
142 return false;
143 }
144
145 GrSurface* srcSurface = srcProxy->peekSurface();
146
147 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
148 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
149 &width, &height, &buffer, &rowBytes)) {
150 return false;
151 }
152
Brian Salomone7499c72019-06-24 12:12:36 -0400153 // TODO: Pass dst buffer's alpha type.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400154 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomone7499c72019-06-24 12:12:36 -0400155 SkASSERT(!unpremul || this->colorSpaceInfo().alphaType() != kUnpremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400156
157 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
158 return false;
159 }
160
161 bool needColorConversion =
162 SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace);
163
164 const GrCaps* caps = direct->priv().caps();
165 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
166 // care so much about getImageData performance. However, in order to ensure putImageData/
167 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
168 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
169 // fContext->vaildaPMUPMConversionExists()).
170 bool canvas2DFastPath =
171 unpremul &&
172 !needColorConversion &&
173 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
174 SkToBool(srcProxy->asTextureProxy()) &&
175 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
176 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
177 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
178 direct->priv().validPMUPMConversionExists();
179
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400180 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
181 if (readFlag == GrCaps::kProtected_ReadFlag) {
182 return false;
183 }
184
185 if (readFlag == GrCaps::kRequiresCopy_ReadFlag || canvas2DFastPath) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186 GrBackendFormat format;
187 GrPixelConfig config;
Brian Salomond6287472019-06-24 15:50:07 -0400188 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189 if (canvas2DFastPath) {
190 config = kRGBA_8888_GrPixelConfig;
191 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomond6287472019-06-24 15:50:07 -0400192 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400193 } else {
194 config = srcProxy->config();
195 format = srcProxy->backendFormat().makeTexture2D();
196 if (!format.isValid()) {
197 return false;
198 }
Brian Salomond6287472019-06-24 15:50:07 -0400199 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400200 }
201 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
202
203 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400204 format, SkBackingFit::kApprox, width, height, config, colorType, std::move(cs), 1,
Greg Daniel6eb8c242019-06-05 10:22:24 -0400205 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
206 if (!tempCtx) {
207 return false;
208 }
209
210 std::unique_ptr<GrFragmentProcessor> fp;
211 if (canvas2DFastPath) {
212 fp = direct->priv().createPMToUPMEffect(
213 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
214 SkMatrix::I()));
215 if (dstColorType == GrColorType::kBGRA_8888) {
216 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
217 dstColorType = GrColorType::kRGBA_8888;
218 }
219 } else {
220 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
221 }
222 if (!fp) {
223 return false;
224 }
225 GrPaint paint;
226 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
227 paint.addColorFragmentProcessor(std::move(fp));
228
229 tempCtx->asRenderTargetContext()->fillRectToRect(
230 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
231 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
232
233 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
234 return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace,
235 buffer, rowBytes, flags);
236 }
237
Greg Daniel6eb8c242019-06-05 10:22:24 -0400238 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomonf30b1c12019-06-20 12:25:02 -0400239 auto supportedRead = caps->supportedReadPixelsColorType(
240 srcProxy->config(), srcProxy->backendFormat(), dstColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400241
Brian Salomonf30b1c12019-06-20 12:25:02 -0400242 bool convert = unpremul || needColorConversion || flip ||
243 (dstColorType != supportedRead.fColorType) ||
244 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400245
Brian Salomonf30b1c12019-06-20 12:25:02 -0400246 std::unique_ptr<char[]> tmpPixels;
247 GrPixelInfo tmpInfo;
248 GrPixelInfo dstInfo;
249 void* readDst = buffer;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400250 if (convert) {
Brian Salomonf30b1c12019-06-20 12:25:02 -0400251 tmpInfo.fColorInfo.fColorType = supportedRead.fColorType;
252 tmpInfo.fColorInfo.fAlphaType = kPremul_SkAlphaType;
253 tmpInfo.fColorInfo.fColorSpace = this->colorSpaceInfo().colorSpace();
254 tmpInfo.fOrigin = srcProxy->origin();
255 tmpInfo.fRowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * width;
256
257 dstInfo.fColorInfo.fColorType = dstColorType;
258 dstInfo.fColorInfo.fAlphaType = unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
259 dstInfo.fColorInfo.fColorSpace = dstColorSpace;
260 dstInfo.fOrigin = kTopLeft_GrSurfaceOrigin;
261 dstInfo.fRowBytes = rowBytes;
262
263 dstInfo.fWidth = tmpInfo.fWidth = width;
264 dstInfo.fHeight = tmpInfo.fHeight = height;
265
266 size_t size = tmpInfo.fRowBytes * height;
267 tmpPixels.reset(new char[size]);
268 // Chrome MSAN bots require this.
269 sk_bzero(tmpPixels.get(), size);
270 readDst = tmpPixels.get();
271 rowBytes = tmpInfo.fRowBytes;
272 top = flip ? srcSurface->height() - top - height : top;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400273 }
274
275 direct->priv().flushSurface(srcProxy);
276
Brian Salomonf30b1c12019-06-20 12:25:02 -0400277 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height,
278 supportedRead.fColorType, readDst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400279 return false;
280 }
281
Greg Daniel6eb8c242019-06-05 10:22:24 -0400282 if (convert) {
Brian Salomonf30b1c12019-06-20 12:25:02 -0400283 return GrConvertPixels(dstInfo, buffer, tmpInfo, tmpPixels.get(), supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400284 }
285 return true;
286}
Robert Phillips0d075de2019-03-04 11:08:13 -0500287
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400288bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
289 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400290 ASSERT_SINGLE_OWNER
291 RETURN_FALSE_IF_ABANDONED
292 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500293 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400294
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400295 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400296 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
297 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400298 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400299 }
Brian Salomonc320b152018-02-20 14:05:36 -0500300 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
301 if (GrColorType::kUnknown == colorType) {
302 return false;
303 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500304
305 auto direct = fContext->priv().asDirectContext();
306 if (!direct) {
307 return false;
308 }
309
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310 return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType,
311 dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
312}
313
314bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
315 GrColorType srcColorType, SkColorSpace* srcColorSpace,
316 const void* srcBuffer, size_t srcRowBytes,
317 uint32_t pixelOpsFlags) {
318 if (GrColorType::kUnknown == srcColorType) {
319 return false;
320 }
321
322 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
323 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
324 return false;
325 }
326
327 GrSurface* dstSurface = dstProxy->peekSurface();
328
329 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
330 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
331 &width, &height, &srcBuffer, &srcRowBytes)) {
332 return false;
333 }
334
Brian Salomone7499c72019-06-24 12:12:36 -0400335 // TODO: Pass src buffer's alpha type.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomone7499c72019-06-24 12:12:36 -0400337 SkASSERT(!premul || this->colorSpaceInfo().alphaType() != kUnpremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400338
339 bool needColorConversion =
340 SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace());
341
342 const GrCaps* caps = direct->priv().caps();
343 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
344 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
345 bool canvas2DFastPath =
346 !caps->avoidWritePixelsFastPath() &&
347 premul &&
348 !needColorConversion &&
349 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
350 SkToBool(this->asRenderTargetContext()) &&
351 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
352 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
353 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
354 direct->priv().validPMUPMConversionExists();
355
356 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
357 GrSurfaceDesc desc;
358 desc.fWidth = width;
359 desc.fHeight = height;
360 desc.fSampleCnt = 1;
Brian Salomond6287472019-06-24 15:50:07 -0400361 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400362
363 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400364 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400365 if (canvas2DFastPath) {
366 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400367 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400368 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomone7499c72019-06-24 12:12:36 -0400369 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400370 } else {
371 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400372 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400373 format = dstProxy->backendFormat().makeTexture2D();
374 if (!format.isValid()) {
375 return false;
376 }
Brian Salomone7499c72019-06-24 12:12:36 -0400377 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400378 }
379
Greg Daniel2e52ad12019-06-13 10:04:16 -0400380 // It is more efficient for us to write pixels into a top left origin so we prefer that.
381 // However, if the final proxy isn't a render target then we must use a copy to move the
382 // data into it which requires the origins to match. If the final proxy is a render target
383 // we can use a draw instead which doesn't have this origin restriction. Thus for render
384 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400385 GrSurfaceOrigin tempOrigin =
386 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400387 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400388 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
389
Greg Daniel6eb8c242019-06-05 10:22:24 -0400390 if (!tempProxy) {
391 return false;
392 }
393 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400394 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395 if (!tempCtx) {
396 return false;
397 }
398 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
399
400 // In the fast path we always write the srcData to the temp context as though it were RGBA.
401 // When the data is really BGRA the write will cause the R and B channels to be swapped in
402 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
403 // dst.
404 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
405 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
406 srcBuffer, srcRowBytes, flags)) {
407 return false;
408 }
409
410 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400411 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400412 if (canvas2DFastPath) {
413 fp = direct->priv().createUPMToPMEffect(
414 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
415 if (srcColorType == GrColorType::kBGRA_8888) {
416 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
417 }
418 } else {
419 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
420 }
421 if (!fp) {
422 return false;
423 }
424 GrPaint paint;
425 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
426 paint.addColorFragmentProcessor(std::move(fp));
427 this->asRenderTargetContext()->fillRectToRect(
428 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
429 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
430 } else {
431 SkIRect srcRect = SkIRect::MakeWH(width, height);
432 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400433 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400434 return false;
435 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400436 }
437 return true;
438 }
439
Greg Daniel6eb8c242019-06-05 10:22:24 -0400440 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
441 return false;
442 }
443
444 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
445 srcColorType);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400446 bool convert = premul || needColorConversion || (srcColorType != allowedColorType) ||
447 dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400448
Brian Salomonf30b1c12019-06-20 12:25:02 -0400449 std::unique_ptr<char[]> tmpPixels;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450 if (convert) {
Brian Salomonf30b1c12019-06-20 12:25:02 -0400451 GrPixelInfo srcInfo;
452 srcInfo.fColorInfo.fAlphaType = (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
453 srcInfo.fColorInfo.fColorType = srcColorType;
454 srcInfo.fColorInfo.fColorSpace = srcColorSpace;
455 srcInfo.fRowBytes = srcRowBytes;
456 srcInfo.fOrigin = kTopLeft_GrSurfaceOrigin;
457
458 GrPixelInfo tmpInfo;
459 tmpInfo.fColorInfo.fAlphaType = kPremul_SkAlphaType;
460 tmpInfo.fColorInfo.fColorType = allowedColorType;
461 tmpInfo.fColorInfo.fColorSpace = this->colorSpaceInfo().colorSpace();
462 tmpInfo.fRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
463 tmpInfo.fOrigin = dstProxy->origin();
464
465 srcInfo.fWidth = tmpInfo.fWidth = width;
466 srcInfo.fHeight = tmpInfo.fHeight = height;
467
468 tmpPixels.reset(new char[tmpInfo.fRowBytes * height]);
469
470 GrConvertPixels(tmpInfo, tmpPixels.get(), srcInfo, srcBuffer);
471
472 srcColorType = tmpInfo.fColorInfo.fColorType;
473 srcBuffer = tmpPixels.get();
474 srcRowBytes = tmpInfo.fRowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400475 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400476 top = dstSurface->height() - top - height;
477 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400478 }
479
480 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
481 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
482 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
483 // destination proxy)
484 // TODO: should this policy decision just be moved into the drawing manager?
485 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
486
487 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
488 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400489}
490
491bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
492 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400493 ASSERT_SINGLE_OWNER
494 RETURN_FALSE_IF_ABANDONED
495 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500496 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400497
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400498 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400499 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400500 }
Brian Salomonc320b152018-02-20 14:05:36 -0500501 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
502 if (GrColorType::kUnknown == colorType) {
503 return false;
504 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500505
506 auto direct = fContext->priv().asDirectContext();
507 if (!direct) {
508 return false;
509 }
510
Greg Danielb3f82dd2019-05-29 14:24:32 -0400511 if (this->asSurfaceProxy()->readOnly()) {
512 return false;
513 }
514
Greg Daniel6eb8c242019-06-05 10:22:24 -0400515 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
516 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500517}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400518
519bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
520 ASSERT_SINGLE_OWNER
521 RETURN_FALSE_IF_ABANDONED
522 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400523 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400524
Greg Daniel46cfbc62019-06-07 11:43:30 -0400525 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
526 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Greg Daniel2c3398d2019-06-19 11:58:01 -0400527 SkASSERT(src->config() == this->asSurfaceProxy()->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400528
529 GrSurfaceProxy* dst = this->asSurfaceProxy();
530
531 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400532 return false;
533 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400534
Greg Daniel46cfbc62019-06-07 11:43:30 -0400535 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400536}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400537