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" 11#include "SkDevice.h" 12#include "SkMallocPixelRef.h" 13 14static const size_t kIgnoreRowBytesValue = (size_t)~0; 15 16class SkSurface_Raster : public SkSurface_Base { 17public: 18 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); 19 20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb, 21 void (*releaseProc)(void* pixels, void* context), void* context, 22 const SkSurfaceProps*); 23 SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*); 24 25 SkCanvas* onNewCanvas() override; 26 SkSurface* onNewSurface(const SkImageInfo&) override; 27 SkImage* onNewImageSnapshot(Budgeted) override; 28 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; 29 void onCopyOnWrite(ContentChangeMode) override; 30 31private: 32 SkBitmap fBitmap; 33 bool fWeOwnThePixels; 34 35 typedef SkSurface_Base INHERITED; 36}; 37 38/////////////////////////////////////////////////////////////////////////////// 39 40bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { 41 if (info.isEmpty()) { 42 return false; 43 } 44 45 static const size_t kMaxTotalSize = SK_MaxS32; 46 47 int shift = 0; 48 switch (info.colorType()) { 49 case kAlpha_8_SkColorType: 50 shift = 0; 51 break; 52 case kRGB_565_SkColorType: 53 shift = 1; 54 break; 55 case kN32_SkColorType: 56 shift = 2; 57 break; 58 default: 59 return false; 60 } 61 62 if (kIgnoreRowBytesValue == rowBytes) { 63 return true; 64 } 65 66 uint64_t minRB = (uint64_t)info.width() << shift; 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 76 uint64_t size = sk_64_mul(info.height(), rowBytes); 77 if (size > kMaxTotalSize) { 78 return false; 79 } 80 81 return true; 82} 83 84SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb, 85 void (*releaseProc)(void* pixels, void* context), void* context, 86 const SkSurfaceProps* props) 87 : INHERITED(info, props) 88{ 89 fBitmap.installPixels(info, pixels, rb, NULL, releaseProc, context); 90 fWeOwnThePixels = false; // We are "Direct" 91} 92 93SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props) 94 : INHERITED(pr->info().width(), pr->info().height(), props) 95{ 96 const SkImageInfo& info = pr->info(); 97 98 fBitmap.setInfo(info, info.minRowBytes()); 99 fBitmap.setPixelRef(pr); 100 fWeOwnThePixels = true; 101 102 if (!info.isOpaque()) { 103 fBitmap.eraseColor(SK_ColorTRANSPARENT); 104 } 105} 106 107SkCanvas* SkSurface_Raster::onNewCanvas() { 108 return SkNEW_ARGS(SkCanvas, (fBitmap, this->props())); 109} 110 111SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { 112 return SkSurface::NewRaster(info); 113} 114 115void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, 116 const SkPaint* paint) { 117 canvas->drawBitmap(fBitmap, x, y, paint); 118} 119 120SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) { 121 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels, &this->props()); 122} 123 124void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { 125 // are we sharing pixelrefs with the image? 126 SkASSERT(this->getCachedImage(kNo_Budgeted)); 127 if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.pixelRef()) { 128 SkASSERT(fWeOwnThePixels); 129 if (kDiscard_ContentChangeMode == mode) { 130 fBitmap.setPixelRef(NULL); 131 fBitmap.allocPixels(); 132 } else { 133 SkBitmap prev(fBitmap); 134 prev.deepCopyTo(&fBitmap); 135 } 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. 139 SkASSERT(this->getCachedCanvas()); 140 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); 141 } 142} 143 144/////////////////////////////////////////////////////////////////////////////// 145 146SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, size_t rb, 147 void (*releaseProc)(void* pixels, void* context), 148 void* context, const SkSurfaceProps* props) { 149 if (NULL == releaseProc) { 150 context = NULL; 151 } 152 if (!SkSurface_Raster::Valid(info, rb)) { 153 return NULL; 154 } 155 if (NULL == pixels) { 156 return NULL; 157 } 158 159 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context, props)); 160} 161 162SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes, 163 const SkSurfaceProps* props) { 164 return NewRasterDirectReleaseProc(info, pixels, rowBytes, NULL, NULL, props); 165} 166 167SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* props) { 168 if (!SkSurface_Raster::Valid(info)) { 169 return NULL; 170 } 171 172 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); 173 if (NULL == pr.get()) { 174 return NULL; 175 } 176 return SkNEW_ARGS(SkSurface_Raster, (pr, props)); 177} 178