blob: ed1a018a020189a6eaee2ee535a502da42dcc31f [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
16bool GrRenderTarget::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000017 GrPixelConfig config, void* buffer,
18 size_t rowBytes) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000019 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000020 GrContext* context = this->getContext();
21 if (NULL == context) {
22 return false;
23 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000024 return context->readRenderTargetPixels(this,
bsalomon@google.com6f379512011-11-16 20:36:03 +000025 left, top,
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000026 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000027 config, buffer, rowBytes);
28}
29
30void GrRenderTarget::writePixels(int left, int top, int width, int height,
31 GrPixelConfig config, const void* buffer,
32 size_t rowBytes) {
33 // go through context so that all necessary flushing occurs
34 GrContext* context = this->getContext();
35 if (NULL == context) {
36 return;
37 }
38 context->writeRenderTargetPixels(this,
39 left, top,
40 width, height,
41 config, buffer, rowBytes);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000042}
43
bsalomon@google.com75f9f252012-01-31 13:35:56 +000044void GrRenderTarget::resolve() {
45 // go through context so that all necessary flushing occurs
46 GrContext* context = this->getContext();
47 if (NULL == context) {
48 return;
49 }
50 context->resolveRenderTarget(this);
51}
52
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000053size_t GrRenderTarget::sizeInBytes() const {
54 int colorBits;
55 if (kUnknown_GrPixelConfig == fConfig) {
56 colorBits = 32; // don't know, make a guess
57 } else {
58 colorBits = GrBytesPerPixel(fConfig);
59 }
bsalomon@google.com99621082011-11-15 16:47:16 +000060 uint64_t size = fWidth;
61 size *= fHeight;
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000062 size *= colorBits;
63 size *= GrMax(1,fSampleCnt);
64 return (size_t)(size / 8);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000065}
66
67void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) {
68 if (kCanResolve_ResolveType == getResolveType()) {
69 if (NULL != rect) {
70 fResolveRect.join(*rect);
71 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
72 fResolveRect.setEmpty();
73 }
74 } else {
75 fResolveRect.setLTRB(0, 0, this->width(), this->height());
76 }
77 }
78}
79
80void GrRenderTarget::overrideResolveRect(const GrIRect rect) {
81 fResolveRect = rect;
82 if (fResolveRect.isEmpty()) {
83 fResolveRect.setLargestInverted();
84 } else {
85 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
86 fResolveRect.setLargestInverted();
87 }
88 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000089}
90
91void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000092 if (NULL != fStencilBuffer) {
93 fStencilBuffer->wasDetachedFromRenderTarget(this);
94 fStencilBuffer->unref();
95 }
96 fStencilBuffer = stencilBuffer;
97 if (NULL != fStencilBuffer) {
98 fStencilBuffer->wasAttachedToRenderTarget(this);
99 fStencilBuffer->ref();
100 }
bsalomon@google.comf6ff4952011-08-09 13:32:14 +0000101}