skia2/tests/FakeStreams.h
Mike Klein d9187c085b Revert "SkMath takes some functions from from SkTypes"
This reverts commit 3b347232bc.

Reason for revert: tree done gone red.

Original change's description:
> SkMath takes some functions from from SkTypes
> 
> Moved to include/core/SkMath.h: Sk{Is|}Align{2|4|8|Ptr}, SkLeftShift,
> SkAbs{32|}, SkM{ax|in}32 SkTM{in|ax}, SkTClamp, SkFastMin32, SkTPin.
> 
> Change-Id: Ibcc07be0fc3677731048e7cc86006e7aa493cb92
> Reviewed-on: https://skia-review.googlesource.com/133381
> Auto-Submit: Hal Canary <halcanary@google.com>
> Reviewed-by: Ben Wagner <bungeman@google.com>
> Commit-Queue: Hal Canary <halcanary@google.com>

TBR=mtklein@google.com,halcanary@google.com,bungeman@google.com,reed@google.com

Change-Id: I44073cf397e2a3a6a941a90f0aa63c6396d4c742
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://skia-review.googlesource.com/152587
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-07 17:32:54 +00:00

87 lines
2.2 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkData.h"
#include "SkStream.h"
#ifndef FakeStreams_DEFINED
#define FakeStreams_DEFINED
// Stream that is not an asset stream (!hasPosition() or !hasLength())
class NotAssetMemStream : public SkStream {
public:
NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
bool hasPosition() const override {
return false;
}
bool hasLength() const override {
return false;
}
size_t peek(void* buf, size_t bytes) const override {
return fStream.peek(buf, bytes);
}
size_t read(void* buf, size_t bytes) override {
return fStream.read(buf, bytes);
}
bool rewind() override {
return fStream.rewind();
}
bool isAtEnd() const override {
return fStream.isAtEnd();
}
private:
SkMemoryStream fStream;
};
/*
* Represents a stream without all of its data.
*/
class HaltingStream : public SkStream {
public:
HaltingStream(sk_sp<SkData> data, size_t initialLimit)
: fTotalSize(data->size())
, fLimit(initialLimit)
, fStream(std::move(data))
{}
void addNewData(size_t extra) {
fLimit = SkTMin(fTotalSize, fLimit + extra);
}
size_t read(void* buffer, size_t size) override {
if (fStream.getPosition() + size > fLimit) {
size = fLimit - fStream.getPosition();
}
return fStream.read(buffer, size);
}
bool isAtEnd() const override {
return fStream.isAtEnd();
}
bool hasLength() const override { return true; }
size_t getLength() const override { return fLimit; }
bool hasPosition() const override { return true; }
size_t getPosition() const override { return fStream.getPosition(); }
bool rewind() override { return fStream.rewind(); }
bool move(long offset) override { return fStream.move(offset); }
bool seek(size_t position) override { return fStream.seek(position); }
bool isAllDataReceived() const { return fLimit == fTotalSize; }
private:
const size_t fTotalSize;
size_t fLimit;
SkMemoryStream fStream;
};
#endif // FakeStreams_DEFINED