reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 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 | |
| 8 | #include "SkDataPixelRef.h" |
| 9 | #include "SkData.h" |
reed@google.com | 9ddef9b | 2012-08-07 16:10:42 +0000 | [diff] [blame] | 10 | #include "SkFlattenableBuffers.h" |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 11 | |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 12 | SkDataPixelRef::SkDataPixelRef(const SkImageInfo& info, |
| 13 | SkData* data, size_t rowBytes) |
| 14 | : INHERITED(info) |
| 15 | , fData(data) |
| 16 | , fRB(rowBytes) |
| 17 | { |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 18 | fData->ref(); |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 19 | this->setPreLocked(const_cast<void*>(fData->data()), rowBytes, NULL); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | SkDataPixelRef::~SkDataPixelRef() { |
| 23 | fData->unref(); |
| 24 | } |
| 25 | |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 26 | bool SkDataPixelRef::onNewLockPixels(LockRec* rec) { |
| 27 | rec->fPixels = const_cast<void*>(fData->data()); |
| 28 | rec->fColorTable = NULL; |
| 29 | rec->fRowBytes = fRB; |
| 30 | return true; |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void SkDataPixelRef::onUnlockPixels() { |
| 34 | // nothing to do |
| 35 | } |
| 36 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 37 | size_t SkDataPixelRef::getAllocatedSizeInBytes() const { |
| 38 | return fData ? fData->size() : 0; |
| 39 | } |
| 40 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 41 | void SkDataPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 42 | this->INHERITED::flatten(buffer); |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 43 | |
reed@google.com | da30055 | 2013-10-14 14:33:11 +0000 | [diff] [blame] | 44 | buffer.writeDataAsByteArray(fData); |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 45 | buffer.write32(fRB); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | SkDataPixelRef::SkDataPixelRef(SkFlattenableReadBuffer& buffer) |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 49 | : INHERITED(buffer, NULL) |
| 50 | { |
reed@google.com | da30055 | 2013-10-14 14:33:11 +0000 | [diff] [blame] | 51 | fData = buffer.readByteArrayAsData(); |
reed@google.com | 6965a0a | 2013-12-11 18:04:56 +0000 | [diff] [blame^] | 52 | fRB = buffer.read32(); |
| 53 | this->setPreLocked(const_cast<void*>(fData->data()), fRB, NULL); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 54 | } |