blob: 8b68448e41bf0216195d2497cfe5042ec659cccf [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
robertphillips@google.com5088eb42012-06-28 20:59:13 +000011#include "SkGrPixelRef.h"
piotaixr0e977052014-09-17 16:24:04 -070012
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000013#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrTexture.h"
piotaixr0e977052014-09-17 16:24:04 -070015#include "SkBitmapCache.h"
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000016#include "SkGr.h"
bsalomon@google.com669fdc42011-04-05 17:08:27 +000017#include "SkRect.h"
reed@google.com9c49bc32011-07-07 13:42:37 +000018
19// since we call lockPixels recursively on fBitmap, we need a distinct mutex,
20// to avoid deadlock with the default one provided by SkPixelRef.
digit@google.com1771cbf2012-01-26 21:26:40 +000021SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex);
reed@google.com9c49bc32011-07-07 13:42:37 +000022
reed@google.combf790232013-12-13 19:45:58 +000023SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info)
24 : INHERITED(info, &gROLockPixelsPixelRefMutex) {}
reed@google.com9c49bc32011-07-07 13:42:37 +000025
reed@google.combf790232013-12-13 19:45:58 +000026SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {}
reed@google.com9c49bc32011-07-07 13:42:37 +000027
reed@google.comd0419b12014-01-06 17:08:27 +000028bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) {
reed@google.com9c49bc32011-07-07 13:42:37 +000029 fBitmap.reset();
30// SkDebugf("---------- calling readpixels in support of lockpixels\n");
31 if (!this->onReadPixels(&fBitmap, NULL)) {
32 SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n");
reed@google.comd0419b12014-01-06 17:08:27 +000033 return false;
reed@google.com9c49bc32011-07-07 13:42:37 +000034 }
35 fBitmap.lockPixels();
reed@google.comd0419b12014-01-06 17:08:27 +000036 if (NULL == fBitmap.getPixels()) {
37 return false;
38 }
39
40 rec->fPixels = fBitmap.getPixels();
41 rec->fColorTable = NULL;
42 rec->fRowBytes = fBitmap.rowBytes();
43 return true;
reed@google.com9c49bc32011-07-07 13:42:37 +000044}
45
46void SkROLockPixelsPixelRef::onUnlockPixels() {
47 fBitmap.unlockPixels();
48}
49
50bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const {
51 return false;
52}
53
54///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com669fdc42011-04-05 17:08:27 +000055
reede4538f52014-06-11 06:09:50 -070056static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkColorType dstCT,
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +000057 const SkIRect* subset) {
reede4538f52014-06-11 06:09:50 -070058 if (NULL == texture || kUnknown_SkColorType == dstCT) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000059 return NULL;
60 }
61 GrContext* context = texture->getContext();
62 if (NULL == context) {
63 return NULL;
64 }
65 GrTextureDesc desc;
66
scroggo@google.coma2a31922012-12-07 19:14:45 +000067 SkIPoint pointStorage;
68 SkIPoint* topLeft;
69 if (subset != NULL) {
70 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset));
71 // Create a new texture that is the size of subset.
72 desc.fWidth = subset->width();
73 desc.fHeight = subset->height();
74 pointStorage.set(subset->x(), subset->y());
75 topLeft = &pointStorage;
76 } else {
77 desc.fWidth = texture->width();
78 desc.fHeight = texture->height();
79 topLeft = NULL;
80 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000081 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
reede4538f52014-06-11 06:09:50 -070082 desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType);
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000083
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000084 GrTexture* dst = context->createUncachedTexture(desc, NULL, 0);
85 if (NULL == dst) {
86 return NULL;
87 }
88
scroggo@google.coma2a31922012-12-07 19:14:45 +000089 context->copyTexture(texture, dst->asRenderTarget(), topLeft);
robertphillips@google.comd881bc12012-06-28 20:02:39 +000090
robertphillips@google.com41efe042012-06-29 20:55:14 +000091 // TODO: figure out if this is responsible for Chrome canvas errors
92#if 0
robertphillips@google.comd881bc12012-06-28 20:02:39 +000093 // The render texture we have created (to perform the copy) isn't fully
94 // functional (since it doesn't have a stencil buffer). Release it here
95 // so the caller doesn't try to render to it.
96 // TODO: we can undo this release when dynamic stencil buffer attach/
97 // detach has been implemented
98 dst->releaseRenderTarget();
robertphillips@google.com41efe042012-06-29 20:55:14 +000099#endif
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000100
reede4538f52014-06-11 06:09:50 -0700101 SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPremul_SkAlphaType);
reed@google.combf790232013-12-13 19:45:58 +0000102 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst));
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000103 SkSafeUnref(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000104 return pixelRef;
105}
106
107///////////////////////////////////////////////////////////////////////////////
108
reed@google.combf790232013-12-13 19:45:58 +0000109SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface,
110 bool transferCacheLock) : INHERITED(info) {
bsalomon71875932014-08-29 08:08:35 -0700111 // For surfaces that are both textures and render targets, the texture owns the
112 // render target but not vice versa. So we ref the texture to keep both alive for
113 // the lifetime of this pixel ref.
114 fSurface = SkSafeRef(surface->asTexture());
reed@google.com61867872013-12-09 13:41:06 +0000115 if (NULL == fSurface) {
bsalomon71875932014-08-29 08:08:35 -0700116 fSurface = SkSafeRef(surface);
reed@google.com61867872013-12-09 13:41:06 +0000117 }
118 fUnlock = transferCacheLock;
commit-bot@chromium.org8e0993d2014-01-28 15:16:45 +0000119
120 if (fSurface) {
reede5ea5002014-09-03 11:54:58 -0700121 SkASSERT(info.width() <= fSurface->width());
122 SkASSERT(info.height() <= fSurface->height());
commit-bot@chromium.org8e0993d2014-01-28 15:16:45 +0000123 }
reed@google.com61867872013-12-09 13:41:06 +0000124}
125
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000126SkGrPixelRef::~SkGrPixelRef() {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000127 if (fUnlock) {
128 GrContext* context = fSurface->getContext();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000129 GrTexture* texture = fSurface->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700130 if (context && texture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000131 context->unlockScratchTexture(texture);
bsalomon@google.com8090e652012-08-28 15:07:11 +0000132 }
133 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000134 SkSafeUnref(fSurface);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000135}
136
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000137GrTexture* SkGrPixelRef::getTexture() {
bsalomon49f085d2014-09-05 13:34:00 -0700138 if (fSurface) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000139 return fSurface->asTexture();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000140 }
141 return NULL;
142}
143
reede4538f52014-06-11 06:09:50 -0700144SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) {
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000145 if (NULL == fSurface) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000146 return NULL;
147 }
piotaixr0e977052014-09-17 16:24:04 -0700148
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000149 // Note that when copying a render-target-backed pixel ref, we
150 // return a texture-backed pixel ref instead. This is because
151 // render-target pixel refs are usually created in conjunction with
152 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live
153 // independently of that texture. Texture-backed pixel refs, on the other
154 // hand, own their GrTextures, and are thus self-contained.
reede4538f52014-06-11 06:09:50 -0700155 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000156}
157
piotaixr0e977052014-09-17 16:24:04 -0700158static bool tryAllocBitmapPixels(SkBitmap* bitmap) {
159 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
160 if (NULL != allocator) {
161 return allocator->allocPixelRef(bitmap, 0);
162 } else {
163 // DiscardableMemory is not available, fallback to default allocator
164 return bitmap->tryAllocPixels();
165 }
166}
167
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000168bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000169 if (NULL == fSurface || fSurface->wasDestroyed()) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000170 return false;
171 }
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000172
piotaixr0e977052014-09-17 16:24:04 -0700173 SkIRect bounds;
bsalomon49f085d2014-09-05 13:34:00 -0700174 if (subset) {
piotaixr0e977052014-09-17 16:24:04 -0700175 bounds = *subset;
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000176 } else {
piotaixr0e977052014-09-17 16:24:04 -0700177 bounds = SkIRect::MakeWH(this->info().width(), this->info().height());
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000178 }
piotaixr0e977052014-09-17 16:24:04 -0700179
180 //Check the cache
181 if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) {
182 //Cache miss
183
184 SkBitmap cachedBitmap;
185 cachedBitmap.setInfo(this->info().makeWH(bounds.width(), bounds.height()));
186
187 // If we can't alloc the pixels, then fail
188 if (!tryAllocBitmapPixels(&cachedBitmap)) {
189 return false;
190 }
191
192 // Try to read the pixels from the surface
193 void* buffer = cachedBitmap.getPixels();
194 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop,
195 bounds.width(), bounds.height(),
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000196 kSkia8888_GrPixelConfig,
piotaixr0e977052014-09-17 16:24:04 -0700197 buffer, cachedBitmap.rowBytes());
198
199 if (!readPixelsOk) {
200 return false;
201 }
202
203 // If we are here, pixels were read correctly from the surface.
204 cachedBitmap.setImmutable();
205 //Add to the cache
206 SkBitmapCache::Add(this->getGenerationID(), bounds, cachedBitmap);
207
208 dst->swap(cachedBitmap);
209 }
210
211 return true;
212
reed@google.com4281d652011-04-08 18:54:20 +0000213}