OSDN Git Service

PixelBuffer: Align width and height of pixelbuffer
[android-x86/external-IA-Hardware-Composer.git] / wsi / pixelbuffer.cpp
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #include "pixelbuffer.h"
18
19 #include "resourcemanager.h"
20
21 namespace hwcomposer {
22
23 PixelBuffer::PixelBuffer() {
24 }
25
26 PixelBuffer::~PixelBuffer() {
27 }
28
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) {
32   int i;
33   int layer_type = is_cursor_buffer ? kLayerCursor : kLayerNormal;
34   uint8_t* byteaddr = (uint8_t*) addr;
35
36   if (!buffer_handler->CreateBuffer(width, height, format, &resource.handle_, layer_type)) {
37     ETRACE("PixelBuffer: CreateBuffer failed");
38     return;
39   }
40
41   HWCNativeHandle &handle = resource.handle_;
42   if (!buffer_handler->ImportBuffer(handle)) {
43     ETRACE("PixelBuffer: ImportBuffer failed");
44     return;
45   }
46
47   if (handle->meta_data_.prime_fd_ <= 0) {
48     ETRACE("PixelBuffer: prime_fd_ is invalid.");
49     return;
50   }
51
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);
54   if (!ptr) {
55     return;
56   }
57
58   for (i = 0; i < height; i++)
59     memcpy(ptr + i * handle->meta_data_.pitches_[0],
60            byteaddr + i * stride,
61            stride);
62
63   Unmap(handle->meta_data_.prime_fd_, ptr, size);
64   needs_texture_upload_ = false;
65 }
66
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);
72   if (!ptr) {
73     return;
74   }
75
76   memcpy(ptr, addr, size);
77   Unmap(handle->meta_data_.prime_fd_, ptr, size);
78   needs_texture_upload_ = false;
79 }
80 };