2 // Copyright (c) 2018 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "pixelbuffer.h"
19 #include "resourcemanager.h"
21 namespace hwcomposer {
23 PixelBuffer::PixelBuffer() {
26 PixelBuffer::~PixelBuffer() {
29 void PixelBuffer::Initialize(const NativeBufferHandler *buffer_handler,
30 uint32_t width, uint32_t height, uint32_t stride, uint32_t format,
31 void *addr, ResourceHandle &resource, bool is_cursor_buffer) {
33 int layer_type = is_cursor_buffer ? kLayerCursor : kLayerNormal;
34 uint8_t* byteaddr = (uint8_t*) addr;
36 if (!buffer_handler->CreateBuffer(width, height, format, &resource.handle_, layer_type)) {
37 ETRACE("PixelBuffer: CreateBuffer failed");
41 HWCNativeHandle &handle = resource.handle_;
42 if (!buffer_handler->ImportBuffer(handle)) {
43 ETRACE("PixelBuffer: ImportBuffer failed");
47 if (handle->meta_data_.prime_fd_ <= 0) {
48 ETRACE("PixelBuffer: prime_fd_ is invalid.");
52 size_t size = handle->meta_data_.height_ * handle->meta_data_.pitches_[0];
53 uint8_t *ptr = (uint8_t *) Map(handle->meta_data_.prime_fd_, size);
58 for (i = 0; i < height; i++)
59 memcpy(ptr + i * handle->meta_data_.pitches_[0],
60 byteaddr + i * stride,
63 Unmap(handle->meta_data_.prime_fd_, ptr, size);
64 needs_texture_upload_ = false;
67 void PixelBuffer::Refresh(void *addr, const ResourceHandle &resource) {
68 needs_texture_upload_ = true;
69 const HWCNativeHandle &handle = resource.handle_;
70 size_t size = handle->meta_data_.height_ * handle->meta_data_.pitches_[0];
71 void *ptr = Map(handle->meta_data_.prime_fd_, size);
76 memcpy(ptr, addr, size);
77 Unmap(handle->meta_data_.prime_fd_, ptr, size);
78 needs_texture_upload_ = false;