Add new constructors for all DesktopFrame inheritances

This change adds constructors for all DesktopFrame inheritances to pass in
DesktopRect instead of DesktopSize.
Because the newly added constructors and DesktopFrame::top_left() function are
not actively used, this change should have no logic impact.

Bug: webrtc:7950
Change-Id: If78187865c991211dfc28d3723403ce6e6fe0290
Reviewed-on: https://chromium-review.googlesource.com/590508
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Commit-Queue: Zijie He <zijiehe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#19204}
This commit is contained in:
Zijie He
2017-07-31 20:26:11 -07:00
committed by Commit Bot
parent b1338fec81
commit 09f16c6a0a
5 changed files with 88 additions and 25 deletions

View File

@ -17,6 +17,17 @@
namespace webrtc { namespace webrtc {
namespace {
DesktopRect TranslateRect(const DesktopRect& rect,
const DesktopVector& top_left) {
DesktopRect result = rect;
result.Translate(top_left);
return result;
}
} // namespace
// A DesktopFrame that is a sub-rect of another DesktopFrame. // A DesktopFrame that is a sub-rect of another DesktopFrame.
class CroppedDesktopFrame : public DesktopFrame { class CroppedDesktopFrame : public DesktopFrame {
public: public:
@ -48,7 +59,7 @@ std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame(
CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame, CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
const DesktopRect& rect) const DesktopRect& rect)
: DesktopFrame(rect.size(), : DesktopFrame(TranslateRect(rect, frame->top_left()),
frame->stride(), frame->stride(),
frame->GetFrameDataAtPos(rect.top_left()), frame->GetFrameDataAtPos(rect.top_left()),
frame->shared_memory()) { frame->shared_memory()) {

View File

@ -79,7 +79,7 @@ DesktopFrameWithCursor::DesktopFrameWithCursor(
std::unique_ptr<DesktopFrame> frame, std::unique_ptr<DesktopFrame> frame,
const MouseCursor& cursor, const MouseCursor& cursor,
const DesktopVector& position) const DesktopVector& position)
: DesktopFrame(frame->size(), : DesktopFrame(frame->rect(),
frame->stride(), frame->stride(),
frame->data(), frame->data(),
frame->shared_memory()) { frame->shared_memory()) {

View File

@ -16,6 +16,7 @@
#include "webrtc/modules/desktop_capture/desktop_geometry.h" #include "webrtc/modules/desktop_capture/desktop_geometry.h"
#include "webrtc/rtc_base/checks.h" #include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/ptr_util.h"
namespace webrtc { namespace webrtc {
@ -37,7 +38,11 @@ DesktopFrame::DesktopFrame(DesktopRect rect,
rect_(rect), rect_(rect),
stride_(stride), stride_(stride),
capture_time_ms_(0), capture_time_ms_(0),
capturer_id_(DesktopCapturerId::kUnknown) {} capturer_id_(DesktopCapturerId::kUnknown) {
RTC_DCHECK(stride_ >= 0);
RTC_DCHECK(rect.width() >= 0);
RTC_DCHECK(rect.height() >= 0);
}
DesktopFrame::~DesktopFrame() = default; DesktopFrame::~DesktopFrame() = default;
@ -68,10 +73,12 @@ uint8_t* DesktopFrame::GetFrameDataAtPos(const DesktopVector& pos) const {
} }
BasicDesktopFrame::BasicDesktopFrame(DesktopSize size) BasicDesktopFrame::BasicDesktopFrame(DesktopSize size)
: DesktopFrame(size, kBytesPerPixel * size.width(), : BasicDesktopFrame(DesktopRect::MakeSize(size)) {}
new uint8_t[kBytesPerPixel * size.width() * size.height()],
NULL) { BasicDesktopFrame::BasicDesktopFrame(DesktopRect rect)
} : DesktopFrame(rect, kBytesPerPixel * rect.width(),
new uint8_t[kBytesPerPixel * rect.width() * rect.height()],
nullptr) {}
BasicDesktopFrame::~BasicDesktopFrame() { BasicDesktopFrame::~BasicDesktopFrame() {
delete[] data_; delete[] data_;
@ -95,29 +102,45 @@ DesktopFrame* BasicDesktopFrame::CopyOf(const DesktopFrame& frame) {
std::unique_ptr<DesktopFrame> SharedMemoryDesktopFrame::Create( std::unique_ptr<DesktopFrame> SharedMemoryDesktopFrame::Create(
DesktopSize size, DesktopSize size,
SharedMemoryFactory* shared_memory_factory) { SharedMemoryFactory* shared_memory_factory) {
size_t buffer_size = size.height() * size.width() * kBytesPerPixel; return Create(DesktopRect::MakeSize(size), shared_memory_factory);
}
// static
std::unique_ptr<DesktopFrame> SharedMemoryDesktopFrame::Create(
DesktopRect rect,
SharedMemoryFactory* shared_memory_factory) {
RTC_DCHECK(shared_memory_factory);
size_t buffer_size = rect.height() * rect.width() * kBytesPerPixel;
std::unique_ptr<SharedMemory> shared_memory = std::unique_ptr<SharedMemory> shared_memory =
shared_memory_factory->CreateSharedMemory(buffer_size); shared_memory_factory->CreateSharedMemory(buffer_size);
if (!shared_memory) if (!shared_memory)
return nullptr; return nullptr;
return Create(size, std::move(shared_memory)); return rtc::MakeUnique<SharedMemoryDesktopFrame>(
} rect, rect.width() * kBytesPerPixel, std::move(shared_memory));
// static
std::unique_ptr<DesktopFrame> SharedMemoryDesktopFrame::Create(
DesktopSize size,
std::unique_ptr<SharedMemory> shared_memory) {
RTC_DCHECK(shared_memory);
int stride = size.width() * kBytesPerPixel;
return std::unique_ptr<DesktopFrame>(new SharedMemoryDesktopFrame(
size, stride, shared_memory.release()));
} }
SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(DesktopSize size, SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(DesktopSize size,
int stride, int stride,
SharedMemory* shared_memory) SharedMemory* shared_memory)
: DesktopFrame(size, : SharedMemoryDesktopFrame(DesktopRect::MakeSize(size),
stride,
shared_memory) {}
SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(
DesktopRect rect,
int stride,
std::unique_ptr<SharedMemory> shared_memory)
: SharedMemoryDesktopFrame(rect,
stride,
shared_memory.release()) {}
SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(
DesktopRect rect,
int stride,
SharedMemory* shared_memory)
: DesktopFrame(rect,
stride, stride,
reinterpret_cast<uint8_t*>(shared_memory->data()), reinterpret_cast<uint8_t*>(shared_memory->data()),
shared_memory) {} shared_memory) {}

View File

@ -86,6 +86,9 @@ class DesktopFrame {
protected: protected:
// Deprecated, use the constructor below. // Deprecated, use the constructor below.
// TODO(zijiehe): Remove deprecated constructors. DesktopFrame should describe
// its own position in the system. See
// https://bugs.chromium.org/p/webrtc/issues/detail?id=7950
DesktopFrame(DesktopSize size, DesktopFrame(DesktopSize size,
int stride, int stride,
uint8_t* data, uint8_t* data,
@ -118,7 +121,12 @@ class DesktopFrame {
// A DesktopFrame that stores data in the heap. // A DesktopFrame that stores data in the heap.
class BasicDesktopFrame : public DesktopFrame { class BasicDesktopFrame : public DesktopFrame {
public: public:
// Deprecated, use the next constructor.
explicit BasicDesktopFrame(DesktopSize size); explicit BasicDesktopFrame(DesktopSize size);
// Preferred.
explicit BasicDesktopFrame(DesktopRect rect);
~BasicDesktopFrame() override; ~BasicDesktopFrame() override;
// Creates a BasicDesktopFrame that contains copy of |frame|. // Creates a BasicDesktopFrame that contains copy of |frame|.
@ -131,23 +139,44 @@ class BasicDesktopFrame : public DesktopFrame {
// A DesktopFrame that stores data in shared memory. // A DesktopFrame that stores data in shared memory.
class SharedMemoryDesktopFrame : public DesktopFrame { class SharedMemoryDesktopFrame : public DesktopFrame {
public: public:
// May return nullptr if |shared_memory_factory| failed to create a
// SharedMemory instance.
// |shared_memory_factory| should not be nullptr.
// Deprecated, use the next Create() function.
static std::unique_ptr<DesktopFrame> Create( static std::unique_ptr<DesktopFrame> Create(
DesktopSize size, DesktopSize size,
SharedMemoryFactory* shared_memory_factory); SharedMemoryFactory* shared_memory_factory);
// Preferred.
static std::unique_ptr<DesktopFrame> Create( static std::unique_ptr<DesktopFrame> Create(
DesktopSize size, DesktopRect rect,
std::unique_ptr<SharedMemory> shared_memory); SharedMemoryFactory* shared_memory_factory);
// Takes ownership of |shared_memory|. // Takes ownership of |shared_memory|.
// TODO(zijiehe): Hide constructors after fake_desktop_capturer.cc has been // Deprecated, use the next constructor.
// migrated, Create() is preferred.
SharedMemoryDesktopFrame(DesktopSize size, SharedMemoryDesktopFrame(DesktopSize size,
int stride, int stride,
SharedMemory* shared_memory); SharedMemory* shared_memory);
// Preferred.
SharedMemoryDesktopFrame(DesktopRect rect,
int stride,
std::unique_ptr<SharedMemory> shared_memory);
~SharedMemoryDesktopFrame() override; ~SharedMemoryDesktopFrame() override;
private: private:
// Avoid unexpected order of parameter evaluation.
// Executing both std::unique_ptr<T>::operator->() and
// std::unique_ptr<T>::release() in the member initializer list is not safe.
// Depends on the order of parameter evaluation,
// std::unique_ptr<T>::operator->() may trigger assertion failure if it has
// been evaluated after std::unique_ptr<T>::release(). By using this
// constructor, std::unique_ptr<T>::operator->() won't be involved anymore.
SharedMemoryDesktopFrame(DesktopRect rect,
int stride,
SharedMemory* shared_memory);
RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryDesktopFrame); RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryDesktopFrame);
}; };

View File

@ -47,7 +47,7 @@ bool SharedDesktopFrame::IsShared() {
} }
SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr<Core> core) SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr<Core> core)
: DesktopFrame((*core)->size(), : DesktopFrame((*core)->rect(),
(*core)->stride(), (*core)->stride(),
(*core)->data(), (*core)->data(),
(*core)->shared_memory()), (*core)->shared_memory()),