| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSurface_Base.h" | 8 #include "SkSurface_Base.h" |
| 9 #include "SkImagePriv.h" | 9 #include "SkImagePriv.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
| 12 #include "SkMallocPixelRef.h" | 12 #include "SkMallocPixelRef.h" |
| 13 | 13 |
| 14 static const size_t kIgnoreRowBytesValue = (size_t)~0; | 14 static const size_t kIgnoreRowBytesValue = (size_t)~0; |
| 15 | 15 |
| 16 class SkSurface_Raster : public SkSurface_Base { | 16 class SkSurface_Raster : public SkSurface_Base { |
| 17 public: | 17 public: |
| 18 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); | 18 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); |
| 19 | 19 |
| 20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb); | 20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb); |
| 21 SkSurface_Raster(const SkImageInfo&, SkPixelRef*, size_t rb); | 21 SkSurface_Raster(SkPixelRef*); |
| 22 | 22 |
| 23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; | 23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; |
| 24 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; | 24 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; |
| 25 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; | 25 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; |
| 26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, | 26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
| 27 const SkPaint*) SK_OVERRIDE; | 27 const SkPaint*) SK_OVERRIDE; |
| 28 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; | 28 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 SkBitmap fBitmap; | 31 SkBitmap fBitmap; |
| 32 bool fWeOwnThePixels; | 32 bool fWeOwnThePixels; |
| 33 | 33 |
| 34 typedef SkSurface_Base INHERITED; | 34 typedef SkSurface_Base INHERITED; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
| 38 | 38 |
| 39 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { | 39 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |
| 40 static const size_t kMaxTotalSize = SK_MaxS32; | 40 static const size_t kMaxTotalSize = SK_MaxS32; |
| 41 | 41 |
| 42 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | |
| 43 | |
| 44 int shift = 0; | 42 int shift = 0; |
| 45 switch (config) { | 43 switch (info.fColorType) { |
| 46 case SkBitmap::kA8_Config: | 44 case kAlpha_8_SkColorType: |
| 47 shift = 0; | 45 shift = 0; |
| 48 break; | 46 break; |
| 49 case SkBitmap::kRGB_565_Config: | 47 case kRGB_565_SkColorType: |
| 50 shift = 1; | 48 shift = 1; |
| 51 break; | 49 break; |
| 52 case SkBitmap::kARGB_8888_Config: | 50 case kPMColor_SkColorType: |
| 53 shift = 2; | 51 shift = 2; |
| 54 break; | 52 break; |
| 55 default: | 53 default: |
| 56 return false; | 54 return false; |
| 57 } | 55 } |
| 58 | 56 |
| 59 // TODO: examine colorspace | |
| 60 | |
| 61 if (kIgnoreRowBytesValue == rowBytes) { | 57 if (kIgnoreRowBytesValue == rowBytes) { |
| 62 return true; | 58 return true; |
| 63 } | 59 } |
| 64 | 60 |
| 65 uint64_t minRB = (uint64_t)info.fWidth << shift; | 61 uint64_t minRB = (uint64_t)info.fWidth << shift; |
| 66 if (minRB > rowBytes) { | 62 if (minRB > rowBytes) { |
| 67 return false; | 63 return false; |
| 68 } | 64 } |
| 69 | 65 |
| 70 size_t alignedRowBytes = rowBytes >> shift << shift; | 66 size_t alignedRowBytes = rowBytes >> shift << shift; |
| 71 if (alignedRowBytes != rowBytes) { | 67 if (alignedRowBytes != rowBytes) { |
| 72 return false; | 68 return false; |
| 73 } | 69 } |
| 74 | 70 |
| 75 uint64_t size = (uint64_t)info.fHeight * rowBytes; | 71 uint64_t size = sk_64_mul(info.fHeight, rowBytes); |
| 76 if (size > kMaxTotalSize) { | 72 if (size > kMaxTotalSize) { |
| 77 return false; | 73 return false; |
| 78 } | 74 } |
| 79 | 75 |
| 80 return true; | 76 return true; |
| 81 } | 77 } |
| 82 | 78 |
| 83 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
rb) | 79 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
rb) |
| 84 : INHERITED(info.fWidth, info.fHeight) { | 80 : INHERITED(info) |
| 85 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 81 { |
| 86 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb, info.fAlphaType); | 82 fBitmap.setConfig(info, rb); |
| 87 fBitmap.setPixels(pixels); | 83 fBitmap.setPixels(pixels); |
| 88 fWeOwnThePixels = false; // We are "Direct" | 84 fWeOwnThePixels = false; // We are "Direct" |
| 89 } | 85 } |
| 90 | 86 |
| 91 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, SkPixelRef* pr, size
_t rb) | 87 SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr) |
| 92 : INHERITED(info.fWidth, info.fHeight) { | 88 : INHERITED(pr->info().fWidth, pr->info().fHeight) |
| 93 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 89 { |
| 94 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb, info.fAlphaType); | 90 const SkImageInfo& info = pr->info(); |
| 91 |
| 92 fBitmap.setConfig(info, info.minRowBytes()); |
| 95 fBitmap.setPixelRef(pr); | 93 fBitmap.setPixelRef(pr); |
| 96 fWeOwnThePixels = true; | 94 fWeOwnThePixels = true; |
| 97 | 95 |
| 98 if (!SkAlphaTypeIsOpaque(info.fAlphaType)) { | 96 if (!info.isOpaque()) { |
| 99 fBitmap.eraseColor(SK_ColorTRANSPARENT); | 97 fBitmap.eraseColor(SK_ColorTRANSPARENT); |
| 100 } | 98 } |
| 101 } | 99 } |
| 102 | 100 |
| 103 SkCanvas* SkSurface_Raster::onNewCanvas() { | 101 SkCanvas* SkSurface_Raster::onNewCanvas() { |
| 104 return SkNEW_ARGS(SkCanvas, (fBitmap)); | 102 return SkNEW_ARGS(SkCanvas, (fBitmap)); |
| 105 } | 103 } |
| 106 | 104 |
| 107 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { | 105 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
| 108 return SkSurface::NewRaster(info); | 106 return SkSurface::NewRaster(info); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 150 |
| 153 SkSurface* SkSurface::NewRaster(const SkImageInfo& info) { | 151 SkSurface* SkSurface::NewRaster(const SkImageInfo& info) { |
| 154 if (!SkSurface_Raster::Valid(info)) { | 152 if (!SkSurface_Raster::Valid(info)) { |
| 155 return NULL; | 153 return NULL; |
| 156 } | 154 } |
| 157 | 155 |
| 158 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); | 156 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
| 159 if (NULL == pr.get()) { | 157 if (NULL == pr.get()) { |
| 160 return NULL; | 158 return NULL; |
| 161 } | 159 } |
| 162 return SkNEW_ARGS(SkSurface_Raster, (info, pr, info.minRowBytes())); | 160 return SkNEW_ARGS(SkSurface_Raster, (pr)); |
| 163 } | 161 } |
| OLD | NEW |