blob: 4b29ce0ace3125c49fae4d1d8bb80df54817bc26 [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/GrAuditTrail.h"
11#include "include/private/GrOpList.h"
12#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
Brian Salomon55091022019-06-17 14:08:58 -040016#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040018#include "src/gpu/GrGpu.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 Salomonf3569f02017-10-24 12:52:33 -040035 GrPixelConfig config,
Robert Phillips0d075de2019-03-04 11:08:13 -050036 sk_sp<SkColorSpace> colorSpace)
Brian Salomonf3569f02017-10-24 12:52:33 -040037 : fContext(context)
Robert Phillips0d075de2019-03-04 11:08:13 -050038 , fColorSpaceInfo(std::move(colorSpace), config) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040039}
40
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;
62 case GrColorType::kRGB_565: return false;
63 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;
75 case GrColorType::kRGB_ETC1: return false;
Robert Phillipsf83c4682019-06-13 16:49:21 +000076 // Experimental (for P016 and P010)
Robert Phillipsfe18de52019-06-06 17:21:50 -040077 case GrColorType::kR_16: return false;
78 case GrColorType::kRG_1616: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040079 }
80 SK_ABORT("Invalid GrColorType");
81 return false;
82}
83
84// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
85// (skbug.com/6718)
86static bool valid_premul_config(GrPixelConfig config) {
87 switch (config) {
88 case kUnknown_GrPixelConfig: return false;
89 case kAlpha_8_GrPixelConfig: return false;
90 case kGray_8_GrPixelConfig: return false;
91 case kRGB_565_GrPixelConfig: return false;
92 case kRGBA_4444_GrPixelConfig: return true;
93 case kRGBA_8888_GrPixelConfig: return true;
94 case kRGB_888_GrPixelConfig: return false;
95 case kRGB_888X_GrPixelConfig: return false;
96 case kRG_88_GrPixelConfig: return false;
97 case kBGRA_8888_GrPixelConfig: return true;
98 case kSRGBA_8888_GrPixelConfig: return true;
99 case kSBGRA_8888_GrPixelConfig: return true;
100 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 Phillipsf83c4682019-06-13 16:49:21 +0000112 // Experimental (for P016 and P010)
Robert Phillipsfe18de52019-06-06 17:21:50 -0400113 case kR_16_GrPixelConfig: return false;
114 case kRG_1616_GrPixelConfig: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400115 }
116 SK_ABORT("Invalid GrPixelConfig");
117 return false;
118}
119
120static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
121 bool premulConversion) {
122 // We only allow premul <-> unpremul conversions for some formats
123 if (premulConversion &&
124 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
125 return false;
126 }
127 return true;
128}
129
130bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width,
131 int height, GrColorType dstColorType,
132 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
133 uint32_t pixelOpsFlags) {
134 SkASSERT(buffer);
135
136 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
137
138 // MDB TODO: delay this instantiation until later in the method
139 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
140 return false;
141 }
142
143 GrSurface* srcSurface = srcProxy->peekSurface();
144
145 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
146 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
147 &width, &height, &buffer, &rowBytes)) {
148 return false;
149 }
150
151 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
152 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
153
154 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
155 return false;
156 }
157
158 bool needColorConversion =
159 SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace);
160
161 const GrCaps* caps = direct->priv().caps();
162 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
163 // care so much about getImageData performance. However, in order to ensure putImageData/
164 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
165 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
166 // fContext->vaildaPMUPMConversionExists()).
167 bool canvas2DFastPath =
168 unpremul &&
169 !needColorConversion &&
170 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
171 SkToBool(srcProxy->asTextureProxy()) &&
172 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
173 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
174 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
175 direct->priv().validPMUPMConversionExists();
176
177 if (!caps->surfaceSupportsReadPixels(srcSurface) ||
178 canvas2DFastPath) {
179 GrBackendFormat format;
180 GrPixelConfig config;
181 if (canvas2DFastPath) {
182 config = kRGBA_8888_GrPixelConfig;
183 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
184 } else {
185 config = srcProxy->config();
186 format = srcProxy->backendFormat().makeTexture2D();
187 if (!format.isValid()) {
188 return false;
189 }
190 }
191 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
192
193 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
194 format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1,
195 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
196 if (!tempCtx) {
197 return false;
198 }
199
200 std::unique_ptr<GrFragmentProcessor> fp;
201 if (canvas2DFastPath) {
202 fp = direct->priv().createPMToUPMEffect(
203 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
204 SkMatrix::I()));
205 if (dstColorType == GrColorType::kBGRA_8888) {
206 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
207 dstColorType = GrColorType::kRGBA_8888;
208 }
209 } else {
210 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
211 }
212 if (!fp) {
213 return false;
214 }
215 GrPaint paint;
216 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
217 paint.addColorFragmentProcessor(std::move(fp));
218
219 tempCtx->asRenderTargetContext()->fillRectToRect(
220 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
221 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
222
223 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
224 return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace,
225 buffer, rowBytes, flags);
226 }
227
Greg Daniel6eb8c242019-06-05 10:22:24 -0400228 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon55091022019-06-17 14:08:58 -0400229 auto supportedRead = caps->supportedReadPixelsColorType(
230 srcProxy->config(), srcProxy->backendFormat(), dstColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400231
Brian Salomon55091022019-06-17 14:08:58 -0400232 bool convert = unpremul || needColorConversion || flip ||
233 (dstColorType != supportedRead.fColorType) ||
234 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400235
Brian Salomon55091022019-06-17 14:08:58 -0400236 std::unique_ptr<char[]> tmpPixels;
237 GrPixelInfo tmpInfo;
238 GrPixelInfo dstInfo;
239 void* readDst = buffer;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400240 if (convert) {
Brian Salomon55091022019-06-17 14:08:58 -0400241 tmpInfo.fColorInfo.fColorType = supportedRead.fColorType;
242 tmpInfo.fColorInfo.fAlphaType = kPremul_SkAlphaType;
243 tmpInfo.fColorInfo.fColorSpace = this->colorSpaceInfo().colorSpace();
244 tmpInfo.fOrigin = srcProxy->origin();
245 tmpInfo.fRowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * width;
246
247 dstInfo.fColorInfo.fColorType = dstColorType;
248 dstInfo.fColorInfo.fAlphaType = unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
249 dstInfo.fColorInfo.fColorSpace = dstColorSpace;
250 dstInfo.fOrigin = kTopLeft_GrSurfaceOrigin;
251 dstInfo.fRowBytes = rowBytes;
252
253 dstInfo.fWidth = tmpInfo.fWidth = width;
254 dstInfo.fHeight = tmpInfo.fHeight = height;
255
256 size_t size = tmpInfo.fRowBytes * height;
257 tmpPixels.reset(new char[size]);
258 // Chrome MSAN bots require this.
259 sk_bzero(tmpPixels.get(), size);
260 readDst = tmpPixels.get();
261 rowBytes = tmpInfo.fRowBytes;
262 top = flip ? srcSurface->height() - top - height : top;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400263 }
264
265 direct->priv().flushSurface(srcProxy);
266
Brian Salomon55091022019-06-17 14:08:58 -0400267 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height,
268 supportedRead.fColorType, readDst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400269 return false;
270 }
271
Greg Daniel6eb8c242019-06-05 10:22:24 -0400272 if (convert) {
Brian Salomon55091022019-06-17 14:08:58 -0400273 return GrConvertPixels(dstInfo, buffer, tmpInfo, tmpPixels.get(), supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400274 }
275 return true;
276}
Robert Phillips0d075de2019-03-04 11:08:13 -0500277
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400278bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
279 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400280 ASSERT_SINGLE_OWNER
281 RETURN_FALSE_IF_ABANDONED
282 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500283 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400284
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400285 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400286 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
287 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400288 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400289 }
Brian Salomonc320b152018-02-20 14:05:36 -0500290 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
291 if (GrColorType::kUnknown == colorType) {
292 return false;
293 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500294
295 auto direct = fContext->priv().asDirectContext();
296 if (!direct) {
297 return false;
298 }
299
Greg Daniel6eb8c242019-06-05 10:22:24 -0400300 return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType,
301 dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
302}
303
304bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
305 GrColorType srcColorType, SkColorSpace* srcColorSpace,
306 const void* srcBuffer, size_t srcRowBytes,
307 uint32_t pixelOpsFlags) {
308 if (GrColorType::kUnknown == srcColorType) {
309 return false;
310 }
311
312 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
313 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
314 return false;
315 }
316
317 GrSurface* dstSurface = dstProxy->peekSurface();
318
319 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
320 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
321 &width, &height, &srcBuffer, &srcRowBytes)) {
322 return false;
323 }
324
325 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
326 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
327
328 bool needColorConversion =
329 SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace());
330
331 const GrCaps* caps = direct->priv().caps();
332 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
333 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
334 bool canvas2DFastPath =
335 !caps->avoidWritePixelsFastPath() &&
336 premul &&
337 !needColorConversion &&
338 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
339 SkToBool(this->asRenderTargetContext()) &&
340 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
341 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
342 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
343 direct->priv().validPMUPMConversionExists();
344
345 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
346 GrSurfaceDesc desc;
347 desc.fWidth = width;
348 desc.fHeight = height;
349 desc.fSampleCnt = 1;
350
351 GrBackendFormat format;
352 if (canvas2DFastPath) {
353 desc.fConfig = kRGBA_8888_GrPixelConfig;
354 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
355 } else {
356 desc.fConfig = dstProxy->config();
357 format = dstProxy->backendFormat().makeTexture2D();
358 if (!format.isValid()) {
359 return false;
360 }
361 }
362
Greg Daniel2e52ad12019-06-13 10:04:16 -0400363 // It is more efficient for us to write pixels into a top left origin so we prefer that.
364 // However, if the final proxy isn't a render target then we must use a copy to move the
365 // data into it which requires the origins to match. If the final proxy is a render target
366 // we can use a draw instead which doesn't have this origin restriction. Thus for render
367 // targets we will use top left and otherwise we will make the origins match.
Brian Salomon55091022019-06-17 14:08:58 -0400368 GrSurfaceOrigin tempOrigin =
369 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400370 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400371 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
372
Greg Daniel6eb8c242019-06-05 10:22:24 -0400373 if (!tempProxy) {
374 return false;
375 }
376 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
377 tempProxy, this->colorSpaceInfo().refColorSpace());
378 if (!tempCtx) {
379 return false;
380 }
381 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
382
383 // In the fast path we always write the srcData to the temp context as though it were RGBA.
384 // When the data is really BGRA the write will cause the R and B channels to be swapped in
385 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
386 // dst.
387 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
388 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
389 srcBuffer, srcRowBytes, flags)) {
390 return false;
391 }
392
393 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400394 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395 if (canvas2DFastPath) {
396 fp = direct->priv().createUPMToPMEffect(
397 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
398 if (srcColorType == GrColorType::kBGRA_8888) {
399 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
400 }
401 } else {
402 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
403 }
404 if (!fp) {
405 return false;
406 }
407 GrPaint paint;
408 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
409 paint.addColorFragmentProcessor(std::move(fp));
410 this->asRenderTargetContext()->fillRectToRect(
411 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
412 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
413 } else {
414 SkIRect srcRect = SkIRect::MakeWH(width, height);
415 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400416 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 return false;
418 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400419 }
420 return true;
421 }
422
Greg Daniel6eb8c242019-06-05 10:22:24 -0400423 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
424 return false;
425 }
426
427 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
428 srcColorType);
Brian Salomon55091022019-06-17 14:08:58 -0400429 bool convert = premul || needColorConversion || (srcColorType != allowedColorType) ||
430 dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400431
Brian Salomon55091022019-06-17 14:08:58 -0400432 std::unique_ptr<char[]> tmpPixels;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400433 if (convert) {
Brian Salomon55091022019-06-17 14:08:58 -0400434 GrPixelInfo srcInfo;
435 srcInfo.fColorInfo.fAlphaType = (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
436 srcInfo.fColorInfo.fColorType = srcColorType;
437 srcInfo.fColorInfo.fColorSpace = srcColorSpace;
438 srcInfo.fRowBytes = srcRowBytes;
439 srcInfo.fOrigin = kTopLeft_GrSurfaceOrigin;
440
441 GrPixelInfo tmpInfo;
442 tmpInfo.fColorInfo.fAlphaType = kPremul_SkAlphaType;
443 tmpInfo.fColorInfo.fColorType = allowedColorType;
444 tmpInfo.fColorInfo.fColorSpace = this->colorSpaceInfo().colorSpace();
445 tmpInfo.fRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
446 tmpInfo.fOrigin = dstProxy->origin();
447
448 srcInfo.fWidth = tmpInfo.fWidth = width;
449 srcInfo.fHeight = tmpInfo.fHeight = height;
450
451 tmpPixels.reset(new char[tmpInfo.fRowBytes * height]);
452
453 GrConvertPixels(tmpInfo, tmpPixels.get(), srcInfo, srcBuffer);
454
455 srcColorType = tmpInfo.fColorInfo.fColorType;
456 srcBuffer = tmpPixels.get();
457 srcRowBytes = tmpInfo.fRowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400458 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400459 top = dstSurface->height() - top - height;
460 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400461 }
462
463 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
464 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
465 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
466 // destination proxy)
467 // TODO: should this policy decision just be moved into the drawing manager?
468 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
469
470 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
471 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400472}
473
474bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
475 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400476 ASSERT_SINGLE_OWNER
477 RETURN_FALSE_IF_ABANDONED
478 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500479 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400480
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400481 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400482 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400483 }
Brian Salomonc320b152018-02-20 14:05:36 -0500484 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
485 if (GrColorType::kUnknown == colorType) {
486 return false;
487 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500488
489 auto direct = fContext->priv().asDirectContext();
490 if (!direct) {
491 return false;
492 }
493
Greg Danielb3f82dd2019-05-29 14:24:32 -0400494 if (this->asSurfaceProxy()->readOnly()) {
495 return false;
496 }
497
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
499 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500500}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400501
502bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
503 ASSERT_SINGLE_OWNER
504 RETURN_FALSE_IF_ABANDONED
505 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400506 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400507
Greg Daniel46cfbc62019-06-07 11:43:30 -0400508 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
509 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
510
511 GrSurfaceProxy* dst = this->asSurfaceProxy();
512
513 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400514 return false;
515 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400516
Greg Daniel46cfbc62019-06-07 11:43:30 -0400517 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400518}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400519