blob: 5630d58297ac2419e8134fd443b472683697d478 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "GrRenderTarget.h"
11
12#include "GrContext.h"
13#include "GrGpu.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000014#include "GrStencilBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000016SK_DEFINE_INST_COUNT(GrRenderTarget)
17
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018bool GrRenderTarget::readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000019 GrPixelConfig config,
20 void* buffer,
21 size_t rowBytes,
22 uint32_t pixelOpsFlags) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000023 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000024 GrContext* context = this->getContext();
25 if (NULL == context) {
26 return false;
27 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000028 return context->readRenderTargetPixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000029 left, top, width, height,
30 config, buffer, rowBytes,
31 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000032}
33
34void GrRenderTarget::writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000035 GrPixelConfig config,
36 const void* buffer,
37 size_t rowBytes,
38 uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000039 // go through context so that all necessary flushing occurs
40 GrContext* context = this->getContext();
41 if (NULL == context) {
42 return;
43 }
44 context->writeRenderTargetPixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000045 left, top, width, height,
46 config, buffer, rowBytes,
47 pixelOpsFlags);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000048}
49
bsalomon@google.com75f9f252012-01-31 13:35:56 +000050void GrRenderTarget::resolve() {
51 // go through context so that all necessary flushing occurs
52 GrContext* context = this->getContext();
53 if (NULL == context) {
54 return;
55 }
56 context->resolveRenderTarget(this);
57}
58
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000059size_t GrRenderTarget::sizeInBytes() const {
60 int colorBits;
robertphillips@google.come98ade42012-06-13 12:53:07 +000061 if (kUnknown_GrPixelConfig == fDesc.fConfig) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000062 colorBits = 32; // don't know, make a guess
63 } else {
robertphillips@google.come98ade42012-06-13 12:53:07 +000064 colorBits = GrBytesPerPixel(fDesc.fConfig);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000065 }
robertphillips@google.come98ade42012-06-13 12:53:07 +000066 uint64_t size = fDesc.fWidth;
67 size *= fDesc.fHeight;
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000068 size *= colorBits;
robertphillips@google.come98ade42012-06-13 12:53:07 +000069 size *= GrMax(1, fDesc.fSampleCnt);
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000070 return (size_t)(size / 8);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000071}
72
73void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) {
74 if (kCanResolve_ResolveType == getResolveType()) {
75 if (NULL != rect) {
76 fResolveRect.join(*rect);
77 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
78 fResolveRect.setEmpty();
79 }
80 } else {
81 fResolveRect.setLTRB(0, 0, this->width(), this->height());
82 }
83 }
84}
85
86void GrRenderTarget::overrideResolveRect(const GrIRect rect) {
87 fResolveRect = rect;
88 if (fResolveRect.isEmpty()) {
89 fResolveRect.setLargestInverted();
90 } else {
91 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
92 fResolveRect.setLargestInverted();
93 }
94 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000095}
96
97void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
bsalomon@google.com8e2999f2012-11-19 14:36:31 +000098 if (stencilBuffer == fStencilBuffer) {
99 return;
100 }
101
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000102 if (NULL != fStencilBuffer) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000103 fStencilBuffer->unref();
104
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000105 GrContext* context = this->getContext();
106 if (NULL != context) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000107 context->purgeCache();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000108 }
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000109
110 if (NULL != context) {
111 context->purgeCache();
112 }
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000113 }
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000114
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000115 fStencilBuffer = stencilBuffer;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000116
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000117 if (NULL != fStencilBuffer) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000118 fStencilBuffer->ref();
119 }
bsalomon@google.comf6ff4952011-08-09 13:32:14 +0000120}
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000121
122void GrRenderTarget::onRelease() {
123 this->setStencilBuffer(NULL);
robertphillips@google.comd3645542012-09-05 18:37:39 +0000124
125 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000126}
127
128void GrRenderTarget::onAbandon() {
129 this->setStencilBuffer(NULL);
robertphillips@google.comd3645542012-09-05 18:37:39 +0000130
131 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000132}