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 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 21 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
| 22 | : INHERITED(info, &gROLockPixelsPixelRefMutex) {} |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 23 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 24 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {} |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 25 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 26 | bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 27 | fBitmap.reset(); |
| 28 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
| 29 | if (!this->onReadPixels(&fBitmap, NULL)) { |
| 30 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 31 | return false; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 32 | } |
| 33 | fBitmap.lockPixels(); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 34 | if (NULL == fBitmap.getPixels()) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | rec->fPixels = fBitmap.getPixels(); |
| 39 | rec->fColorTable = NULL; |
| 40 | rec->fRowBytes = fBitmap.rowBytes(); |
| 41 | return true; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void SkROLockPixelsPixelRef::onUnlockPixels() { |
| 45 | fBitmap.unlockPixels(); |
| 46 | } |
| 47 | |
| 48 | bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 53 | |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 54 | static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkColorType dstCT, |
commit-bot@chromium.org | eeef0cc | 2014-05-21 15:58:00 +0000 | [diff] [blame] | 55 | const SkIRect* subset) { |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 56 | if (NULL == texture || kUnknown_SkColorType == dstCT) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
| 59 | GrContext* context = texture->getContext(); |
| 60 | if (NULL == context) { |
| 61 | return NULL; |
| 62 | } |
| 63 | GrTextureDesc desc; |
| 64 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 65 | SkIPoint pointStorage; |
| 66 | SkIPoint* topLeft; |
| 67 | if (subset != NULL) { |
| 68 | SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset)); |
| 69 | // Create a new texture that is the size of subset. |
| 70 | desc.fWidth = subset->width(); |
| 71 | desc.fHeight = subset->height(); |
| 72 | pointStorage.set(subset->x(), subset->y()); |
| 73 | topLeft = &pointStorage; |
| 74 | } else { |
| 75 | desc.fWidth = texture->width(); |
| 76 | desc.fHeight = texture->height(); |
| 77 | topLeft = NULL; |
| 78 | } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 79 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 80 | desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType); |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 81 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 82 | GrTexture* dst = context->createUncachedTexture(desc, NULL, 0); |
| 83 | if (NULL == dst) { |
| 84 | return NULL; |
| 85 | } |
| 86 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 87 | context->copyTexture(texture, dst->asRenderTarget(), topLeft); |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 88 | |
robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 89 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 90 | #if 0 |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 91 | // The render texture we have created (to perform the copy) isn't fully |
| 92 | // functional (since it doesn't have a stencil buffer). Release it here |
| 93 | // so the caller doesn't try to render to it. |
| 94 | // TODO: we can undo this release when dynamic stencil buffer attach/ |
| 95 | // detach has been implemented |
| 96 | dst->releaseRenderTarget(); |
robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 97 | #endif |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 98 | |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 99 | SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPremul_SkAlphaType); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 100 | SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst)); |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 101 | SkSafeUnref(dst); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 102 | return pixelRef; |
| 103 | } |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 107 | SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface, |
| 108 | bool transferCacheLock) : INHERITED(info) { |
bsalomon | 7187593 | 2014-08-29 08:08:35 -0700 | [diff] [blame^] | 109 | // For surfaces that are both textures and render targets, the texture owns the |
| 110 | // render target but not vice versa. So we ref the texture to keep both alive for |
| 111 | // the lifetime of this pixel ref. |
| 112 | fSurface = SkSafeRef(surface->asTexture()); |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 113 | if (NULL == fSurface) { |
bsalomon | 7187593 | 2014-08-29 08:08:35 -0700 | [diff] [blame^] | 114 | fSurface = SkSafeRef(surface); |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 115 | } |
| 116 | fUnlock = transferCacheLock; |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 117 | |
| 118 | if (fSurface) { |
| 119 | SkASSERT(info.fWidth <= fSurface->width()); |
| 120 | SkASSERT(info.fHeight <= fSurface->height()); |
| 121 | } |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 124 | SkGrPixelRef::~SkGrPixelRef() { |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 125 | if (fUnlock) { |
| 126 | GrContext* context = fSurface->getContext(); |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 127 | GrTexture* texture = fSurface->asTexture(); |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 128 | if (NULL != context && NULL != texture) { |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 129 | context->unlockScratchTexture(texture); |
bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 132 | SkSafeUnref(fSurface); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 133 | } |
| 134 | |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 135 | GrTexture* SkGrPixelRef::getTexture() { |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 136 | if (NULL != fSurface) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 137 | return fSurface->asTexture(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 142 | SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) { |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 143 | if (NULL == fSurface) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 146 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 147 | // Note that when copying a render-target-backed pixel ref, we |
| 148 | // return a texture-backed pixel ref instead. This is because |
| 149 | // render-target pixel refs are usually created in conjunction with |
| 150 | // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 151 | // independently of that texture. Texture-backed pixel refs, on the other |
| 152 | // hand, own their GrTextures, and are thus self-contained. |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 153 | return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 156 | bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 157 | if (NULL == fSurface || fSurface->wasDestroyed()) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 160 | |
| 161 | int left, top, width, height; |
| 162 | if (NULL != subset) { |
| 163 | left = subset->fLeft; |
| 164 | width = subset->width(); |
| 165 | top = subset->fTop; |
| 166 | height = subset->height(); |
| 167 | } else { |
| 168 | left = 0; |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 169 | width = this->info().fWidth; |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 170 | top = 0; |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 171 | height = this->info().fHeight; |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 172 | } |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 173 | if (!dst->allocPixels(SkImageInfo::MakeN32Premul(width, height))) { |
bsalomon@google.com | 009bcca | 2012-12-18 21:40:08 +0000 | [diff] [blame] | 174 | SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n"); |
| 175 | return false; |
| 176 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 177 | SkAutoLockPixels al(*dst); |
| 178 | void* buffer = dst->getPixels(); |
| 179 | return fSurface->readPixels(left, top, width, height, |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 180 | kSkia8888_GrPixelConfig, |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 181 | buffer, dst->rowBytes()); |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 182 | } |