| 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 |
#include "src/image/SkSurface_Raster.h" |
| 8 |
|
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCPURecorder.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkCapabilities.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkMallocPixelRef.h" |
| 15 |
#include "include/core/SkPixelRef.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkSurface.h" |
| 20 |
#include "include/private/base/SkAssert.h" |
| 21 |
#include "include/private/base/SkMath.h" |
| 22 |
#include "src/core/SkBitmapDevice.h" |
| 23 |
#include "src/core/SkCPURecorderImpl.h" |
| 24 |
#include "src/core/SkDevice.h" |
| 25 |
#include "src/core/SkImageInfoPriv.h" |
| 26 |
#include "src/core/SkSurfacePriv.h" |
| 27 |
#include "src/image/SkImage_Raster.h" |
| 28 |
|
| 29 |
#include <cstdint> |
| 30 |
#include <cstring> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
class SkImage; |
| 34 |
class SkPaint; |
| 35 |
class SkPixmap; |
| 36 |
class SkSurfaceProps; |
| 37 |
|
| 38 |
bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) { |
| 39 |
if (!SkImageInfoIsValid(info)) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if (kIgnoreRowBytesValue == rowBytes) { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
if (!info.validRowBytes(rowBytes)) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
uint64_t size = sk_64_mul(info.height(), rowBytes); |
| 52 |
static const size_t kMaxTotalSize = SK_MaxS32; |
| 53 |
if (size > kMaxTotalSize) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, |
| 61 |
void* pixels, |
| 62 |
size_t rb, |
| 63 |
void (*releaseProc)(void* pixels, void* context), |
| 64 |
void* context, |
| 65 |
const SkSurfaceProps* props) |
| 66 |
: SkSurface_Raster( |
| 67 |
asRRI(skcpu::Recorder::TODO()), info, pixels, rb, releaseProc, context, props) {} |
| 68 |
|
| 69 |
SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, |
| 70 |
sk_sp<SkPixelRef> pr, |
| 71 |
const SkSurfaceProps* props) |
| 72 |
: SkSurface_Raster(asRRI(skcpu::Recorder::TODO()), info, pr, props) {} |
| 73 |
|
| 74 |
SkSurface_Raster::SkSurface_Raster(skcpu::RecorderImpl* recorder, |
| 75 |
const SkImageInfo& info, |
| 76 |
void* pixels, |
| 77 |
size_t rowBytes, |
| 78 |
void (*releaseProc)(void* pixels, void* context), |
| 79 |
void* context, |
| 80 |
const SkSurfaceProps* props) |
| 81 |
: SkSurface_Base(info, props), fRecorder(recorder) { |
| 82 |
fBitmap.installPixels(info, pixels, rowBytes, releaseProc, context); |
| 83 |
fWeOwnThePixels = false; // We are "Direct" |
| 84 |
} |
| 85 |
|
| 86 |
SkSurface_Raster::SkSurface_Raster(skcpu::RecorderImpl* recorder, |
| 87 |
const SkImageInfo& info, |
| 88 |
sk_sp<SkPixelRef> pr, |
| 89 |
const SkSurfaceProps* props) |
| 90 |
: SkSurface_Base(pr->width(), pr->height(), props), fRecorder(recorder) { |
| 91 |
fBitmap.setInfo(info, pr->rowBytes()); |
| 92 |
fBitmap.setPixelRef(std::move(pr), 0, 0); |
| 93 |
fWeOwnThePixels = true; |
| 94 |
} |
| 95 |
|
| 96 |
SkCanvas* SkSurface_Raster::onNewCanvas() { |
| 97 |
SkASSERT(fRecorder); |
| 98 |
return new SkCanvas(sk_make_sp<SkBitmapDevice>(fRecorder, fBitmap, this->props())); |
| 99 |
} |
| 100 |
|
| 101 |
sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
| 102 |
SkASSERT(fRecorder); |
| 103 |
return fRecorder->makeBitmapSurface(info, 0, &this->props()); |
| 104 |
} |
| 105 |
|
| 106 |
void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 107 |
const SkSamplingOptions& sampling, const SkPaint* paint) { |
| 108 |
canvas->drawImage(fBitmap.asImage().get(), x, y, sampling, paint); |
| 109 |
} |
| 110 |
|
| 111 |
sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot(const SkIRect* subset) { |
| 112 |
if (subset) { |
| 113 |
SkASSERT(SkIRect::MakeWH(fBitmap.width(), fBitmap.height()).contains(*subset)); |
| 114 |
SkBitmap dst; |
| 115 |
dst.allocPixels(fBitmap.info().makeDimensions(subset->size())); |
| 116 |
SkAssertResult(fBitmap.readPixels(dst.pixmap(), subset->left(), subset->top())); |
| 117 |
dst.setImmutable(); // key, so MakeFromBitmap doesn't make a copy of the buffer |
| 118 |
return dst.asImage(); |
| 119 |
} |
| 120 |
|
| 121 |
SkCopyPixelsMode cpm = SkCopyPixelsMode::kIfMutable; |
| 122 |
if (fWeOwnThePixels) { |
| 123 |
// SkImage_raster requires these pixels are immutable for its full lifetime. |
| 124 |
// We'll undo this via onRestoreBackingMutability() if we can avoid the COW. |
| 125 |
if (SkPixelRef* pr = fBitmap.pixelRef()) { |
| 126 |
pr->setTemporarilyImmutable(); |
| 127 |
} |
| 128 |
} else { |
| 129 |
cpm = SkCopyPixelsMode::kAlways; |
| 130 |
} |
| 131 |
|
| 132 |
// Our pixels are in memory, so read access on the snapshot SkImage could be cheap. |
| 133 |
// Lock the shared pixel ref to ensure peekPixels() is usable. |
| 134 |
return SkImage_Raster::MakeFromBitmap(fBitmap, cpm); |
| 135 |
} |
| 136 |
|
| 137 |
void SkSurface_Raster::onWritePixels(const SkPixmap& src, int x, int y) { |
| 138 |
fBitmap.writePixels(src, x, y); |
| 139 |
} |
| 140 |
|
| 141 |
void SkSurface_Raster::onRestoreBackingMutability() { |
| 142 |
SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. |
| 143 |
if (SkPixelRef* pr = fBitmap.pixelRef()) { |
| 144 |
pr->restoreMutability(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
bool SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
| 149 |
// are we sharing pixelrefs with the image? |
| 150 |
sk_sp<SkImage> cached(this->refCachedImage()); |
| 151 |
SkASSERT(cached); |
| 152 |
SkASSERT(as_IB(cached)->isRasterBacked()); |
| 153 |
if (static_cast<SkImage_Raster*>(cached.get())->getPixelRef() == fBitmap.pixelRef()) { |
| 154 |
SkASSERT(fWeOwnThePixels); |
| 155 |
if (kDiscard_ContentChangeMode == mode) { |
| 156 |
if (!fBitmap.tryAllocPixels()) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
} else { |
| 160 |
SkBitmap prev(fBitmap); |
| 161 |
if (!fBitmap.tryAllocPixels()) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
SkASSERT(prev.info() == fBitmap.info()); |
| 165 |
SkASSERT(prev.rowBytes() == fBitmap.rowBytes()); |
| 166 |
memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.computeByteSize()); |
| 167 |
} |
| 168 |
|
| 169 |
// Now fBitmap is a deep copy of itself (and therefore different from |
| 170 |
// what is being used by the image. Next we update the canvas to use |
| 171 |
// this as its backend, so we can't modify the image's pixels anymore. |
| 172 |
SkASSERT(this->getCachedCanvas()); |
| 173 |
SkBitmapDevice* bmDev = static_cast<SkBitmapDevice*>(this->getCachedCanvas()->rootDevice()); |
| 174 |
bmDev->replaceBitmapBackendForRasterSurface(fBitmap); |
| 175 |
} |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
sk_sp<const SkCapabilities> SkSurface_Raster::onCapabilities() { |
| 180 |
return SkCapabilities::RasterBackend(); |
| 181 |
} |
| 182 |
|
| 183 |
SkRecorder* SkSurface_Raster::onGetBaseRecorder() const { |
| 184 |
return fRecorder; |
| 185 |
} |
| 186 |
|
| 187 |
/////////////////////////////////////////////////////////////////////////////// |
| 188 |
namespace SkSurfaces { |
| 189 |
sk_sp<SkSurface> WrapPixels(const SkImageInfo& info, |
| 190 |
void* pixels, |
| 191 |
size_t rowBytes, |
| 192 |
PixelsReleaseProc releaseProc, |
| 193 |
void* context, |
| 194 |
const SkSurfaceProps* props) { |
| 195 |
if (nullptr == releaseProc) { |
| 196 |
context = nullptr; |
| 197 |
} |
| 198 |
if (!SkSurfaceValidateRasterInfo(info, rowBytes)) { |
| 199 |
return nullptr; |
| 200 |
} |
| 201 |
if (nullptr == pixels) { |
| 202 |
return nullptr; |
| 203 |
} |
| 204 |
|
| 205 |
return sk_make_sp<SkSurface_Raster>(info, pixels, rowBytes, releaseProc, context, props); |
| 206 |
} |
| 207 |
|
| 208 |
sk_sp<SkSurface> WrapPixels(const SkImageInfo& info, |
| 209 |
void* pixels, |
| 210 |
size_t rowBytes, |
| 211 |
const SkSurfaceProps* props) { |
| 212 |
return WrapPixels(info, pixels, rowBytes, nullptr, nullptr, props); |
| 213 |
} |
| 214 |
|
| 215 |
sk_sp<SkSurface> Raster(const SkImageInfo& info, size_t rowBytes, const SkSurfaceProps* props) { |
| 216 |
if (!SkSurfaceValidateRasterInfo(info)) { |
| 217 |
return nullptr; |
| 218 |
} |
| 219 |
|
| 220 |
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes); |
| 221 |
if (!pr) { |
| 222 |
return nullptr; |
| 223 |
} |
| 224 |
if (rowBytes) { |
| 225 |
SkASSERT(pr->rowBytes() == rowBytes); |
| 226 |
} |
| 227 |
return sk_make_sp<SkSurface_Raster>(info, std::move(pr), props); |
| 228 |
} |
| 229 |
|
| 230 |
} // namespace SkSurfaces |
| 231 |
|
| 232 |
namespace skcpu { |
| 233 |
|
| 234 |
sk_sp<SkSurface> Recorder::makeBitmapSurface(const SkImageInfo& imageInfo, |
| 235 |
const SkSurfaceProps* surfaceProps) { |
| 236 |
return this->makeBitmapSurface(imageInfo, 0, surfaceProps); |
| 237 |
} |
| 238 |
|
| 239 |
sk_sp<SkSurface> Recorder::makeBitmapSurface(const SkImageInfo& imageInfo, |
| 240 |
size_t rowBytes, |
| 241 |
const SkSurfaceProps* surfaceProps) { |
| 242 |
if (!SkSurfaceValidateRasterInfo(imageInfo)) { |
| 243 |
return nullptr; |
| 244 |
} |
| 245 |
|
| 246 |
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(imageInfo, rowBytes); |
| 247 |
if (!pr) { |
| 248 |
return nullptr; |
| 249 |
} |
| 250 |
if (rowBytes) { |
| 251 |
SkASSERT(pr->rowBytes() == rowBytes); |
| 252 |
} |
| 253 |
|
| 254 |
return sk_make_sp<SkSurface_Raster>(asRRI(this), imageInfo, std::move(pr), surfaceProps); |
| 255 |
} |
| 256 |
|
| 257 |
} // namespace skcpu |