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 "SkDevice.h" |
12 #include "SkMallocPixelRef.h" | 12 #include "SkMallocPixelRef.h" |
13 | 13 |
14 static const size_t kIgnoreRowBytesValue = (size_t)~0; | 14 static const size_t kIgnoreRowBytesValue = (size_t)~0; |
15 | 15 |
16 class SkSurface_Raster : public SkSurface_Base { | 16 class SkSurface_Raster : public SkSurface_Base { |
17 public: | 17 public: |
18 static bool Valid(const SkImage::Info&, size_t rb = kIgnoreRowBytesValue); | 18 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); |
19 | 19 |
20 SkSurface_Raster(const SkImage::Info&, void*, size_t rb); | 20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb); |
21 SkSurface_Raster(const SkImage::Info&, SkPixelRef*, size_t rb); | 21 SkSurface_Raster(const SkImageInfo&, SkPixelRef*, size_t rb); |
22 | 22 |
23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; | 23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; |
24 virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE; | 24 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; |
25 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; | 25 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; |
26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, | 26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
27 const SkPaint*) SK_OVERRIDE; | 27 const SkPaint*) SK_OVERRIDE; |
28 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; | 28 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; |
29 | 29 |
30 private: | 30 private: |
31 SkBitmap fBitmap; | 31 SkBitmap fBitmap; |
32 bool fWeOwnThePixels; | 32 bool fWeOwnThePixels; |
33 | 33 |
34 typedef SkSurface_Base INHERITED; | 34 typedef SkSurface_Base INHERITED; |
35 }; | 35 }; |
36 | 36 |
37 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
38 | 38 |
39 bool SkSurface_Raster::Valid(const SkImage::Info& info, size_t rowBytes) { | 39 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |
40 static const size_t kMaxTotalSize = SK_MaxS32; | 40 static const size_t kMaxTotalSize = SK_MaxS32; |
41 | 41 |
42 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 42 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
43 | 43 |
44 int shift = 0; | 44 int shift = 0; |
45 switch (config) { | 45 switch (config) { |
46 case SkBitmap::kA8_Config: | 46 case SkBitmap::kA8_Config: |
47 shift = 0; | 47 shift = 0; |
48 break; | 48 break; |
49 case SkBitmap::kRGB_565_Config: | 49 case SkBitmap::kRGB_565_Config: |
(...skipping 23 matching lines...) Expand all Loading... |
73 } | 73 } |
74 | 74 |
75 uint64_t size = (uint64_t)info.fHeight * rowBytes; | 75 uint64_t size = (uint64_t)info.fHeight * rowBytes; |
76 if (size > kMaxTotalSize) { | 76 if (size > kMaxTotalSize) { |
77 return false; | 77 return false; |
78 } | 78 } |
79 | 79 |
80 return true; | 80 return true; |
81 } | 81 } |
82 | 82 |
83 SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, void* pixels, size
_t rb) | 83 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
rb) |
84 : INHERITED(info.fWidth, info.fHeight) { | 84 : INHERITED(info.fWidth, info.fHeight) { |
85 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 85 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
86 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb, info.fAlphaType); | 86 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb, info.fAlphaType); |
87 fBitmap.setPixels(pixels); | 87 fBitmap.setPixels(pixels); |
88 fWeOwnThePixels = false; // We are "Direct" | 88 fWeOwnThePixels = false; // We are "Direct" |
89 } | 89 } |
90 | 90 |
91 SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, SkPixelRef* pr, si
ze_t rb) | 91 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, SkPixelRef* pr, size
_t rb) |
92 : INHERITED(info.fWidth, info.fHeight) { | 92 : INHERITED(info.fWidth, info.fHeight) { |
93 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 93 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
94 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb, info.fAlphaType); | 94 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb, info.fAlphaType); |
95 fBitmap.setPixelRef(pr); | 95 fBitmap.setPixelRef(pr); |
96 fWeOwnThePixels = true; | 96 fWeOwnThePixels = true; |
97 | 97 |
98 if (!SkAlphaTypeIsOpaque(info.fAlphaType)) { | 98 if (!SkAlphaTypeIsOpaque(info.fAlphaType)) { |
99 fBitmap.eraseColor(SK_ColorTRANSPARENT); | 99 fBitmap.eraseColor(SK_ColorTRANSPARENT); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 SkCanvas* SkSurface_Raster::onNewCanvas() { | 103 SkCanvas* SkSurface_Raster::onNewCanvas() { |
104 return SkNEW_ARGS(SkCanvas, (fBitmap)); | 104 return SkNEW_ARGS(SkCanvas, (fBitmap)); |
105 } | 105 } |
106 | 106 |
107 SkSurface* SkSurface_Raster::onNewSurface(const SkImage::Info& info) { | 107 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
108 return SkSurface::NewRaster(info); | 108 return SkSurface::NewRaster(info); |
109 } | 109 } |
110 | 110 |
111 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 111 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
112 const SkPaint* paint) { | 112 const SkPaint* paint) { |
113 canvas->drawBitmap(fBitmap, x, y, paint); | 113 canvas->drawBitmap(fBitmap, x, y, paint); |
114 } | 114 } |
115 | 115 |
116 SkImage* SkSurface_Raster::onNewImageSnapshot() { | 116 SkImage* SkSurface_Raster::onNewImageSnapshot() { |
117 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); | 117 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); |
(...skipping 14 matching lines...) Expand all Loading... |
132 // Now fBitmap is a deep copy of itself (and therefore different from | 132 // Now fBitmap is a deep copy of itself (and therefore different from |
133 // what is being used by the image. Next we update the canvas to use | 133 // what is being used by the image. Next we update the canvas to use |
134 // this as its backend, so we can't modify the image's pixels anymore. | 134 // this as its backend, so we can't modify the image's pixels anymore. |
135 SkASSERT(NULL != this->getCachedCanvas()); | 135 SkASSERT(NULL != this->getCachedCanvas()); |
136 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
ce(fBitmap); | 136 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
ce(fBitmap); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 /////////////////////////////////////////////////////////////////////////////// | 140 /////////////////////////////////////////////////////////////////////////////// |
141 | 141 |
142 SkSurface* SkSurface::NewRasterDirect(const SkImage::Info& info, void* pixels, s
ize_t rowBytes) { | 142 SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, siz
e_t rowBytes) { |
143 if (!SkSurface_Raster::Valid(info, rowBytes)) { | 143 if (!SkSurface_Raster::Valid(info, rowBytes)) { |
144 return NULL; | 144 return NULL; |
145 } | 145 } |
146 if (NULL == pixels) { | 146 if (NULL == pixels) { |
147 return NULL; | 147 return NULL; |
148 } | 148 } |
149 | 149 |
150 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rowBytes)); | 150 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rowBytes)); |
151 } | 151 } |
152 | 152 |
153 SkSurface* SkSurface::NewRaster(const SkImage::Info& info) { | 153 SkSurface* SkSurface::NewRaster(const SkImageInfo& info) { |
154 if (!SkSurface_Raster::Valid(info)) { | 154 if (!SkSurface_Raster::Valid(info)) { |
155 return NULL; | 155 return NULL; |
156 } | 156 } |
157 | 157 |
158 static const size_t kMaxTotalSize = SK_MaxS32; | 158 static const size_t kMaxTotalSize = SK_MaxS32; |
159 size_t rowBytes = SkImageMinRowBytes(info); | 159 size_t rowBytes = SkImageMinRowBytes(info); |
160 uint64_t size64 = (uint64_t)info.fHeight * rowBytes; | 160 uint64_t size64 = (uint64_t)info.fHeight * rowBytes; |
161 if (size64 > kMaxTotalSize) { | 161 if (size64 > kMaxTotalSize) { |
162 return NULL; | 162 return NULL; |
163 } | 163 } |
164 | 164 |
165 size_t size = (size_t)size64; | 165 size_t size = (size_t)size64; |
166 void* pixels = sk_malloc_throw(size); | 166 void* pixels = sk_malloc_throw(size); |
167 if (NULL == pixels) { | 167 if (NULL == pixels) { |
168 return NULL; | 168 return NULL; |
169 } | 169 } |
170 | 170 |
171 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL
, true))); | 171 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL
, true))); |
172 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes)); | 172 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes)); |
173 } | 173 } |
OLD | NEW |