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@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 20 | SkSurface_Raster(const SkImageInfo&, void*, size_t rb); |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 21 | SkSurface_Raster(SkPixelRef*); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 22 | |
| 23 | virtual SkCanvas* onNewCanvas() SK_OVERRIDE; |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 24 | virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 25 | virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 26 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
| 27 | const SkPaint*) SK_OVERRIDE; |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 28 | virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | SkBitmap fBitmap; |
| 32 | bool fWeOwnThePixels; |
| 33 | |
| 34 | typedef SkSurface_Base INHERITED; |
| 35 | }; |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 39 | bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 40 | static const size_t kMaxTotalSize = SK_MaxS32; |
| 41 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 42 | int shift = 0; |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 43 | switch (info.fColorType) { |
| 44 | case kAlpha_8_SkColorType: |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 45 | shift = 0; |
| 46 | break; |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 47 | case kRGB_565_SkColorType: |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 48 | shift = 1; |
| 49 | break; |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame^] | 50 | case kN32_SkColorType: |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 51 | shift = 2; |
| 52 | break; |
| 53 | default: |
| 54 | return false; |
| 55 | } |
| 56 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 57 | if (kIgnoreRowBytesValue == rowBytes) { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | uint64_t minRB = (uint64_t)info.fWidth << shift; |
| 62 | if (minRB > rowBytes) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | size_t alignedRowBytes = rowBytes >> shift << shift; |
| 67 | if (alignedRowBytes != rowBytes) { |
| 68 | return false; |
| 69 | } |
| 70 | |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 71 | uint64_t size = sk_64_mul(info.fHeight, rowBytes); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 72 | if (size > kMaxTotalSize) { |
| 73 | return false; |
| 74 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 75 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 79 | SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb) |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 80 | : INHERITED(info) |
| 81 | { |
| 82 | fBitmap.setConfig(info, rb); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 83 | fBitmap.setPixels(pixels); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 84 | fWeOwnThePixels = false; // We are "Direct" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 85 | } |
| 86 | |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 87 | SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr) |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 88 | : INHERITED(pr->info().fWidth, pr->info().fHeight) |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 89 | { |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 90 | const SkImageInfo& info = pr->info(); |
| 91 | |
| 92 | fBitmap.setConfig(info, info.minRowBytes()); |
| 93 | fBitmap.setPixelRef(pr); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 94 | fWeOwnThePixels = true; |
| 95 | |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 96 | if (!info.isOpaque()) { |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 97 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | SkCanvas* SkSurface_Raster::onNewCanvas() { |
| 102 | return SkNEW_ARGS(SkCanvas, (fBitmap)); |
| 103 | } |
| 104 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 105 | SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 106 | return SkSurface::NewRaster(info); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 107 | } |
| 108 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 109 | void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 110 | const SkPaint* paint) { |
| 111 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 112 | } |
| 113 | |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 114 | SkImage* SkSurface_Raster::onNewImageSnapshot() { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 115 | return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); |
| 116 | } |
| 117 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 118 | void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 119 | // are we sharing pixelrefs with the image? |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 120 | SkASSERT(NULL != this->getCachedImage()); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 121 | if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 122 | SkASSERT(fWeOwnThePixels); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 123 | if (kDiscard_ContentChangeMode == mode) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 124 | fBitmap.setPixelRef(NULL); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 125 | fBitmap.allocPixels(); |
| 126 | } else { |
| 127 | SkBitmap prev(fBitmap); |
commit-bot@chromium.org | 6285f4f | 2014-02-20 19:08:07 +0000 | [diff] [blame] | 128 | prev.deepCopyTo(&fBitmap); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 129 | } |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 130 | // Now fBitmap is a deep copy of itself (and therefore different from |
| 131 | // what is being used by the image. Next we update the canvas to use |
| 132 | // this as its backend, so we can't modify the image's pixels anymore. |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 133 | SkASSERT(NULL != this->getCachedCanvas()); |
| 134 | this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 138 | /////////////////////////////////////////////////////////////////////////////// |
| 139 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 140 | SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 141 | if (!SkSurface_Raster::Valid(info, rowBytes)) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
| 144 | if (NULL == pixels) { |
| 145 | return NULL; |
| 146 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 147 | |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 148 | return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rowBytes)); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 149 | } |
| 150 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 151 | SkSurface* SkSurface::NewRaster(const SkImageInfo& info) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 152 | if (!SkSurface_Raster::Valid(info)) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 155 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 156 | SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
| 157 | if (NULL == pr.get()) { |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 160 | return SkNEW_ARGS(SkSurface_Raster, (pr)); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 161 | } |