blob: 07f277dc4e616ab014358e4fc99d04d500c4bcd7 [file] [log] [blame]
// Copyright 2024 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once
#include "pw_display_driver/display_driver.h"
#include "pw_framebuffer/framebuffer.h"
#include "pw_framebuffer_pool/framebuffer_pool.h"
#include "pw_geometry/size.h"
#include "pw_status/status.h"
/// @defgroup pw_display
namespace pw::display {
/// @ingroup pw_display
///
/// The display is an object that represents a single display (or screen)
/// attached to the host. There is a 1:1 correspondence with the screen
/// that it manages. It has one or more framebuffers which its clients may
/// use for rendering.
class Display {
public:
Display(pw::display_driver::DisplayDriver& display_driver,
pw::geometry::Size<uint16_t> size,
pw::framebuffer_pool::FramebufferPool& framebuffer_pool);
virtual ~Display();
/// Return a framebuffer to which the caller may draw. When drawing is
/// complete the framebuffer must be returned using ReleaseFramebuffer(). An
/// invalid framebuffer may be returned, so the caller should verify it is
/// valid before use. This function will block until a framebuffer is
/// available for use. A valid framebuffer will always be returned.
pw::framebuffer::Framebuffer GetFramebuffer();
/// Release the framebuffer back to the display. The display will
/// send the framebuffer data to the screen. This function will block until
/// the transfer has completed.
///
/// This function should only be passed a valid framebuffer returned by
/// a paired call to GetFramebuffer.
///
/// If The pw_display_DISPLAY_RESIZE build variable is set and the display
/// size is different than the framebuffer size then the framebuffer contents
/// will be resized using the nearest-neighbor algorithm.
Status ReleaseFramebuffer(pw::framebuffer::Framebuffer framebuffer);
/// Return the width (in pixels) of the associated display.
uint16_t GetWidth() const { return size_.width; }
/// Return the height (in pixels) of the associated display.
uint16_t GetHeight() const { return size_.height; }
private:
#if DISPLAY_RESIZE
/// Update screen while scaling the framebuffer using nearest
/// neighbor algorithm.
Status UpdateNearestNeighbor(const pw::framebuffer::Framebuffer& framebuffer);
#endif // if DISPLAY_RESIZE
pw::display_driver::DisplayDriver& display_driver_;
const pw::geometry::Size<uint16_t> size_;
pw::framebuffer_pool::FramebufferPool& framebuffer_pool_;
};
} // namespace pw::display