blob: 52ace3ff32c8b8615a536ab1e21089cba41b025f [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
8#include "GrSurfaceContext.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -04009
10#include "GrContextPriv.h"
Robert Phillips2de8cfa2017-06-28 10:33:41 -040011#include "GrDrawingManager.h"
12#include "GrOpList.h"
Brian Osman47299142017-03-07 13:22:22 -050013#include "SkColorSpace_Base.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040014#include "SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050015
16#include "../private/GrAuditTrail.h"
17
Robert Phillips2de8cfa2017-06-28 10:33:41 -040018#define ASSERT_SINGLE_OWNER \
19 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
20#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050021
22// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
23// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
24// stack. When this occurs with a closed GrOpList, a new one will be allocated
25// when the renderTargetContext attempts to use it (via getOpList).
26GrSurfaceContext::GrSurfaceContext(GrContext* context,
Robert Phillips72152832017-01-25 17:31:35 -050027 GrDrawingManager* drawingMgr,
Robert Phillips2c862492017-01-18 10:08:39 -050028 sk_sp<SkColorSpace> colorSpace,
Brian Osman45580d32016-11-23 09:37:01 -050029 GrAuditTrail* auditTrail,
30 GrSingleOwner* singleOwner)
31 : fContext(context)
Robert Phillips2c862492017-01-18 10:08:39 -050032 , fColorSpace(std::move(colorSpace))
Brian Osman45580d32016-11-23 09:37:01 -050033 , fAuditTrail(auditTrail)
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040034 , fDrawingManager(drawingMgr)
Brian Osman45580d32016-11-23 09:37:01 -050035#ifdef SK_DEBUG
36 , fSingleOwner(singleOwner)
37#endif
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040038{
39}
40
41bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
42 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040043 ASSERT_SINGLE_OWNER
44 RETURN_FALSE_IF_ABANDONED
45 SkDEBUGCODE(this->validate();)
46 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::readPixels");
47
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040048 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
49 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
50 if (kUnknown_GrPixelConfig == config) {
51 return false;
52 }
53
54 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
55 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
56 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
57 }
58
Robert Phillipsf41c22f2017-04-18 07:48:58 -040059 return fContext->contextPriv().readSurfacePixels(this, x, y,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040060 dstInfo.width(), dstInfo.height(), config,
61 dstInfo.colorSpace(),
62 dstBuffer, dstRowBytes, flags);
63}
64
65bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
66 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040067 ASSERT_SINGLE_OWNER
68 RETURN_FALSE_IF_ABANDONED
69 SkDEBUGCODE(this->validate();)
70 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::writePixels");
71
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040072 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
73 GrPixelConfig config = SkImageInfo2GrPixelConfig(srcInfo, *fContext->caps());
74 if (kUnknown_GrPixelConfig == config) {
75 return false;
76 }
77 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
78 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
79 }
80
Robert Phillipsf41c22f2017-04-18 07:48:58 -040081 return fContext->contextPriv().writeSurfacePixels(this, x, y,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040082 srcInfo.width(), srcInfo.height(),
83 config, srcInfo.colorSpace(),
84 srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -050085}
Robert Phillips2de8cfa2017-06-28 10:33:41 -040086
87bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
88 ASSERT_SINGLE_OWNER
89 RETURN_FALSE_IF_ABANDONED
90 SkDEBUGCODE(this->validate();)
91 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::onCopy");
92
93 return this->getOpList()->copySurface(*fContext->caps(),
94 this->asSurfaceProxy(), src, srcRect, dstPoint);
95}