epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkFlipPixelRef.h" |
| 9 | #include "SkFlattenable.h" |
| 10 | #include "SkRegion.h" |
| 11 | |
| 12 | SkFlipPixelRef::SkFlipPixelRef(SkBitmap::Config config, int width, int height) |
| 13 | : fFlipper(width, height) { |
| 14 | fConfig = config; |
| 15 | fSize = SkBitmap::ComputeSize(config, width, height); |
| 16 | fStorage = sk_malloc_throw(fSize << 1); |
| 17 | fPage0 = fStorage; |
| 18 | fPage1 = (char*)fStorage + fSize; |
| 19 | } |
| 20 | |
| 21 | SkFlipPixelRef::~SkFlipPixelRef() { |
| 22 | sk_free(fStorage); |
| 23 | } |
| 24 | |
| 25 | const SkRegion& SkFlipPixelRef::beginUpdate(SkBitmap* device) { |
| 26 | void* writeAddr; |
| 27 | const void* readAddr; |
| 28 | this->getFrontBack(&readAddr, &writeAddr); |
| 29 | |
| 30 | device->setConfig(fConfig, fFlipper.width(), fFlipper.height()); |
| 31 | device->setPixels(writeAddr); |
| 32 | |
| 33 | SkRegion copyBits; |
| 34 | const SkRegion& dirty = fFlipper.update(©Bits); |
| 35 | |
| 36 | SkFlipPixelRef::CopyBitsFromAddr(*device, copyBits, readAddr); |
| 37 | return dirty; |
| 38 | } |
| 39 | |
| 40 | void SkFlipPixelRef::endUpdate() { |
| 41 | this->swapPages(); |
| 42 | } |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | void* SkFlipPixelRef::onLockPixels(SkColorTable** ct) { |
| 47 | fMutex.acquire(); |
| 48 | *ct = NULL; |
| 49 | return fPage0; |
| 50 | } |
| 51 | |
| 52 | void SkFlipPixelRef::onUnlockPixels() { |
| 53 | fMutex.release(); |
| 54 | } |
| 55 | |
| 56 | void SkFlipPixelRef::swapPages() { |
| 57 | fMutex.acquire(); |
| 58 | SkTSwap<void*>(fPage0, fPage1); |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 59 | this->notifyPixelsChanged(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | fMutex.release(); |
| 61 | } |
| 62 | |
| 63 | void SkFlipPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 64 | this->INHERITED::flatten(buffer); |
| 65 | |
| 66 | buffer.write32(fSize); |
| 67 | // only need to write page0 |
| 68 | buffer.writePad(fPage0, fSize); |
| 69 | } |
| 70 | |
| 71 | SkFlipPixelRef::SkFlipPixelRef(SkFlattenableReadBuffer& buffer) |
| 72 | : INHERITED(buffer, NULL) { |
| 73 | fSize = buffer.readU32(); |
| 74 | fStorage = sk_malloc_throw(fSize << 1); |
| 75 | fPage0 = fStorage; |
| 76 | fPage1 = (char*)fStorage + fSize; |
| 77 | buffer.read(fPage0, fSize); |
| 78 | } |
| 79 | |
| 80 | SkPixelRef* SkFlipPixelRef::Create(SkFlattenableReadBuffer& buffer) { |
| 81 | return SkNEW_ARGS(SkFlipPixelRef, (buffer)); |
| 82 | } |
| 83 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 84 | static SkPixelRef::Registrar reg("SkFlipPixelRef", |
| 85 | SkFlipPixelRef::Create); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
| 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | |
| 89 | static void copyRect(const SkBitmap& dst, const SkIRect& rect, |
| 90 | const void* srcAddr, int shift) { |
| 91 | const size_t offset = rect.fTop * dst.rowBytes() + (rect.fLeft << shift); |
| 92 | char* dstP = static_cast<char*>(dst.getPixels()) + offset; |
| 93 | const char* srcP = static_cast<const char*>(srcAddr) + offset; |
| 94 | const size_t rb = dst.rowBytes(); |
| 95 | const size_t bytes = rect.width() << shift; |
| 96 | |
| 97 | int height = rect.height(); |
| 98 | while (--height >= 0) { |
| 99 | memcpy(dstP, srcP, bytes); |
| 100 | dstP += rb; |
| 101 | srcP += rb; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static int getShift(SkBitmap::Config config) { |
| 106 | switch (config) { |
| 107 | case SkBitmap::kARGB_8888_Config: |
| 108 | return 2; |
| 109 | case SkBitmap::kRGB_565_Config: |
| 110 | case SkBitmap::kARGB_4444_Config: |
| 111 | return 1; |
| 112 | case SkBitmap::kIndex8_Config: |
| 113 | case SkBitmap::kA8_Config: |
| 114 | return 0; |
| 115 | default: |
| 116 | return -1; // signal not supported |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void SkFlipPixelRef::CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip, |
| 121 | const void* srcAddr) { |
| 122 | const int shift = getShift(dst.config()); |
| 123 | if (shift < 0) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | const SkIRect bounds = {0, 0, dst.width(), dst.height()}; |
| 128 | SkRegion::Cliperator iter(clip, bounds); |
| 129 | |
| 130 | while (!iter.done()) { |
| 131 | copyRect(dst, iter.rect(), srcAddr, shift); |
| 132 | iter.next(); |
| 133 | } |
| 134 | } |