Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSurface_Raster.cpp
Go to the documentation of this file.
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
9
16#include "include/core/SkRect.h"
26
27#include <cstdint>
28#include <cstring>
29#include <utility>
30
31class SkImage;
32class SkPaint;
33class SkPixmap;
34class SkSurfaceProps;
35
36bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) {
38 return false;
39 }
40
41 if (kIgnoreRowBytesValue == rowBytes) {
42 return true;
43 }
44
45 if (!info.validRowBytes(rowBytes)) {
46 return false;
47 }
48
49 uint64_t size = sk_64_mul(info.height(), rowBytes);
50 static const size_t kMaxTotalSize = SK_MaxS32;
51 if (size > kMaxTotalSize) {
52 return false;
53 }
54
55 return true;
56}
57
58SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
59 void (*releaseProc)(void* pixels, void* context), void* context,
60 const SkSurfaceProps* props)
61 : INHERITED(info, props)
62{
63 fBitmap.installPixels(info, pixels, rb, releaseProc, context);
64 fWeOwnThePixels = false; // We are "Direct"
65}
66
68 const SkSurfaceProps* props)
69 : INHERITED(pr->width(), pr->height(), props)
70{
71 fBitmap.setInfo(info, pr->rowBytes());
72 fBitmap.setPixelRef(std::move(pr), 0, 0);
73 fWeOwnThePixels = true;
74}
75
76SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
77
81
83 const SkSamplingOptions& sampling, const SkPaint* paint) {
84 canvas->drawImage(fBitmap.asImage().get(), x, y, sampling, paint);
85}
86
88 if (subset) {
89 SkASSERT(SkIRect::MakeWH(fBitmap.width(), fBitmap.height()).contains(*subset));
90 SkBitmap dst;
91 dst.allocPixels(fBitmap.info().makeDimensions(subset->size()));
92 SkAssertResult(fBitmap.readPixels(dst.pixmap(), subset->left(), subset->top()));
93 dst.setImmutable(); // key, so MakeFromBitmap doesn't make a copy of the buffer
94 return dst.asImage();
95 }
96
98 if (fWeOwnThePixels) {
99 // SkImage_raster requires these pixels are immutable for its full lifetime.
100 // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
101 if (SkPixelRef* pr = fBitmap.pixelRef()) {
102 pr->setTemporarilyImmutable();
103 }
104 } else {
106 }
107
108 // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
109 // Lock the shared pixel ref to ensure peekPixels() is usable.
110 return SkMakeImageFromRasterBitmap(fBitmap, cpm);
111}
112
113void SkSurface_Raster::onWritePixels(const SkPixmap& src, int x, int y) {
114 fBitmap.writePixels(src, x, y);
115}
116
118 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there.
119 if (SkPixelRef* pr = fBitmap.pixelRef()) {
120 pr->restoreMutability();
121 }
122}
123
125 // are we sharing pixelrefs with the image?
126 sk_sp<SkImage> cached(this->refCachedImage());
127 SkASSERT(cached);
128 if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) {
129 SkASSERT(fWeOwnThePixels);
130 if (kDiscard_ContentChangeMode == mode) {
131 if (!fBitmap.tryAllocPixels()) {
132 return false;
133 }
134 } else {
135 SkBitmap prev(fBitmap);
136 if (!fBitmap.tryAllocPixels()) {
137 return false;
138 }
139 SkASSERT(prev.info() == fBitmap.info());
140 SkASSERT(prev.rowBytes() == fBitmap.rowBytes());
141 memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.computeByteSize());
142 }
143
144 // Now fBitmap is a deep copy of itself (and therefore different from
145 // what is being used by the image. Next we update the canvas to use
146 // this as its backend, so we can't modify the image's pixels anymore.
147 SkASSERT(this->getCachedCanvas());
148 SkBitmapDevice* bmDev = static_cast<SkBitmapDevice*>(this->getCachedCanvas()->rootDevice());
149 bmDev->replaceBitmapBackendForRasterSurface(fBitmap);
150 }
151 return true;
152}
153
157
158///////////////////////////////////////////////////////////////////////////////
159namespace SkSurfaces {
161 void* pixels,
162 size_t rowBytes,
164 void* context,
165 const SkSurfaceProps* props) {
166 if (nullptr == releaseProc) {
167 context = nullptr;
168 }
169 if (!SkSurfaceValidateRasterInfo(info, rowBytes)) {
170 return nullptr;
171 }
172 if (nullptr == pixels) {
173 return nullptr;
174 }
175
176 return sk_make_sp<SkSurface_Raster>(info, pixels, rowBytes, releaseProc, context, props);
177}
178
180 void* pixels,
181 size_t rowBytes,
182 const SkSurfaceProps* props) {
183 return WrapPixels(info, pixels, rowBytes, nullptr, nullptr, props);
184}
185
186sk_sp<SkSurface> Raster(const SkImageInfo& info, size_t rowBytes, const SkSurfaceProps* props) {
188 return nullptr;
189 }
190
192 if (!pr) {
193 return nullptr;
194 }
195 if (rowBytes) {
196 SkASSERT(pr->rowBytes() == rowBytes);
197 }
198 return sk_make_sp<SkSurface_Raster>(info, std::move(pr), props);
199}
200
201} // namespace SkSurfaces
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static float prev(float f)
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SkASSERT(cond)
Definition SkAssert.h:116
static void releaseProc(const void *ptr, void *context)
static bool SkImageInfoIsValid(const SkImageInfo &info)
SK_SPI sk_sp< SkImage > SkMakeImageFromRasterBitmap(const SkBitmap &, SkCopyPixelsMode)
SkCopyPixelsMode
Definition SkImagePriv.h:17
@ kIfMutable_SkCopyPixelsMode
only copy src pixels if they are marked mutable
Definition SkImagePriv.h:18
@ kAlways_SkCopyPixelsMode
always copy src pixels (even if they are marked immutable)
Definition SkImagePriv.h:19
const SkPixelRef * SkBitmapImageGetPixelRef(const SkImage *rasterImage)
static int64_t sk_64_mul(int64_t a, int64_t b)
Definition SkMath.h:33
static constexpr int32_t SK_MaxS32
Definition SkMath.h:21
bool SkSurfaceValidateRasterInfo(const SkImageInfo &, size_t rb=kIgnoreRowBytesValue)
constexpr size_t kIgnoreRowBytesValue
bool SkSurfaceValidateRasterInfo(const SkImageInfo &info, size_t rowBytes)
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition SkBitmap.cpp:323
SkPixelRef * pixelRef() const
Definition SkBitmap.h:720
size_t computeByteSize() const
Definition SkBitmap.h:293
int width() const
Definition SkBitmap.h:149
bool writePixels(const SkPixmap &src, int dstX, int dstY)
Definition SkBitmap.cpp:501
size_t rowBytes() const
Definition SkBitmap.h:238
void * getPixels() const
Definition SkBitmap.h:283
const SkImageInfo & info() const
Definition SkBitmap.h:139
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition SkBitmap.cpp:488
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Definition SkBitmap.cpp:114
int height() const
Definition SkBitmap.h:158
void setPixelRef(sk_sp< SkPixelRef > pixelRef, int dx, int dy)
Definition SkBitmap.cpp:182
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:271
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< const SkCapabilities > RasterBackend()
friend class SkCanvas
sk_sp< SkImage > refCachedImage()
SkCanvas * getCachedCanvas()
bool hasCachedImage() const
void onDraw(SkCanvas *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
SkCanvas * onNewCanvas() override
SkSurface_Raster(const SkImageInfo &, void *, size_t rb, void(*releaseProc)(void *pixels, void *context), void *context, const SkSurfaceProps *)
sk_sp< const SkCapabilities > onCapabilities() override
sk_sp< SkSurface > onNewSurface(const SkImageInfo &) override
bool onCopyOnWrite(ContentChangeMode) override
void onWritePixels(const SkPixmap &, int x, int y) override
void onRestoreBackingMutability() override
sk_sp< SkImage > onNewImageSnapshot(const SkIRect *subset) override
@ kDiscard_ContentChangeMode
discards surface on change
Definition SkSurface.h:204
const SkSurfaceProps & props() const
Definition SkSurface.h:604
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
float SkScalar
Definition extension.cpp:12
double y
double x
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
void(void *pixels, void *context) PixelsReleaseProc
Definition SkSurface.h:116
int32_t height
int32_t width
constexpr int32_t top() const
Definition SkRect.h:120
constexpr SkISize size() const
Definition SkRect.h:172
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
constexpr int32_t left() const
Definition SkRect.h:113
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463
SkImageInfo makeDimensions(SkISize newSize) const