OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkConfig8888.h" | 9 #include "SkConfig8888.h" |
10 #include "SkDeviceProperties.h" | 10 #include "SkDeviceProperties.h" |
11 #include "SkDraw.h" | 11 #include "SkDraw.h" |
| 12 #include "SkImagePriv.h" |
12 #include "SkImage_Base.h" | 13 #include "SkImage_Base.h" |
13 #include "SkRasterClip.h" | 14 #include "SkRasterClip.h" |
14 #include "SkShader.h" | 15 #include "SkShader.h" |
15 #include "SkSurface.h" | 16 #include "SkSurface.h" |
16 | 17 |
17 #define CHECK_FOR_ANNOTATION(paint) \ | 18 #define CHECK_FOR_ANNOTATION(paint) \ |
18 do { if (paint.getAnnotation()) { return; } } while (0) | 19 do { if (paint.getAnnotation()) { return; } } while (0) |
19 | 20 |
20 static bool valid_for_bitmap_device(const SkImageInfo& info, | 21 static bool valid_for_bitmap_device(const SkImageInfo& info, |
21 SkAlphaType* newAlphaType) { | 22 SkAlphaType* newAlphaType) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 SkASSERT(valid_for_bitmap_device(bitmap.info(), NULL)); | 64 SkASSERT(valid_for_bitmap_device(bitmap.info(), NULL)); |
64 } | 65 } |
65 | 66 |
66 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties&
deviceProperties) | 67 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties&
deviceProperties) |
67 : SkBaseDevice(deviceProperties) | 68 : SkBaseDevice(deviceProperties) |
68 , fBitmap(bitmap) | 69 , fBitmap(bitmap) |
69 { | 70 { |
70 SkASSERT(valid_for_bitmap_device(bitmap.info(), NULL)); | 71 SkASSERT(valid_for_bitmap_device(bitmap.info(), NULL)); |
71 } | 72 } |
72 | 73 |
| 74 SkBitmapDevice::~SkBitmapDevice() { |
| 75 } |
| 76 |
73 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, | 77 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, |
74 const SkDeviceProperties* props) { | 78 const SkDeviceProperties* props) { |
75 SkAlphaType newAT = origInfo.alphaType(); | 79 SkAlphaType newAT = origInfo.alphaType(); |
76 if (!valid_for_bitmap_device(origInfo, &newAT)) { | 80 if (!valid_for_bitmap_device(origInfo, &newAT)) { |
77 return NULL; | 81 return NULL; |
78 } | 82 } |
79 | 83 |
80 const SkImageInfo info = origInfo.makeAlphaType(newAT); | 84 const SkImageInfo info = origInfo.makeAlphaType(newAT); |
81 SkBitmap bitmap; | 85 SkBitmap bitmap; |
82 | 86 |
(...skipping 14 matching lines...) Expand all Loading... |
97 return SkNEW_ARGS(SkBitmapDevice, (bitmap, *props)); | 101 return SkNEW_ARGS(SkBitmapDevice, (bitmap, *props)); |
98 } else { | 102 } else { |
99 return SkNEW_ARGS(SkBitmapDevice, (bitmap)); | 103 return SkNEW_ARGS(SkBitmapDevice, (bitmap)); |
100 } | 104 } |
101 } | 105 } |
102 | 106 |
103 SkImageInfo SkBitmapDevice::imageInfo() const { | 107 SkImageInfo SkBitmapDevice::imageInfo() const { |
104 return fBitmap.info(); | 108 return fBitmap.info(); |
105 } | 109 } |
106 | 110 |
107 void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) { | 111 void SkBitmapDevice::deepCopyBackendIfNeeded() { |
108 SkASSERT(bm.width() == fBitmap.width()); | 112 if (NULL != fSnapshot) { |
109 SkASSERT(bm.height() == fBitmap.height()); | 113 if (!fSnapshot->unique()) { |
110 fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config) | 114 SkBitmap prev(fBitmap); |
111 fBitmap.lockPixels(); | 115 prev.deepCopyTo(&fBitmap); |
| 116 fBitmap.lockPixels(); |
| 117 } |
| 118 fSnapshot.reset(NULL); |
| 119 } |
112 } | 120 } |
113 | 121 |
114 SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPa
int*) { | 122 SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPa
int*) { |
115 SkDeviceProperties leaky(cinfo.fPixelGeometry); | 123 SkDeviceProperties leaky(cinfo.fPixelGeometry); |
116 return SkBitmapDevice::Create(cinfo.fInfo, &leaky); | 124 return SkBitmapDevice::Create(cinfo.fInfo, &leaky); |
117 } | 125 } |
118 | 126 |
119 void SkBitmapDevice::lockPixels() { | 127 void SkBitmapDevice::lockPixels() { |
120 if (fBitmap.lockPixelsAreWritable()) { | 128 if (fBitmap.lockPixelsAreWritable()) { |
121 fBitmap.lockPixels(); | 129 fBitmap.lockPixels(); |
122 } | 130 } |
123 } | 131 } |
124 | 132 |
125 void SkBitmapDevice::unlockPixels() { | 133 void SkBitmapDevice::unlockPixels() { |
126 if (fBitmap.lockPixelsAreWritable()) { | 134 if (fBitmap.lockPixelsAreWritable()) { |
127 fBitmap.unlockPixels(); | 135 fBitmap.unlockPixels(); |
128 } | 136 } |
129 } | 137 } |
130 | 138 |
131 const SkBitmap& SkBitmapDevice::onAccessBitmap() { | 139 const SkBitmap& SkBitmapDevice::onAccessBitmap() { |
| 140 this->deepCopyBackendIfNeeded(); |
132 return fBitmap; | 141 return fBitmap; |
133 } | 142 } |
134 | 143 |
135 void* SkBitmapDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) { | 144 void* SkBitmapDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) { |
136 if (fBitmap.getPixels()) { | 145 if (fBitmap.getPixels()) { |
| 146 this->deepCopyBackendIfNeeded(); |
137 *info = fBitmap.info(); | 147 *info = fBitmap.info(); |
138 *rowBytes = fBitmap.rowBytes(); | 148 *rowBytes = fBitmap.rowBytes(); |
139 return fBitmap.getPixels(); | 149 return fBitmap.getPixels(); |
140 } | 150 } |
141 return NULL; | 151 return NULL; |
142 } | 152 } |
143 | 153 |
144 #include "SkConfig8888.h" | 154 #include "SkConfig8888.h" |
145 #include "SkPixelRef.h" | 155 #include "SkPixelRef.h" |
146 | 156 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 const SkPoint verts[], const SkPoint textures[
], | 354 const SkPoint verts[], const SkPoint textures[
], |
345 const SkColor colors[], SkXfermode* xmode, | 355 const SkColor colors[], SkXfermode* xmode, |
346 const uint16_t indices[], int indexCount, | 356 const uint16_t indices[], int indexCount, |
347 const SkPaint& paint) { | 357 const SkPaint& paint) { |
348 draw.drawVertices(vmode, vertexCount, verts, textures, colors, xmode, | 358 draw.drawVertices(vmode, vertexCount, verts, textures, colors, xmode, |
349 indices, indexCount, paint); | 359 indices, indexCount, paint); |
350 } | 360 } |
351 | 361 |
352 void SkBitmapDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, | 362 void SkBitmapDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
353 int x, int y, const SkPaint& paint) { | 363 int x, int y, const SkPaint& paint) { |
354 const SkBitmap& src = device->accessBitmap(false); | 364 SkAutoTUnref<SkImage> image(device->newImageSnapshot()); |
| 365 if (NULL == image) { |
| 366 return; |
| 367 } |
| 368 SkBitmap src; |
| 369 if (!as_IB(image.get())->getROPixels(&src)) { |
| 370 return; |
| 371 } |
355 draw.drawSprite(src, x, y, paint); | 372 draw.drawSprite(src, x, y, paint); |
356 } | 373 } |
357 | 374 |
358 SkSurface* SkBitmapDevice::newSurface(const SkImageInfo& info, const SkSurfacePr
ops& props) { | 375 SkSurface* SkBitmapDevice::newSurface(const SkImageInfo& info, const SkSurfacePr
ops& props) { |
359 return SkSurface::NewRaster(info, &props); | 376 return SkSurface::NewRaster(info, &props); |
360 } | 377 } |
361 | 378 |
| 379 void SkBitmapDevice::discard() { |
| 380 if (NULL != fSnapshot) { |
| 381 if (!fSnapshot->unique()) { |
| 382 fBitmap.setPixelRef(NULL); |
| 383 fBitmap.allocPixels(); |
| 384 } |
| 385 fSnapshot.reset(NULL); |
| 386 } |
| 387 } |
| 388 |
| 389 SkImage* SkBitmapDevice::newImageSnapshot() { |
| 390 if (NULL == fSnapshot) { |
| 391 fSnapshot.reset(this->onNewImageSnapshot()); |
| 392 } |
| 393 return SkSafeRef(fSnapshot.get()); |
| 394 } |
| 395 |
| 396 SkImage* SkBitmapDevice::onNewImageSnapshot() { |
| 397 return SkNewImageFromBitmap(fBitmap, NULL); |
| 398 } |
| 399 |
362 const void* SkBitmapDevice::peekPixels(SkImageInfo* info, size_t* rowBytes) { | 400 const void* SkBitmapDevice::peekPixels(SkImageInfo* info, size_t* rowBytes) { |
363 const SkImageInfo bmInfo = fBitmap.info(); | 401 const SkImageInfo bmInfo = fBitmap.info(); |
364 if (fBitmap.getPixels() && (kUnknown_SkColorType != bmInfo.colorType())) { | 402 if (fBitmap.getPixels() && (kUnknown_SkColorType != bmInfo.colorType())) { |
365 if (info) { | 403 if (info) { |
366 *info = bmInfo; | 404 *info = bmInfo; |
367 } | 405 } |
368 if (rowBytes) { | 406 if (rowBytes) { |
369 *rowBytes = fBitmap.rowBytes(); | 407 *rowBytes = fBitmap.rowBytes(); |
370 } | 408 } |
371 return fBitmap.getPixels(); | 409 return fBitmap.getPixels(); |
(...skipping 14 matching lines...) Expand all Loading... |
386 paint.getRasterizer() || | 424 paint.getRasterizer() || |
387 paint.getPathEffect() || | 425 paint.getPathEffect() || |
388 paint.isFakeBoldText() || | 426 paint.isFakeBoldText() || |
389 paint.getStyle() != SkPaint::kFill_Style || | 427 paint.getStyle() != SkPaint::kFill_Style || |
390 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)) | 428 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)) |
391 { | 429 { |
392 return true; | 430 return true; |
393 } | 431 } |
394 return false; | 432 return false; |
395 } | 433 } |
OLD | NEW |