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 "SkBitmapDevice.h" |
12 #include "SkMallocPixelRef.h" | 12 #include "SkMallocPixelRef.h" |
| 13 #include "SkDeviceProperties.h" |
13 | 14 |
14 static const size_t kIgnoreRowBytesValue = (size_t)~0; | 15 static const size_t kIgnoreRowBytesValue = (size_t)~0; |
15 | 16 |
| 17 class SkRasterSurfaceDevice; |
| 18 |
16 class SkSurface_Raster : public SkSurface_Base { | 19 class SkSurface_Raster : public SkSurface_Base { |
17 public: | 20 public: |
18 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); | 21 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); |
19 | 22 |
20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb, | 23 SkSurface_Raster(const SkImageInfo&, void*, size_t rb, |
21 void (*releaseProc)(void* pixels, void* context), void* con
text, | 24 void (*releaseProc)(void* pixels, void* context), void* con
text, |
22 const SkSurfaceProps*); | 25 const SkSurfaceProps*); |
23 SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*); | 26 SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*); |
24 | 27 |
| 28 ~SkSurface_Raster(); |
| 29 |
25 SkCanvas* onNewCanvas() SK_OVERRIDE; | 30 SkCanvas* onNewCanvas() SK_OVERRIDE; |
26 SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; | 31 SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; |
27 SkImage* onNewImageSnapshot(Budgeted) SK_OVERRIDE; | 32 SkImage* onNewImageSnapshot(Budgeted) SK_OVERRIDE; |
28 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, | 33 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
29 const SkPaint*) SK_OVERRIDE; | 34 const SkPaint*) SK_OVERRIDE; |
30 void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; | 35 void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; |
31 | 36 |
| 37 /** Creates a surface that is attached to a device. |
| 38 * The device will own the reference to the surface, and the surface will go
out of scope when |
| 39 * the device goes out of scope. Note: This function is deprecated, it will
be renamed to |
| 40 * SkSurface_Raster* createCompatibleSurface(const SkBaseDevice::CreateInfo&
cinfo) |
| 41 */ |
| 42 SkBaseDevice* createCompatibleDeviceDeprecated(const SkBaseDevice::CreateInf
o& cinfo); |
| 43 |
| 44 SkRasterSurfaceDevice* CreateWithReverseOwnership(SkPixelRef*, const SkDevic
eProperties&, |
| 45 const SkSurfaceProps*); |
32 private: | 46 private: |
33 SkBitmap fBitmap; | 47 /** Deprecated constructor for standalone devices, eg. devices that own the
surface. |
34 bool fWeOwnThePixels; | 48 */ |
| 49 SkSurface_Raster(SkPixelRef*, const SkDeviceProperties&, const SkSurfaceProp
s*); |
| 50 void initWithPixelRef(SkPixelRef* pr); |
| 51 |
| 52 SkBitmap fBitmap; |
| 53 SkRasterSurfaceDevice* fDevice; |
| 54 bool fSurfaceOwnsDevice;; |
| 55 bool fWeOwnThePixels; |
35 | 56 |
36 typedef SkSurface_Base INHERITED; | 57 typedef SkSurface_Base INHERITED; |
37 }; | 58 }; |
38 | 59 |
| 60 /** A bitmap device that is backed by SkSurface_Raster. |
| 61 */ |
| 62 class SkRasterSurfaceDevice : public SkBitmapDevice { |
| 63 public: |
| 64 SkRasterSurfaceDevice(SkSurface_Raster*, const SkBitmap& bitmap); |
| 65 |
| 66 /** Deprecated constructor for devices that own their surface.*/ |
| 67 SkRasterSurfaceDevice(SkSurface_Raster*, const SkBitmap& bitmap, |
| 68 const SkDeviceProperties& deviceProps); |
| 69 |
| 70 ~SkRasterSurfaceDevice(); |
| 71 |
| 72 void swapBitmap(const SkBitmap&); |
| 73 |
| 74 void discard() SK_OVERRIDE; |
| 75 |
| 76 private: |
| 77 SkBaseDevice* onCreateCompatibleDevice(const CreateInfo& cinfo) SK_OVERRIDE; |
| 78 const SkBitmap& onAccessBitmap() SK_OVERRIDE; |
| 79 |
| 80 SkSurface_Raster* fSurface; |
| 81 bool fDeviceOwnsSurface; |
| 82 |
| 83 typedef SkBitmapDevice INHERITED; |
| 84 }; |
| 85 |
| 86 SkRasterSurfaceDevice::SkRasterSurfaceDevice(SkSurface_Raster* surface, const Sk
Bitmap& bitmap) |
| 87 : INHERITED(bitmap) |
| 88 , fSurface(surface) |
| 89 , fDeviceOwnsSurface(false) { |
| 90 } |
| 91 |
| 92 SkRasterSurfaceDevice::SkRasterSurfaceDevice(SkSurface_Raster* surface, |
| 93 const SkBitmap& bitmap, |
| 94 const SkDeviceProperties& devicePro
ps) |
| 95 : INHERITED(bitmap, deviceProps) |
| 96 , fSurface(surface) |
| 97 , fDeviceOwnsSurface(true) { |
| 98 } |
| 99 |
| 100 SkRasterSurfaceDevice::~SkRasterSurfaceDevice() { |
| 101 if (fDeviceOwnsSurface) { |
| 102 fSurface->unref(); |
| 103 } |
| 104 } |
| 105 |
| 106 SkBaseDevice* SkRasterSurfaceDevice::onCreateCompatibleDevice(const CreateInfo&
cinfo) { |
| 107 return fSurface->createCompatibleDeviceDeprecated(cinfo); |
| 108 } |
| 109 |
| 110 void SkRasterSurfaceDevice::swapBitmap(const SkBitmap& bitmap) { |
| 111 this->replaceBitmapBackendForRasterSurface(bitmap); |
| 112 } |
| 113 |
| 114 const SkBitmap& SkRasterSurfaceDevice::onAccessBitmap() { |
| 115 if (fSurface) { |
| 116 fSurface->aboutToDraw(SkSurface::kRetain_ContentChangeMode); |
| 117 } |
| 118 return this->INHERITED::onAccessBitmap(); |
| 119 } |
| 120 |
| 121 void SkRasterSurfaceDevice::discard() { |
| 122 if (fSurface) { |
| 123 fSurface->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); |
| 124 } |
| 125 } |
| 126 |
39 /////////////////////////////////////////////////////////////////////////////// | 127 /////////////////////////////////////////////////////////////////////////////// |
40 | 128 |
41 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { | 129 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |
42 if (info.isEmpty()) { | 130 if (info.isEmpty()) { |
43 return false; | 131 return false; |
44 } | 132 } |
45 | 133 |
46 static const size_t kMaxTotalSize = SK_MaxS32; | 134 static const size_t kMaxTotalSize = SK_MaxS32; |
47 | 135 |
48 int shift = 0; | 136 int shift = 0; |
(...skipping 29 matching lines...) Expand all Loading... |
78 if (size > kMaxTotalSize) { | 166 if (size > kMaxTotalSize) { |
79 return false; | 167 return false; |
80 } | 168 } |
81 | 169 |
82 return true; | 170 return true; |
83 } | 171 } |
84 | 172 |
85 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
rb, | 173 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
rb, |
86 void (*releaseProc)(void* pixels, void* conte
xt), void* context, | 174 void (*releaseProc)(void* pixels, void* conte
xt), void* context, |
87 const SkSurfaceProps* props) | 175 const SkSurfaceProps* props) |
88 : INHERITED(info, props) | 176 : INHERITED(info, props) { |
89 { | |
90 fBitmap.installPixels(info, pixels, rb, NULL, releaseProc, context); | 177 fBitmap.installPixels(info, pixels, rb, NULL, releaseProc, context); |
91 fWeOwnThePixels = false; // We are "Direct" | 178 fWeOwnThePixels = false; // We are "Direct" |
92 } | 179 } |
93 | 180 |
94 SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props) | 181 SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props) |
95 : INHERITED(pr->info().width(), pr->info().height(), props) | 182 : INHERITED(pr->info().width(), pr->info().height(), props) { |
96 { | 183 this->initWithPixelRef(pr); |
| 184 fDevice = SkNEW_ARGS(SkRasterSurfaceDevice, (this, fBitmap)); |
| 185 fSurfaceOwnsDevice = true; |
| 186 } |
| 187 |
| 188 SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkDeviceProperties& dev
iceProps, |
| 189 const SkSurfaceProps* props) |
| 190 : INHERITED(pr->info().width(), pr->info().height(), props) { |
| 191 this->initWithPixelRef(pr); |
| 192 fDevice = SkNEW_ARGS(SkRasterSurfaceDevice, (this, fBitmap, deviceProps)); |
| 193 fSurfaceOwnsDevice = false; |
| 194 } |
| 195 |
| 196 void SkSurface_Raster::initWithPixelRef(SkPixelRef* pr) { |
97 const SkImageInfo& info = pr->info(); | 197 const SkImageInfo& info = pr->info(); |
98 | 198 |
99 fBitmap.setInfo(info, info.minRowBytes()); | 199 fBitmap.setInfo(info, info.minRowBytes()); |
100 fBitmap.setPixelRef(pr); | 200 fBitmap.setPixelRef(pr); |
101 fWeOwnThePixels = true; | 201 fWeOwnThePixels = true; |
102 | 202 |
103 if (!info.isOpaque()) { | 203 if (!info.isOpaque()) { |
104 fBitmap.eraseColor(SK_ColorTRANSPARENT); | 204 fBitmap.eraseColor(SK_ColorTRANSPARENT); |
105 } | 205 } |
106 } | 206 } |
107 | 207 |
| 208 SkSurface_Raster::~SkSurface_Raster() { |
| 209 if (fSurfaceOwnsDevice) { |
| 210 fDevice->unref(); |
| 211 } |
| 212 } |
| 213 SkRasterSurfaceDevice* SkSurface_Raster::CreateWithReverseOwnership( |
| 214 SkPixelRef* pr, const SkDeviceProperties& deviceProperties, |
| 215 const SkSurfaceProps* surfaceProperties) { |
| 216 SkSurface_Raster* surface = SkNEW_ARGS(SkSurface_Raster, |
| 217 (pr, deviceProperties, surfacePropert
ies)); |
| 218 |
| 219 // Caller owns ref to the device, and the device owns ref to the surface. |
| 220 return surface->fDevice; |
| 221 } |
| 222 |
108 SkCanvas* SkSurface_Raster::onNewCanvas() { | 223 SkCanvas* SkSurface_Raster::onNewCanvas() { |
109 return SkNEW_ARGS(SkCanvas, (fBitmap, this->props())); | 224 return SkNEW_ARGS(SkCanvas, (fDevice, &this->props(), SkCanvas::kDefault_Ini
tFlags)); |
110 } | 225 } |
111 | 226 |
112 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { | 227 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
113 return SkSurface::NewRaster(info); | 228 return SkSurface::NewRaster(info); |
114 } | 229 } |
115 | 230 |
116 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 231 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
117 const SkPaint* paint) { | 232 const SkPaint* paint) { |
118 canvas->drawBitmap(fBitmap, x, y, paint); | 233 canvas->drawBitmap(fBitmap, x, y, paint); |
119 } | 234 } |
(...skipping 10 matching lines...) Expand all Loading... |
130 if (kDiscard_ContentChangeMode == mode) { | 245 if (kDiscard_ContentChangeMode == mode) { |
131 fBitmap.setPixelRef(NULL); | 246 fBitmap.setPixelRef(NULL); |
132 fBitmap.allocPixels(); | 247 fBitmap.allocPixels(); |
133 } else { | 248 } else { |
134 SkBitmap prev(fBitmap); | 249 SkBitmap prev(fBitmap); |
135 prev.deepCopyTo(&fBitmap); | 250 prev.deepCopyTo(&fBitmap); |
136 } | 251 } |
137 // Now fBitmap is a deep copy of itself (and therefore different from | 252 // Now fBitmap is a deep copy of itself (and therefore different from |
138 // what is being used by the image. Next we update the canvas to use | 253 // what is being used by the image. Next we update the canvas to use |
139 // this as its backend, so we can't modify the image's pixels anymore. | 254 // this as its backend, so we can't modify the image's pixels anymore. |
140 SkASSERT(this->getCachedCanvas()); | 255 fDevice->swapBitmap(fBitmap); |
141 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
ce(fBitmap); | |
142 } | 256 } |
143 } | 257 } |
144 | 258 |
| 259 SkBaseDevice* SkSurface_Raster::createCompatibleDeviceDeprecated( |
| 260 const SkBaseDevice::CreateInfo& cinfo) { |
| 261 SkDeviceProperties leaky(cinfo.fPixelGeometry); |
| 262 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(cinfo.fInfo, 0, NU
LL)); |
| 263 if (NULL == pr.get()) { |
| 264 return NULL; |
| 265 } |
| 266 return SkSurface_Raster::CreateWithReverseOwnership(pr, leaky, &this->props(
)); |
| 267 } |
| 268 |
145 /////////////////////////////////////////////////////////////////////////////// | 269 /////////////////////////////////////////////////////////////////////////////// |
146 | 270 |
147 SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void*
pixels, size_t rb, | 271 SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void*
pixels, size_t rb, |
148 void (*releaseProc)(void* pixel
s, void* context), | 272 void (*releaseProc)(void* pixel
s, void* context), |
149 void* context, const SkSurfaceP
rops* props) { | 273 void* context, const SkSurfaceP
rops* props) { |
150 if (NULL == releaseProc) { | 274 if (NULL == releaseProc) { |
151 context = NULL; | 275 context = NULL; |
152 } | 276 } |
153 if (!SkSurface_Raster::Valid(info, rb)) { | 277 if (!SkSurface_Raster::Valid(info, rb)) { |
154 return NULL; | 278 return NULL; |
(...skipping 14 matching lines...) Expand all Loading... |
169 if (!SkSurface_Raster::Valid(info)) { | 293 if (!SkSurface_Raster::Valid(info)) { |
170 return NULL; | 294 return NULL; |
171 } | 295 } |
172 | 296 |
173 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); | 297 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
174 if (NULL == pr.get()) { | 298 if (NULL == pr.get()) { |
175 return NULL; | 299 return NULL; |
176 } | 300 } |
177 return SkNEW_ARGS(SkSurface_Raster, (pr, props)); | 301 return SkNEW_ARGS(SkSurface_Raster, (pr, props)); |
178 } | 302 } |
OLD | NEW |