bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | |
| 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 14 | #include "GrStencilBuffer.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | |
| 16 | bool GrRenderTarget::readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 17 | GrPixelConfig config, void* buffer, |
| 18 | size_t rowBytes) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 19 | // go through context so that all necessary flushing occurs |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 20 | GrContext* context = this->getContext(); |
| 21 | if (NULL == context) { |
| 22 | return false; |
| 23 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 24 | return context->readRenderTargetPixels(this, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 25 | left, top, |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 26 | width, height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 27 | config, buffer, rowBytes); |
| 28 | } |
| 29 | |
| 30 | void 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | size_t GrRenderTarget::sizeInBytes() const { |
| 45 | int colorBits; |
| 46 | if (kUnknown_GrPixelConfig == fConfig) { |
| 47 | colorBits = 32; // don't know, make a guess |
| 48 | } else { |
| 49 | colorBits = GrBytesPerPixel(fConfig); |
| 50 | } |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 51 | uint64_t size = fWidth; |
| 52 | size *= fHeight; |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 53 | size *= colorBits; |
| 54 | size *= GrMax(1,fSampleCnt); |
| 55 | return (size_t)(size / 8); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) { |
| 59 | if (kCanResolve_ResolveType == getResolveType()) { |
| 60 | if (NULL != rect) { |
| 61 | fResolveRect.join(*rect); |
| 62 | if (!fResolveRect.intersect(0, 0, this->width(), this->height())) { |
| 63 | fResolveRect.setEmpty(); |
| 64 | } |
| 65 | } else { |
| 66 | fResolveRect.setLTRB(0, 0, this->width(), this->height()); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void GrRenderTarget::overrideResolveRect(const GrIRect rect) { |
| 72 | fResolveRect = rect; |
| 73 | if (fResolveRect.isEmpty()) { |
| 74 | fResolveRect.setLargestInverted(); |
| 75 | } else { |
| 76 | if (!fResolveRect.intersect(0, 0, this->width(), this->height())) { |
| 77 | fResolveRect.setLargestInverted(); |
| 78 | } |
| 79 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 83 | if (NULL != fStencilBuffer) { |
| 84 | fStencilBuffer->wasDetachedFromRenderTarget(this); |
| 85 | fStencilBuffer->unref(); |
| 86 | } |
| 87 | fStencilBuffer = stencilBuffer; |
| 88 | if (NULL != fStencilBuffer) { |
| 89 | fStencilBuffer->wasAttachedToRenderTarget(this); |
| 90 | fStencilBuffer->ref(); |
| 91 | } |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 92 | } |