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 | |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 16 | SK_DEFINE_INST_COUNT(GrRenderTarget) |
| 17 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 18 | bool GrRenderTarget::readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 19 | GrPixelConfig config, |
| 20 | void* buffer, |
| 21 | size_t rowBytes, |
| 22 | uint32_t pixelOpsFlags) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 23 | // go through context so that all necessary flushing occurs |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 24 | GrContext* context = this->getContext(); |
| 25 | if (NULL == context) { |
| 26 | return false; |
| 27 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 28 | return context->readRenderTargetPixels(this, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 29 | left, top, width, height, |
| 30 | config, buffer, rowBytes, |
| 31 | pixelOpsFlags); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void GrRenderTarget::writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 35 | GrPixelConfig config, |
| 36 | const void* buffer, |
| 37 | size_t rowBytes, |
| 38 | uint32_t pixelOpsFlags) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 39 | // 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.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 45 | left, top, width, height, |
| 46 | config, buffer, rowBytes, |
| 47 | pixelOpsFlags); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 48 | } |
| 49 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 50 | void 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 59 | size_t GrRenderTarget::sizeInBytes() const { |
| 60 | int colorBits; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 61 | if (kUnknown_GrPixelConfig == fDesc.fConfig) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 62 | colorBits = 32; // don't know, make a guess |
| 63 | } else { |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 64 | colorBits = GrBytesPerPixel(fDesc.fConfig); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 65 | } |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 66 | uint64_t size = fDesc.fWidth; |
| 67 | size *= fDesc.fHeight; |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 68 | size *= colorBits; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 69 | size *= GrMax(1, fDesc.fSampleCnt); |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 70 | return (size_t)(size / 8); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void 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 | |
| 86 | void 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 98 | if (NULL != fStencilBuffer) { |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame^] | 99 | fStencilBuffer->unref(); |
| 100 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 101 | GrContext* context = this->getContext(); |
| 102 | if (NULL != context) { |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame^] | 103 | context->purgeCache(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 104 | } |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 105 | |
| 106 | if (NULL != context) { |
| 107 | context->purgeCache(); |
| 108 | } |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 109 | } |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 111 | fStencilBuffer = stencilBuffer; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 112 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 113 | if (NULL != fStencilBuffer) { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 114 | fStencilBuffer->ref(); |
| 115 | } |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 116 | } |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 117 | |
| 118 | void GrRenderTarget::onRelease() { |
| 119 | this->setStencilBuffer(NULL); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 120 | |
| 121 | INHERITED::onRelease(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void GrRenderTarget::onAbandon() { |
| 125 | this->setStencilBuffer(NULL); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 126 | |
| 127 | INHERITED::onAbandon(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 128 | } |