reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkSurface_Base.h" |
| 9 | #include "SkImagePriv.h" |
| 10 | #include "SkCanvas.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 12 | #include "SkMallocPixelRef.h" |
| 13 | |
| 14 | static const size_t kIgnoreRowBytesValue = (size_t)~0; |
| 15 | |
| 16 | class SkSurface_Raster : public SkSurface_Base { |
| 17 | public: |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 18 | static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 19 | |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 20 | SkSurface_Raster(const SkImageInfo&, void*, size_t rb, |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 21 | void (*releaseProc)(void* pixels, void* context), void* context, |
| 22 | const SkSurfaceProps*); |
| 23 | SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | SkCanvas* onNewCanvas() override; |
| 26 | SkSurface* onNewSurface(const SkImageInfo&) override; |
| 27 | SkImage* onNewImageSnapshot(Budgeted) override; |
tfarina | 7831a4b | 2015-04-27 07:53:07 -0700 | [diff] [blame] | 28 | void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | void onCopyOnWrite(ContentChangeMode) override; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 30 | |
| 31 | private: |
| 32 | SkBitmap fBitmap; |
| 33 | bool fWeOwnThePixels; |
| 34 | |
| 35 | typedef SkSurface_Base INHERITED; |
| 36 | }; |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 40 | bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 41 | if (info.isEmpty()) { |
| 42 | return false; |
| 43 | } |
| 44 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 45 | static const size_t kMaxTotalSize = SK_MaxS32; |
| 46 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 47 | int shift = 0; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 48 | switch (info.colorType()) { |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 49 | case kAlpha_8_SkColorType: |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 50 | shift = 0; |
| 51 | break; |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 52 | case kRGB_565_SkColorType: |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 53 | shift = 1; |
| 54 | break; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 55 | case kN32_SkColorType: |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 56 | shift = 2; |
| 57 | break; |
| 58 | default: |
| 59 | return false; |
| 60 | } |
| 61 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 62 | if (kIgnoreRowBytesValue == rowBytes) { |
| 63 | return true; |
| 64 | } |
| 65 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 66 | uint64_t minRB = (uint64_t)info.width() << shift; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 67 | if (minRB > rowBytes) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | size_t alignedRowBytes = rowBytes >> shift << shift; |
| 72 | if (alignedRowBytes != rowBytes) { |
| 73 | return false; |
| 74 | } |
| 75 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 76 | uint64_t size = sk_64_mul(info.height(), rowBytes); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 77 | if (size > kMaxTotalSize) { |
| 78 | return false; |
| 79 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 80 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 84 | SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb, |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 85 | void (*releaseProc)(void* pixels, void* context), void* context, |
| 86 | const SkSurfaceProps* props) |
| 87 | : INHERITED(info, props) |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 88 | { |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 89 | fBitmap.installPixels(info, pixels, rb, NULL, releaseProc, context); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 90 | fWeOwnThePixels = false; // We are "Direct" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 91 | } |
| 92 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 93 | SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props) |
| 94 | : INHERITED(pr->info().width(), pr->info().height(), props) |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 95 | { |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 96 | const SkImageInfo& info = pr->info(); |
| 97 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 98 | fBitmap.setInfo(info, info.minRowBytes()); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 99 | fBitmap.setPixelRef(pr); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 100 | fWeOwnThePixels = true; |
| 101 | |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 102 | if (!info.isOpaque()) { |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 103 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | SkCanvas* SkSurface_Raster::onNewCanvas() { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 108 | return SkNEW_ARGS(SkCanvas, (fBitmap, this->props())); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 109 | } |
| 110 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 111 | SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 112 | return SkSurface::NewRaster(info); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 113 | } |
| 114 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 115 | void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 116 | const SkPaint* paint) { |
| 117 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 118 | } |
| 119 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 120 | SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 121 | return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels, &this->props()); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 122 | } |
| 123 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 124 | void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 125 | // are we sharing pixelrefs with the image? |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 126 | SkASSERT(this->getCachedImage(kNo_Budgeted)); |
| 127 | if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.pixelRef()) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 128 | SkASSERT(fWeOwnThePixels); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 129 | if (kDiscard_ContentChangeMode == mode) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 130 | fBitmap.setPixelRef(NULL); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 131 | fBitmap.allocPixels(); |
| 132 | } else { |
| 133 | SkBitmap prev(fBitmap); |
commit-bot@chromium.org | 6285f4f | 2014-02-20 19:08:07 +0000 | [diff] [blame] | 134 | prev.deepCopyTo(&fBitmap); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 135 | } |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 136 | // Now fBitmap is a deep copy of itself (and therefore different from |
| 137 | // what is being used by the image. Next we update the canvas to use |
| 138 | // this as its backend, so we can't modify the image's pixels anymore. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 139 | SkASSERT(this->getCachedCanvas()); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 140 | this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 146 | SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, size_t rb, |
| 147 | void (*releaseProc)(void* pixels, void* context), |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 148 | void* context, const SkSurfaceProps* props) { |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 149 | if (NULL == releaseProc) { |
| 150 | context = NULL; |
| 151 | } |
| 152 | if (!SkSurface_Raster::Valid(info, rb)) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | } |
| 155 | if (NULL == pixels) { |
| 156 | return NULL; |
| 157 | } |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 158 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 159 | return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context, props)); |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 160 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 161 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 162 | SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 163 | const SkSurfaceProps* props) { |
| 164 | return NewRasterDirectReleaseProc(info, pixels, rowBytes, NULL, NULL, props); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 165 | } |
| 166 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 167 | SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* props) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 168 | if (!SkSurface_Raster::Valid(info)) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 171 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 172 | SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
| 173 | if (NULL == pr.get()) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 176 | return SkNEW_ARGS(SkSurface_Raster, (pr, props)); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 177 | } |