Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkImage_Base.h" |
michael@0 | 9 | #include "SkImagePriv.h" |
michael@0 | 10 | #include "SkBitmap.h" |
michael@0 | 11 | #include "SkCanvas.h" |
michael@0 | 12 | #include "SkData.h" |
michael@0 | 13 | #include "SkMallocPixelRef.h" |
michael@0 | 14 | |
michael@0 | 15 | class SkImage_Raster : public SkImage_Base { |
michael@0 | 16 | public: |
michael@0 | 17 | static bool ValidArgs(const Info& info, size_t rowBytes) { |
michael@0 | 18 | const int maxDimension = SK_MaxS32 >> 2; |
michael@0 | 19 | const size_t kMaxPixelByteSize = SK_MaxS32; |
michael@0 | 20 | |
michael@0 | 21 | if (info.fWidth < 0 || info.fHeight < 0) { |
michael@0 | 22 | return false; |
michael@0 | 23 | } |
michael@0 | 24 | if (info.fWidth > maxDimension || info.fHeight > maxDimension) { |
michael@0 | 25 | return false; |
michael@0 | 26 | } |
michael@0 | 27 | if ((unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType) { |
michael@0 | 28 | return false; |
michael@0 | 29 | } |
michael@0 | 30 | if ((unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType) { |
michael@0 | 31 | return false; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | if (SkImageInfoToBitmapConfig(info) == SkBitmap::kNo_Config) { |
michael@0 | 35 | return false; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | // TODO: check colorspace |
michael@0 | 39 | |
michael@0 | 40 | if (rowBytes < SkImageMinRowBytes(info)) { |
michael@0 | 41 | return false; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | int64_t size = (int64_t)info.fHeight * rowBytes; |
michael@0 | 45 | if (size > (int64_t)kMaxPixelByteSize) { |
michael@0 | 46 | return false; |
michael@0 | 47 | } |
michael@0 | 48 | return true; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | static SkImage* NewEmpty(); |
michael@0 | 52 | |
michael@0 | 53 | SkImage_Raster(const SkImageInfo&, SkData*, size_t rb); |
michael@0 | 54 | virtual ~SkImage_Raster(); |
michael@0 | 55 | |
michael@0 | 56 | virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE; |
michael@0 | 57 | virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE; |
michael@0 | 58 | virtual bool onReadPixels(SkBitmap*, const SkIRect&) const SK_OVERRIDE; |
michael@0 | 59 | virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const SK_OVERRIDE; |
michael@0 | 60 | virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE; |
michael@0 | 61 | |
michael@0 | 62 | // exposed for SkSurface_Raster via SkNewImageFromPixelRef |
michael@0 | 63 | SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes); |
michael@0 | 64 | |
michael@0 | 65 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
michael@0 | 66 | |
michael@0 | 67 | private: |
michael@0 | 68 | SkImage_Raster() : INHERITED(0, 0) {} |
michael@0 | 69 | |
michael@0 | 70 | SkBitmap fBitmap; |
michael@0 | 71 | |
michael@0 | 72 | typedef SkImage_Base INHERITED; |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 76 | |
michael@0 | 77 | SkImage* SkImage_Raster::NewEmpty() { |
michael@0 | 78 | // Returns lazily created singleton |
michael@0 | 79 | static SkImage* gEmpty; |
michael@0 | 80 | if (NULL == gEmpty) { |
michael@0 | 81 | gEmpty = SkNEW(SkImage_Raster); |
michael@0 | 82 | } |
michael@0 | 83 | gEmpty->ref(); |
michael@0 | 84 | return gEmpty; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | static void release_data(void* addr, void* context) { |
michael@0 | 88 | SkData* data = static_cast<SkData*>(context); |
michael@0 | 89 | data->unref(); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes) |
michael@0 | 93 | : INHERITED(info.fWidth, info.fHeight) |
michael@0 | 94 | { |
michael@0 | 95 | data->ref(); |
michael@0 | 96 | void* addr = const_cast<void*>(data->data()); |
michael@0 | 97 | |
michael@0 | 98 | fBitmap.installPixels(info, addr, rowBytes, release_data, data); |
michael@0 | 99 | fBitmap.setImmutable(); |
michael@0 | 100 | fBitmap.lockPixels(); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes) |
michael@0 | 104 | : INHERITED(info.fWidth, info.fHeight) |
michael@0 | 105 | { |
michael@0 | 106 | fBitmap.setConfig(info, rowBytes); |
michael@0 | 107 | fBitmap.setPixelRef(pr); |
michael@0 | 108 | fBitmap.lockPixels(); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | SkImage_Raster::~SkImage_Raster() {} |
michael@0 | 112 | |
michael@0 | 113 | void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) { |
michael@0 | 114 | canvas->drawBitmap(fBitmap, x, y, paint); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, |
michael@0 | 118 | const SkRect& dst, const SkPaint* paint) { |
michael@0 | 119 | canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | bool SkImage_Raster::onReadPixels(SkBitmap* dst, const SkIRect& subset) const { |
michael@0 | 123 | if (dst->pixelRef()) { |
michael@0 | 124 | return this->INHERITED::onReadPixels(dst, subset); |
michael@0 | 125 | } else { |
michael@0 | 126 | SkBitmap src; |
michael@0 | 127 | if (!fBitmap.extractSubset(&src, subset)) { |
michael@0 | 128 | return false; |
michael@0 | 129 | } |
michael@0 | 130 | return src.copyTo(dst, src.colorType()); |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr, |
michael@0 | 135 | size_t* rowBytesPtr) const { |
michael@0 | 136 | SkImageInfo info; |
michael@0 | 137 | if (!fBitmap.asImageInfo(&info) || !fBitmap.getPixels()) { |
michael@0 | 138 | return NULL; |
michael@0 | 139 | } |
michael@0 | 140 | *infoPtr = info; |
michael@0 | 141 | *rowBytesPtr = fBitmap.rowBytes(); |
michael@0 | 142 | return fBitmap.getPixels(); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | bool SkImage_Raster::getROPixels(SkBitmap* dst) const { |
michael@0 | 146 | *dst = fBitmap; |
michael@0 | 147 | return true; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 151 | |
michael@0 | 152 | SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, size_t rowBytes) { |
michael@0 | 153 | if (!SkImage_Raster::ValidArgs(info, rowBytes)) { |
michael@0 | 154 | return NULL; |
michael@0 | 155 | } |
michael@0 | 156 | if (0 == info.fWidth && 0 == info.fHeight) { |
michael@0 | 157 | return SkImage_Raster::NewEmpty(); |
michael@0 | 158 | } |
michael@0 | 159 | // check this after empty-check |
michael@0 | 160 | if (NULL == pixels) { |
michael@0 | 161 | return NULL; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | // Here we actually make a copy of the caller's pixel data |
michael@0 | 165 | SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.fHeight * rowBytes)); |
michael@0 | 166 | return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes)); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | |
michael@0 | 170 | SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t rowBytes) { |
michael@0 | 171 | if (!SkImage_Raster::ValidArgs(info, rowBytes)) { |
michael@0 | 172 | return NULL; |
michael@0 | 173 | } |
michael@0 | 174 | if (0 == info.fWidth && 0 == info.fHeight) { |
michael@0 | 175 | return SkImage_Raster::NewEmpty(); |
michael@0 | 176 | } |
michael@0 | 177 | // check this after empty-check |
michael@0 | 178 | if (NULL == data) { |
michael@0 | 179 | return NULL; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | // did they give us enough data? |
michael@0 | 183 | size_t size = info.fHeight * rowBytes; |
michael@0 | 184 | if (data->size() < size) { |
michael@0 | 185 | return NULL; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes)); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, |
michael@0 | 192 | size_t rowBytes) { |
michael@0 | 193 | return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes)); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) { |
michael@0 | 197 | return ((SkImage_Raster*)image)->getPixelRef(); |
michael@0 | 198 | } |