| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // 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 |
| // |
| // http://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. |
| |
| package state |
| |
| import ( |
| "android.googlesource.com/platform/tools/gpu/framework/binary" |
| "android.googlesource.com/platform/tools/gpu/framework/interval" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| "android.googlesource.com/platform/tools/gpu/gapid/builder/ranges" |
| "android.googlesource.com/platform/tools/gpu/gapid/database" |
| "android.googlesource.com/platform/tools/gpu/gapid/gfxapi" |
| "android.googlesource.com/platform/tools/gpu/gapid/memory" |
| "android.googlesource.com/platform/tools/gpu/gapid/replay/value" |
| "android.googlesource.com/platform/tools/gpu/gapid/service" |
| ) |
| |
| // New returns a new, default-initialized State object. |
| func New(ctx log.Context) *gfxapi.State { |
| // Retrieve capture and database, get used memory ranges and set up an allocator from the free list. |
| capture, d := service.GetCaptureAndDatabase(ctx) |
| rng, err := database.Build(ctx, &ranges.GetMemoryRanges{Capture: capture}, d) |
| if err != nil { |
| panic(err) |
| } |
| |
| freeList := memory.InvertMemoryRanges(rng.(*ranges.MemoryRanges).UsedList) |
| interval.Remove(&freeList, interval.U64Span{Start: 0, End: value.FirstValidAddress}) |
| return withAllocator(ctx, memory.NewBasicAllocator(freeList)) |
| } |
| |
| func NewWithEmptyAllocator(ctx log.Context) *gfxapi.State { |
| return withAllocator(ctx, memory.NewBasicAllocator(value.ValidMemoryRanges)) |
| } |
| |
| func NewWithNilAllocator(ctx log.Context) *gfxapi.State { |
| return withAllocator(ctx, nil) |
| } |
| |
| // WithAllocator returns a new, default-initialized State object, |
| // that uses the given memory.Allocator instance. |
| func withAllocator(ctx log.Context, allocator memory.Allocator) *gfxapi.State { |
| return &gfxapi.State{ |
| MemoryLayout: gfxapi.DefaultMemoryLayout, |
| Memory: map[memory.PoolID]*memory.Pool{ |
| memory.ApplicationPool: {}, |
| }, |
| NextPoolID: memory.ApplicationPool + 1, |
| APIs: map[gfxapi.API]binary.Object{}, |
| Allocator: allocator, |
| } |
| } |