epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
robertphillips@google.com | 5088eb4 | 2012-06-28 20:59:13 +0000 | [diff] [blame] | 11 | #include "SkGrPixelRef.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 14 | #include "SkGr.h" |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 16 | |
| 17 | // since we call lockPixels recursively on fBitmap, we need a distinct mutex, |
| 18 | // to avoid deadlock with the default one provided by SkPixelRef. |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 19 | SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 20 | |
| 21 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef() : INHERITED(&gROLockPixelsPixelRefMutex) { |
| 22 | } |
| 23 | |
| 24 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() { |
| 25 | } |
| 26 | |
| 27 | void* SkROLockPixelsPixelRef::onLockPixels(SkColorTable** ctable) { |
| 28 | if (ctable) { |
| 29 | *ctable = NULL; |
| 30 | } |
| 31 | fBitmap.reset(); |
| 32 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
| 33 | if (!this->onReadPixels(&fBitmap, NULL)) { |
| 34 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
| 35 | return NULL; |
| 36 | } |
| 37 | fBitmap.lockPixels(); |
| 38 | return fBitmap.getPixels(); |
| 39 | } |
| 40 | |
| 41 | void SkROLockPixelsPixelRef::onUnlockPixels() { |
| 42 | fBitmap.unlockPixels(); |
| 43 | } |
| 44 | |
| 45 | bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 50 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 51 | static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config dstConfig, |
| 52 | const SkIRect* subset) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 53 | if (NULL == texture) { |
| 54 | return NULL; |
| 55 | } |
| 56 | GrContext* context = texture->getContext(); |
| 57 | if (NULL == context) { |
| 58 | return NULL; |
| 59 | } |
| 60 | GrTextureDesc desc; |
| 61 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 62 | SkIPoint pointStorage; |
| 63 | SkIPoint* topLeft; |
| 64 | if (subset != NULL) { |
| 65 | SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset)); |
| 66 | // Create a new texture that is the size of subset. |
| 67 | desc.fWidth = subset->width(); |
| 68 | desc.fHeight = subset->height(); |
| 69 | pointStorage.set(subset->x(), subset->y()); |
| 70 | topLeft = &pointStorage; |
| 71 | } else { |
| 72 | desc.fWidth = texture->width(); |
| 73 | desc.fHeight = texture->height(); |
| 74 | topLeft = NULL; |
| 75 | } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 76 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 77 | desc.fConfig = SkBitmapConfig2GrPixelConfig(dstConfig); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 78 | |
| 79 | GrTexture* dst = context->createUncachedTexture(desc, NULL, 0); |
| 80 | if (NULL == dst) { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 84 | context->copyTexture(texture, dst->asRenderTarget(), topLeft); |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 85 | |
robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 86 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 87 | #if 0 |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 88 | // The render texture we have created (to perform the copy) isn't fully |
| 89 | // functional (since it doesn't have a stencil buffer). Release it here |
| 90 | // so the caller doesn't try to render to it. |
| 91 | // TODO: we can undo this release when dynamic stencil buffer attach/ |
| 92 | // detach has been implemented |
| 93 | dst->releaseRenderTarget(); |
robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 94 | #endif |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 95 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 96 | SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (dst)); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 97 | GrSafeUnref(dst); |
| 98 | return pixelRef; |
| 99 | } |
| 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 103 | SkGrPixelRef::SkGrPixelRef(GrSurface* surface, bool transferCacheLock) { |
robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 104 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 105 | #if 0 |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 106 | // The GrTexture has a ref to the GrRenderTarget but not vice versa. |
| 107 | // If the GrTexture exists take a ref to that (rather than the render |
| 108 | // target) |
| 109 | fSurface = surface->asTexture(); |
robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 110 | #else |
| 111 | fSurface = NULL; |
| 112 | #endif |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 113 | if (NULL == fSurface) { |
| 114 | fSurface = surface; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 115 | } |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 116 | fUnlock = transferCacheLock; |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 117 | GrSafeRef(surface); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 118 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 120 | SkGrPixelRef::~SkGrPixelRef() { |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 121 | if (fUnlock) { |
| 122 | GrContext* context = fSurface->getContext(); |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 123 | GrTexture* texture = fSurface->asTexture(); |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 124 | if (NULL != context && NULL != texture) { |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 125 | context->unlockScratchTexture(texture); |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 128 | GrSafeUnref(fSurface); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 129 | } |
| 130 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 131 | SkGpuTexture* SkGrPixelRef::getTexture() { |
| 132 | if (NULL != fSurface) { |
| 133 | return (SkGpuTexture*) fSurface->asTexture(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 134 | } |
| 135 | return NULL; |
| 136 | } |
| 137 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 138 | SkPixelRef* SkGrPixelRef::deepCopy(SkBitmap::Config dstConfig, const SkIRect* subset) { |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 139 | if (NULL == fSurface) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 142 | |
| 143 | // Note that when copying a render-target-backed pixel ref, we |
| 144 | // return a texture-backed pixel ref instead. This is because |
| 145 | // render-target pixel refs are usually created in conjunction with |
| 146 | // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 147 | // independently of that texture. Texture-backed pixel refs, on the other |
| 148 | // hand, own their GrTextures, and are thus self-contained. |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 149 | return copyToTexturePixelRef(fSurface->asTexture(), dstConfig, subset); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 150 | } |
| 151 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 152 | bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 153 | if (NULL == fSurface || !fSurface->isValid()) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 154 | return false; |
| 155 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 156 | |
| 157 | int left, top, width, height; |
| 158 | if (NULL != subset) { |
| 159 | left = subset->fLeft; |
| 160 | width = subset->width(); |
| 161 | top = subset->fTop; |
| 162 | height = subset->height(); |
| 163 | } else { |
| 164 | left = 0; |
| 165 | width = fSurface->width(); |
| 166 | top = 0; |
| 167 | height = fSurface->height(); |
| 168 | } |
| 169 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
bsalomon@google.com | 009bcca | 2012-12-18 21:40:08 +0000 | [diff] [blame^] | 170 | if (!dst->allocPixels()) { |
| 171 | SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n"); |
| 172 | return false; |
| 173 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 174 | SkAutoLockPixels al(*dst); |
| 175 | void* buffer = dst->getPixels(); |
| 176 | return fSurface->readPixels(left, top, width, height, |
| 177 | kSkia8888_PM_GrPixelConfig, |
| 178 | buffer, dst->rowBytes()); |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 179 | } |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 180 | |